| 1 |
//go:build containers_image_sequoia |
| 2 |
|
| 3 |
package simplesequoia |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"os" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/opencontainers/go-digest" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"go.podman.io/image/v5/docker/reference" |
| 14 |
internalSig "go.podman.io/image/v5/internal/signature" |
| 15 |
internalSigner "go.podman.io/image/v5/internal/signer" |
| 16 |
"go.podman.io/image/v5/signature" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
// testImageManifestDigest is the Docker manifest digest of "image.manifest.json" |
| 21 |
testImageManifestDigest = digest.Digest("sha256:20bf21ed457b390829cdbeec8795a7bea1626991fda603e0d01b4e7f60427e55") |
| 22 |
|
| 23 |
testSequoiaHome = "./testdata" |
| 24 |
// testKeyFingerprint is a fingerprint of a test key in testSequoiaHome, generated using |
| 25 |
// > sq --home $(pwd)/signature/simplesequoia/testdata key generate --name 'Skopeo Sequoia testing key' --own-key --expiration=never |
| 26 |
testKeyFingerprint = "50DDE898DF4E48755C8C2B7AF6F908B6FA48A229" |
| 27 |
// testKeyFingerprintWithPassphrase is a fingerprint of a test key in testSequoiaHome, generated using |
| 28 |
// > sq --home $(pwd)/signature/simplesequoia/testdata key generate --name 'Skopeo Sequoia testing key with passphrase' --own-key --expiration=never |
| 29 |
testKeyFingerprintWithPassphrase = "1F5825285B785E1DB13BF36D2D11A19ABA41C6AE" |
| 30 |
// testPassphrase is the passphrase for testKeyFingerprintWithPassphrase. |
| 31 |
testPassphrase = "WithPassphrase123" |
| 32 |
) |
| 33 |
|
| 34 |
func TestNewSigner(t *testing.T) { |
| 35 |
// An option causes an error |
| 36 |
_, err := NewSigner(WithSequoiaHome(testSequoiaHome), WithKeyFingerprint(testKeyFingerprint), WithPassphrase("\n")) |
| 37 |
assert.Error(t, err) |
| 38 |
|
| 39 |
// WithKeyFingerprint is missing |
| 40 |
_, err = NewSigner(WithSequoiaHome(testSequoiaHome), WithPassphrase("something")) |
| 41 |
assert.Error(t, err) |
| 42 |
|
| 43 |
// A smoke test |
| 44 |
s, err := NewSigner(WithSequoiaHome(testSequoiaHome), WithKeyFingerprint(testKeyFingerprint)) |
| 45 |
require.NoError(t, err) |
| 46 |
err = s.Close() |
| 47 |
assert.NoError(t, err) |
| 48 |
|
| 49 |
t.Setenv("SEQUOIA_CRYPTO_POLICY", "this/does/not/exist") // Both unreadable files, and relative paths, should cause an error. |
| 50 |
_, err = NewSigner(WithSequoiaHome(testSequoiaHome), WithKeyFingerprint(testKeyFingerprint)) |
| 51 |
assert.Error(t, err) |
| 52 |
} |
| 53 |
|
| 54 |
func TestSimpleSignerProgressMessage(t *testing.T) { |
| 55 |
// Just a smoke test |
| 56 |
s, err := NewSigner(WithSequoiaHome(testSequoiaHome), WithKeyFingerprint(testKeyFingerprint)) |
| 57 |
require.NoError(t, err) |
| 58 |
defer func() { |
| 59 |
err = s.Close() |
| 60 |
assert.NoError(t, err) |
| 61 |
}() |
| 62 |
|
| 63 |
_ = internalSigner.ProgressMessage(s) |
| 64 |
} |
| 65 |
|
| 66 |
func TestSimpleSignerSignImageManifest(t *testing.T) { |
| 67 |
manifest, err := os.ReadFile("../fixtures/image.manifest.json") |
| 68 |
require.NoError(t, err) |
| 69 |
testImageSignatureReference, err := reference.ParseNormalizedNamed("example.com/testing/manifest:notlatest") |
| 70 |
require.NoError(t, err) |
| 71 |
|
| 72 |
// Successful signing |
| 73 |
for _, c := range []struct { |
| 74 |
name string |
| 75 |
publicKeyPath string |
| 76 |
fingerprint string |
| 77 |
opts []Option |
| 78 |
}{ |
| 79 |
{ |
| 80 |
name: "No passphrase", |
| 81 |
publicKeyPath: "./testdata/no-passphrase.pub", |
| 82 |
fingerprint: testKeyFingerprint, |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "With passphrase", |
| 86 |
publicKeyPath: "./testdata/with-passphrase.pub", |
| 87 |
fingerprint: testKeyFingerprintWithPassphrase, |
| 88 |
opts: []Option{WithPassphrase(testPassphrase)}, |
| 89 |
}, |
| 90 |
} { |
| 91 |
s, err := NewSigner(append([]Option{WithSequoiaHome(testSequoiaHome), WithKeyFingerprint(c.fingerprint)}, c.opts...)...) |
| 92 |
require.NoError(t, err, c.name) |
| 93 |
defer s.Close() |
| 94 |
|
| 95 |
sig, err := internalSigner.SignImageManifest(context.Background(), s, manifest, testImageSignatureReference) |
| 96 |
require.NoError(t, err, c.name) |
| 97 |
simpleSig, ok := sig.(internalSig.SimpleSigning) |
| 98 |
require.True(t, ok) |
| 99 |
|
| 100 |
publicKey, err := os.ReadFile(c.publicKeyPath) |
| 101 |
require.NoError(t, err) |
| 102 |
mech, importedFingerprint, err := signature.NewEphemeralGPGSigningMechanism(publicKey) |
| 103 |
require.NoError(t, err) |
| 104 |
assert.Equal(t, []string{c.fingerprint}, importedFingerprint) |
| 105 |
defer mech.Close() |
| 106 |
|
| 107 |
verified, err := signature.VerifyDockerManifestSignature(simpleSig.UntrustedSignature(), manifest, testImageSignatureReference.String(), mech, c.fingerprint) |
| 108 |
require.NoError(t, err) |
| 109 |
assert.Equal(t, testImageSignatureReference.String(), verified.DockerReference) |
| 110 |
assert.Equal(t, testImageManifestDigest, verified.DockerManifestDigest) |
| 111 |
} |
| 112 |
|
| 113 |
invalidManifest, err := os.ReadFile("../fixtures/v2s1-invalid-signatures.manifest.json") |
| 114 |
require.NoError(t, err) |
| 115 |
invalidReference, err := reference.ParseNormalizedNamed("no-tag") |
| 116 |
require.NoError(t, err) |
| 117 |
for _, c := range []struct { |
| 118 |
name string |
| 119 |
opts []Option |
| 120 |
// NOTE: We DO NOT promise that things that don't fail during NewSigner won't start failing there. |
| 121 |
// Actually we’d prefer failures to be identified early. This field only records current expected behavior, not the _desired_ end state. |
| 122 |
creationFails bool |
| 123 |
creationErrorContains string |
| 124 |
manifest []byte |
| 125 |
ref reference.Named |
| 126 |
}{ |
| 127 |
{ |
| 128 |
name: "No key to sign with", |
| 129 |
opts: []Option{WithSequoiaHome(testSequoiaHome)}, |
| 130 |
creationFails: true, |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "Invalid passphrase", |
| 134 |
opts: []Option{ |
| 135 |
WithSequoiaHome(testSequoiaHome), |
| 136 |
WithKeyFingerprint(testKeyFingerprintWithPassphrase), |
| 137 |
WithPassphrase(testPassphrase + "\n"), |
| 138 |
}, |
| 139 |
creationFails: true, |
| 140 |
creationErrorContains: "invalid passphrase", |
| 141 |
ref: testImageSignatureReference, |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "Wrong passphrase", |
| 145 |
opts: []Option{ |
| 146 |
WithSequoiaHome(testSequoiaHome), |
| 147 |
WithKeyFingerprint(testKeyFingerprintWithPassphrase), |
| 148 |
WithPassphrase("wrong"), |
| 149 |
}, |
| 150 |
ref: testImageSignatureReference, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "No passphrase", |
| 154 |
opts: []Option{WithKeyFingerprint(testKeyFingerprintWithPassphrase)}, |
| 155 |
ref: testImageSignatureReference, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "Error computing Docker manifest", |
| 159 |
opts: []Option{ |
| 160 |
WithSequoiaHome(testSequoiaHome), |
| 161 |
WithKeyFingerprint(testKeyFingerprint), |
| 162 |
}, |
| 163 |
manifest: invalidManifest, |
| 164 |
ref: testImageSignatureReference, |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: "Invalid reference", |
| 168 |
opts: []Option{ |
| 169 |
WithSequoiaHome(testSequoiaHome), |
| 170 |
WithKeyFingerprint(testKeyFingerprint), |
| 171 |
}, |
| 172 |
ref: invalidReference, |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "Error signing", |
| 176 |
opts: []Option{ |
| 177 |
WithSequoiaHome(testSequoiaHome), |
| 178 |
WithKeyFingerprint("this fingerprint doesn't exist"), |
| 179 |
}, |
| 180 |
ref: testImageSignatureReference, |
| 181 |
}, |
| 182 |
} { |
| 183 |
s, err := NewSigner(c.opts...) |
| 184 |
if c.creationFails { |
| 185 |
assert.Error(t, err, c.name) |
| 186 |
if c.creationErrorContains != "" { |
| 187 |
assert.ErrorContains(t, err, c.creationErrorContains, c.name) |
| 188 |
} |
| 189 |
} else { |
| 190 |
require.NoError(t, err, c.name) |
| 191 |
defer s.Close() |
| 192 |
|
| 193 |
m := manifest |
| 194 |
if c.manifest != nil { |
| 195 |
m = c.manifest |
| 196 |
} |
| 197 |
_, err = internalSigner.SignImageManifest(context.Background(), s, m, c.ref) |
| 198 |
assert.Error(t, err, c.name) |
| 199 |
} |
| 200 |
} |
| 201 |
} |