-
Notifications
You must be signed in to change notification settings - Fork 13
/
secret.go
285 lines (239 loc) · 8.01 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-License-Identifier: Apache-2.0
package database
import (
"database/sql"
"encoding/base64"
"errors"
"strings"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/lib/pq"
)
var (
// ErrEmptySecretName defines the error type when a
// Secret type has an empty Name field provided.
ErrEmptySecretName = errors.New("empty secret name provided")
// ErrEmptySecretOrg defines the error type when a
// Secret type has an empty Org field provided.
ErrEmptySecretOrg = errors.New("empty secret org provided")
// ErrEmptySecretRepo defines the error type when a
// Secret type has an empty Repo field provided.
ErrEmptySecretRepo = errors.New("empty secret repo provided")
// ErrEmptySecretTeam defines the error type when a
// Secret type has an empty Team field provided.
ErrEmptySecretTeam = errors.New("empty secret team provided")
// ErrEmptySecretType defines the error type when a
// Secret type has an empty Type field provided.
ErrEmptySecretType = errors.New("empty secret type provided")
// ErrEmptySecretValue defines the error type when a
// Secret type has an empty Value field provided.
ErrEmptySecretValue = errors.New("empty secret value provided")
)
// Secret is the database representation of a secret.
type Secret struct {
ID sql.NullInt64 `sql:"id"`
Org sql.NullString `sql:"org"`
Repo sql.NullString `sql:"repo"`
Team sql.NullString `sql:"team"`
Name sql.NullString `sql:"name"`
Value sql.NullString `sql:"value"`
Type sql.NullString `sql:"type"`
Images pq.StringArray `sql:"images" gorm:"type:varchar(1000)"`
Events pq.StringArray `sql:"events" gorm:"type:varchar(1000)"`
AllowCommand sql.NullBool `sql:"allow_command"`
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullString `sql:"created_by"`
UpdatedAt sql.NullInt64 `sql:"updated_at"`
UpdatedBy sql.NullString `sql:"updated_by"`
}
// Decrypt will manipulate the existing secret value by
// base64 decoding that value. Then, a AES-256 cipher
// block is created from the encryption key in order to
// decrypt the base64 decoded secret value.
func (s *Secret) Decrypt(key string) error {
// base64 decode the encrypted secret value
decoded, err := base64.StdEncoding.DecodeString(s.Value.String)
if err != nil {
return err
}
// decrypt the base64 decoded secret value
decrypted, err := decrypt(key, decoded)
if err != nil {
return err
}
// set the decrypted secret value
s.Value = sql.NullString{
String: string(decrypted),
Valid: true,
}
return nil
}
// Encrypt will manipulate the existing secret value by
// creating a AES-256 cipher block from the encryption
// key in order to encrypt the secret value. Then, the
// secret value is base64 encoded for transport across
// network boundaries.
func (s *Secret) Encrypt(key string) error {
// encrypt the secret value
encrypted, err := encrypt(key, []byte(s.Value.String))
if err != nil {
return err
}
// base64 encode the encrypted secret data to make it network safe
s.Value = sql.NullString{
String: base64.StdEncoding.EncodeToString(encrypted),
Valid: true,
}
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Secret type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (s *Secret) Nullify() *Secret {
if s == nil {
return nil
}
// check if the ID field should be false
if s.ID.Int64 == 0 {
s.ID.Valid = false
}
// check if the Org field should be false
if len(s.Org.String) == 0 {
s.Org.Valid = false
}
// check if the Repo field should be false
if len(s.Repo.String) == 0 {
s.Repo.Valid = false
}
// check if the Team field should be false
if len(s.Team.String) == 0 {
s.Team.Valid = false
}
// check if the Name field should be false
if len(s.Name.String) == 0 {
s.Name.Valid = false
}
// check if the Value field should be false
if len(s.Value.String) == 0 {
s.Value.Valid = false
}
// check if the Value should be false
if len(s.Type.String) == 0 {
s.Type.Valid = false
}
// check if the CreatedAt field should be false
if s.CreatedAt.Int64 == 0 {
s.CreatedAt.Valid = false
}
// check if the CreatedBy field should be false
if len(s.CreatedBy.String) == 0 {
s.CreatedBy.Valid = false
}
// check if the UpdatedAt field should be false
if s.UpdatedAt.Int64 == 0 {
s.UpdatedAt.Valid = false
}
// check if the UpdatedBy field should be false
if len(s.UpdatedBy.String) == 0 {
s.UpdatedBy.Valid = false
}
return s
}
// ToLibrary converts the Secret type
// to a library Secret type.
func (s *Secret) ToLibrary() *library.Secret {
secret := new(library.Secret)
secret.SetID(s.ID.Int64)
secret.SetOrg(s.Org.String)
secret.SetRepo(s.Repo.String)
secret.SetTeam(s.Team.String)
secret.SetName(s.Name.String)
secret.SetValue(s.Value.String)
secret.SetType(s.Type.String)
secret.SetImages(s.Images)
secret.SetEvents(s.Events)
secret.SetAllowCommand(s.AllowCommand.Bool)
secret.SetCreatedAt(s.CreatedAt.Int64)
secret.SetCreatedBy(s.CreatedBy.String)
secret.SetUpdatedAt(s.UpdatedAt.Int64)
secret.SetUpdatedBy(s.UpdatedBy.String)
return secret
}
// Validate verifies the necessary fields for
// the Secret type are populated correctly.
func (s *Secret) Validate() error {
// verify the Type field is populated
if len(s.Type.String) == 0 {
return ErrEmptySecretType
}
// verify the Org field is populated
if len(s.Org.String) == 0 {
return ErrEmptySecretOrg
}
// check if an org or repo secret
if strings.EqualFold(s.Type.String, constants.SecretRepo) ||
strings.EqualFold(s.Type.String, constants.SecretOrg) {
// verify the Repo field is populated
if len(s.Repo.String) == 0 {
return ErrEmptySecretRepo
}
}
// check if a shared secret
if strings.EqualFold(s.Type.String, constants.SecretShared) {
// verify the Team field is populated
if len(s.Team.String) == 0 {
return ErrEmptySecretTeam
}
}
// verify the Name field is populated
if len(s.Name.String) == 0 {
return ErrEmptySecretName
}
// verify the Value field is populated
if len(s.Value.String) == 0 {
return ErrEmptySecretValue
}
// ensure that all Secret string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
s.Org = sql.NullString{String: sanitize(s.Org.String), Valid: s.Org.Valid}
s.Repo = sql.NullString{String: sanitize(s.Repo.String), Valid: s.Repo.Valid}
s.Team = sql.NullString{String: sanitize(s.Team.String), Valid: s.Team.Valid}
s.Name = sql.NullString{String: sanitize(s.Name.String), Valid: s.Name.Valid}
s.Type = sql.NullString{String: sanitize(s.Type.String), Valid: s.Type.Valid}
// ensure that all Images are sanitized
// to avoid unsafe HTML content
for i, v := range s.Images {
s.Images[i] = sanitize(v)
}
// ensure that all Events are sanitized
// to avoid unsafe HTML content
for i, v := range s.Events {
s.Events[i] = sanitize(v)
}
return nil
}
// SecretFromLibrary converts the library Secret type
// to a database Secret type.
func SecretFromLibrary(s *library.Secret) *Secret {
secret := &Secret{
ID: sql.NullInt64{Int64: s.GetID(), Valid: true},
Org: sql.NullString{String: s.GetOrg(), Valid: true},
Repo: sql.NullString{String: s.GetRepo(), Valid: true},
Team: sql.NullString{String: s.GetTeam(), Valid: true},
Name: sql.NullString{String: s.GetName(), Valid: true},
Value: sql.NullString{String: s.GetValue(), Valid: true},
Type: sql.NullString{String: s.GetType(), Valid: true},
Images: pq.StringArray(s.GetImages()),
Events: pq.StringArray(s.GetEvents()),
AllowCommand: sql.NullBool{Bool: s.GetAllowCommand(), Valid: true},
CreatedAt: sql.NullInt64{Int64: s.GetCreatedAt(), Valid: true},
CreatedBy: sql.NullString{String: s.GetCreatedBy(), Valid: true},
UpdatedAt: sql.NullInt64{Int64: s.GetUpdatedAt(), Valid: true},
UpdatedBy: sql.NullString{String: s.GetUpdatedBy(), Valid: true},
}
return secret.Nullify()
}