-
Notifications
You must be signed in to change notification settings - Fork 48
dir: 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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package directory | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/opencontainers/go-digest" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.podman.io/image/v5/pkg/blobinfocache/memory" | ||
| "go.podman.io/image/v5/types" | ||
| ) | ||
|
|
||
| func TestVersionAssignment(t *testing.T) { | ||
| for _, c := range []struct { | ||
| name string | ||
| algorithms []digest.Algorithm | ||
| expectedVersion version | ||
| }{ | ||
| { | ||
| name: "SHA256 only gets version 1.1", | ||
| algorithms: []digest.Algorithm{digest.SHA256}, | ||
| expectedVersion: version1_1, | ||
| }, | ||
| { | ||
| name: "SHA512 only gets version 1.2", | ||
| algorithms: []digest.Algorithm{digest.SHA512}, | ||
| expectedVersion: version1_2, | ||
| }, | ||
| { | ||
| name: "Mixed SHA256 and SHA512 gets version 1.2", | ||
| algorithms: []digest.Algorithm{digest.SHA256, digest.SHA512}, | ||
| expectedVersion: version1_2, | ||
| }, | ||
| } { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| ref, tmpDir := refToTempDir(t) | ||
| cache := memory.New() | ||
|
|
||
| dest, err := ref.NewImageDestination(context.Background(), nil) | ||
| require.NoError(t, err) | ||
| defer dest.Close() | ||
|
|
||
| for i, algo := range c.algorithms { | ||
| blobData := []byte("test-blob-" + algo.String() + "-" + string(rune(i))) | ||
| var blobDigest digest.Digest | ||
| if algo == digest.SHA256 { | ||
| blobDigest = "" | ||
| } else { | ||
| blobDigest = algo.FromBytes(blobData) | ||
| } | ||
| _, err = dest.PutBlob(context.Background(), bytes.NewReader(blobData), types.BlobInfo{Digest: blobDigest, Size: int64(len(blobData))}, cache, false) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| err = dest.Commit(context.Background(), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| versionBytes, err := os.ReadFile(filepath.Join(tmpDir, "version")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, c.expectedVersion.String(), string(versionBytes)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,7 +146,7 @@ func (ref dirReference) NewImage(ctx context.Context, sys *types.SystemContext) | |
| // NewImageSource returns a types.ImageSource for this reference. | ||
| // The caller must call .Close() on the returned ImageSource. | ||
| func (ref dirReference) NewImageSource(ctx context.Context, sys *types.SystemContext) (types.ImageSource, error) { | ||
| return newImageSource(ref), nil | ||
| return newImageSource(ref) | ||
| } | ||
|
|
||
| // NewImageDestination returns a types.ImageDestination for this reference. | ||
|
|
@@ -172,12 +172,19 @@ func (ref dirReference) manifestPath(instanceDigest *digest.Digest) (string, err | |
| } | ||
|
|
||
| // layerPath returns a path for a layer tarball within a directory using our conventions. | ||
| 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. | ||
| func (ref dirReference) layerPath(d digest.Digest) (string, error) { | ||
| if err := d.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 d.Algorithm() == digest.Canonical { | ||
| filename = d.Encoded() | ||
| } else { | ||
| filename = d.Algorithm().String() + "-" + d.Encoded() | ||
|
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 wonder whether we need to do a version check and conditionally use the old logic here. I guess that doesn’t matter in practice. |
||
| } | ||
|
|
||
| return filepath.Join(ref.path, filename), nil | ||
| } | ||
|
|
||
| // signaturePath returns a path for a signature within a directory using our conventions. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package directory | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| const ( | ||
| versionPrefix = "Directory Transport Version: " | ||
| ) | ||
|
|
||
| // version represents a parsed directory transport version | ||
| type version struct { | ||
| major int | ||
| minor int | ||
| } | ||
|
|
||
| // Supported versions | ||
| // Write version file based on digest algorithm used. | ||
| // 1.1 for sha256-only images, 1.2 otherwise. | ||
| var ( | ||
| version1_1 = version{major: 1, minor: 1} | ||
| version1_2 = version{major: 1, minor: 2} | ||
| maxSupportedVersion = version1_2 | ||
| ) | ||
|
|
||
| // String formats a version as a string suitable for writing to the version file | ||
| func (v version) String() string { | ||
| return fmt.Sprintf("%s%d.%d\n", versionPrefix, v.major, v.minor) | ||
| } | ||
|
|
||
| // parseVersion parses a version string into major and minor components. | ||
| // Returns an error if the format is invalid. | ||
| func parseVersion(versionStr string) (version, error) { | ||
| var v version | ||
| expectedFormat := versionPrefix + "%d.%d\n" | ||
| // Sscanf parsing is a bit loose (treats spaces specially), but a strict check immediately follows | ||
| n, err := fmt.Sscanf(versionStr, expectedFormat, &v.major, &v.minor) | ||
| if err != nil || n != 2 || versionStr != v.String() { | ||
| return version{}, fmt.Errorf("invalid version format") | ||
| } | ||
| return v, nil | ||
| } | ||
|
|
||
| // TODO: Potential refactor for better interoperability with `cmp` | ||
| // https://github.com/containers/container-libs/pull/475#discussion_r2571131267 | ||
| // isGreaterThan returns true if v is greater than other | ||
| func (v version) isGreaterThan(other version) bool { | ||
| if v.major != other.major { | ||
| return v.major > other.major | ||
| } | ||
| return v.minor > other.minor | ||
| } | ||
|
|
||
| // UnsupportedVersionError indicates that the directory uses a version newer than we support | ||
| type UnsupportedVersionError struct { | ||
| Version string // The unsupported version string found | ||
| Path string // The path to the directory | ||
| } | ||
|
|
||
| func (e UnsupportedVersionError) Error() string { | ||
| return fmt.Sprintf("unsupported directory transport version %q at %s", e.Version, e.Path) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package directory | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseVersion(t *testing.T) { | ||
| for _, c := range []struct { | ||
| input string | ||
| expected version | ||
| shouldError bool | ||
| }{ | ||
| { | ||
| input: "Directory Transport Version: 1.1\n", | ||
| expected: version{major: 1, minor: 1}, | ||
| }, | ||
| { | ||
| input: "Directory Transport Version: 1.2\n", | ||
| expected: version{major: 1, minor: 2}, | ||
| }, | ||
| { | ||
| input: "Invalid prefix 1.1\n", | ||
| shouldError: true, | ||
| }, | ||
| { | ||
| input: "Directory Transport Version: 1.1", | ||
| shouldError: true, | ||
| }, | ||
| { | ||
| input: "Directory Transport Version: abc\n", | ||
| shouldError: true, | ||
| }, | ||
| } { | ||
| t.Run(c.input, func(t *testing.T) { | ||
| v, err := parseVersion(c.input) | ||
| if c.shouldError { | ||
| assert.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.Equal(t, c.expected, v) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestVersionComparison(t *testing.T) { | ||
| assert.False(t, version1_1.isGreaterThan(version1_1)) | ||
| assert.False(t, version1_1.isGreaterThan(version1_2)) | ||
| assert.True(t, version1_2.isGreaterThan(version1_1)) | ||
| assert.True(t, version{major: 2, minor: 0}.isGreaterThan(version1_2)) | ||
| assert.True(t, version{major: 1, minor: 3}.isGreaterThan(version1_2)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.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'll keep the dir changes here because of the review comments. Separate PR for image/internal at #486