-
Notifications
You must be signed in to change notification settings - Fork 29
[sha512] image: add configurable digest support #375
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
Open
lsm5
wants to merge
1
commit into
containers:main
Choose a base branch
from
lsm5:image-agile-digest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.podman.io/image/v5/pkg/compression" | ||
supportedDigests "go.podman.io/storage/pkg/supported-digests" | ||
) | ||
|
||
const ( | ||
|
@@ -87,6 +88,9 @@ func TestMatchesDigest(t *testing.T) { | |
{"v2s1.manifest.json", TestDockerV2S2ManifestDigest, false}, | ||
// Unrecognized algorithm | ||
{"v2s2.manifest.json", digest.Digest("md5:2872f31c5c1f62a694fbd20c1e85257c"), false}, | ||
// SHA512 test cases (these should fail because we're using SHA256 by default) | ||
{"v2s2.manifest.json", digest.SHA512.FromBytes([]byte("test")), false}, | ||
{"v2s1.manifest.json", digest.SHA512.FromBytes([]byte("test")), false}, | ||
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. This is not much of a test case… and |
||
// Mangled format | ||
{"v2s2.manifest.json", digest.Digest(TestDockerV2S2ManifestDigest.String() + "abc"), false}, | ||
{"v2s2.manifest.json", digest.Digest(TestDockerV2S2ManifestDigest.String()[:20]), false}, | ||
|
@@ -112,6 +116,74 @@ func TestMatchesDigest(t *testing.T) { | |
assert.NoError(t, err) | ||
} | ||
|
||
func TestMatchesDigestWithSHA512(t *testing.T) { | ||
// Save original algorithm and restore it after the test | ||
originalAlgorithm := supportedDigests.TmpDigestForNewObjects() | ||
defer func() { | ||
err := supportedDigests.TmpSetDigestForNewObjects(originalAlgorithm) | ||
require.NoError(t, err) | ||
}() | ||
|
||
// Set SHA512 algorithm | ||
err := supportedDigests.TmpSetDigestForNewObjects(digest.SHA512) | ||
require.NoError(t, err) | ||
|
||
cases := []struct { | ||
path string | ||
expectedDigest digest.Digest | ||
result bool | ||
}{ | ||
// Success cases with SHA512 | ||
{"v2s2.manifest.json", digest.SHA512.FromBytes([]byte("test")), false}, // Wrong data | ||
{"v2s1.manifest.json", digest.SHA512.FromBytes([]byte("test")), false}, // Wrong data | ||
// Empty manifest | ||
{"", digest.SHA512.FromBytes([]byte{}), true}, | ||
// Wrong algorithm (SHA256 when SHA512 is configured) | ||
{"v2s2.manifest.json", TestDockerV2S2ManifestDigest, false}, | ||
{"v2s1.manifest.json", TestDockerV2S1ManifestDigest, false}, | ||
// Mangled format | ||
{"v2s2.manifest.json", digest.Digest("sha512:invalid"), false}, | ||
{"v2s2.manifest.json", digest.Digest(""), false}, | ||
} | ||
|
||
for _, c := range cases { | ||
var manifest []byte | ||
var err error | ||
|
||
if c.path == "" { | ||
// Empty manifest case | ||
manifest = []byte{} | ||
} else { | ||
manifest, err = os.ReadFile(filepath.Join("testdata", c.path)) | ||
require.NoError(t, err) | ||
} | ||
|
||
// For success cases, compute the correct SHA512 digest | ||
if c.result { | ||
c.expectedDigest = digest.SHA512.FromBytes(manifest) | ||
} | ||
|
||
res, err := MatchesDigest(manifest, c.expectedDigest) | ||
require.NoError(t, err) | ||
assert.Equal(t, c.result, res, "path=%s, expectedDigest=%s", c.path, c.expectedDigest) | ||
} | ||
|
||
// Test with actual manifest files and their correct SHA512 digests | ||
manifestFiles := []string{"v2s2.manifest.json", "v2s1.manifest.json"} | ||
for _, file := range manifestFiles { | ||
manifest, err := os.ReadFile(filepath.Join("testdata", file)) | ||
require.NoError(t, err) | ||
|
||
// Use the Digest function to get the correct digest (which handles signature stripping) | ||
expectedDigest, err := Digest(manifest) | ||
require.NoError(t, err) | ||
|
||
res, err := MatchesDigest(manifest, expectedDigest) | ||
require.NoError(t, err) | ||
assert.True(t, res, "MatchesDigest should work with SHA512 for %s", file) | ||
} | ||
} | ||
|
||
func TestNormalizedMIMEType(t *testing.T) { | ||
for _, c := range []string{ // Valid MIME types, normalized to themselves | ||
DockerV2Schema1MediaType, | ||
|
Oops, something went wrong.
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.
This works locally, but there’s the
cachedDiffID
code path where we use an older value. That probably needs to be adjusted, if we want the behavior to be predictable for users.(This is relevant for schema1 only, and that is nowadays entirely disabled in Docker and the distribution/distribution registry, at least by default. Arguably interoperable support for sha512+schema1 is never going to happen … but, for us, it might be easier to generate sha512+schema1 and let it fail, than to have an ~undocumented exception where we ignore the configuration, or to specifically hard-code an error path and make it absolutely impossible to use such a setup.)