-
Notifications
You must be signed in to change notification settings - Fork 124
/
project_vars.go
406 lines (369 loc) · 11.5 KB
/
project_vars.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package model
import (
"fmt"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/mongodb/anser/bsonutil"
adb "github.com/mongodb/anser/db"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
var (
projectVarIdKey = bsonutil.MustHaveTag(ProjectVars{}, "Id")
projectVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "Vars")
privateVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "PrivateVars")
adminOnlyVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "AdminOnlyVars")
)
const (
ProjectVarsCollection = "project_vars"
ProjectAWSSSHKeyName = "__project_aws_ssh_key_name"
ProjectAWSSSHKeyValue = "__project_aws_ssh_key_value"
)
// ProjectVars holds a map of variables specific to a given project.
// They can be fetched at run time by the agent, so that settings which are
// sensitive or subject to frequent change don't need to be hard-coded into
// yml files.
type ProjectVars struct {
//Should match the identifier of the project it refers to
Id string `bson:"_id" json:"_id"`
//The actual mapping of variables for this project
Vars map[string]string `bson:"vars" json:"vars"`
//PrivateVars keeps track of which variables are private and should therefore not
//be returned to the UI server.
PrivateVars map[string]bool `bson:"private_vars" json:"private_vars"`
// AdminOnlyVars keeps track of variables that are only accessible by project admins
AdminOnlyVars map[string]bool `bson:"admin_only_vars" json:"admin_only_vars"`
}
type AWSSSHKey struct {
Name string
Value string
}
func FindOneProjectVars(projectId string) (*ProjectVars, error) {
projectVars := &ProjectVars{}
q := db.Query(bson.M{projectVarIdKey: projectId})
err := db.FindOneQ(ProjectVarsCollection, q, projectVars)
if adb.ResultsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return projectVars, nil
}
// FindMergedProjectVars merges vars from the target project's ProjectVars and its parent repo's vars
func FindMergedProjectVars(projectID string) (*ProjectVars, error) {
project, err := FindBranchProjectRef(projectID)
if err != nil {
return nil, errors.Wrapf(err, "getting project '%s'", projectID)
}
if project == nil {
return nil, errors.Errorf("project '%s' does not exist", projectID)
}
projectVars, err := FindOneProjectVars(project.Id)
if err != nil {
return nil, errors.Wrapf(err, "getting project vars for project '%s'", projectID)
}
if !project.UseRepoSettings() {
return projectVars, nil
}
repoVars, err := FindOneProjectVars(project.RepoRefId)
if err != nil {
return nil, errors.Wrapf(err, "getting project vars for repo '%s'", project.RepoRefId)
}
if repoVars == nil {
return projectVars, nil
}
if projectVars == nil {
repoVars.Id = project.Id
return repoVars, nil
}
projectVars.MergeWithRepoVars(repoVars)
return projectVars, nil
}
// UpdateProjectVarsByValue searches all projects who have a variable set to the toReplace input parameter, and replaces all
// matching project variables with the replacement input parameter. If dryRun is set to true, the update is not performed.
// We return a list of keys that were replaced (or, the list of keys that would be replaced in the case that dryRun is true).
func UpdateProjectVarsByValue(toReplace, replacement, username string, dryRun bool) (map[string][]string, error) {
catcher := grip.NewBasicCatcher()
matchingProjectVars, err := getVarsByValue(toReplace)
if err != nil {
catcher.Wrap(err, "fetching projects with matching value")
}
if matchingProjectVars == nil {
catcher.New("no projects with matching value found")
}
changes := map[string][]string{}
for _, projectVars := range matchingProjectVars {
for key, val := range projectVars.Vars {
if val == toReplace {
if !dryRun {
var beforeVars ProjectVars
err = util.DeepCopy(*projectVars, &beforeVars)
if err != nil {
catcher.Wrap(err, "copying project variables")
continue
}
before := ProjectSettings{
Vars: beforeVars,
}
projectVars.Vars[key] = replacement
err = projectVars.updateSingleVar(key, replacement)
if err != nil {
catcher.Wrapf(err, "overwriting variable '%s' for project '%s'", key, projectVars.Id)
continue
}
after := ProjectSettings{
Vars: *projectVars,
}
if err = LogProjectModified(projectVars.Id, username, &before, &after); err != nil {
catcher.Wrapf(err, "logging project modification for project '%s'", projectVars.Id)
}
}
changes[projectVars.Id] = append(changes[projectVars.Id], key)
}
}
}
return changes, catcher.Resolve()
}
func (projectVars *ProjectVars) updateSingleVar(key, val string) error {
if len(projectVars.Vars) == 0 && len(projectVars.PrivateVars) == 0 &&
len(projectVars.AdminOnlyVars) == 0 {
return nil
}
return db.Update(
ProjectVarsCollection,
bson.M{
projectVarIdKey: projectVars.Id,
},
bson.M{
"$set": bson.M{
bsonutil.GetDottedKeyName(projectVarsMapKey, key): val,
},
},
)
}
// CopyProjectVars copies the variables for the first project to the second
func CopyProjectVars(oldProjectId, newProjectId string) error {
vars, err := FindOneProjectVars(oldProjectId)
if err != nil {
return errors.Wrapf(err, "finding variables for project '%s'", oldProjectId)
}
if vars == nil {
vars = &ProjectVars{}
}
vars.Id = newProjectId
_, err = vars.Upsert()
return errors.Wrapf(err, "inserting variables for project '%s", newProjectId)
}
func SetAWSKeyForProject(projectId string, ssh *AWSSSHKey) error {
vars, err := FindOneProjectVars(projectId)
if err != nil {
return errors.Wrap(err, "getting project vars")
}
if vars == nil {
vars = &ProjectVars{}
}
if vars.Vars == nil {
vars.Vars = map[string]string{}
}
if vars.PrivateVars == nil {
vars.PrivateVars = map[string]bool{}
}
vars.Vars[ProjectAWSSSHKeyName] = ssh.Name
vars.Vars[ProjectAWSSSHKeyValue] = ssh.Value
vars.PrivateVars[ProjectAWSSSHKeyValue] = true // redact value, but not key name
_, err = vars.Upsert()
return errors.Wrap(err, "saving project keys")
}
func GetAWSKeyForProject(projectId string) (*AWSSSHKey, error) {
vars, err := FindMergedProjectVars(projectId)
if err != nil {
return nil, errors.Wrap(err, "getting project vars")
}
if vars == nil {
return nil, errors.New("no variables for project")
}
return &AWSSSHKey{
Name: vars.Vars[ProjectAWSSSHKeyName],
Value: vars.Vars[ProjectAWSSSHKeyValue],
}, nil
}
func (projectVars *ProjectVars) Upsert() (*adb.ChangeInfo, error) {
return db.Upsert(
ProjectVarsCollection,
bson.M{
projectVarIdKey: projectVars.Id,
},
bson.M{
"$set": bson.M{
projectVarsMapKey: projectVars.Vars,
privateVarsMapKey: projectVars.PrivateVars,
adminOnlyVarsMapKey: projectVars.AdminOnlyVars,
},
},
)
}
func (projectVars *ProjectVars) Insert() error {
return db.Insert(
ProjectVarsCollection,
projectVars,
)
}
func (projectVars *ProjectVars) FindAndModify(varsToDelete []string) (*adb.ChangeInfo, error) {
setUpdate := bson.M{}
unsetUpdate := bson.M{}
update := bson.M{}
if len(projectVars.Vars) == 0 && len(projectVars.PrivateVars) == 0 &&
len(projectVars.AdminOnlyVars) == 0 && len(varsToDelete) == 0 {
return nil, nil
}
for key, val := range projectVars.Vars {
setUpdate[bsonutil.GetDottedKeyName(projectVarsMapKey, key)] = val
}
for key, val := range projectVars.PrivateVars {
setUpdate[bsonutil.GetDottedKeyName(privateVarsMapKey, key)] = val
}
for key, val := range projectVars.AdminOnlyVars {
setUpdate[bsonutil.GetDottedKeyName(adminOnlyVarsMapKey, key)] = val
}
if len(setUpdate) > 0 {
update["$set"] = setUpdate
}
for _, val := range varsToDelete {
unsetUpdate[bsonutil.GetDottedKeyName(projectVarsMapKey, val)] = 1
unsetUpdate[bsonutil.GetDottedKeyName(privateVarsMapKey, val)] = 1
unsetUpdate[bsonutil.GetDottedKeyName(adminOnlyVarsMapKey, val)] = 1
}
if len(unsetUpdate) > 0 {
update["$unset"] = unsetUpdate
}
return db.FindAndModify(
ProjectVarsCollection,
bson.M{projectVarIdKey: projectVars.Id},
nil,
adb.Change{
Update: update,
ReturnNew: true,
Upsert: true,
},
projectVars,
)
}
func (projectVars *ProjectVars) GetVars(t *task.Task) map[string]string {
vars := map[string]string{}
isAdmin := projectVars.ShouldGetAdminOnlyVars(t)
for k, v := range projectVars.Vars {
if !projectVars.AdminOnlyVars[k] || isAdmin {
vars[k] = v
}
}
return vars
}
func (projectVars *ProjectVars) ShouldGetAdminOnlyVars(t *task.Task) bool {
if utility.StringSliceContains(evergreen.SystemVersionRequesterTypes, t.Requester) {
return true
} else if t.ActivatedBy == "" {
return false
}
u, err := user.FindOneById(t.ActivatedBy)
if err != nil {
grip.Error(message.WrapError(err, message.Fields{
"message": fmt.Sprintf("problem with fetching user '%s'", t.ActivatedBy),
"task_id": t.Id,
}))
return false
}
isAdmin := false
if u != nil {
isAdmin = u.HasPermission(gimlet.PermissionOpts{
Resource: t.Project,
ResourceType: evergreen.ProjectResourceType,
Permission: evergreen.PermissionProjectSettings,
RequiredLevel: evergreen.ProjectSettingsEdit.Value,
})
}
return isAdmin
}
func (projectVars *ProjectVars) RedactPrivateVars() *ProjectVars {
res := &ProjectVars{
Vars: map[string]string{},
PrivateVars: map[string]bool{},
AdminOnlyVars: map[string]bool{},
}
if projectVars == nil {
return res
}
res.Id = projectVars.Id
if projectVars.Vars == nil {
return res
}
if projectVars.AdminOnlyVars == nil {
res.AdminOnlyVars = map[string]bool{}
}
if projectVars.PrivateVars == nil {
res.PrivateVars = map[string]bool{}
}
// Redact private variables
for k, v := range projectVars.Vars {
if val, ok := projectVars.PrivateVars[k]; ok && val {
res.Vars[k] = ""
res.PrivateVars[k] = projectVars.PrivateVars[k]
} else {
res.Vars[k] = v
}
if val, ok := projectVars.AdminOnlyVars[k]; ok && val {
res.AdminOnlyVars[k] = projectVars.AdminOnlyVars[k]
}
}
return res
}
func getVarsByValue(val string) ([]*ProjectVars, error) {
matchingProjects := []*ProjectVars{}
pipeline := []bson.M{
{"$addFields": bson.M{projectVarsMapKey: bson.M{"$objectToArray": "$" + projectVarsMapKey}}},
{"$match": bson.M{bsonutil.GetDottedKeyName(projectVarsMapKey, "v"): val}},
{"$addFields": bson.M{projectVarsMapKey: bson.M{"$arrayToObject": "$" + projectVarsMapKey}}},
}
err := db.Aggregate(ProjectVarsCollection, pipeline, &matchingProjects)
if adb.ResultsNotFound(err) {
return nil, nil
}
if err != nil {
return nil, err
}
return matchingProjects, nil
}
// MergeWithRepoVars merges the project and repo variables
func (projectVars *ProjectVars) MergeWithRepoVars(repoVars *ProjectVars) {
if projectVars.Vars == nil {
projectVars.Vars = map[string]string{}
}
if projectVars.PrivateVars == nil {
projectVars.PrivateVars = map[string]bool{}
}
if projectVars.AdminOnlyVars == nil {
projectVars.AdminOnlyVars = map[string]bool{}
}
if repoVars == nil {
return
}
// Branch-level vars have priority, so we only need to add a repo vars if it doesn't already exist in the branch
for key, val := range repoVars.Vars {
if _, ok := projectVars.Vars[key]; !ok {
projectVars.Vars[key] = val
if v, ok := repoVars.PrivateVars[key]; ok {
projectVars.PrivateVars[key] = v
}
if v, ok := repoVars.AdminOnlyVars[key]; ok {
projectVars.AdminOnlyVars[key] = v
}
}
}
}