Skip to content

Commit

Permalink
Add checking insecure flag when creating pipeline resources
Browse files Browse the repository at this point in the history
  • Loading branch information
16yuki0702 authored and tekton-robot committed Oct 1, 2019
1 parent 52bc003 commit 73bf89b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
8 changes: 6 additions & 2 deletions pkg/apis/pipeline/v1alpha1/pipelineresource_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"context"
"strconv"
"strings"

"k8s.io/apimachinery/pkg/api/equality"
Expand All @@ -37,7 +38,7 @@ func (rs *PipelineResourceSpec) Validate(ctx context.Context) *apis.FieldError {
return apis.ErrMissingField(apis.CurrentField)
}
if rs.Type == PipelineResourceTypeCluster {
var usernameFound, cadataFound, nameFound bool
var usernameFound, cadataFound, nameFound, isInsecure bool
for _, param := range rs.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
Expand All @@ -50,6 +51,9 @@ func (rs *PipelineResourceSpec) Validate(ctx context.Context) *apis.FieldError {
cadataFound = true
case strings.EqualFold(param.Name, "name"):
nameFound = true
case strings.EqualFold(param.Name, "insecure"):
b, _ := strconv.ParseBool(param.Value)
isInsecure = b
}
}

Expand All @@ -68,7 +72,7 @@ func (rs *PipelineResourceSpec) Validate(ctx context.Context) *apis.FieldError {
if !usernameFound {
return apis.ErrMissingField("username param")
}
if !cadataFound {
if !cadataFound && !isInsecure {
return apis.ErrMissingField("CAData param")
}
}
Expand Down
43 changes: 33 additions & 10 deletions pkg/apis/pipeline/v1alpha1/pipelineresource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,39 @@ func TestResourceValidation_Invalid(t *testing.T) {
}

func TestClusterResourceValidation_Valid(t *testing.T) {
res := tb.PipelineResource("test-cluster-resource", "foo", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeCluster,
tb.PipelineResourceSpecParam("name", "test_cluster_resource"),
tb.PipelineResourceSpecParam("url", "http://10.10.10.10"),
tb.PipelineResourceSpecParam("cadata", "bXktY2x1c3Rlci1jZXJ0Cg"),
tb.PipelineResourceSpecParam("username", "admin"),
tb.PipelineResourceSpecParam("token", "my-token"),
))
if err := res.Validate(context.Background()); err != nil {
t.Errorf("Unexpected PipelineRun.Validate() error = %v", err)
tests := []struct {
name string
res *v1alpha1.PipelineResource
}{
{
name: "success validate",
res: tb.PipelineResource("test-cluster-resource", "foo", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeCluster,
tb.PipelineResourceSpecParam("name", "test_cluster_resource"),
tb.PipelineResourceSpecParam("url", "http://10.10.10.10"),
tb.PipelineResourceSpecParam("cadata", "bXktY2x1c3Rlci1jZXJ0Cg"),
tb.PipelineResourceSpecParam("username", "admin"),
tb.PipelineResourceSpecParam("token", "my-token"),
)),
},
{
name: "specify insecure without cadata",
res: tb.PipelineResource("test-cluster-resource", "foo", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeCluster,
tb.PipelineResourceSpecParam("name", "test_cluster_resource"),
tb.PipelineResourceSpecParam("url", "http://10.10.10.10"),
tb.PipelineResourceSpecParam("username", "admin"),
tb.PipelineResourceSpecParam("token", "my-token"),
tb.PipelineResourceSpecParam("insecure", "true"),
)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.res.Validate(context.Background()); err != nil {
t.Errorf("Unexpected PipelineRun.Validate() error = %v", err)
}
})
}
}

Expand Down

0 comments on commit 73bf89b

Please sign in to comment.