-
Notifications
You must be signed in to change notification settings - Fork 7
/
policy.go
383 lines (344 loc) · 12.3 KB
/
policy.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
package policy
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
securejoin "github.com/cyphar/filepath-securejoin"
git "github.com/go-git/go-git/v5"
"github.com/lunarway/release-manager/internal/commitinfo"
internalgit "github.com/lunarway/release-manager/internal/git"
"github.com/lunarway/release-manager/internal/log"
"github.com/lunarway/release-manager/internal/tracing"
"github.com/lunarway/release-manager/internal/try"
"github.com/pkg/errors"
)
var (
// ErrNotParsable indicates that a policies file could not be parsed against
// the specification.
ErrNotParsable = errors.New("policies not parsable")
// ErrUnknownFields indicates that a policies file contains an unknown field.
ErrUnknownFields = errors.New("policies contains unknown fields")
// ErrNotFound indicates that policies are not found for a service.
ErrNotFound = errors.New("not found")
// ErrConflict indicates that polices are not compatible
ErrConflict = errors.New("conflict")
)
type Service struct {
Tracer tracing.Tracer
Git GitService
MaxRetries int
GlobalBranchRestrictionPolicies []BranchRestriction
}
type GitService interface {
MasterPath() string
Clone(context.Context, string) (*git.Repository, error)
Commit(ctx context.Context, rootPath, changesPath, msg string) error
}
type Actor struct {
Name string
Email string
}
// GetAutoReleases gets stored auto-release policies for service svc. If no
// policies are found a nil slice is returned.
func (s *Service) GetAutoReleases(ctx context.Context, svc, branch string) ([]AutoReleasePolicy, error) {
span, ctx := s.Tracer.FromCtx(ctx, "policy.GetAutoReleases")
defer span.Finish()
policies, err := s.Get(ctx, svc)
if err != nil {
if errors.Cause(err) == ErrNotFound {
return nil, nil
}
return nil, err
}
var autoReleases []AutoReleasePolicy
for i := range policies.AutoReleases {
if policies.AutoReleases[i].Branch == branch {
autoReleases = append(autoReleases, policies.AutoReleases[i])
}
}
return autoReleases, nil
}
// Get gets stored policies for service svc. If no policies are stored
// ErrNotFound is returned. This method also returns globally configured
// policies along with the service specific ones.
func (s *Service) Get(ctx context.Context, svc string) (Policies, error) {
span, ctx := s.Tracer.FromCtx(ctx, "policy.Get")
defer span.Finish()
policies, err := s.servicePolicies(svc)
if err != nil {
// we will only return if an unknown error occoured and no global policies
// are defined.
if err != ErrNotFound || len(s.GlobalBranchRestrictionPolicies) == 0 {
return Policies{}, err
}
policies = Policies{
Service: svc,
}
}
// merge global policies with local ones where globals take precedence
policies.BranchRestrictions = mergeBranchRestrictions(ctx, svc, s.GlobalBranchRestrictionPolicies, policies.BranchRestrictions)
log.WithContext(ctx).WithFields("globalPolicies", s.GlobalBranchRestrictionPolicies, "localPolicies", policies).Infof("Found %d policies", len(policies.BranchRestrictions)+len(policies.AutoReleases))
// a policy file might exist, but if all policies have been removed from it
// we can just act as if it didn't exist
if !policies.HasPolicies() {
return Policies{}, ErrNotFound
}
return policies, nil
}
// servicePolicies returns policies for a specific service. If no policy file is
// found ErrNotFound is returned.
func (s *Service) servicePolicies(svc string) (Policies, error) {
// make sure policy directory exists
policiesDir := path.Join(s.Git.MasterPath(), "policies")
policiesPath, err := securejoin.SecureJoin(policiesDir, fmt.Sprintf("%s.json", svc))
if err != nil {
return Policies{}, errors.WithMessage(err, "join policy path")
}
policiesFile, err := os.OpenFile(policiesPath, os.O_RDONLY, os.ModePerm)
if err != nil {
if os.IsNotExist(err) {
return Policies{}, ErrNotFound
}
return Policies{}, errors.WithMessagef(err, "open policies in '%s'", policiesPath)
}
defer policiesFile.Close()
policies, err := parse(policiesFile)
if err != nil {
return Policies{}, errors.WithMessagef(err, "parse policies in '%s'", policiesPath)
}
return policies, nil
}
func mergeBranchRestrictions(ctx context.Context, svc string, global, local []BranchRestriction) []BranchRestriction {
if len(global) == 0 {
return local
}
branchRestrictions := append([]BranchRestriction(nil), global...)
// copy all local restrictions over that does not conflict with the global
// one
for _, localRestriction := range local {
if conflictingBranchRestriction(ctx, svc, global, localRestriction) {
continue
}
branchRestrictions = append(branchRestrictions, localRestriction)
}
return branchRestrictions
}
func conflictingBranchRestriction(ctx context.Context, svc string, global []BranchRestriction, localRestriction BranchRestriction) bool {
for _, globalRestriction := range global {
if globalRestriction.Environment == localRestriction.Environment && globalRestriction.BranchRegex != localRestriction.BranchRegex {
log.WithContext(ctx).WithFields("global", globalRestriction, "local", localRestriction).Errorf("Global and local branch restriction policies conflict for service '%s': local policy dropped", svc)
return true
}
}
return false
}
// ApplyAutoRelease applies an auto-release policy for service svc from branch
// to environment env.
func (s *Service) ApplyAutoRelease(ctx context.Context, actor Actor, svc, branch, env string) (string, error) {
span, ctx := s.Tracer.FromCtx(ctx, "policy.ApplyAutoRelease")
defer span.Finish()
ok, err := s.CanRelease(ctx, svc, branch, env)
if err != nil {
return "", errors.WithMessage(err, "validate release policies")
}
if !ok {
return "", ErrConflict
}
commitMsg := commitinfo.PolicyUpdateApplyCommitMessage(env, svc, "auto-release")
var policyID string
err = s.updatePolicies(ctx, actor, svc, commitMsg, func(p *Policies) {
policyID = p.SetAutoRelease(branch, env)
})
if err != nil {
return "", err
}
return policyID, nil
}
// Delete deletes policies by ID for service svc.
func (s *Service) Delete(ctx context.Context, actor Actor, svc string, ids []string) (int, error) {
span, ctx := s.Tracer.FromCtx(ctx, "policy.Delete")
defer span.Finish()
commitMsg := commitinfo.PolicyUpdateDeleteCommitMessage(svc)
var deleted int
err := s.updatePolicies(ctx, actor, svc, commitMsg, func(p *Policies) {
deleted = p.Delete(ids...)
})
if err != nil {
return 0, err
}
return deleted, nil
}
func (s *Service) updatePolicies(ctx context.Context, actor Actor, svc, commitMsg string, f func(p *Policies)) error {
span, ctx := s.Tracer.FromCtx(ctx, "policy.updatePolicies")
defer span.Finish()
return try.Do(ctx, s.Tracer, s.MaxRetries, func(ctx context.Context, attempt int) (bool, error) {
configRepoPath, close, err := internalgit.TempDirAsync(ctx, s.Tracer, "k8s-config-notify")
if err != nil {
return true, err
}
defer close(ctx)
logger := log.WithContext(ctx)
// read part of this code is the same as the Get function but differs in the
// file flags used. This is to avoid opening and closing to file multiple
// times during the operation.
logger.Debugf("internal/policy: clone config repository")
_, err = s.Git.Clone(ctx, configRepoPath)
if err != nil {
return true, errors.WithMessage(err, fmt.Sprintf("clone to '%s'", configRepoPath))
}
// make sure policy directory exists
logger.Debugf("internal/policy: ensure policies directory")
policiesDir := path.Join(configRepoPath, "policies")
err = os.MkdirAll(policiesDir, os.ModePerm)
if err != nil {
return true, errors.WithMessagef(err, "make policies directory '%s'", policiesDir)
}
policiesPath, err := securejoin.SecureJoin(policiesDir, fmt.Sprintf("%s.json", svc))
if err != nil {
return true, errors.WithMessagef(err, "join policy path '%s'", policiesDir)
}
logger.Debugf("internal/policy: open policies file '%s'", policiesPath)
policiesFile, err := os.OpenFile(policiesPath, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return true, errors.WithMessagef(err, "open policies in '%s'", policiesPath)
}
defer policiesFile.Close()
// read existing policies
logger.Debugf("internal/policy: parse policies file '%s'", policiesPath)
policies, err := parse(policiesFile)
if err != nil {
return true, errors.WithMessagef(err, "parse policies in '%s'", policiesPath)
}
logger.Debugf("internal/policy: parseed policy: %+v", policies)
policies.Service = svc
f(&policies)
// store file
// truncate and reset the offset of the file before writing to it
// to overwrite the contents
err = policiesFile.Truncate(0)
if err != nil {
return true, errors.WithMessagef(err, "truncate file '%s'", policiesPath)
}
_, err = policiesFile.Seek(0, 0)
if err != nil {
return true, errors.WithMessagef(err, "reset seek on '%s'", policiesPath)
}
logger.Debugf("internal/policy: persist policies file '%s'", policiesPath)
err = persist(policiesFile, policies)
if err != nil {
return true, errors.WithMessagef(err, "write policies in '%s'", policiesPath)
}
// commit changes
logger.Debugf("internal/policy: commit policies file '%s'", policiesPath)
err = s.Git.Commit(ctx, configRepoPath, path.Join(".", "policies"), commitMsg)
if err != nil {
// indicates that the applied policy was already set
if errors.Cause(err) == internalgit.ErrNothingToCommit {
return true, nil
}
return false, errors.WithMessage(err, fmt.Sprintf("commit changes from path '%s'", policiesPath))
}
return true, nil
})
}
func parse(r io.Reader) (Policies, error) {
var p Policies
decoder := json.NewDecoder(r)
decoder.DisallowUnknownFields()
err := decoder.Decode(&p)
if err != nil {
if err == io.EOF {
return Policies{}, nil
}
_, ok := err.(*json.SyntaxError)
if ok {
return Policies{}, ErrNotParsable
}
// there is no other way to detect this error type unfortunately
// https://github.com/golang/go/blob/277609f844ed9254d25e975f7cf202d042beecc6/src/encoding/json/decode.go#L739
if strings.HasPrefix(err.Error(), "json: unknown field") {
return Policies{}, errors.WithMessagef(ErrUnknownFields, "%v", err)
}
return Policies{}, errors.WithMessage(err, "decode policy as json")
}
return p, nil
}
func persist(w io.Writer, p Policies) error {
encode := json.NewEncoder(w)
encode.SetIndent("", " ")
err := encode.Encode(p)
if err != nil {
return err
}
return nil
}
type Policies struct {
Service string `json:"service,omitempty"`
AutoReleases []AutoReleasePolicy `json:"autoReleases,omitempty"`
BranchRestrictions []BranchRestriction `json:"branchRestrictions,omitempty"`
}
type AutoReleasePolicy struct {
ID string `json:"id,omitempty"`
Branch string `json:"branch,omitempty"`
Environment string `json:"environment,omitempty"`
}
// HasPolicies returns whether any policies are applied.
func (p *Policies) HasPolicies() bool {
return len(p.AutoReleases) != 0 || len(p.BranchRestrictions) != 0
}
// SetAutoRelease sets an auto-release policy for specified branch and
// environment.
//
// If an auto-release policy exists for the same environment it is overwritten.
func (p *Policies) SetAutoRelease(branch, env string) string {
id := fmt.Sprintf("auto-release-%s-%s", branch, env)
newPolicy := AutoReleasePolicy{
ID: id,
Branch: branch,
Environment: env,
}
newPolicies := make([]AutoReleasePolicy, len(p.AutoReleases))
var replaced bool
for i, policy := range p.AutoReleases {
if policy.Environment == env {
newPolicies[i] = newPolicy
replaced = true
continue
}
newPolicies[i] = p.AutoReleases[i]
}
if !replaced {
newPolicies = append(newPolicies, newPolicy)
}
p.AutoReleases = newPolicies
return id
}
// Delete deletes any policies with a matching id.
func (p *Policies) Delete(ids ...string) int {
var deleted int
for _, id := range ids {
var filtered []AutoReleasePolicy
for i := range p.AutoReleases {
if p.AutoReleases[i].ID != id {
filtered = append(filtered, p.AutoReleases[i])
continue
}
deleted++
}
p.AutoReleases = filtered
var filteredBranchRestrictions []BranchRestriction
for i := range p.BranchRestrictions {
if p.BranchRestrictions[i].ID != id {
filteredBranchRestrictions = append(filteredBranchRestrictions, p.BranchRestrictions[i])
continue
}
deleted++
}
p.BranchRestrictions = filteredBranchRestrictions
}
return deleted
}