Skip to content

Commit d980fc9

Browse files
committed
add support for secret plugin
1 parent c588598 commit d980fc9

File tree

2 files changed

+122
-51
lines changed

2 files changed

+122
-51
lines changed

action/pipeline/exec.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ func reportMissingSecrets(s map[string]string) {
250250

251251
// collectMissingSecrets searches a given pipeline for used secrets
252252
// and returns a map of secrets not set in the current environment.
253-
// The map key is is the step or stage+step name formatted to match
254-
// the local exec log output.
253+
// The map key is is the step, stage+step, or secret name formatted
254+
// to match the local exec log output.
255255
func collectMissingSecrets(p *pipeline.Build) map[string]string {
256256
if p == nil {
257257
return make(map[string]string)
@@ -262,19 +262,28 @@ func collectMissingSecrets(p *pipeline.Build) map[string]string {
262262
for _, stage := range p.Stages {
263263
for _, step := range stage.Steps {
264264
for _, secret := range step.Secrets {
265-
stepName := formatStepIdentifier(stage.Name, step.Name)
265+
stepName := formatStepIdentifier(stage.Name, step.Name, false)
266266
secrets[stepName] = secret.Target
267267
}
268268
}
269269
}
270270

271271
for _, step := range p.Steps {
272272
for _, secret := range step.Secrets {
273-
stepName := formatStepIdentifier("", step.Name)
273+
stepName := formatStepIdentifier("", step.Name, false)
274274
secrets[stepName] = secret.Target
275275
}
276276
}
277277

278+
for _, s := range p.Secrets {
279+
if !s.Origin.Empty() {
280+
for _, secret := range s.Origin.Secrets {
281+
stepName := formatStepIdentifier("", s.Origin.Name, true)
282+
secrets[stepName] = secret.Target
283+
}
284+
}
285+
}
286+
278287
for step, secret := range secrets {
279288
// if the secret was supplied, remove it from the map
280289
// we only care about unset secrets
@@ -294,15 +303,25 @@ func collectMissingSecrets(p *pipeline.Build) map[string]string {
294303
// formatStepIdentifier formats a step name to be consistent with what
295304
// the worker logs to make it easier to associate a missing secret
296305
// with a step.
297-
func formatStepIdentifier(stageName, stepName string) string {
306+
func formatStepIdentifier(stageName, stepName string, isSecret bool) string {
307+
const (
308+
secretPrefix = "[secret: %s]" //nolint:gosec // false positive
309+
stagePrefix = "[stage: %s]"
310+
stepPrefix = "[step: %s]"
311+
)
312+
298313
output := strings.Builder{}
299314

300315
if stageName != "" {
301-
output.WriteString(fmt.Sprintf("[stage: %s]", stageName))
316+
output.WriteString(fmt.Sprintf(stagePrefix, stageName))
302317
}
303318

304319
if stepName != "" {
305-
output.WriteString(fmt.Sprintf("[step: %s]", stepName))
320+
if isSecret {
321+
output.WriteString(fmt.Sprintf(secretPrefix, stepName))
322+
} else {
323+
output.WriteString(fmt.Sprintf(stepPrefix, stepName))
324+
}
306325
}
307326

308327
return output.String()

action/pipeline/exec_test.go

Lines changed: 96 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
package pipeline
44

55
import (
6-
"io"
76
"reflect"
87
"testing"
98

10-
"github.com/sirupsen/logrus"
11-
129
"github.com/go-vela/server/compiler/types/pipeline"
1310
)
1411

@@ -91,6 +88,55 @@ func TestCollectMissingSecrets(t *testing.T) {
9188
},
9289
want: map[string]string{"[stage: stage1][step: step1]": "STAGE_SECRET"},
9390
},
91+
{
92+
name: "secret plugin - supplied",
93+
pipeline: &pipeline.Build{
94+
Steps: []*pipeline.Container{
95+
{
96+
Name: "step1",
97+
},
98+
},
99+
Secrets: []*pipeline.Secret{
100+
{
101+
Origin: &pipeline.Container{
102+
Name: "secret from elsewhere",
103+
Secrets: pipeline.StepSecretSlice{
104+
{
105+
Source: "source",
106+
Target: "SECRET_FOR_SECRET",
107+
},
108+
},
109+
},
110+
},
111+
},
112+
},
113+
envVars: map[string]string{"SECRET_FOR_SECRET": "value"},
114+
want: map[string]string{},
115+
},
116+
{
117+
name: "secret plugin - missing",
118+
pipeline: &pipeline.Build{
119+
Steps: []*pipeline.Container{
120+
{
121+
Name: "step1",
122+
},
123+
},
124+
Secrets: []*pipeline.Secret{
125+
{
126+
Origin: &pipeline.Container{
127+
Name: "secret from elsewhere",
128+
Secrets: pipeline.StepSecretSlice{
129+
{
130+
Source: "source",
131+
Target: "SECRET_FOR_SECRET",
132+
},
133+
},
134+
},
135+
},
136+
},
137+
},
138+
want: map[string]string{"[secret: secret from elsewhere]": "SECRET_FOR_SECRET"},
139+
},
94140
{
95141
name: "provided step secret but value empty",
96142
pipeline: &pipeline.Build{
@@ -133,6 +179,53 @@ func TestCollectMissingSecrets(t *testing.T) {
133179
}
134180
}
135181

182+
func TestFormatStepIdentifier(t *testing.T) {
183+
tests := []struct {
184+
name string
185+
stageName string
186+
stepName string
187+
isSecret bool
188+
want string
189+
}{
190+
{
191+
name: "basic format",
192+
stageName: "build",
193+
stepName: "test",
194+
isSecret: false,
195+
want: "[stage: build][step: test]",
196+
},
197+
{
198+
name: "empty stage name",
199+
stageName: "",
200+
stepName: "test",
201+
isSecret: false,
202+
want: "[step: test]",
203+
},
204+
{
205+
name: "secret format",
206+
stageName: "",
207+
stepName: "test",
208+
isSecret: true,
209+
want: "[secret: test]",
210+
},
211+
{
212+
name: "empty stage and step name",
213+
stageName: "",
214+
stepName: "",
215+
isSecret: false,
216+
want: "",
217+
},
218+
}
219+
220+
for _, tt := range tests {
221+
t.Run(tt.name, func(t *testing.T) {
222+
if got := formatStepIdentifier(tt.stageName, tt.stepName, tt.isSecret); got != tt.want {
223+
t.Errorf("formatStepIdentifier() = %v, want %v", got, tt.want)
224+
}
225+
})
226+
}
227+
}
228+
136229
func TestSkipSteps(t *testing.T) {
137230
tests := []struct {
138231
name string
@@ -267,44 +360,3 @@ func TestSkipSteps(t *testing.T) {
267360
})
268361
}
269362
}
270-
271-
func TestFormatStepIdentifier(t *testing.T) {
272-
tests := []struct {
273-
name string
274-
stageName string
275-
stepName string
276-
want string
277-
}{
278-
{
279-
name: "basic format",
280-
stageName: "build",
281-
stepName: "test",
282-
want: "[stage: build][step: test]",
283-
},
284-
{
285-
name: "empty stage name",
286-
stageName: "",
287-
stepName: "test",
288-
want: "[step: test]",
289-
},
290-
{
291-
name: "empty stage and step name",
292-
stageName: "",
293-
stepName: "",
294-
want: "",
295-
},
296-
}
297-
298-
for _, tt := range tests {
299-
t.Run(tt.name, func(t *testing.T) {
300-
if got := formatStepIdentifier(tt.stageName, tt.stepName); got != tt.want {
301-
t.Errorf("formatStepIdentifier() = %v, want %v", got, tt.want)
302-
}
303-
})
304-
}
305-
}
306-
307-
func init() {
308-
// discard logs for tests
309-
logrus.SetOutput(io.Discard)
310-
}

0 commit comments

Comments
 (0)