forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.go
53 lines (44 loc) · 1.19 KB
/
secrets.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
package fi
import (
crypto_rand "crypto/rand"
"encoding/base64"
"fmt"
"k8s.io/kops/util/pkg/vfs"
"strings"
)
type SecretStore interface {
// Get a secret. Returns an error if not found
Secret(id string) (*Secret, error)
// Find a secret, if exists. Returns nil,nil if not found
FindSecret(id string) (*Secret, error)
// Create or replace a secret
GetOrCreateSecret(id string, secret *Secret) (current *Secret, created bool, err error)
// Lists the ids of all known secrets
ListSecrets() ([]string, error)
// VFSPath returns the path where the SecretStore is stored
VFSPath() vfs.Path
}
type Secret struct {
Data []byte
}
func (s *Secret) AsString() (string, error) {
// Nicer behaviour because this is called from templates
if s == nil {
return "", fmt.Errorf("AsString called on nil Secret")
}
return string(s.Data), nil
}
func CreateSecret() (*Secret, error) {
data := make([]byte, 128)
_, err := crypto_rand.Read(data)
if err != nil {
return nil, fmt.Errorf("error reading crypto_rand: %v", err)
}
s := base64.StdEncoding.EncodeToString(data)
r := strings.NewReplacer("+", "", "=", "", "/", "")
s = r.Replace(s)
s = s[:32]
return &Secret{
Data: []byte(s),
}, nil
}