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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
//go:build debian_no_fulcio
// +build debian_no_fulcio
package signature
import (
"encoding/json"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// xNewPRSigstoreSigned is like NewPRSigstoreSigned, except it must not fail.
func xNewPRSigstoreSigned(options ...PRSigstoreSignedOption) PolicyRequirement {
pr, err := NewPRSigstoreSigned(options...)
if err != nil {
panic("xNewPRSigstoreSigned failed")
}
return pr
}
func TestNewPRSigstoreSigned(t *testing.T) {
const testKeyPath = "/foo/bar"
testKeyData := []byte("abc")
testFulcio, err := NewPRSigstoreSignedFulcio(
PRSigstoreSignedFulcioWithCAPath("fixtures/fulcio_v1.crt.pem"),
PRSigstoreSignedFulcioWithOIDCIssuer("https://github.com/login/oauth"),
PRSigstoreSignedFulcioWithSubjectEmail("mitr@redhat.com"),
)
require.NoError(t, err)
const testRekorKeyPath = "/foo/baz"
testRekorKeyData := []byte("def")
testIdentity := NewPRMMatchRepoDigestOrExact()
// Success: combinatoric combinations of key source and Rekor uses
for _, c := range []struct {
options []PRSigstoreSignedOption
requiresRekor bool
expected prSigstoreSigned
}{
{
options: []PRSigstoreSignedOption{
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
expected: prSigstoreSigned{
prCommon: prCommon{prTypeSigstoreSigned},
KeyPath: testKeyPath,
KeyData: nil,
Fulcio: nil,
SignedIdentity: testIdentity,
},
},
{
options: []PRSigstoreSignedOption{
PRSigstoreSignedWithKeyData(testKeyData),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
expected: prSigstoreSigned{
prCommon: prCommon{prTypeSigstoreSigned},
KeyPath: "",
KeyData: testKeyData,
Fulcio: nil,
SignedIdentity: testIdentity,
},
},
{
options: []PRSigstoreSignedOption{
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
requiresRekor: true,
expected: prSigstoreSigned{
prCommon: prCommon{prTypeSigstoreSigned},
KeyPath: "",
KeyData: nil,
Fulcio: testFulcio,
SignedIdentity: testIdentity,
},
},
} {
for _, c2 := range []struct {
rekorOptions []PRSigstoreSignedOption
rekorExpected prSigstoreSigned
}{
{ // No Rekor
rekorOptions: []PRSigstoreSignedOption{},
rekorExpected: prSigstoreSigned{},
},
{
rekorOptions: []PRSigstoreSignedOption{
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
},
rekorExpected: prSigstoreSigned{
RekorPublicKeyPath: testRekorKeyPath,
},
},
{
rekorOptions: []PRSigstoreSignedOption{
PRSigstoreSignedWithRekorPublicKeyData(testRekorKeyData),
},
rekorExpected: prSigstoreSigned{
RekorPublicKeyData: testRekorKeyData,
},
},
} {
// Special-case this rejected combination:
if c.requiresRekor && len(c2.rekorOptions) == 0 {
continue
}
pr, err := newPRSigstoreSigned(append(c.options, c2.rekorOptions...)...)
require.NoError(t, err)
expected := c.expected // A shallow copy
expected.RekorPublicKeyPath = c2.rekorExpected.RekorPublicKeyPath
expected.RekorPublicKeyData = c2.rekorExpected.RekorPublicKeyData
assert.Equal(t, &expected, pr)
}
}
testFulcio2, err := NewPRSigstoreSignedFulcio(
PRSigstoreSignedFulcioWithCAPath("fixtures/fulcio_v1.crt.pem"),
PRSigstoreSignedFulcioWithOIDCIssuer("https://github.com/login/oauth"),
PRSigstoreSignedFulcioWithSubjectEmail("test-user@example.com"),
)
require.NoError(t, err)
for _, c := range [][]PRSigstoreSignedOption{
{}, // None of keyPath nor keyData, fulcio specified
{ // Both keyPath and keyData specified
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithKeyData(testKeyData),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // both keyPath and fulcio specified
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // both keyData and fulcio specified
PRSigstoreSignedWithKeyData(testKeyData),
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Duplicate keyPath
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithKeyPath(testKeyPath + "1"),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Duplicate keyData
PRSigstoreSignedWithKeyData(testKeyData),
PRSigstoreSignedWithKeyData([]byte("def")),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Duplicate fulcio
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithFulcio(testFulcio2),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // fulcio without Rekor
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Both rekorKeyPath and rekorKeyData specified
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
PRSigstoreSignedWithRekorPublicKeyData(testRekorKeyData),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Duplicate rekorKeyPath
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath),
PRSigstoreSignedWithRekorPublicKeyPath(testRekorKeyPath + "1"),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Duplicate keyData
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithRekorPublicKeyData(testRekorKeyData),
PRSigstoreSignedWithRekorPublicKeyData([]byte("def")),
PRSigstoreSignedWithSignedIdentity(testIdentity),
},
{ // Missing signedIdentity
PRSigstoreSignedWithKeyPath(testKeyPath),
},
{ // Duplicate signedIdentity}
PRSigstoreSignedWithKeyPath(testKeyPath),
PRSigstoreSignedWithSignedIdentity(testIdentity),
PRSigstoreSignedWithSignedIdentity(newPRMMatchRepository()),
},
} {
_, err = newPRSigstoreSigned(c...)
assert.Error(t, err)
}
}
func TestNewPRSigstoreSignedKeyPath(t *testing.T) {
const testPath = "/foo/bar"
signedIdentity := NewPRMMatchRepoDigestOrExact()
_pr, err := NewPRSigstoreSignedKeyPath(testPath, signedIdentity)
require.NoError(t, err)
pr, ok := _pr.(*prSigstoreSigned)
require.True(t, ok)
assert.Equal(t, &prSigstoreSigned{
prCommon: prCommon{Type: prTypeSigstoreSigned},
KeyPath: testPath,
SignedIdentity: NewPRMMatchRepoDigestOrExact(),
}, pr)
}
func TestNewPRSigstoreSignedKeyData(t *testing.T) {
testData := []byte("abc")
signedIdentity := NewPRMMatchRepoDigestOrExact()
_pr, err := NewPRSigstoreSignedKeyData(testData, signedIdentity)
require.NoError(t, err)
pr, ok := _pr.(*prSigstoreSigned)
require.True(t, ok)
assert.Equal(t, &prSigstoreSigned{
prCommon: prCommon{Type: prTypeSigstoreSigned},
KeyData: testData,
SignedIdentity: NewPRMMatchRepoDigestOrExact(),
}, pr)
}
// Return the result of modifying validJSON with fn and unmarshaling it into *pr
func tryUnmarshalModifiedSigstoreSigned(t *testing.T, pr *prSigstoreSigned, validJSON []byte, modifyFn func(mSA)) error {
var tmp mSA
err := json.Unmarshal(validJSON, &tmp)
require.NoError(t, err)
modifyFn(tmp)
*pr = prSigstoreSigned{}
return jsonUnmarshalFromObject(t, tmp, &pr)
}
func TestPRSigstoreSignedUnmarshalJSON(t *testing.T) {
keyDataTests := policyJSONUmarshallerTests[PolicyRequirement]{
newDest: func() json.Unmarshaler { return &prSigstoreSigned{} },
newValidObject: func() (PolicyRequirement, error) {
return NewPRSigstoreSignedKeyData([]byte("abc"), NewPRMMatchRepoDigestOrExact())
},
otherJSONParser: newPolicyRequirementFromJSON,
breakFns: []func(mSA){
// The "type" field is missing
func(v mSA) { delete(v, "type") },
// Wrong "type" field
func(v mSA) { v["type"] = 1 },
func(v mSA) { v["type"] = "this is invalid" },
// Extra top-level sub-object
func(v mSA) { v["unexpected"] = 1 },
// All of "keyPath" and "keyData", and "fulcio" is missing
func(v mSA) { delete(v, "keyData") },
// Both "keyPath" and "keyData" is present
func(v mSA) { v["keyPath"] = "/foo/bar" },
// Both "keyData" and "fulcio" is present
func(v mSA) {
v["fulcio"] = mSA{
"caPath": "/foo/baz",
"oidcIssuer": "https://example.com",
"subjectEmail": "test@example.com",
}
},
// Invalid "keyPath" field
func(v mSA) { delete(v, "keyData"); v["keyPath"] = 1 },
// Invalid "keyData" field
func(v mSA) { v["keyData"] = 1 },
func(v mSA) { v["keyData"] = "this is invalid base64" },
// Invalid "fulcio" field
func(v mSA) { v["fulcio"] = 1 },
func(v mSA) { v["fulcio"] = mSA{} },
// "fulcio" is explicit nil
func(v mSA) { v["fulcio"] = nil },
// Both "rekorKeyPath" and "rekorKeyData" is present
func(v mSA) {
v["rekorPublicKeyPath"] = "/foo/baz"
v["rekorPublicKeyData"] = ""
},
// Invalid "rekorPublicKeyPath" field
func(v mSA) { v["rekorPublicKeyPath"] = 1 },
// Invalid "rekorPublicKeyData" field
func(v mSA) { v["rekorPublicKeyData"] = 1 },
func(v mSA) { v["rekorPublicKeyData"] = "this is invalid base64" },
// Invalid "signedIdentity" field
func(v mSA) { v["signedIdentity"] = "this is invalid" },
// "signedIdentity" an explicit nil
func(v mSA) { v["signedIdentity"] = nil },
},
duplicateFields: []string{"type", "keyData", "signedIdentity"},
}
keyDataTests.run(t)
// Test keyPath-specific duplicate fields
policyJSONUmarshallerTests[PolicyRequirement]{
newDest: func() json.Unmarshaler { return &prSigstoreSigned{} },
newValidObject: func() (PolicyRequirement, error) {
return NewPRSigstoreSignedKeyPath("/foo/bar", NewPRMMatchRepoDigestOrExact())
},
otherJSONParser: newPolicyRequirementFromJSON,
duplicateFields: []string{"type", "keyPath", "signedIdentity"},
}.run(t)
// Test Fulcio and rekorPublicKeyPath duplicate fields
testFulcio, err := NewPRSigstoreSignedFulcio(
PRSigstoreSignedFulcioWithCAPath("fixtures/fulcio_v1.crt.pem"),
PRSigstoreSignedFulcioWithOIDCIssuer("https://github.com/login/oauth"),
PRSigstoreSignedFulcioWithSubjectEmail("mitr@redhat.com"),
)
require.NoError(t, err)
policyJSONUmarshallerTests[PolicyRequirement]{
newDest: func() json.Unmarshaler { return &prSigstoreSigned{} },
newValidObject: func() (PolicyRequirement, error) {
return NewPRSigstoreSigned(
PRSigstoreSignedWithFulcio(testFulcio),
PRSigstoreSignedWithRekorPublicKeyPath("/foo/rekor"),
PRSigstoreSignedWithSignedIdentity(NewPRMMatchRepoDigestOrExact()),
)
},
otherJSONParser: newPolicyRequirementFromJSON,
duplicateFields: []string{"type", "fulcio", "rekorPublicKeyPath", "signedIdentity"},
}.run(t)
// Test rekorPublicKeyData duplicate fields
policyJSONUmarshallerTests[PolicyRequirement]{
newDest: func() json.Unmarshaler { return &prSigstoreSigned{} },
newValidObject: func() (PolicyRequirement, error) {
return NewPRSigstoreSigned(
PRSigstoreSignedWithKeyPath("/foo/bar"),
PRSigstoreSignedWithRekorPublicKeyData([]byte("foo")),
PRSigstoreSignedWithSignedIdentity(NewPRMMatchRepoDigestOrExact()),
)
},
otherJSONParser: newPolicyRequirementFromJSON,
duplicateFields: []string{"type", "keyPath", "rekorPublicKeyData", "signedIdentity"},
}.run(t)
var pr prSigstoreSigned
// Start with a valid JSON.
_, validJSON := keyDataTests.validObjectAndJSON(t)
// Various allowed modifications to the requirement
allowedModificationFns := []func(mSA){
// Delete the signedIdentity field
func(v mSA) { delete(v, "signedIdentity") },
}
for _, fn := range allowedModificationFns {
err := tryUnmarshalModifiedSigstoreSigned(t, &pr, validJSON, fn)
require.NoError(t, err)
}
// Various ways to set signedIdentity to the default value
signedIdentityDefaultFns := []func(mSA){
// Set signedIdentity to the default explicitly
func(v mSA) { v["signedIdentity"] = NewPRMMatchRepoDigestOrExact() },
// Delete the signedIdentity field
func(v mSA) { delete(v, "signedIdentity") },
}
for _, fn := range signedIdentityDefaultFns {
err := tryUnmarshalModifiedSigstoreSigned(t, &pr, validJSON, fn)
require.NoError(t, err)
assert.Equal(t, NewPRMMatchRepoDigestOrExact(), pr.SignedIdentity)
}
}
func TestNewPRSigstoreSignedFulcio(t *testing.T) {
const testCAPath = "/foo/bar"
testCAData := []byte("abc")
const testOIDCIssuer = "https://example.com"
const testSubjectEmail = "test@example.com"
// Success:
for _, c := range []struct {
options []PRSigstoreSignedFulcioOption
expected prSigstoreSignedFulcio
}{
{
options: []PRSigstoreSignedFulcioOption{
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
expected: prSigstoreSignedFulcio{
CAPath: testCAPath,
OIDCIssuer: testOIDCIssuer,
SubjectEmail: testSubjectEmail,
},
},
{
options: []PRSigstoreSignedFulcioOption{
PRSigstoreSignedFulcioWithCAData(testCAData),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
expected: prSigstoreSignedFulcio{
CAData: testCAData,
OIDCIssuer: testOIDCIssuer,
SubjectEmail: testSubjectEmail,
},
},
} {
pr, err := newPRSigstoreSignedFulcio(c.options...)
require.NoError(t, err)
assert.Equal(t, &c.expected, pr)
}
for _, c := range [][]PRSigstoreSignedFulcioOption{
{ // Neither caPath nor caData specified
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Both caPath and caData specified
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithCAData(testCAData),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Duplicate caPath
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithCAPath(testCAPath + "1"),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Duplicate caData
PRSigstoreSignedFulcioWithCAData(testCAData),
PRSigstoreSignedFulcioWithCAData([]byte("def")),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Missing oidcIssuer
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Duplicate oidcIssuer
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer + "1"),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
},
{ // Missing subjectEmail
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
},
{ // Duplicate subjectEmail
PRSigstoreSignedFulcioWithCAPath(testCAPath),
PRSigstoreSignedFulcioWithOIDCIssuer(testOIDCIssuer),
PRSigstoreSignedFulcioWithSubjectEmail(testSubjectEmail),
PRSigstoreSignedFulcioWithSubjectEmail("1" + testSubjectEmail),
},
} {
_, err := newPRSigstoreSignedFulcio(c...)
logrus.Errorf("%#v", err)
assert.Error(t, err)
}
}
func TestPRSigstoreSignedFulcioUnmarshalJSON(t *testing.T) {
policyJSONUmarshallerTests[PRSigstoreSignedFulcio]{
newDest: func() json.Unmarshaler { return &prSigstoreSignedFulcio{} },
newValidObject: func() (PRSigstoreSignedFulcio, error) {
return NewPRSigstoreSignedFulcio(
PRSigstoreSignedFulcioWithCAPath("fixtures/fulcio_v1.crt.pem"),
PRSigstoreSignedFulcioWithOIDCIssuer("https://github.com/login/oauth"),
PRSigstoreSignedFulcioWithSubjectEmail("mitr@redhat.com"),
)
},
otherJSONParser: nil,
breakFns: []func(mSA){
// Extra top-level sub-object
func(v mSA) { v["unexpected"] = 1 },
// Both of "caPath" and "caData" are missing
func(v mSA) { delete(v, "caPath") },
// Both "caPath" and "caData" is present
func(v mSA) { v["caData"] = "" },
// Invalid "caPath" field
func(v mSA) { v["caPath"] = 1 },
// Invalid "oidcIssuer" field
func(v mSA) { v["oidcIssuer"] = 1 },
// "oidcIssuer" is missing
func(v mSA) { delete(v, "oidcIssuer") },
// Invalid "subjectEmail" field
func(v mSA) { v["subjectEmail"] = 1 },
// "subjectEmail" is missing
func(v mSA) { delete(v, "subjectEmail") },
},
duplicateFields: []string{"caPath", "oidcIssuer", "subjectEmail"},
}.run(t)
// Test caData specifics
policyJSONUmarshallerTests[PRSigstoreSignedFulcio]{
newDest: func() json.Unmarshaler { return &prSigstoreSignedFulcio{} },
newValidObject: func() (PRSigstoreSignedFulcio, error) {
return NewPRSigstoreSignedFulcio(
PRSigstoreSignedFulcioWithCAData([]byte("abc")),
PRSigstoreSignedFulcioWithOIDCIssuer("https://github.com/login/oauth"),
PRSigstoreSignedFulcioWithSubjectEmail("mitr@redhat.com"),
)
},
otherJSONParser: nil,
breakFns: []func(mSA){
// Invalid "caData" field
func(v mSA) { v["caData"] = 1 },
func(v mSA) { v["caData"] = "this is invalid base64" },
},
duplicateFields: []string{"caData", "oidcIssuer", "subjectEmail"},
}.run(t)
}
|