-
Notifications
You must be signed in to change notification settings - Fork 46
image/internal: sha512 support for skopeo copy #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,8 +176,15 @@ func (ref dirReference) layerPath(digest digest.Digest) (string, error) { | |
| if err := digest.Validate(); err != nil { // digest.Digest.Encoded() panics on failure, and could possibly result in a path with ../, so validate explicitly. | ||
| return "", err | ||
| } | ||
| // FIXME: Should we keep the digest identification? | ||
| return filepath.Join(ref.path, digest.Encoded()), nil | ||
|
|
||
| var filename string | ||
| if digest.Algorithm().String() == "sha256" { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(I don’t have much of a preference for |
||
| filename = digest.Encoded() | ||
| } else { | ||
| filename = digest.Algorithm().String() + "-" + digest.Encoded() | ||
| } | ||
|
|
||
| return filepath.Join(ref.path, filename), nil | ||
| } | ||
|
|
||
| // signaturePath returns a path for a signature within a directory using our conventions. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package image | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/opencontainers/go-digest" | ||
| ) | ||
|
|
||
| func validateBlobAgainstDigest(blob []byte, expectedDigest digest.Digest) error { | ||
mtrmac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if expectedDigest == "" { | ||
| return fmt.Errorf("expected digest is empty") | ||
| } | ||
mtrmac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| err := expectedDigest.Validate() | ||
| if err != nil { | ||
| return fmt.Errorf("invalid digest format %q: %w", expectedDigest, err) | ||
| } | ||
| digestAlgorithm := expectedDigest.Algorithm() | ||
| if !digestAlgorithm.Available() { | ||
| return fmt.Errorf("unsupported digest algorithm: %s", digestAlgorithm) | ||
| } | ||
| computedDigest := digestAlgorithm.FromBytes(blob) | ||
| if computedDigest != expectedDigest { | ||
| return fmt.Errorf("blob digest %s does not match expected %s", computedDigest, expectedDigest) | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package image | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/opencontainers/go-digest" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestValidateBlobAgainstDigest(t *testing.T) { | ||
| testBlob := []byte("test data") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| blob []byte | ||
| expectedDigest digest.Digest | ||
| expectError bool | ||
| errorContains string | ||
| }{ | ||
| { | ||
| name: "empty digest", | ||
| blob: testBlob, | ||
| expectedDigest: "", | ||
| expectError: true, | ||
| errorContains: "expected digest is empty", | ||
| }, | ||
| { | ||
| name: "invalid digest format - no algorithm", | ||
| blob: testBlob, | ||
| expectedDigest: "invalidsyntax", | ||
| expectError: true, | ||
| errorContains: "invalid digest format", | ||
| }, | ||
| { | ||
| name: "invalid digest format - sha256 prefix w/ malformed hex", | ||
| blob: testBlob, | ||
| expectedDigest: "sha256:notahexstring!@#", | ||
| expectError: true, | ||
| errorContains: "invalid digest format", | ||
| }, | ||
| { | ||
| name: "invalid digest format - sha512 prefix w/ malformed hex", | ||
| blob: testBlob, | ||
| expectedDigest: "sha512:notahexstring!@#", | ||
| expectError: true, | ||
| errorContains: "invalid digest format", | ||
| }, | ||
| { | ||
| name: "invalid digest format - unknown algorithm", | ||
| blob: testBlob, | ||
| expectedDigest: "unknown-algo:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", | ||
| expectError: true, | ||
| errorContains: "invalid digest format", | ||
| }, | ||
| { | ||
| name: "digest mismatch", | ||
| blob: testBlob, | ||
| expectedDigest: digest.SHA256.FromBytes([]byte("different data")), | ||
| expectError: true, | ||
| errorContains: "blob digest", | ||
| }, | ||
| { | ||
| name: "empty blob with matching digest", | ||
| blob: []byte{}, | ||
| expectedDigest: digest.SHA256.FromBytes([]byte{}), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "unavailable algorithm - blake2b", | ||
| blob: testBlob, | ||
| expectedDigest: "blake2b:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", | ||
| expectError: true, | ||
| errorContains: "invalid digest format", | ||
| }, | ||
| { | ||
| name: "sha256 digest success", | ||
| blob: testBlob, | ||
| expectedDigest: digest.SHA256.FromBytes(testBlob), | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "sha512 digest success", | ||
| blob: testBlob, | ||
| expectedDigest: digest.SHA512.FromBytes(testBlob), | ||
| expectError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validateBlobAgainstDigest(tt.blob, tt.expectedDigest) | ||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), tt.errorContains) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the destination-transport-side digest computation must be a more complex logic, see in the earlier PR about the interaction with
cannotModifyManifestReason.… and
dirReference.layerPathdiscards the algorithm name; that does not generalize for other algorithms, we need to move towards agility where adding an extra algorithm is a ~parameter change and does not require any more changes to the “code proper”; i.e. discarding algorithm names is no longer much of an option.(We need to keep the existing file names for
sha256, to retain compatibility. And… do we define a new value forversionPath?!)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PutBlob to store blob under provided digest algorithm with the algorithm name prepended (except for Canonical) along with a canonical digest hardlink.
Doesn't break existing behaviour but there's new stuff, so maybe we should? I'll defer to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And, anyway, readers of
dir:can only start with the manifest, and the values provided in the manifest. So ifPutBlobreturns sha512, and the manifest is written to include sha512, readers will not know the sha256 value and have no way to use it.So I don’t think we need to compute both digests at all; just the
layerPathchanges to the path computation, + some (as-yet-undefined) logic forPutBlobto use “the algorithm the user wanted”, should be sufficient.I think with the changes to
layerPath, we need to. Previously it was, hypothetically, possible to read a complete sha512 image fromdir:, and those images will now break. And we will need to update bothdir…Dest…anddir…Src…: destinations should refuse to work on future versions, and still assign the existing1.1version for sha256 images for maximum compatibility. sources should detect+refuse future versions.Consider making the
dirchanges a separate PR.