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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
package signature
import (
"fmt"
"testing"
"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/internal/testing/mocks"
"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
fullRHELRef = "registry.access.redhat.com/rhel7/rhel:7.2.3"
untaggedRHELRef = "registry.access.redhat.com/rhel7/rhel"
digestSuffix = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
digestSuffixOther = "@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
)
func TestParseImageAndDockerReference(t *testing.T) {
const (
ok1 = "busybox"
ok2 = fullRHELRef
bad1 = "UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES"
bad2 = ""
)
// Success
ref, err := reference.ParseNormalizedNamed(ok1)
require.NoError(t, err)
r1, r2, err := parseImageAndDockerReference(refImageMock{ref: ref}, ok2)
require.NoError(t, err)
assert.Equal(t, ok1, reference.FamiliarString(r1))
assert.Equal(t, ok2, reference.FamiliarString(r2))
// Unidentified images are rejected.
_, _, err = parseImageAndDockerReference(refImageMock{ref: nil}, ok2)
require.Error(t, err)
assert.IsType(t, PolicyRequirementError(""), err)
// Failures
for _, refs := range [][]string{
{bad1, ok2},
{ok1, bad2},
{bad1, bad2},
} {
ref, err := reference.ParseNormalizedNamed(refs[0])
if err == nil {
_, _, err := parseImageAndDockerReference(refImageMock{ref: ref}, refs[1])
assert.Error(t, err)
}
}
}
// refImageMock is a mock of private.UnparsedImage which returns itself in Reference().DockerReference.
type refImageMock struct {
mocks.ForbiddenUnparsedImage
ref reference.Named
}
func (ref refImageMock) Reference() types.ImageReference {
return refImageReferenceMock{ref: ref.ref}
}
// refImageReferenceMock is a mock of types.ImageReference which returns itself in DockerReference.
type refImageReferenceMock struct {
mocks.ForbiddenImageReference
ref reference.Named
}
func (ref refImageReferenceMock) Transport() types.ImageTransport {
return mocks.NameImageTransport("== Transport mock")
}
func (ref refImageReferenceMock) StringWithinTransport() string {
// We use this in error messages, so sadly we must return something. But right now we do so only when DockerReference is nil, so restrict to that.
if ref.ref == nil {
return "== StringWithinTransport for an image with no Docker support"
}
panic("unexpected call to a mock function")
}
func (ref refImageReferenceMock) DockerReference() reference.Named {
return ref.ref
}
type prmSymmetricTableTest struct {
refA, refB string
result bool
}
// Test cases for exact reference match. The behavior is supposed to be symmetric.
var prmExactMatchTestTable = []prmSymmetricTableTest{
// Success, simple matches
{"busybox:latest", "busybox:latest", true},
{fullRHELRef, fullRHELRef, true},
{"busybox" + digestSuffix, "busybox" + digestSuffix, true}, // NOTE: This is not documented; signing digests is not recommended at this time.
// Non-canonical reference format is canonicalized
{"library/busybox:latest", "busybox:latest", true},
{"docker.io/library/busybox:latest", "busybox:latest", true},
{"library/busybox" + digestSuffix, "busybox" + digestSuffix, true},
// Mismatch
{"busybox:latest", "busybox:notlatest", false},
{"busybox:latest", "notbusybox:latest", false},
{"busybox:latest", "hostname/library/busybox:notlatest", false},
{"hostname/library/busybox:latest", "busybox:notlatest", false},
{"busybox:latest", fullRHELRef, false},
{"busybox" + digestSuffix, "notbusybox" + digestSuffix, false},
{"busybox:latest", "busybox" + digestSuffix, false},
{"busybox" + digestSuffix, "busybox" + digestSuffixOther, false},
// NameOnly references
{"busybox", "busybox:latest", false},
{"busybox", "busybox" + digestSuffix, false},
{"busybox", "busybox", false},
// References with both tags and digests: We match them exactly (requiring BOTH to match)
// NOTE: Again, this is not documented behavior; the recommendation is to sign tags, not digests, and then tag-and-digest references won’t match the signed identity.
{"busybox:latest" + digestSuffix, "busybox:latest" + digestSuffix, true},
{"busybox:latest" + digestSuffix, "busybox:latest" + digestSuffixOther, false},
{"busybox:latest" + digestSuffix, "busybox:notlatest" + digestSuffix, false},
{"busybox:latest" + digestSuffix, "busybox" + digestSuffix, false},
{"busybox:latest" + digestSuffix, "busybox:latest", false},
// Invalid format
{"UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES", "busybox:latest", false},
{"", "UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES", false},
// Even if they are exactly equal, invalid values are rejected.
{"INVALID", "INVALID", false},
}
// Test cases for repository-only reference match. The behavior is supposed to be symmetric.
var prmRepositoryMatchTestTable = []prmSymmetricTableTest{
// Success, simple matches
{"busybox:latest", "busybox:latest", true},
{fullRHELRef, fullRHELRef, true},
{"busybox" + digestSuffix, "busybox" + digestSuffix, true}, // NOTE: This is not documented; signing digests is not recommended at this time.
// Non-canonical reference format is canonicalized
{"library/busybox:latest", "busybox:latest", true},
{"docker.io/library/busybox:latest", "busybox:latest", true},
{"library/busybox" + digestSuffix, "busybox" + digestSuffix, true},
// The same as above, but with mismatching tags
{"busybox:latest", "busybox:notlatest", true},
{fullRHELRef + "tagsuffix", fullRHELRef, true},
{"library/busybox:latest", "busybox:notlatest", true},
{"busybox:latest", "library/busybox:notlatest", true},
{"docker.io/library/busybox:notlatest", "busybox:latest", true},
{"busybox:notlatest", "docker.io/library/busybox:latest", true},
{"busybox:latest", "busybox" + digestSuffix, true},
{"busybox" + digestSuffix, "busybox" + digestSuffixOther, true}, // Even this is accepted here. (This could more reasonably happen with two different digest algorithms.)
// The same as above, but with defaulted tags (which can happen with /usr/bin/cosign)
{"busybox", "busybox:notlatest", true},
{fullRHELRef, untaggedRHELRef, true},
{"busybox", "busybox" + digestSuffix, true},
{"library/busybox", "busybox", true},
{"docker.io/library/busybox", "busybox", true},
// Mismatch
{"busybox:latest", "notbusybox:latest", false},
{"hostname/library/busybox:latest", "busybox:notlatest", false},
{"busybox:latest", fullRHELRef, false},
{"busybox" + digestSuffix, "notbusybox" + digestSuffix, false},
// References with both tags and digests: We ignore both anyway.
{"busybox:latest" + digestSuffix, "busybox:latest" + digestSuffix, true},
{"busybox:latest" + digestSuffix, "busybox:latest" + digestSuffixOther, true},
{"busybox:latest" + digestSuffix, "busybox:notlatest" + digestSuffix, true},
{"busybox:latest" + digestSuffix, "busybox" + digestSuffix, true},
{"busybox:latest" + digestSuffix, "busybox:latest", true},
// Invalid format
{"UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES", "busybox:latest", false},
{"", "UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES", false},
// Even if they are exactly equal, invalid values are rejected.
{"INVALID", "INVALID", false},
}
// Test cases for matchRepoDigestOrExact
var matchRepoDigestOrExactTestTable = []struct {
imageRef, sigRef string
result bool
}{
// Tag mismatch
{"busybox:latest", "busybox:notlatest", false},
{fullRHELRef + "tagsuffix", fullRHELRef, false},
{"library/busybox:latest", "busybox:notlatest", false},
{"busybox:latest", "library/busybox:notlatest", false},
{"docker.io/library/busybox:notlatest", "busybox:latest", false},
{"busybox:notlatest", "docker.io/library/busybox:latest", false},
// NameOnly references
{"busybox", "busybox:latest", false},
{"busybox:latest", "busybox", false},
{"busybox", "busybox" + digestSuffix, false},
{"busybox" + digestSuffix, "busybox", false},
{fullRHELRef, untaggedRHELRef, false},
{"busybox", "busybox", false},
// Tag references only accept signatures with matching tags.
{"busybox:latest", "busybox" + digestSuffix, false},
// Digest references accept any signature with matching repository.
{"busybox" + digestSuffix, "busybox:latest", true},
{"busybox" + digestSuffix, "busybox" + digestSuffixOther, true}, // Even this is accepted here. (This could more reasonably happen with two different digest algorithms.)
// References with both tags and digests: We match them exactly (requiring BOTH to match).
{"busybox:latest" + digestSuffix, "busybox:latest", false},
{"busybox:latest" + digestSuffix, "busybox:notlatest", false},
{"busybox:latest", "busybox:latest" + digestSuffix, false},
{"busybox:latest" + digestSuffix, "busybox:latest" + digestSuffixOther, false},
{"busybox:latest" + digestSuffix, "busybox:notlatest" + digestSuffixOther, false},
}
func testImageAndSig(t *testing.T, prm PolicyReferenceMatch, imageRef, sigRef string, result bool) {
// This assumes that all ways to obtain a reference.Named perform equivalent validation,
// and therefore values refused by reference.ParseNormalizedNamed can not happen in practice.
parsedImageRef, err := reference.ParseNormalizedNamed(imageRef)
require.NoError(t, err)
res := prm.matchesDockerReference(refImageMock{ref: parsedImageRef}, sigRef)
assert.Equal(t, result, res, fmt.Sprintf("%s vs. %s", imageRef, sigRef))
}
// testPossiblyInvalidImageAndSig is a variant of testImageAndSig
// that does not fail if the imageRef is invalid (which should never happen in practice,
// but makes testing of symmetrical properties using shared tables easier)
func testPossiblyInvalidImageAndSig(t *testing.T, prm PolicyReferenceMatch, imageRef, sigRef string, result bool) {
// This assumes that all ways to obtain a reference.Named perform equivalent validation,
// and therefore values refused by reference.ParseNormalizedNamed can not happen in practice.
_, err := reference.ParseNormalizedNamed(imageRef)
if err != nil {
return
}
testImageAndSig(t, prm, imageRef, sigRef, result)
}
func TestMatchRepoDigestOrExactReferenceValues(t *testing.T) {
// prmMatchRepoDigestOrExact is a middle ground between prmMatchExact and prmMatchRepository:
// It accepts anything prmMatchExact accepts,…
for _, test := range prmExactMatchTestTable {
if test.result == true {
refA, errA := reference.ParseNormalizedNamed(test.refA)
refB, errB := reference.ParseNormalizedNamed(test.refB)
if errA == nil && errB == nil {
res1 := matchRepoDigestOrExactReferenceValues(refA, refB)
assert.Equal(t, test.result, res1)
res2 := matchRepoDigestOrExactReferenceValues(refB, refA)
assert.Equal(t, test.result, res2)
}
}
}
// … and it rejects everything prmMatchRepository rejects.
for _, test := range prmRepositoryMatchTestTable {
if test.result == false {
refA, errA := reference.ParseNormalizedNamed(test.refA)
refB, errB := reference.ParseNormalizedNamed(test.refB)
if errA == nil && errB == nil {
res1 := matchRepoDigestOrExactReferenceValues(refA, refB)
assert.Equal(t, test.result, res1)
res2 := matchRepoDigestOrExactReferenceValues(refB, refA)
assert.Equal(t, test.result, res2)
}
}
}
// The other cases, possibly asymmetrical:
for _, test := range matchRepoDigestOrExactTestTable {
imageRef, err := reference.ParseNormalizedNamed(test.imageRef)
require.NoError(t, err)
sigRef, err := reference.ParseNormalizedNamed(test.sigRef)
require.NoError(t, err)
res := matchRepoDigestOrExactReferenceValues(imageRef, sigRef)
assert.Equal(t, test.result, res)
}
}
func TestPRMMatchExactMatchesDockerReference(t *testing.T) {
prm := NewPRMMatchExact()
for _, test := range prmExactMatchTestTable {
testPossiblyInvalidImageAndSig(t, prm, test.refA, test.refB, test.result)
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)
}
func TestPRMMatchRepoDigestOrExactMatchesDockerReference(t *testing.T) {
prm := NewPRMMatchRepoDigestOrExact()
// prmMatchRepoDigestOrExact is a middle ground between prmMatchExact and prmMatchRepository:
// It accepts anything prmMatchExact accepts,…
for _, test := range prmExactMatchTestTable {
if test.result == true {
testPossiblyInvalidImageAndSig(t, prm, test.refA, test.refB, test.result)
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
}
// … and it rejects everything prmMatchRepository rejects.
for _, test := range prmRepositoryMatchTestTable {
if test.result == false {
testPossiblyInvalidImageAndSig(t, prm, test.refA, test.refB, test.result)
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
}
// The other cases, possibly asymmetrical:
for _, test := range matchRepoDigestOrExactTestTable {
testImageAndSig(t, prm, test.imageRef, test.sigRef, test.result)
}
}
func TestPRMMatchRepositoryMatchesDockerReference(t *testing.T) {
prm := NewPRMMatchRepository()
for _, test := range prmRepositoryMatchTestTable {
testPossiblyInvalidImageAndSig(t, prm, test.refA, test.refB, test.result)
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)
}
func TestParseDockerReferences(t *testing.T) {
const (
ok1 = "busybox"
ok2 = fullRHELRef
bad1 = "UPPERCASE_IS_INVALID_IN_DOCKER_REFERENCES"
bad2 = ""
)
// Success
r1, r2, err := parseDockerReferences(ok1, ok2)
require.NoError(t, err)
assert.Equal(t, ok1, reference.FamiliarString(r1))
assert.Equal(t, ok2, reference.FamiliarString(r2))
// Failures
for _, refs := range [][]string{
{bad1, ok2},
{ok1, bad2},
{bad1, bad2},
} {
_, _, err := parseDockerReferences(refs[0], refs[1])
assert.Error(t, err)
}
}
func testExactPRMAndSig(t *testing.T, prmFactory func(string) PolicyReferenceMatch, imageRef, sigRef string, result bool) {
prm := prmFactory(imageRef)
res := prm.matchesDockerReference(mocks.ForbiddenUnparsedImage{}, sigRef)
assert.Equal(t, result, res, fmt.Sprintf("%s vs. %s", imageRef, sigRef))
}
func prmExactReferenceFactory(ref string) PolicyReferenceMatch {
// Do not use NewPRMExactReference, we want to also test the case with an invalid DockerReference,
// even though NewPRMExactReference should never let it happen.
return &prmExactReference{DockerReference: ref}
}
func TestPRMExactReferenceMatchesDockerReference(t *testing.T) {
for _, test := range prmExactMatchTestTable {
testExactPRMAndSig(t, prmExactReferenceFactory, test.refA, test.refB, test.result)
testExactPRMAndSig(t, prmExactReferenceFactory, test.refB, test.refA, test.result)
}
}
func prmExactRepositoryFactory(ref string) PolicyReferenceMatch {
// Do not use NewPRMExactRepository, we want to also test the case with an invalid DockerReference,
// even though NewPRMExactRepository should never let it happen.
return &prmExactRepository{DockerRepository: ref}
}
func TestPRMExactRepositoryMatchesDockerReference(t *testing.T) {
for _, test := range prmRepositoryMatchTestTable {
testExactPRMAndSig(t, prmExactRepositoryFactory, test.refA, test.refB, test.result)
testExactPRMAndSig(t, prmExactRepositoryFactory, test.refB, test.refA, test.result)
}
}
func TestPRMRemapIdentityRefMatchesPrefix(t *testing.T) {
for _, c := range []struct {
ref, prefix string
expected bool
}{
// Prefix is a reference.Domain() value
{"docker.io/image", "docker.io", true},
{"docker.io/image", "example.com", false},
{"example.com:5000/image", "example.com:5000", true},
{"example.com:50000/image", "example.com:5000", false},
{"example.com:5000/image", "example.com", false},
{"example.com/foo", "example.com", true},
{"example.com/foo/bar", "example.com", true},
{"example.com/foo/bar:baz", "example.com", true},
{"example.com/foo/bar" + digestSuffix, "example.com", true},
// Prefix is a reference.Named.Name() value or a repo namespace
{"docker.io/ns/image", "docker.io/library", false},
{"example.com/library", "docker.io/library", false},
{"docker.io/libraryy/image", "docker.io/library", false},
{"docker.io/library/busybox", "docker.io/library", true},
{"example.com/ns/image", "example.com/ns", true},
{"example.com/ns2/image", "example.com/ns", false},
{"example.com/n2/image", "example.com/ns", false},
{"example.com", "example.com/library/busybox", false},
{"example.com:5000/ns/image", "example.com/ns", false},
{"example.com/ns/image", "example.com:5000/ns", false},
{"docker.io/library/busybox", "docker.io/library/busybox", true},
{"example.com/library/busybox", "docker.io/library/busybox", false},
{"docker.io/library/busybox2", "docker.io/library/busybox", false},
{"example.com/ns/image", "example.com/ns/image", true},
{"example.com/ns/imag2", "example.com/ns/image", false},
{"example.com/ns/imagee", "example.com/ns/image", false},
{"example.com:5000/ns/image", "example.com/ns/image", false},
{"example.com/ns/image", "example.com:5000/ns/image", false},
{"example.com/ns/image:tag", "example.com/ns/image", true},
{"example.com/ns/image" + digestSuffix, "example.com/ns/image", true},
{"example.com/ns/image:tag" + digestSuffix, "example.com/ns/image", true},
} {
prm, err := newPRMRemapIdentity(c.prefix, "docker.io/library/signed-prefix")
require.NoError(t, err, c.prefix)
ref, err := reference.ParseNormalizedNamed(c.ref)
require.NoError(t, err, c.ref)
res := prm.refMatchesPrefix(ref)
assert.Equal(t, c.expected, res, fmt.Sprintf("%s vs. %s", c.ref, c.prefix))
}
}
func TestPRMRemapIdentityRemapReferencePrefix(t *testing.T) {
for _, c := range []struct{ prefix, signedPrefix, ref, expected string }{
// Match sanity checking, primarily tested in TestPRMRefMatchesPrefix
{"mirror.example", "vendor.example", "mirror.example/ns/image:tag", "vendor.example/ns/image:tag"},
{"mirror.example", "vendor.example", "different.com/ns/image:tag", "different.com/ns/image:tag"},
{"mirror.example/ns", "vendor.example/vendor-ns", "mirror.example/different-ns/image:tag", "mirror.example/different-ns/image:tag"},
{"docker.io", "not-docker-signed.example/ns", "busybox", "not-docker-signed.example/ns/library/busybox"},
// Rewrites work as expected
{"mirror.example", "vendor.example", "mirror.example/ns/image:tag", "vendor.example/ns/image:tag"},
{"example.com/mirror", "example.com/vendor", "example.com/mirror/image:tag", "example.com/vendor/image:tag"},
{"example.com/ns/mirror", "example.com/ns/vendor", "example.com/ns/mirror:tag", "example.com/ns/vendor:tag"},
{"mirror.example", "vendor.example", "prefixmirror.example/ns/image:tag", "prefixmirror.example/ns/image:tag"},
{"docker.io", "not-docker-signed.example", "busybox", "not-docker-signed.example/library/busybox"},
{"docker.io/library", "not-docker-signed.example/ns", "busybox", "not-docker-signed.example/ns/busybox"},
{"docker.io/library/busybox", "not-docker-signed.example/ns/notbusybox", "busybox", "not-docker-signed.example/ns/notbusybox"},
// On match, tag/digest is preserved
{"mirror.example", "vendor.example", "mirror.example/image", "vendor.example/image"}, // This one should not actually happen, testing for completeness
{"mirror.example", "vendor.example", "mirror.example/image:tag", "vendor.example/image:tag"},
{"mirror.example", "vendor.example", "mirror.example/image" + digestSuffix, "vendor.example/image" + digestSuffix},
{"mirror.example", "vendor.example", "mirror.example/image:tag" + digestSuffix, "vendor.example/image:tag" + digestSuffix},
// Rewrite creating an invalid reference
{"mirror.example/ns/image", "vendor.example:5000", "mirror.example/ns/image:tag", ""},
// Rewrite creating a valid reference string in short format, which would imply a docker.io prefix and is rejected
{"mirror.example/ns/image", "vendor.example:5000", "mirror.example/ns/image" + digestSuffix, ""}, // vendor.example:5000@digest
{"mirror.example/ns/image", "notlocalhost", "mirror.example/ns/image:tag", ""}, // notlocalhost:tag
} {
testName := fmt.Sprintf("%#v", c)
prm, err := newPRMRemapIdentity(c.prefix, c.signedPrefix)
require.NoError(t, err, testName)
ref, err := reference.ParseNormalizedNamed(c.ref)
require.NoError(t, err, testName)
res, err := prm.remapReferencePrefix(ref)
if c.expected == "" {
assert.Error(t, err, testName)
} else {
require.NoError(t, err, testName)
assert.Equal(t, c.expected, res.String(), testName)
}
}
}
// modifiedString returns some string that is different from the input,
// consistent across calls with the same input;
// in particular it just replaces the first letter.
func modifiedString(t *testing.T, input string) string {
c := input[0]
switch {
case c >= 'a' && c <= 'y':
c++
case c == 'z':
c = 'a'
default:
require.Fail(t, "unimplemented leading character '%c'", c)
}
return string(c) + input[1:]
}
// prmRemapIdentityMRDOETestCase is a helper for TestPRMRemapIdentityMatchesDockerReference,
// verifying that the behavior is consistent with prmMatchRepoDigestOrExact,
// while still smoke-testing the rewriting behavior.
// The test succeeds if imageRefString is invalid and ignoreInvalidImageRef.
func prmRemapIdentityMRDOETestCase(t *testing.T, ignoreInvalidImageRef bool, imageRef, sigRef string, result bool) {
parsedImageRef, err := reference.ParseNormalizedNamed(imageRef)
if ignoreInvalidImageRef && err != nil {
return
}
require.NoError(t, err)
// No rewriting happens.
prm, err := NewPRMRemapIdentity("never-causes-a-rewrite.example", "never-causes-a-rewrite.example")
require.NoError(t, err)
testImageAndSig(t, prm, imageRef, sigRef, result)
// Rewrite imageRef
domain := reference.Domain(parsedImageRef)
prm, err = NewPRMRemapIdentity(modifiedString(t, domain), domain)
require.NoError(t, err)
modifiedImageRef, err := reference.ParseNormalizedNamed(modifiedString(t, parsedImageRef.String()))
require.NoError(t, err)
testImageAndSig(t, prm, modifiedImageRef.String(), sigRef, result)
}
func TestPRMRemapIdentityMatchesDockerReference(t *testing.T) {
// Basic sanity checks. More detailed testing is done in TestPRMRemapIdentityRemapReferencePrefix
// and TestMatchRepoDigestOrExactReferenceValues.
for _, c := range []struct {
prefix, signedPrefix, imageRef, sigRef string
result bool
}{
// No match rewriting
{"does-not-match.com", "does-not-match.rewritten", "busybox:latest", "busybox:latest", true},
{"does-not-match.com", "does-not-match.rewritten", "busybox:latest", "notbusybox:latest", false},
// Match rewriting non-docker
{"mirror.example", "public.com", "mirror.example/busybox:1", "public.com/busybox:1", true},
{"mirror.example", "public.com", "mirror.example/busybox:1", "public.com/busybox:not1", false},
// Rewriting to docker.io
{"mirror.example", "docker.io/library", "mirror.example/busybox:latest", "busybox:latest", true},
{"mirror.example", "docker.io/library", "mirror.example/alpine:latest", "busybox:latest", false},
// Rewriting from docker.io
{"docker.io/library", "original.com", "copied:latest", "original.com/copied:latest", true},
{"docker.io/library", "original.com", "copied:latest", "original.com/ns/copied:latest", false},
// Invalid object: prefix is not a host name
{"busybox", "example.com/busybox", "busybox:latest", "example.com/busybox:latest", false},
// Invalid object: signedPrefix is not a host name
{"docker.io/library/busybox", "busybox", "docker.io/library/busybox:latest", "busybox:latest", false},
// Invalid object: invalid prefix
{"UPPERCASE", "example.com", "example.com/foo:latest", "example.com/foo:latest", true}, // Happens to work, not an API promise
{"example.com", "UPPERCASE", "example.com/foo:latest", "UPPERCASE/foo:latest", false},
} {
// Do not use NewPRMRemapIdentity, we want to also test the cases with invalid values,
// even though NewPRMExactReference should never let it happen.
prm := &prmRemapIdentity{Prefix: c.prefix, SignedPrefix: c.signedPrefix}
testImageAndSig(t, prm, c.imageRef, c.sigRef, c.result)
}
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
prm, err := NewPRMRemapIdentity("docker.io", "docker.io")
require.NoError(t, err)
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)
// Verify that the behavior is otherwise the same as for prmMatchRepoDigestOrExact:
// prmMatchRepoDigestOrExact is a middle ground between prmMatchExact and prmMatchRepository:
// It accepts anything prmMatchExact accepts,…
for _, test := range prmExactMatchTestTable {
if test.result == true {
prmRemapIdentityMRDOETestCase(t, true, test.refA, test.refB, test.result)
prmRemapIdentityMRDOETestCase(t, true, test.refB, test.refA, test.result)
}
}
// … and it rejects everything prmMatchRepository rejects.
for _, test := range prmRepositoryMatchTestTable {
if test.result == false {
prmRemapIdentityMRDOETestCase(t, true, test.refA, test.refB, test.result)
prmRemapIdentityMRDOETestCase(t, true, test.refB, test.refA, test.result)
}
}
// The other cases, possibly asymmetrical:
for _, test := range matchRepoDigestOrExactTestTable {
prmRemapIdentityMRDOETestCase(t, false, test.imageRef, test.sigRef, test.result)
}
}
|