This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
secret.go
215 lines (176 loc) · 5.28 KB
/
secret.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package secret
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"io"
"strings"
"unicode/utf8"
"github.com/caos/orbos/internal/utils/clientgo"
"gopkg.in/yaml.v3"
)
var Masterkey = "empty"
// Secret: Secret handled with orbctl so no manual changes are required
type Secret struct {
//Encryption algorithm used for the secret
Encryption string `json:"encryption,omitempty" yaml:"encryption,omitempty"`
//Encoding algorithm used for the secret
Encoding string `json:"encoding,omitempty" yaml:"encoding,omitempty"`
//Encrypted and encoded Value
Value string `json:"value,omitempty" yaml:"value,omitempty"`
}
type secretAlias Secret
// Existing: Used secret that has to be already existing in the cluster
type Existing struct {
//Name of the Secret
Name string `json:"name" yaml:"name"`
//Key in the secret from where the value should be used
Key string `json:"key" yaml:"key"`
//Name which should be used internally, should be unique for the volume and volumemounts
InternalName string `json:"internalName,omitempty" yaml:"internalName,omitempty"`
}
// Existing: Used secret that has to be already existing in the cluster and should contain id/username and secret/password
type ExistingIDSecret struct {
//Name of the Secret
Name string `json:"name" yaml:"name"`
//Key in the secret which contains the ID
IDKey string `json:"idKey" yaml:"idKey"`
//Key in the secret which contains the secret
SecretKey string `json:"secretKey" yaml:"secretKey"`
//Name which should be used internally, should be unique for the volume and volumemounts
InternalName string `json:"internalName,omitempty" yaml:"internalName,omitempty"`
}
func (s *Secret) IsZero() bool {
if s.Value == "" {
return true
}
return false
}
func (s *Secret) UnmarshalYAMLWithExisting(node *yaml.Node, existing *Existing) error {
if err := s.UnmarshalYAML(node); err != nil {
return err
}
if s.Value == "" {
if existing != nil && existing.Name != "" && existing.Key != "" {
secret, err := clientgo.GetSecret(existing.Name, "caos-system")
if err != nil {
return errors.New("Error while reading existing secret")
}
value, found := secret.Data[existing.Key]
if !found {
return errors.New("Error while reading existing secret, key non-existent")
}
s.Value = string(value)
}
}
return nil
}
func unmarshal(s *Secret) (string, error) {
if s.Value == "" {
return "", nil
}
cipherText, err := base64.URLEncoding.DecodeString(s.Value)
if err != nil {
return "", err
}
if len(Masterkey) < 1 || len(Masterkey) > 32 {
return "", nil
//return errors.New("Master key size must be between 1 and 32 characters")
}
masterKeyLocal := make([]byte, 32)
for idx, char := range []byte(strings.Trim(Masterkey, "\n")) {
masterKeyLocal[idx] = char
}
block, err := aes.NewCipher(masterKeyLocal)
if err != nil {
return "", err
}
if len(cipherText) < aes.BlockSize {
return "", errors.New("Ciphertext block size is too short")
}
//IV needs to be unique, but doesn't have to be secure.
//It's common to put it at the beginning of the ciphertext.
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(cipherText, cipherText)
if !utf8.Valid(cipherText) {
return "", errors.New("Decryption failed")
}
// s.monitor.Info("Decoded and decrypted secret")
return string(cipherText), nil
}
func (s *Secret) Unmarshal(masterkey string) error {
Masterkey = masterkey
unm, err := unmarshal(s)
if err != nil {
return err
}
s.Value = unm
return nil
}
func (s *Secret) UnmarshalYAML(node *yaml.Node) error {
alias := new(secretAlias)
err := node.Decode(alias)
s.Encoding = alias.Encoding
s.Encryption = alias.Encryption
s.Value = alias.Value
if alias.Value == "" {
return nil
}
if len(Masterkey) < 1 || len(Masterkey) > 32 {
return nil
//return errors.New("Master key size must be between 1 and 32 characters")
}
unmarshalled, err := unmarshal(s)
if err != nil {
return err
}
// s.monitor.Info("Decoded and decrypted secret")
s.Encoding = alias.Encoding
s.Encryption = alias.Encryption
s.Value = unmarshalled
return nil
}
func (s *Secret) MarshalYAML() (interface{}, error) {
if s.Value == "" {
return nil, nil
}
if len(Masterkey) < 1 || len(Masterkey) > 32 {
return nil, errors.New("Master key size must be between 1 and 32 characters")
}
masterKey := make([]byte, 32)
for idx, char := range []byte(strings.Trim(Masterkey, "\n")) {
masterKey[idx] = char
}
c, err := aes.NewCipher(masterKey)
if err != nil {
return nil, err
}
cipherText := make([]byte, aes.BlockSize+len(s.Value))
iv := cipherText[:aes.BlockSize]
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(c, iv)
stream.XORKeyStream(cipherText[aes.BlockSize:], []byte(s.Value))
return &secretAlias{Encryption: "AES256", Encoding: "Base64", Value: base64.URLEncoding.EncodeToString(cipherText)}, nil
}
func InitIfNil(sec *Secret) *Secret {
if sec == nil {
return &Secret{}
}
return sec
}
func AppendSecrets(prefix string, into, add map[string]*Secret) {
for key, secret := range add {
name := key
if prefix != "" {
name = prefix + "." + name
}
into[name] = secret
}
}