File: sign.go

package info (click to toggle)
golang-github-notaryproject-notation 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: sh: 346; makefile: 79; python: 60
file content (302 lines) | stat: -rw-r--r-- 12,695 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
	"github.com/notaryproject/notation-core-go/signature/cose"
	. "github.com/notaryproject/notation/test/e2e/internal/notation"
	"github.com/notaryproject/notation/test/e2e/internal/utils"
	. "github.com/notaryproject/notation/test/e2e/suite/common"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

const (
	TamperKeyID                 = "TAMPER_KEY_ID"
	TamperSignature             = "TAMPER_SIGNATURE"
	TamperSignatureAlgorithm    = "TAMPER_SIGNATURE_ALGORITHM"
	TamperCertificateChain      = "TAMPER_CERTIFICATE_CHAIN"
	TamperSignatureEnvelope     = "TAMPER_SIGNATURE_ENVELOPE"
	TamperSignatureEnvelopeType = "TAMPER_SIGNATURE_ENVELOPE_TYPE"
	TamperAnnotation            = "TAMPER_ANNOTATION"
)

var _ = Describe("notation plugin sign", func() {
	It("with JWS format and capability SIGNATURE_GENERATOR.RAW", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true").
				MatchKeyWords("plugin-key")

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
				).
				MatchKeyWords(SignSuccessfully)

			OldNotation().Exec("verify", artifact.ReferenceWithDigest()).
				MatchKeyWords(VerifySuccessfully)
		})
	})

	It("with COSE format and capability SIGNATURE_GENERATOR.RAW", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true").
				MatchKeyWords("plugin-key")

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "--signature-format", "cose", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
				).
				MatchKeyWords(SignSuccessfully)

			OldNotation().Exec("verify", artifact.ReferenceWithDigest()).
				MatchKeyWords(VerifySuccessfully)
		})
	})

	It("with JWS format and capability SIGNATURE_GENERATOR.ENVELOPE", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true").
				MatchKeyWords("plugin-key")

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin generate-envelope request",
				).
				MatchKeyWords(SignSuccessfully)

			OldNotation().Exec("verify", artifact.ReferenceWithDigest()).
				MatchKeyWords(VerifySuccessfully)
		})
	})

	It("with COSE format and capability SIGNATURE_GENERATOR.ENVELOPE", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true").
				MatchKeyWords("plugin-key")

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "--signature-format", "cose", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin generate-envelope request",
				).
				MatchKeyWords(SignSuccessfully)

			OldNotation().Exec("verify", artifact.ReferenceWithDigest()).
				MatchKeyWords(VerifySuccessfully)
		})
	})

	It("with capability SIGNATURE_GENERATOR.RAW and tampered KeyID", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true",
				"--plugin-config", TamperKeyID+"=key10").
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
					`keyID in generateSignature response "key10" does not match request "key1"`,
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.RAW and tampered signature", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true",
				"--plugin-config", TamperSignature+"=invalid_sig").
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
					"generated signature failed verification: signature is invalid. Error: crypto/rsa: verification error",
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.RAW and tampered signatureAlgorithm", func() {
		Skip("signatureAlgorithm returned by plugin is not verified.")
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true",
				"--plugin-config", TamperSignatureAlgorithm+"=invalid_alg").
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.RAW and tampered certificate chain", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilitySignatureGenerator)+"=true",
				"--plugin-config", TamperCertificateChain+"=invalid_cert_chain").
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin describe-key request",
					"Plugin generate-signature request",
					"x509: malformed certificate",
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.ENVELOPE and tampered signature envelope", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true",
				"--plugin-config", TamperSignatureEnvelope+"={}").
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin generate-envelope request",
					"Verifying signature envelope generated by the plugin",
					"generated signature failed verification: certificate chain is not present",
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.ENVELOPE and tampered signature envelope type", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true",
				"--plugin-config", TamperSignatureEnvelopeType+"="+cose.MediaTypeEnvelope).
				MatchKeyWords("plugin-key")

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin generate-envelope request",
					`signatureEnvelopeType in generateEnvelope response "application/cose" does not match request "application/jose+json"`,
				)
		})
	})

	It("with capability SIGNATURE_GENERATOR.ENVELOPE and tampered annotation", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			Skip("annotation returned by plugin is not processed")
			// setup plugin and plugin-key
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true",
				"--plugin-config", TamperAnnotation+"=k1=v1").
				MatchKeyWords("plugin-key")

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords(
					"Plugin get-plugin-metadata request",
					"Plugin generate-envelope request",
				)

			// check signature annotation
			descriptors, err := artifact.SignatureDescriptors()
			Expect(err).ShouldNot(HaveOccurred())

			// should have 1 signature
			Expect(len(descriptors)).Should(Equal(1))
			// should have the annotation
			Expect(descriptors[0].Annotations).Should(HaveKeyWithValue("k1", "v1"))
		})
	})

	It("incorrect NOTATION_LIBEXEC path", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup incorrect NOTATION_LIBEXEC path
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true",
				"--plugin-config", TamperAnnotation+"=k1=v1").
				MatchKeyWords("plugin-key")

			vhost.UpdateEnv(map[string]string{"NOTATION_LIBEXEC": "/not/exist"})

			// run signing
			notation.ExpectFailure().Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchErrKeyWords("no such file or directory")
		})
	})

	It("correct NOTATION_LIBEXEC path", func() {
		Host(BaseOptions(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
			// setup incorrect NOTATION_LIBEXEC path
			vhost.SetOption(AddPlugin(NotationE2EPluginPath))
			notation.Exec("key", "add", "plugin-key", "--id", "key1", "--plugin", "e2e-plugin",
				"--plugin-config", string(CapabilityEnvelopeGenerator)+"=true",
				"--plugin-config", TamperAnnotation+"=k1=v1").
				MatchKeyWords("plugin-key")

			vhost.UpdateEnv(map[string]string{"NOTATION_LIBEXEC": vhost.AbsolutePath(NotationDirName)})

			// run signing
			notation.Exec("sign", artifact.ReferenceWithDigest(), "--key", "plugin-key", "-d").
				MatchKeyWords("Successfully signed")
		})
	})
})