Skip to content

Commit 7f1e5e4

Browse files
committed
image/directory: store blobs with digest algorithm prefix
When storing blobs with non-canonical digest algorithms (e.g., sha512), store the blob under the provided digest algorithm with an algorithm prefix (e.g., "sha512-abc" instead of just "abc"). SHA256 (canonical) digests continue to be stored without a prefix for backward compatibility. Signed-off-by: Lokesh Mandvekar <[email protected]>
1 parent 4fc82df commit 7f1e5e4

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

image/directory/directory_dest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ func (d *dirImageDestination) PutBlobWithOptions(ctx context.Context, stream io.
151151
}
152152
}()
153153

154-
digester, stream := putblobdigest.DigestIfCanonicalUnknown(stream, inputInfo)
154+
digester, stream := putblobdigest.DigestIfUnknown(stream, inputInfo)
155+
155156
// TODO: This can take quite some time, and should ideally be cancellable using ctx.Done().
156157
size, err := io.Copy(blobFile, stream)
157158
if err != nil {

image/directory/directory_src.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type dirImageSource struct {
2626

2727
// newImageSource returns an ImageSource reading from an existing directory.
2828
// The caller must call .Close() on the returned ImageSource.
29-
func newImageSource(ref dirReference) private.ImageSource {
29+
func newImageSource(ref dirReference) (private.ImageSource, error) {
3030
s := &dirImageSource{
3131
PropertyMethodsInitialize: impl.PropertyMethods(impl.Properties{
3232
HasThreadSafeGetBlob: false,
@@ -36,7 +36,7 @@ func newImageSource(ref dirReference) private.ImageSource {
3636
ref: ref,
3737
}
3838
s.Compat = impl.AddCompat(s)
39-
return s
39+
return s, nil
4040
}
4141

4242
// Reference returns the reference used to set up this source, _as specified by the user_

image/directory/directory_transport.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (ref dirReference) NewImage(ctx context.Context, sys *types.SystemContext)
146146
// NewImageSource returns a types.ImageSource for this reference.
147147
// The caller must call .Close() on the returned ImageSource.
148148
func (ref dirReference) NewImageSource(ctx context.Context, sys *types.SystemContext) (types.ImageSource, error) {
149-
return newImageSource(ref), nil
149+
return newImageSource(ref)
150150
}
151151

152152
// NewImageDestination returns a types.ImageDestination for this reference.
@@ -172,12 +172,19 @@ func (ref dirReference) manifestPath(instanceDigest *digest.Digest) (string, err
172172
}
173173

174174
// layerPath returns a path for a layer tarball within a directory using our conventions.
175-
func (ref dirReference) layerPath(digest digest.Digest) (string, error) {
176-
if err := digest.Validate(); err != nil { // digest.Digest.Encoded() panics on failure, and could possibly result in a path with ../, so validate explicitly.
175+
func (ref dirReference) layerPath(d digest.Digest) (string, error) {
176+
if err := d.Validate(); err != nil { // digest.Digest.Encoded() panics on failure, and could possibly result in a path with ../, so validate explicitly.
177177
return "", err
178178
}
179-
// FIXME: Should we keep the digest identification?
180-
return filepath.Join(ref.path, digest.Encoded()), nil
179+
180+
var filename string
181+
if d.Algorithm() == digest.Canonical {
182+
filename = d.Encoded()
183+
} else {
184+
filename = d.Algorithm().String() + "-" + d.Encoded()
185+
}
186+
187+
return filepath.Join(ref.path, filename), nil
181188
}
182189

183190
// signaturePath returns a path for a signature within a directory using our conventions.

image/directory/directory_transport_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,21 @@ func TestReferenceManifestPath(t *testing.T) {
209209
}
210210

211211
func TestReferenceLayerPath(t *testing.T) {
212-
const hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
212+
const hex256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
213+
const hex512 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
213214

214215
ref, tmpDir := refToTempDir(t)
215216
dirRef, ok := ref.(dirReference)
216217
require.True(t, ok)
217-
res, err := dirRef.layerPath("sha256:" + hex)
218+
219+
res, err := dirRef.layerPath("sha256:" + hex256)
220+
require.NoError(t, err)
221+
assert.Equal(t, tmpDir+"/"+hex256, res)
222+
223+
res, err = dirRef.layerPath("sha512:" + hex512)
218224
require.NoError(t, err)
219-
assert.Equal(t, tmpDir+"/"+hex, res)
225+
assert.Equal(t, tmpDir+"/sha512-"+hex512, res)
226+
220227
_, err = dirRef.layerPath(digest.Digest("sha256:../hello"))
221228
assert.Error(t, err)
222229
}

0 commit comments

Comments
 (0)