Skip to content

Commit 0e39e5a

Browse files
author
Dmitriy Matrenichev
committed
chore: use Once from xsync package
Move stuff to github.com/siderolabs/gen Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
1 parent 25d4124 commit 0e39e5a

3 files changed

Lines changed: 16 additions & 31 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/golang/protobuf v1.5.2
1313
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
1414
github.com/hashicorp/go-memdb v1.3.3
15+
github.com/siderolabs/gen v0.2.0
1516
github.com/siderolabs/go-pointer v1.0.0
1617
github.com/siderolabs/protoenc v0.2.0
1718
github.com/stretchr/testify v1.8.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3333
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3434
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
36+
github.com/siderolabs/gen v0.2.0 h1:Fq+lXJmry1C+bMvp9IipyHX4nUq4j132BrpuV1vWbd8=
37+
github.com/siderolabs/gen v0.2.0/go.mod h1:vzcXVRNpo9j2tyQJU+9ZQ1J6yoebX9KL1TkHt18HNqw=
3638
github.com/siderolabs/go-pointer v1.0.0 h1:6TshPKep2doDQJAAtHUuHWXbca8ZfyRySjSBT/4GsMU=
3739
github.com/siderolabs/go-pointer v1.0.0/go.mod h1:HTRFUNYa3R+k0FFKNv11zgkaCLzEkWVzoYZ433P3kHc=
3840
github.com/siderolabs/protoenc v0.2.0 h1:QFxWIAo//12+/bm27GNYoK/TpQGTYsRrrZCu9jSghvU=

pkg/state/impl/store/encryption/marshaler.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"crypto/rand"
1212
"fmt"
1313
"io"
14-
"sync"
14+
15+
"github.com/siderolabs/gen/pair"
16+
"github.com/siderolabs/gen/xsync"
1517

1618
"github.com/cosi-project/runtime/pkg/resource"
1719
"github.com/cosi-project/runtime/pkg/state/impl/store"
@@ -56,48 +58,43 @@ func (m *Marshaler) UnmarshalResource(b []byte) (resource.Resource, error) { //n
5658
// Cipher provides encryption and decryption.
5759
type Cipher struct {
5860
keyProvider KeyProvider
59-
once Once[cipherData]
61+
once xsync.Once[pair.Pair[cipher.AEAD, error]]
6062
}
6163

6264
// NewCipher creates new Cipher.
6365
func NewCipher(provider KeyProvider) *Cipher {
6466
return &Cipher{keyProvider: provider}
6567
}
6668

67-
type cipherData struct {
68-
aead cipher.AEAD
69-
err error
70-
}
71-
7269
func (c *Cipher) init() (cipher.AEAD, error) {
73-
res := c.once.Do(func() cipherData {
70+
res := c.once.Do(func() pair.Pair[cipher.AEAD, error] {
7471
key, err := c.keyProvider.ProvideKey()
7572
if err != nil {
76-
return cipherData{err: fmt.Errorf("failed to get key: %w", err)}
73+
return pair.MakePair[cipher.AEAD, error](nil, fmt.Errorf("failed to provide key: %w", err))
7774
}
7875

7976
if len(key) != 32 {
80-
return cipherData{err: fmt.Errorf("key length is not 32 bytes")}
77+
return pair.MakePair[cipher.AEAD, error](nil, fmt.Errorf("key length is not 32 bytes"))
8178
}
8279

8380
block, err := aes.NewCipher(key)
8481
if err != nil {
85-
return cipherData{err: fmt.Errorf("failed to create cipher: %w", err)}
82+
return pair.MakePair[cipher.AEAD, error](nil, fmt.Errorf("failed to create cipher: %w", err))
8683
}
8784

8885
aead, err := cipher.NewGCM(block)
8986
if err != nil {
90-
return cipherData{err: fmt.Errorf("failed to create GCM: %w", err)}
87+
return pair.MakePair[cipher.AEAD, error](nil, fmt.Errorf("failed to create GCM: %w", err))
9188
}
9289

93-
return cipherData{aead: aead}
90+
return pair.MakePair[cipher.AEAD, error](aead, nil)
9491
})
95-
if res.err != nil {
96-
return nil, res.err
92+
if res.F2 != nil {
93+
return nil, res.F2
9794
}
9895

9996
// According to https://github.com/golang/go/issues/25882 cipher.AEAD is safe to share between goroutines.
100-
return res.aead, nil
97+
return res.F1, nil
10198
}
10299

103100
// Encrypt encrypts data.
@@ -159,18 +156,3 @@ type KeyProviderFunc func() ([]byte, error)
159156
func (f KeyProviderFunc) ProvideKey() ([]byte, error) {
160157
return f()
161158
}
162-
163-
// Once is small wrapper around [sync.Once]. It stores the result inside.
164-
type Once[T any] struct {
165-
val T
166-
once sync.Once
167-
}
168-
169-
// Do runs the function only once.
170-
func (o *Once[T]) Do(fn func() T) T {
171-
o.once.Do(func() {
172-
o.val = fn()
173-
})
174-
175-
return o.val
176-
}

0 commit comments

Comments
 (0)