Skip to content

Commit 364de4c

Browse files
committed
Wrap creation of CryptoConfig in constructors
Signed-off-by: Stefan Berger <[email protected]>
1 parent f776141 commit 364de4c

5 files changed

Lines changed: 32 additions & 30 deletions

File tree

cmd/ctr/commands/images/decrypt.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ var decryptCommand = cli.Command{
8888
return err
8989
}
9090

91-
cc := &encconfig.CryptoConfig{
92-
DecryptConfig: &encconfig.DecryptConfig{
93-
Parameters: dcparameters,
94-
},
95-
}
91+
cc := encconfig.InitDecryption(dcparameters)
92+
9693
_, err = decryptImage(client, ctx, local, newName, cc, layers32, context.StringSlice("platform"))
9794

9895
return err

cmd/ctr/commands/images/encrypt.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,8 @@ var encryptCommand = cli.Command{
133133
dcparameters["privkeys-passwords"] = privKeysPasswords
134134
dcparameters["x509s"] = decX509s
135135

136-
cc := &encconfig.CryptoConfig{
137-
EncryptConfig: &encconfig.EncryptConfig{
138-
Parameters: parameters,
139-
DecryptConfig: encconfig.DecryptConfig{
140-
Parameters: dcparameters,
141-
},
142-
},
143-
}
136+
cc := encconfig.InitEncryption(parameters, dcparameters)
137+
144138
_, err = encryptImage(client, ctx, local, newName, cc, layers32, context.StringSlice("platform"))
145139

146140
return err

image_enc_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,7 @@ func TestImageEncryption(t *testing.T) {
129129
dcparameters["privkeys"] = [][]byte{privateKey}
130130
dcparameters["privkeys-passwords"] = [][]byte{{}}
131131

132-
cc := &encconfig.CryptoConfig{
133-
EncryptConfig: &encconfig.EncryptConfig{
134-
Parameters: parameters,
135-
DecryptConfig: encconfig.DecryptConfig{
136-
Parameters: dcparameters,
137-
},
138-
},
139-
}
132+
cc := encconfig.InitEncryption(parameters, dcparameters)
140133

141134
// Perform encryption of image
142135
encSpec, modified, err := imgenc.EncryptImage(ctx, client.ContentStore(), image.Target, cc, lf)
@@ -156,11 +149,8 @@ func TestImageEncryption(t *testing.T) {
156149
t.Fatalf("Unable to create image: %v", err)
157150
}
158151

159-
cc = &encconfig.CryptoConfig{
160-
DecryptConfig: &encconfig.DecryptConfig{
161-
Parameters: dcparameters,
162-
},
163-
}
152+
cc = encconfig.InitDecryption(dcparameters)
153+
164154
// Clean up function cancels lease before deleting the image so the images are
165155
// properly deleted
166156
defer func() {

images/encryption/encryption.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,13 @@ func DecryptImage(ctx context.Context, cs content.Store, desc ocispec.Descriptor
415415
// It takes decrypting of the layers only as far as decrypting the asymmetrically encrypted data
416416
// The decryption is only done for the current platform
417417
func CheckAuthorization(ctx context.Context, cs content.Store, desc ocispec.Descriptor, dc *encconfig.DecryptConfig) error {
418-
cc := encconfig.CryptoConfig{
419-
DecryptConfig: dc,
420-
}
418+
cc := encconfig.InitDecryption(dc.Parameters)
421419

422420
lf := func(desc ocispec.Descriptor) bool {
423421
return true
424422
}
425423

426-
_, _, err := cryptImage(ctx, cs, desc, &cc, lf, cryptoOpUnwrapOnly)
424+
_, _, err := cryptImage(ctx, cs, desc, cc, lf, cryptoOpUnwrapOnly)
427425
if err != nil {
428426
return errors.Wrapf(err, "you are not authorized to use this image")
429427
}

pkg/encryption/config/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,26 @@ type CryptoConfig struct {
3838
EncryptConfig *EncryptConfig
3939
DecryptConfig *DecryptConfig
4040
}
41+
42+
// InitDecryption initialized a CryptoConfig object with parameters used for decryption
43+
func InitDecryption(dcparameters map[string][][]byte) *CryptoConfig {
44+
return &CryptoConfig{
45+
DecryptConfig: &DecryptConfig{
46+
Parameters: dcparameters,
47+
},
48+
}
49+
}
50+
51+
// InitEncryption initializes a CryptoConfig object with parameters used for encryption
52+
// It also takes dcparameters that may be needed for decryption when adding a recipient
53+
// to an already encrypted image
54+
func InitEncryption(parameters, dcparameters map[string][][]byte) *CryptoConfig {
55+
return &CryptoConfig{
56+
EncryptConfig: &EncryptConfig{
57+
Parameters: parameters,
58+
DecryptConfig: DecryptConfig{
59+
Parameters: dcparameters,
60+
},
61+
},
62+
}
63+
}

0 commit comments

Comments
 (0)