This repository was archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption_impl.go
More file actions
71 lines (57 loc) · 2.25 KB
/
Copy pathencryption_impl.go
File metadata and controls
71 lines (57 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package objectenc
import (
"context"
"sync"
"github.com/pkg/errors"
)
// ErrDuplicateImpl is returned when a duplicate encryption implementation is registered.
var ErrDuplicateImpl = errors.New("duplicate encryption implementation")
// EncryptionImpl is an implementation of a encryption type.
type EncryptionImpl interface {
// GetEncryptionType returns the encryption type this implementation satisfies.
GetEncryptionType() EncryptionType
// ValidateMetadata checks the metadata field.
// If metadata is not expected, this should check that it doesn't exist.
ValidateMetadata([]byte) error
// DecryptBlob decrypts an encrypted blob.
DecryptBlob(context.Context, ResourceResolverFunc, *EncryptedBlob) ([]byte, error)
// EncryptBlob encrypts a blob.
// The last argument is the uncompressed data, if the data is compressed.
EncryptBlob(context.Context, ResourceResolverFunc, []byte, []byte) (*EncryptedBlob, error)
}
// encryptionImplsMtx is the mutex on the encryptionImpls map
var encryptionImplsMtx sync.RWMutex
// encryptionImpls contains registered implementations.
var encryptionImpls = make(map[EncryptionType]EncryptionImpl)
// MustRegisterEncryptionImpl registers an encryption implementation or panics.
// expected to be called from Init(), but can be deferred
func MustRegisterEncryptionImpl(impl EncryptionImpl) {
if err := RegisterEncryptionImpl(impl); err != nil {
panic(err)
}
}
// RegisterEncryptionImpl registers an encryption implementation.
// expected to be called from Init(), but can be deferred
func RegisterEncryptionImpl(impl EncryptionImpl) error {
encType := impl.GetEncryptionType()
encryptionImplsMtx.Lock()
defer encryptionImplsMtx.Unlock()
if _, ok := encryptionImpls[encType]; ok {
return ErrDuplicateImpl
}
encryptionImpls[encType] = impl
return nil
}
// GetEncryptionImpl returns the registered implementation of the type.
func GetEncryptionImpl(kind EncryptionType) (impl EncryptionImpl, err error) {
if _, ok := EncryptionType_name[int32(kind)]; !ok {
return nil, errors.Errorf("encryption type unknown: %v", kind.String())
}
encryptionImplsMtx.RLock()
impl = encryptionImpls[kind]
encryptionImplsMtx.RUnlock()
if impl == nil {
err = errors.Errorf("unimplemented encryption type: %v", kind.String())
}
return
}