Skip to content

Commit 9008597

Browse files
committed
Invert the top-level control flow of encryption steps
Exit early if we are not changing anything, to make the "no change" case easier to follow, and most of the code less indented. Should not change behavior. Signed-off-by: Miloslav Trmač <[email protected]>
1 parent 0958a34 commit 9008597

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

copy/encryption.go

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,28 @@ type bpDecryptionStepData struct {
3434
// srcInfo is only used for error messages.
3535
// Returns data for other steps; the caller should eventually use updateCryptoOperation.
3636
func (ic *imageCopier) blobPipelineDecryptionStep(stream *sourceStream, srcInfo types.BlobInfo) (*bpDecryptionStepData, error) {
37-
if isOciEncrypted(stream.info.MediaType) && ic.c.ociDecryptConfig != nil {
38-
if ic.cannotModifyManifestReason != "" {
39-
return nil, fmt.Errorf("layer %s should be decrypted, but we can’t modify the manifest: %s", srcInfo.Digest, ic.cannotModifyManifestReason)
40-
}
41-
desc := imgspecv1.Descriptor{
42-
Annotations: stream.info.Annotations,
43-
}
44-
reader, decryptedDigest, err := ocicrypt.DecryptLayer(ic.c.ociDecryptConfig, stream.reader, desc, false)
45-
if err != nil {
46-
return nil, fmt.Errorf("decrypting layer %s: %w", srcInfo.Digest, err)
47-
}
48-
49-
stream.reader = reader
50-
stream.info.Digest = decryptedDigest
51-
stream.info.Size = -1
52-
maps.DeleteFunc(stream.info.Annotations, func(k string, _ string) bool {
53-
return strings.HasPrefix(k, "org.opencontainers.image.enc")
54-
})
37+
if !isOciEncrypted(stream.info.MediaType) || ic.c.ociDecryptConfig == nil {
5538
return &bpDecryptionStepData{
56-
decrypting: true,
39+
decrypting: false,
5740
}, nil
5841
}
42+
43+
desc := imgspecv1.Descriptor{
44+
Annotations: stream.info.Annotations,
45+
}
46+
reader, decryptedDigest, err := ocicrypt.DecryptLayer(ic.c.ociDecryptConfig, stream.reader, desc, false)
47+
if err != nil {
48+
return nil, fmt.Errorf("decrypting layer %s: %w", srcInfo.Digest, err)
49+
}
50+
51+
stream.reader = reader
52+
stream.info.Digest = decryptedDigest
53+
stream.info.Size = -1
54+
maps.DeleteFunc(stream.info.Annotations, func(k string, _ string) bool {
55+
return strings.HasPrefix(k, "org.opencontainers.image.enc")
56+
})
5957
return &bpDecryptionStepData{
60-
decrypting: false,
58+
decrypting: true,
6159
}, nil
6260
}
6361

@@ -79,36 +77,33 @@ type bpEncryptionStepData struct {
7977
// Returns data for other steps; the caller should eventually call updateCryptoOperationAndAnnotations.
8078
func (ic *imageCopier) blobPipelineEncryptionStep(stream *sourceStream, toEncrypt bool, srcInfo types.BlobInfo,
8179
decryptionStep *bpDecryptionStepData) (*bpEncryptionStepData, error) {
82-
if toEncrypt && !isOciEncrypted(srcInfo.MediaType) && ic.c.ociEncryptConfig != nil {
83-
if ic.cannotModifyManifestReason != "" {
84-
return nil, fmt.Errorf("layer %s should be encrypted, but we can’t modify the manifest: %s", srcInfo.Digest, ic.cannotModifyManifestReason)
85-
}
86-
87-
var annotations map[string]string
88-
if !decryptionStep.decrypting {
89-
annotations = srcInfo.Annotations
90-
}
91-
desc := imgspecv1.Descriptor{
92-
MediaType: srcInfo.MediaType,
93-
Digest: srcInfo.Digest,
94-
Size: srcInfo.Size,
95-
Annotations: annotations,
96-
}
97-
reader, finalizer, err := ocicrypt.EncryptLayer(ic.c.ociEncryptConfig, stream.reader, desc)
98-
if err != nil {
99-
return nil, fmt.Errorf("encrypting blob %s: %w", srcInfo.Digest, err)
100-
}
101-
102-
stream.reader = reader
103-
stream.info.Digest = ""
104-
stream.info.Size = -1
80+
if !toEncrypt || isOciEncrypted(srcInfo.MediaType) || ic.c.ociEncryptConfig == nil {
10581
return &bpEncryptionStepData{
106-
encrypting: true,
107-
finalizer: finalizer,
82+
encrypting: false,
10883
}, nil
10984
}
85+
86+
var annotations map[string]string
87+
if !decryptionStep.decrypting {
88+
annotations = srcInfo.Annotations
89+
}
90+
desc := imgspecv1.Descriptor{
91+
MediaType: srcInfo.MediaType,
92+
Digest: srcInfo.Digest,
93+
Size: srcInfo.Size,
94+
Annotations: annotations,
95+
}
96+
reader, finalizer, err := ocicrypt.EncryptLayer(ic.c.ociEncryptConfig, stream.reader, desc)
97+
if err != nil {
98+
return nil, fmt.Errorf("encrypting blob %s: %w", srcInfo.Digest, err)
99+
}
100+
101+
stream.reader = reader
102+
stream.info.Digest = ""
103+
stream.info.Size = -1
110104
return &bpEncryptionStepData{
111-
encrypting: false,
105+
encrypting: true,
106+
finalizer: finalizer,
112107
}, nil
113108
}
114109

0 commit comments

Comments
 (0)