/
pipeline_invariants.go
79 lines (64 loc) · 1.8 KB
/
pipeline_invariants.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
package signature
import (
"fmt"
"strings"
"github.com/buildkite/go-pipeline"
)
var _ SignedFielder = (*CommandStepWithInvariants)(nil)
// CommandStepWithInvariants is a CommandStep with PipelineInvariants.
type CommandStepWithInvariants struct {
pipeline.CommandStep
RepositoryURL string
}
// SignedFields returns the default fields for signing.
func (c *CommandStepWithInvariants) SignedFields() (map[string]any, error) {
return map[string]any{
"command": c.Command,
"env": c.Env,
"plugins": c.Plugins,
"matrix": c.Matrix,
"repository_url": c.RepositoryURL,
}, nil
}
// ValuesForFields returns the contents of fields to sign.
func (c *CommandStepWithInvariants) ValuesForFields(fields []string) (map[string]any, error) {
// Make a set of required fields. As fields is processed, mark them off by
// deleting them.
required := map[string]struct{}{
"command": {},
"env": {},
"plugins": {},
"matrix": {},
"repository_url": {},
}
out := make(map[string]any, len(fields))
for _, f := range fields {
delete(required, f)
switch f {
case "command":
out["command"] = c.Command
case "env":
out["env"] = c.Env
case "plugins":
out["plugins"] = c.Plugins
case "matrix":
out["matrix"] = c.Matrix
case "repository_url":
out["repository_url"] = c.RepositoryURL
default:
// All env:: values come from outside the step.
if strings.HasPrefix(f, EnvNamespacePrefix) {
break
}
return nil, fmt.Errorf("unknown or unsupported field for signing %q", f)
}
}
if len(required) > 0 {
missing := make([]string, 0, len(required))
for k := range required {
missing = append(missing, k)
}
return nil, fmt.Errorf("one or more required fields are not present: %v", missing)
}
return out, nil
}