Skip to content

Commit dde436e

Browse files
dmcgowanlumjjb
authored andcommitted
Crypto library movement and changes to content helper interfaces
Signed-off-by: Derek McGowan <[email protected]>
1 parent bf8804c commit dde436e

29 files changed

Lines changed: 695 additions & 692 deletions

cmd/ctr/commands/images/crypt_utils.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import (
2828

2929
"github.com/containerd/containerd"
3030
"github.com/containerd/containerd/images"
31-
"github.com/containerd/containerd/images/encryption"
32-
encconfig "github.com/containerd/containerd/images/encryption/config"
33-
encutils "github.com/containerd/containerd/images/encryption/utils"
31+
imgenc "github.com/containerd/containerd/images/encryption"
3432
"github.com/containerd/containerd/leases"
33+
"github.com/containerd/containerd/pkg/encryption"
34+
encconfig "github.com/containerd/containerd/pkg/encryption/config"
35+
encutils "github.com/containerd/containerd/pkg/encryption/utils"
3536
"github.com/containerd/containerd/platforms"
3637

3738
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -209,7 +210,7 @@ func getGPGPrivateKeys(context *cli.Context, gpgSecretKeyRingFiles [][]byte, des
209210
return encryption.GPGGetPrivateKey(descs, gpgClient, gpgVault, mustFindKey, dcparameters)
210211
}
211212

212-
func createLayerFilter(client *containerd.Client, ctx gocontext.Context, desc ocispec.Descriptor, layers []int32, platformList []ocispec.Platform) (images.LayerFilter, error) {
213+
func createLayerFilter(client *containerd.Client, ctx gocontext.Context, desc ocispec.Descriptor, layers []int32, platformList []ocispec.Platform) (imgenc.LayerFilter, error) {
213214
alldescs, err := images.GetImageLayerDescriptors(ctx, client.ContentStore(), desc)
214215
if err != nil {
215216
return nil, err
@@ -261,9 +262,9 @@ func cryptImage(client *containerd.Client, ctx gocontext.Context, name, newName
261262
defer ls.Delete(ctx, l, leases.SynchronousDelete)
262263

263264
if encrypt {
264-
newSpec, modified, err = images.EncryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
265+
newSpec, modified, err = imgenc.EncryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
265266
} else {
266-
newSpec, modified, err = images.DecryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
267+
newSpec, modified, err = imgenc.DecryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
267268
}
268269
if err != nil {
269270
return image, err

cmd/ctr/commands/images/decrypt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"fmt"
2121

2222
"github.com/containerd/containerd/cmd/ctr/commands"
23-
"github.com/containerd/containerd/images"
24-
encconfig "github.com/containerd/containerd/images/encryption/config"
23+
imgenc "github.com/containerd/containerd/images/encryption"
24+
encconfig "github.com/containerd/containerd/pkg/encryption/config"
2525
"github.com/pkg/errors"
2626
"github.com/urfave/cli"
2727
)
@@ -77,7 +77,7 @@ var decryptCommand = cli.Command{
7777
return err
7878
}
7979

80-
isEncrypted := images.HasEncryptedLayer(ctx, descs)
80+
isEncrypted := imgenc.HasEncryptedLayer(ctx, descs)
8181
if !isEncrypted {
8282
fmt.Printf("Nothing to decrypted.\n")
8383
return nil

cmd/ctr/commands/images/encrypt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/containerd/containerd/cmd/ctr/commands"
23-
encconfig "github.com/containerd/containerd/images/encryption/config"
23+
encconfig "github.com/containerd/containerd/pkg/encryption/config"
2424
"github.com/pkg/errors"
2525
"github.com/urfave/cli"
2626
)

cmd/ctr/commands/images/layerinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"text/tabwriter"
2525

2626
"github.com/containerd/containerd/cmd/ctr/commands"
27-
"github.com/containerd/containerd/images/encryption"
27+
"github.com/containerd/containerd/pkg/encryption"
2828
"github.com/containerd/containerd/platforms"
2929

3030
"github.com/pkg/errors"

content/helpers.go

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,6 @@ func WriteBlob(ctx context.Context, cs Ingester, ref string, r io.Reader, desc o
8787
return Copy(ctx, cw, r, desc.Size, desc.Digest, opts...)
8888
}
8989

90-
// WriteBlobBlind writes data without expected digest into the content store. If
91-
// expected already exists, the method returns immediately and the reader will
92-
// not be consumed.
93-
//
94-
// This is useful when the digest and size are NOT known beforehand.
95-
//
96-
// Copy is buffered, so no need to wrap reader in buffered io.
97-
func WriteBlobBlind(ctx context.Context, cs Ingester, ref string, r io.Reader, opts ...Opt) (digest.Digest, int64, error) {
98-
cw, err := OpenWriter(ctx, cs, WithRef(ref))
99-
if err != nil {
100-
return "", 0, errors.Wrap(err, "failed to open writer")
101-
}
102-
defer cw.Close()
103-
104-
ws, err := cw.Status()
105-
if err != nil {
106-
return "", 0, errors.Wrap(err, "failed to get status")
107-
}
108-
109-
if ws.Offset > 0 {
110-
// not needed
111-
return "", 0, errors.New("ws.Offset > 0 is not supported")
112-
}
113-
114-
size, err := copyWithBuffer(cw, r)
115-
if err != nil {
116-
return "", 0, errors.Wrap(err, "failed to copy")
117-
}
118-
119-
digest := cw.Digest()
120-
121-
if err := cw.Commit(ctx, size, digest); err != nil {
122-
if !errdefs.IsAlreadyExists(err) {
123-
return "", 0, errors.Wrapf(err, "failed commit block")
124-
}
125-
}
126-
127-
return digest, size, err
128-
}
129-
13090
// OpenWriter opens a new writer for the given reference, retrying if the writer
13191
// is locked until the reference is available or returns an error.
13292
func OpenWriter(ctx context.Context, cs Ingester, opts ...WriterOpt) (Writer, error) {
@@ -209,6 +169,28 @@ func CopyReaderAt(cw Writer, ra ReaderAt, n int64) error {
209169
return err
210170
}
211171

172+
// CopyReader copies to a writer from a given reader, returning
173+
// the number of bytes copied.
174+
// Note: if the writer has a non-zero offset, the total number
175+
// of bytes read may be greater than those copied if the reader
176+
// is not an io.Seeker.
177+
// This copy does not commit the writer.
178+
func CopyReader(cw Writer, r io.Reader) (int64, error) {
179+
ws, err := cw.Status()
180+
if err != nil {
181+
return 0, errors.Wrap(err, "failed to get status")
182+
}
183+
184+
if ws.Offset > 0 {
185+
r, err = seekReader(r, ws.Offset, 0)
186+
if err != nil {
187+
return 0, errors.Wrapf(err, "unable to resume write to %v", ws.Ref)
188+
}
189+
}
190+
191+
return copyWithBuffer(cw, r)
192+
}
193+
212194
// seekReader attempts to seek the reader to the given offset, either by
213195
// resolving `io.Seeker`, by detecting `io.ReaderAt`, or discarding
214196
// up to the given offset.

image_enc_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
"github.com/containerd/containerd/content"
2626
"github.com/containerd/containerd/errdefs"
2727
"github.com/containerd/containerd/images"
28-
encconfig "github.com/containerd/containerd/images/encryption/config"
29-
"github.com/containerd/containerd/images/encryption/utils"
28+
imgenc "github.com/containerd/containerd/images/encryption"
3029
"github.com/containerd/containerd/leases"
30+
encconfig "github.com/containerd/containerd/pkg/encryption/config"
31+
"github.com/containerd/containerd/pkg/encryption/utils"
3132
"github.com/containerd/containerd/platforms"
3233
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3334
)
@@ -142,7 +143,7 @@ func TestImageEncryption(t *testing.T) {
142143
defer ls.Delete(ctx, l, leases.SynchronousDelete)
143144

144145
// Perform encryption of image
145-
encSpec, modified, err := images.EncryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
146+
encSpec, modified, err := imgenc.EncryptImage(ctx, client.ContentStore(), ls, l, image.Target, cc, lf)
146147
if err != nil {
147148
t.Fatal(err)
148149
}
@@ -180,7 +181,7 @@ func TestImageEncryption(t *testing.T) {
180181
}
181182
defer ls.Delete(ctx, l, leases.SynchronousDelete)
182183

183-
decSpec, modified, err := images.DecryptImage(ctx, client.ContentStore(), ls, l, encSpec, cc, lf)
184+
decSpec, modified, err := imgenc.DecryptImage(ctx, client.ContentStore(), ls, l, encSpec, cc, lf)
184185
if err != nil {
185186
t.Fatal(err)
186187
}

0 commit comments

Comments
 (0)