-
Notifications
You must be signed in to change notification settings - Fork 63
[sha512] image/copy: allow users to force digest algorithm in CLI (e.g. skopeo copy --force-digest)
#552
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
8
commits into
containers:main
Choose a base branch
from
lsm5:skopeo-copy-force-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
[sha512] image/copy: allow users to force digest algorithm in CLI (e.g. skopeo copy --force-digest)
#552
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b2024f
Add internal/digests.Options and internal/digests.Options.Choose
mtrmac 6579f7e
Add image.copy.digestOptions
mtrmac 29bbd4f
Add DigestIfAlgorithmUnknown
mtrmac 358b694
WIP: Sketch PutBlob... digest choice in c/image/copy + transports
mtrmac a922b75
manifest: Add UpdateConfigDigest() to manifest abstraction layer
lsm5 d2cf28c
image/copy: Add BrokenSetForceDestinationDigestAlgorithm() API
lsm5 f167c0d
image/copy: Implement core digest forcing logic
lsm5 eca922f
image/copy: Add guards for unsupported digest forcing scenarios
lsm5 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
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ import ( | |
| "github.com/sirupsen/logrus" | ||
| "github.com/vbauerster/mpb/v8" | ||
| "go.podman.io/image/v5/docker/reference" | ||
| "go.podman.io/image/v5/internal/digests" | ||
| "go.podman.io/image/v5/internal/image" | ||
| "go.podman.io/image/v5/internal/pkg/platform" | ||
| "go.podman.io/image/v5/internal/private" | ||
|
|
@@ -592,12 +593,27 @@ func (ic *imageCopier) copyUpdatedConfigAndManifest(ctx context.Context, instanc | |
| return nil, "", fmt.Errorf("reading manifest: %w", err) | ||
| } | ||
|
|
||
| if err := ic.copyConfig(ctx, pendingImage); err != nil { | ||
| newConfigDigest, err := ic.copyConfig(ctx, pendingImage) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| // FIXME: Single image only; multi-arch needs per-instance config digest updates. | ||
| // See https://github.com/containers/container-libs/pull/552#discussion_r2611627578 | ||
| if newConfigDigest != nil { | ||
| man, err = ic.updateManifestConfigDigest(man, pendingImage, *newConfigDigest) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("updating manifest config digest: %w", err) | ||
| } | ||
| } | ||
|
|
||
| ic.c.Printf("Writing manifest to image destination\n") | ||
| manifestDigest, err := manifest.Digest(man) | ||
| // Choose the digest algorithm based on digest options | ||
| manifestDigestAlgo, err := ic.c.options.digestOptions.Choose(digests.Situation{}) | ||
| if err != nil { | ||
| return nil, "", fmt.Errorf("choosing manifest digest algorithm: %w", err) | ||
| } | ||
| manifestDigest, err := manifest.DigestWithAlgorithm(man, manifestDigestAlgo) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
@@ -611,13 +627,33 @@ func (ic *imageCopier) copyUpdatedConfigAndManifest(ctx context.Context, instanc | |
| return man, manifestDigest, nil | ||
| } | ||
|
|
||
| // updateManifestConfigDigest updates the config digest in the manifest using the manifest abstraction layer. | ||
| func (ic *imageCopier) updateManifestConfigDigest(manifestBlob []byte, src types.Image, newConfigDigest digest.Digest) ([]byte, error) { | ||
|
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. This needs to go through the manifest abstraction in (It can be a method on |
||
| _, mt, err := src.Manifest(context.Background()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("getting manifest type: %w", err) | ||
| } | ||
|
|
||
| m, err := manifest.FromBlob(manifestBlob, mt) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parsing manifest: %w", err) | ||
| } | ||
|
|
||
| if err := m.UpdateConfigDigest(newConfigDigest); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return m.Serialize() | ||
| } | ||
|
|
||
| // copyConfig copies config.json, if any, from src to dest. | ||
| func (ic *imageCopier) copyConfig(ctx context.Context, src types.Image) error { | ||
| // It returns the new config digest if it changed (due to digest algorithm forcing), or nil otherwise. | ||
| func (ic *imageCopier) copyConfig(ctx context.Context, src types.Image) (*digest.Digest, error) { | ||
| srcInfo := src.ConfigInfo() | ||
| if srcInfo.Digest != "" { | ||
| if err := ic.c.concurrentBlobCopiesSemaphore.Acquire(ctx, 1); err != nil { | ||
| // This can only fail with ctx.Err(), so no need to blame acquiring the semaphore. | ||
| return fmt.Errorf("copying config: %w", err) | ||
| return nil, fmt.Errorf("copying config: %w", err) | ||
| } | ||
| defer ic.c.concurrentBlobCopiesSemaphore.Release(1) | ||
|
|
||
|
|
@@ -645,13 +681,17 @@ func (ic *imageCopier) copyConfig(ctx context.Context, src types.Image) error { | |
| return destInfo, nil | ||
| }() | ||
| if err != nil { | ||
| return err | ||
| return nil, err | ||
| } | ||
| if destInfo.Digest != srcInfo.Digest { | ||
| return fmt.Errorf("Internal error: copying uncompressed config blob %s changed digest to %s", srcInfo.Digest, destInfo.Digest) | ||
| // If algorithms match, the whole digest values must match | ||
| if destInfo.Digest.Algorithm() == srcInfo.Digest.Algorithm() { | ||
| return nil, fmt.Errorf("Internal error: copying uncompressed config blob %s changed digest to %s", srcInfo.Digest, destInfo.Digest) | ||
| } | ||
| return &destInfo.Digest, nil | ||
| } | ||
| } | ||
| return nil | ||
| return nil, nil | ||
| } | ||
|
|
||
| // diffIDResult contains both a digest value and an error from diffIDComputationGoroutine. | ||
|
|
@@ -743,19 +783,34 @@ func (ic *imageCopier) copyLayer(ctx context.Context, srcInfo types.BlobInfo, to | |
| tocDigest = *d | ||
| } | ||
|
|
||
| reused, reusedBlob, err := ic.c.dest.TryReusingBlobWithOptions(ctx, srcInfo, private.TryReusingBlobOptions{ | ||
| Cache: ic.c.blobInfoCache, | ||
| CanSubstitute: canSubstitute, | ||
| EmptyLayer: emptyLayer, | ||
| LayerIndex: &layerIndex, | ||
| SrcRef: srcRef, | ||
| PossibleManifestFormats: append([]string{ic.manifestConversionPlan.preferredMIMEType}, ic.manifestConversionPlan.otherMIMETypeCandidates...), | ||
| RequiredCompression: requiredCompression, | ||
| OriginalCompression: srcInfo.CompressionAlgorithm, | ||
| TOCDigest: tocDigest, | ||
| }) | ||
| if err != nil { | ||
| return types.BlobInfo{}, "", fmt.Errorf("trying to reuse blob %s at destination: %w", srcInfo.Digest, err) | ||
| // FIXME: Blob reuse disabled when forcing different digest algorithm. | ||
| canTryReuse := true | ||
| if forcedAlgo := ic.c.options.digestOptions.MustUseSet(); forcedAlgo != "" { | ||
| if srcInfo.Digest.Algorithm() != forcedAlgo { | ||
| logrus.Debugf("Skipping blob reuse for %s: digest algorithm %s doesn't match forced algorithm %s", | ||
| srcInfo.Digest, srcInfo.Digest.Algorithm(), forcedAlgo) | ||
| canTryReuse = false | ||
| } | ||
| } | ||
|
|
||
| reused := false | ||
| var reusedBlob private.ReusedBlob | ||
| if canTryReuse { | ||
| var err error | ||
| reused, reusedBlob, err = ic.c.dest.TryReusingBlobWithOptions(ctx, srcInfo, private.TryReusingBlobOptions{ | ||
| Cache: ic.c.blobInfoCache, | ||
| CanSubstitute: canSubstitute, | ||
| EmptyLayer: emptyLayer, | ||
| LayerIndex: &layerIndex, | ||
| SrcRef: srcRef, | ||
| PossibleManifestFormats: append([]string{ic.manifestConversionPlan.preferredMIMEType}, ic.manifestConversionPlan.otherMIMETypeCandidates...), | ||
| RequiredCompression: requiredCompression, | ||
| OriginalCompression: srcInfo.CompressionAlgorithm, | ||
| TOCDigest: tocDigest, | ||
| }) | ||
| if err != nil { | ||
| return types.BlobInfo{}, "", fmt.Errorf("trying to reuse blob %s at destination: %w", srcInfo.Digest, err) | ||
| } | ||
| } | ||
| if reused { | ||
| logrus.Debugf("Skipping blob %s (already present):", srcInfo.Digest) | ||
|
|
||
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
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.
Fail if this is already set? It might protect against oversights. OTOH it can also prevent some code patterns — if that turned out to be troublesome, we can always remove that enforcement later.