generated from replicatedhq/krew-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
72 lines (58 loc) · 2.04 KB
/
manager.go
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
72
package keys
import (
"fmt"
"github.com/cloudogu/cesapp-lib/core"
"github.com/cloudogu/cesapp-lib/keys"
"github.com/cloudogu/cesapp-lib/registry"
)
var log = core.GetLogger()
// NewKeyManager creates a new KeyManager which is able to find and create keys for the given dogu
func NewKeyManager(registry cesRegistry, doguName string) (*etcdKeyManager, error) {
keyType, err := registry.GlobalConfig().Get("key_provider")
if err != nil {
return nil, fmt.Errorf("could not create etcd key manager: could not read key type: %w", err)
}
keyProvider, err := keys.NewKeyProvider(keyType)
if err != nil {
return nil, fmt.Errorf("could not create etcd key manager: could not create key provider: %w", err)
}
return &etcdKeyManager{
doguConfig: registry.DoguConfig(doguName),
keyProvider: keyProvider,
}, nil
}
type etcdKeyManager struct {
doguConfig configurationContext
keyProvider keyProvider
}
// GetPublicKey returns the public key from the dogus etcd directory.
func (ekm *etcdKeyManager) GetPublicKey() (*keys.PublicKey, error) {
exists, err := ekm.ExistsPublicKey()
if err != nil {
return nil, err
}
if exists {
return ekm.fetchPublicKey()
}
return nil, fmt.Errorf("could not find public key in configuration context: %w", err)
}
// ExistsPublicKey true if the public key exists in the configuration context
func (ekm *etcdKeyManager) ExistsPublicKey() (bool, error) {
exists, err := ekm.doguConfig.Exists(registry.KeyDoguPublicKey)
if err != nil {
return false, fmt.Errorf("failed to check if public key exists: %w", err)
}
return exists, nil
}
func (ekm *etcdKeyManager) fetchPublicKey() (*keys.PublicKey, error) {
log.Debugf("fetch public key from configuration context")
publicKeyString, err := ekm.doguConfig.Get(registry.KeyDoguPublicKey)
if err != nil {
return nil, fmt.Errorf("failed to fetch public key from registry: %w", err)
}
publicKey, err := ekm.keyProvider.ReadPublicKeyFromString(publicKeyString)
if err != nil {
return nil, fmt.Errorf("failed to create public key from string: %w", err)
}
return publicKey, nil
}