Skip to content

Commit 35fbdaa

Browse files
adshmhtekton-robot
authored andcommitted
Validate PullRequest resource secrets fields
A PullRequest type pipeline resource is now validated: an unrecognised 'fieldName' in secrets will trigger an error. Part of work on #1818 As stated in #1818, this will help improve the debuggability of the PullRequst pipeline resource, as the validatiob will cause a failure in response to invalid input as early as possible. Signed-off-by: Arash Deshmeh <[email protected]>
1 parent 4c5f1d5 commit 35fbdaa

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pkg/apis/resource/v1alpha1/pipelineresource_validation.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"net/url"
2223
"strconv"
2324
"strings"
@@ -111,6 +112,12 @@ func (rs *PipelineResourceSpec) Validate(ctx context.Context) *apis.FieldError {
111112
}
112113
}
113114

115+
if rs.Type == PipelineResourceTypePullRequest {
116+
if err := validatePullRequest(rs); err != nil {
117+
return err
118+
}
119+
}
120+
114121
for _, allowedType := range AllResourceTypes {
115122
if allowedType == rs.Type {
116123
return nil
@@ -140,3 +147,12 @@ func validateURL(u, path string) *apis.FieldError {
140147
}
141148
return nil
142149
}
150+
151+
func validatePullRequest(s *PipelineResourceSpec) *apis.FieldError {
152+
for _, param := range s.SecretParams {
153+
if param.FieldName != "authToken" {
154+
return apis.ErrInvalidValue(fmt.Sprintf("invalid field name %q in secret parameter. Expected %q", param.FieldName, "authToken"), "spec.secrets.fieldName")
155+
}
156+
}
157+
return nil
158+
}

pkg/apis/resource/v1alpha1/pipelineresource_validation_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ func TestResourceValidation_Invalid(t *testing.T) {
143143
name: "missing spec",
144144
res: &v1alpha1.PipelineResource{},
145145
want: apis.ErrMissingField("spec.type"),
146+
}, {
147+
name: "pull request with invalid field name in secrets",
148+
res: &v1alpha1.PipelineResource{
149+
Spec: v1alpha1.PipelineResourceSpec{
150+
Type: v1alpha1.PipelineResourceTypePullRequest,
151+
SecretParams: []v1alpha1.SecretParam{{
152+
FieldName: "INVALID_FIELD_NAME",
153+
}},
154+
},
155+
},
156+
want: apis.ErrInvalidValue("invalid field name \"INVALID_FIELD_NAME\" in secret parameter. Expected \"authToken\"", "spec.secrets.fieldName"),
146157
},
147158
}
148159
for _, tt := range tests {
@@ -202,6 +213,14 @@ func TestClusterResourceValidation_Valid(t *testing.T) {
202213
},
203214
},
204215
},
216+
{
217+
name: "specify pullrequest with no secrets",
218+
res: &v1alpha1.PipelineResource{
219+
Spec: v1alpha1.PipelineResourceSpec{
220+
Type: v1alpha1.PipelineResourceTypePullRequest,
221+
},
222+
},
223+
},
205224
}
206225
for _, tt := range tests {
207226
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)