Skip to content

Commit d5ebe07

Browse files
committed
support v1 for sign and verofy command
This commit adds v1 support for sign and verify. Signed-off-by: Yongxuan Zhang [email protected]
1 parent 95548d3 commit d5ebe07

20 files changed

+275
-81
lines changed

docs/cmd/tkn_pipeline_sign.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ or using kms
3636
-o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).
3737
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
3838
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
39+
-v, --version string apiVersion of the Pipeline to be signed (default "v1")
3940
```
4041

4142
### Options inherited from parent commands

docs/cmd/tkn_pipeline_verify.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ or using kms
3535
-o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).
3636
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
3737
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
38+
-v, --version string apiVersion of the Pipeline to be verified (default "v1")
3839
```
3940

4041
### Options inherited from parent commands

docs/cmd/tkn_task_sign.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ or using kms
3636
-o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).
3737
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
3838
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
39+
-v, --version string apiVersion of the Task to be signed (default "v1")
3940
```
4041

4142
### Options inherited from parent commands

docs/cmd/tkn_task_verify.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ or using kms
3535
-o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).
3636
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
3737
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
38+
-v, --version string apiVersion of the Task to be verified (default "v1")
3839
```
3940

4041
### Options inherited from parent commands

docs/man/man1/tkn-pipeline-sign.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ For KMS:
6363
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
6464
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].
6565

66+
.PP
67+
\fB\-v\fP, \fB\-\-version\fP="v1"
68+
apiVersion of the Pipeline to be signed
69+
6670

6771
.SH OPTIONS INHERITED FROM PARENT COMMANDS
6872
.PP

docs/man/man1/tkn-pipeline-verify.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ For KMS:
5959
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
6060
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].
6161

62+
.PP
63+
\fB\-v\fP, \fB\-\-version\fP="v1"
64+
apiVersion of the Pipeline to be verified
65+
6266

6367
.SH OPTIONS INHERITED FROM PARENT COMMANDS
6468
.PP

docs/man/man1/tkn-task-sign.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ For KMS:
6363
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
6464
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].
6565

66+
.PP
67+
\fB\-v\fP, \fB\-\-version\fP="v1"
68+
apiVersion of the Task to be signed
69+
6670

6771
.SH OPTIONS INHERITED FROM PARENT COMMANDS
6872
.PP

docs/man/man1/tkn-task-verify.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ For KMS:
5959
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
6060
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].
6161

62+
.PP
63+
\fB\-v\fP, \fB\-\-version\fP="v1"
64+
apiVersion of the Task to be verified
65+
6266

6367
.SH OPTIONS INHERITED FROM PARENT COMMANDS
6468
.PP

pkg/cmd/pipeline/sign.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"github.com/spf13/cobra"
2323
"github.com/tektoncd/cli/pkg/cli"
2424
"github.com/tektoncd/cli/pkg/trustedresources"
25+
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2526
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
2729
"sigs.k8s.io/yaml"
2830
)
@@ -31,6 +33,7 @@ type signOptions struct {
3133
keyfile string
3234
kmsKey string
3335
targetFile string
36+
apiVersion string
3437
}
3538

3639
func signCommand() *cobra.Command {
@@ -70,7 +73,13 @@ or using kms
7073
return err
7174
}
7275

73-
crd := &v1beta1.Pipeline{}
76+
var crd metav1.Object
77+
if opts.apiVersion == "v1beta1" {
78+
crd = &v1beta1.Pipeline{}
79+
} else {
80+
crd = &v1.Pipeline{}
81+
}
82+
7483
if err := yaml.Unmarshal(b, &crd); err != nil {
7584
return fmt.Errorf("error unmarshalling Pipeline: %v", err)
7685
}
@@ -87,7 +96,7 @@ or using kms
8796
c.Flags().StringVarP(&opts.keyfile, "key-file", "K", "", "Key file")
8897
c.Flags().StringVarP(&opts.kmsKey, "kms-key", "m", "", "KMS key url")
8998
c.Flags().StringVarP(&opts.targetFile, "file-name", "f", "", "Fle name of the signed pipeline, using the original file name will overwrite the file")
90-
99+
c.Flags().StringVarP(&opts.apiVersion, "version", "v", "v1", "apiVersion of the Pipeline to be signed")
91100
return c
92101
}
93102

pkg/cmd/pipeline/sign_test.go

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package pipeline
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"os"
2021
"path/filepath"
2122
"testing"
@@ -28,37 +29,52 @@ import (
2829
func TestSign(t *testing.T) {
2930
ctx := context.Background()
3031
p := &test.Params{}
31-
32-
task := Command(p)
33-
32+
pipeline := Command(p)
3433
os.Setenv("PRIVATE_PASSWORD", "1234")
35-
tmpDir := t.TempDir()
36-
targetFile := filepath.Join(tmpDir, "signed.yaml")
37-
out, err := test.ExecuteCommand(task, "sign", "testdata/pipeline.yaml", "-K", "testdata/cosign.key", "-f", targetFile)
38-
if err != nil {
39-
t.Errorf("Unexpected error: %v", err)
40-
}
41-
expected := "*Warning*: This is an experimental command, it's usage and behavior can change in the next release(s)\nPipeline testdata/pipeline.yaml is signed successfully \n"
42-
test.AssertOutput(t, expected, out)
4334

44-
// verify the signed task
45-
verifier, err := cosignsignature.LoadPublicKey(ctx, "testdata/cosign.pub")
46-
if err != nil {
47-
t.Errorf("error getting verifier from key file: %v", err)
48-
}
35+
testcases := []struct {
36+
name string
37+
taskFile string
38+
apiVersion string
39+
}{{
40+
name: "sign and verify v1beta1 Pipeline",
41+
taskFile: "testdata/pipeline.yaml",
42+
apiVersion: "v1beta1",
43+
}, {
44+
name: "sign and verify v1 Pipeline",
45+
taskFile: "testdata/pipeline-v1.yaml",
46+
apiVersion: "v1",
47+
}}
48+
for _, tc := range testcases {
49+
t.Run(tc.name, func(t *testing.T) {
50+
tmpDir := t.TempDir()
51+
targetFile := filepath.Join(tmpDir, "signed.yaml")
52+
out, err := test.ExecuteCommand(pipeline, "sign", tc.taskFile, "-K", "testdata/cosign.key", "-f", targetFile, "-v", tc.apiVersion)
53+
if err != nil {
54+
t.Errorf("Unexpected error: %v", err)
55+
}
56+
expected := fmt.Sprintf("*Warning*: This is an experimental command, it's usage and behavior can change in the next release(s)\nPipeline %s is signed successfully \n", tc.taskFile)
57+
test.AssertOutput(t, expected, out)
4958

50-
signed, err := os.ReadFile(targetFile)
51-
if err != nil {
52-
t.Fatalf("error reading file: %v", err)
53-
}
59+
// verify the signed task
60+
verifier, err := cosignsignature.LoadPublicKey(ctx, "testdata/cosign.pub")
61+
if err != nil {
62+
t.Errorf("error getting verifier from key file: %v", err)
63+
}
5464

55-
target, signature, err := trustedresources.UnmarshalCRD(signed, "Pipeline")
56-
if err != nil {
57-
t.Fatalf("error unmarshalling crd: %v", err)
58-
}
65+
signed, err := os.ReadFile(targetFile)
66+
if err != nil {
67+
t.Fatalf("error reading file: %v", err)
68+
}
5969

60-
if err := trustedresources.VerifyInterface(target, verifier, signature); err != nil {
61-
t.Fatalf("VerifyInterface get error: %v", err)
62-
}
70+
target, signature, err := trustedresources.UnmarshalCRD(signed, "Pipeline", tc.apiVersion)
71+
if err != nil {
72+
t.Fatalf("error unmarshalling crd: %v", err)
73+
}
6374

75+
if err := trustedresources.VerifyInterface(target, verifier, signature); err != nil {
76+
t.Fatalf("VerifyInterface get error: %v", err)
77+
}
78+
})
79+
}
6480
}

0 commit comments

Comments
 (0)