Repository navigation
Expand file tree
/
Copy pathkeyprovider.go
More file actions
61 lines (54 loc) · 2.12 KB
/
Copy pathkeyprovider.go
File metadata and controls
61 lines (54 loc) · 2.12 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
package cipher
import (
"context"
"github.com/getsops/sops/v3"
)
// KeyProvider returns the sops key groups used by an Encoder to wrap
// the per-file data key. Each backend (age, AWS KMS, GCP KMS, Vault,
// PGP, Azure Key Vault) implements KeyProvider in its own subpackage.
//
// The return type is sops.KeyGroup, the same shape sops uses internally.
// This keeps backend interop straightforward and lets callers compose
// groups across providers.
type KeyProvider interface {
// KeyGroups returns the key groups to use, in priority order.
// Implementations should return a non-empty slice or a non-nil error.
//
// The ctx parameter is part of the contract but the built-in
// backend providers currently ignore it. The sops master-key
// constructors they wrap do their network work later, inside
// Encode/Decode, and accept no context. Callers should still pass
// a real context: custom providers may honor it, and a future
// version will use it once sops master keys plumb context through.
KeyGroups(ctx context.Context) ([]sops.KeyGroup, error)
}
// KeyProviderFunc adapts a plain function to KeyProvider.
type KeyProviderFunc func(ctx context.Context) ([]sops.KeyGroup, error)
// KeyGroups calls f.
func (f KeyProviderFunc) KeyGroups(ctx context.Context) ([]sops.KeyGroup, error) {
return f(ctx)
}
// StaticKeyProvider returns a KeyProvider that always yields the given
// groups. Useful for tests and call-site composition.
func StaticKeyProvider(groups ...sops.KeyGroup) KeyProvider {
g := append([]sops.KeyGroup(nil), groups...)
return KeyProviderFunc(func(context.Context) ([]sops.KeyGroup, error) {
return g, nil
})
}
// ChainKeyProviders returns a KeyProvider that concatenates the groups
// from each underlying provider, in order. Errors from any underlying
// provider abort the chain.
func ChainKeyProviders(providers ...KeyProvider) KeyProvider {
return KeyProviderFunc(func(ctx context.Context) ([]sops.KeyGroup, error) {
var out []sops.KeyGroup
for _, p := range providers {
groups, err := p.KeyGroups(ctx)
if err != nil {
return nil, err
}
out = append(out, groups...)
}
return out, nil
})
}