-
Notifications
You must be signed in to change notification settings - Fork 11
/
secrets.go
86 lines (71 loc) · 1.95 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package provider
import (
"encoding/json"
"fmt"
"github.com/nats-io/nkeys"
)
// Type alias to use for sensitive values to avoid accidentally logging them
type SecretStringValue struct {
value string
}
func (s SecretStringValue) String() string {
return "redacted(string)"
}
func (s SecretStringValue) Reveal() string {
return s.value
}
type SecretBytesValue struct {
value []byte
}
func (s SecretBytesValue) String() string {
return "redacted(bytes)"
}
func (s SecretBytesValue) Reveal() []byte {
return s.value
}
type SecretValue struct {
String SecretStringValue
Bytes SecretBytesValue
}
// Secret values are serialized as either a String or Bytes value, e.g.
// {"kind": "String", "value": "my secret"} or {"kind": "Bytes", "value": [1, 2, 3]}
func (s *SecretValue) UnmarshalJSON(data []byte) error {
var jsonSecret map[string]interface{}
err := json.Unmarshal(data, &jsonSecret)
if err != nil {
return err
}
switch jsonSecret["kind"] {
case "String":
s.String = SecretStringValue{value: jsonSecret["value"].(string)}
case "Bytes":
s.Bytes = SecretBytesValue{value: jsonSecret["value"].([]byte)}
default:
return fmt.Errorf("invalid secret kind: %s", jsonSecret["kind"])
}
return nil
}
func (s *SecretStringValue) UnmarshalJSON(data []byte) error {
var stringValue string
err := json.Unmarshal(data, &stringValue)
if err != nil {
return err
}
s.value = stringValue
return nil
}
func DecryptSecrets(encryptedBytes *[]byte, xkey nkeys.KeyPair, sender string) (map[string]SecretValue, error) {
var sourceSecrets = make(map[string]SecretValue)
// If the source secrets are empty or not present, we don't need to decrypt/unmarshal them
if encryptedBytes != nil && len(*encryptedBytes) != 0 {
sourceSecretBytes, err := xkey.Open(*encryptedBytes, sender)
if err != nil {
return sourceSecrets, err
}
err = json.Unmarshal(sourceSecretBytes, &sourceSecrets)
if err != nil {
return sourceSecrets, err
}
}
return sourceSecrets, nil
}