| 1 |
package rekor |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/ecdsa" |
| 6 |
"encoding/base64" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"os" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/hashicorp/go-retryablehttp" |
| 15 |
"github.com/sigstore/sigstore/pkg/cryptoutils" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
"go.podman.io/image/v5/signature/internal" |
| 19 |
) |
| 20 |
|
| 21 |
func Test_rekorUploadKeyOrCert(t *testing.T) { |
| 22 |
REKOR_SERVER := os.Getenv("REKOR_SERVER_URL") |
| 23 |
if REKOR_SERVER == "" { |
| 24 |
t.Skip("REKOR_SERVER_URL not set or empty. This test requires a proper rekor server to run against, use signature/sigstore/rekor/scripts/start-rekor.sh to set one up quickly") |
| 25 |
} |
| 26 |
|
| 27 |
cosignCertBytes, err := os.ReadFile("../../internal/testdata/rekor-cert") |
| 28 |
require.NoError(t, err) |
| 29 |
cosignSigBase64, err := os.ReadFile("../../internal/testdata/rekor-sig") |
| 30 |
require.NoError(t, err) |
| 31 |
cosignPayloadBytes, err := os.ReadFile("../../internal/testdata/rekor-payload") |
| 32 |
require.NoError(t, err) |
| 33 |
|
| 34 |
// server needs a moment to set to retry a bit |
| 35 |
resp, err := retryablehttp.Get(REKOR_SERVER + "/api/v1/log/publicKey") |
| 36 |
require.NoError(t, err) |
| 37 |
require.Equal(t, http.StatusOK, resp.StatusCode, "unexpected status code from rekor server") |
| 38 |
|
| 39 |
rekorPubKeyPEM, err := io.ReadAll(resp.Body) |
| 40 |
require.NoError(t, err) |
| 41 |
resp.Body.Close() |
| 42 |
|
| 43 |
rekorKey, err := cryptoutils.UnmarshalPEMToPublicKey(rekorPubKeyPEM) |
| 44 |
require.NoError(t, err) |
| 45 |
rekorKeyECDSA, ok := rekorKey.(*ecdsa.PublicKey) |
| 46 |
require.True(t, ok) |
| 47 |
rekorKeysECDSA := []*ecdsa.PublicKey{rekorKeyECDSA} |
| 48 |
|
| 49 |
type args struct { |
| 50 |
keyOrCertBytes []byte |
| 51 |
signatureBase64 string |
| 52 |
payloadBytes []byte |
| 53 |
} |
| 54 |
tests := []struct { |
| 55 |
name string |
| 56 |
args args |
| 57 |
wantErr string |
| 58 |
}{ |
| 59 |
{ |
| 60 |
name: "upload valid signature", |
| 61 |
args: args{ |
| 62 |
keyOrCertBytes: cosignCertBytes, |
| 63 |
signatureBase64: string(cosignSigBase64), |
| 64 |
payloadBytes: cosignPayloadBytes, |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "invalid key", |
| 69 |
args: args{ |
| 70 |
keyOrCertBytes: []byte{1, 2, 3, 4}, |
| 71 |
signatureBase64: string(cosignSigBase64), |
| 72 |
payloadBytes: cosignPayloadBytes, |
| 73 |
}, |
| 74 |
wantErr: "Rekor /api/v1/log/entries failed: bad request (400), {Code:400 Message:error processing entry: invalid public key: failure decoding PEM}", |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "invalid signature", |
| 78 |
args: args{ |
| 79 |
keyOrCertBytes: cosignCertBytes, |
| 80 |
signatureBase64: "AAAA" + string(cosignSigBase64), |
| 81 |
payloadBytes: cosignPayloadBytes, |
| 82 |
}, |
| 83 |
wantErr: "Rekor /api/v1/log/entries failed: bad request (400), {Code:400 Message:error processing entry: verifying signature: ecdsa: Invalid IEEE_P1363 encoded bytes}", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "invalid payload", |
| 87 |
args: args{ |
| 88 |
keyOrCertBytes: cosignCertBytes, |
| 89 |
signatureBase64: string(cosignSigBase64), |
| 90 |
payloadBytes: []byte{2, 3, 4}, |
| 91 |
}, |
| 92 |
wantErr: "Rekor /api/v1/log/entries failed: bad request (400), {Code:400 Message:error processing entry: verifying signature: invalid signature when validating ASN.1 encoded signature}", |
| 93 |
}, |
| 94 |
} |
| 95 |
u, err := url.Parse(REKOR_SERVER) |
| 96 |
require.NoError(t, err) |
| 97 |
for _, tt := range tests { |
| 98 |
t.Run(tt.name, func(t *testing.T) { |
| 99 |
cl := newRekorClient(u) |
| 100 |
|
| 101 |
signatureBytes, err := base64.StdEncoding.DecodeString(tt.args.signatureBase64) |
| 102 |
require.NoError(t, err) |
| 103 |
|
| 104 |
currentTime := time.Now() |
| 105 |
|
| 106 |
// with go 1.24 this should use t.Context() |
| 107 |
got, err := cl.uploadKeyOrCert(context.Background(), tt.args.keyOrCertBytes, signatureBytes, tt.args.payloadBytes) |
| 108 |
if tt.wantErr != "" { |
| 109 |
assert.ErrorContains(t, err, tt.wantErr) |
| 110 |
return |
| 111 |
} |
| 112 |
require.NoError(t, err) |
| 113 |
|
| 114 |
// Now verify the returned rekor set |
| 115 |
tm, err := internal.VerifyRekorSET(rekorKeysECDSA, got, tt.args.keyOrCertBytes, tt.args.signatureBase64, tt.args.payloadBytes) |
| 116 |
require.NoError(t, err) |
| 117 |
// Check that the returned timestamp makes sense. |
| 118 |
// Note that using time.After()/Before() to match will yield incorrect result. |
| 119 |
// time.Now() has nanosecond precision while the rekor time is constructed of the unix seconds. |
| 120 |
// That is why we can only compare the full unix seconds here. |
| 121 |
assert.GreaterOrEqual(t, tm.Unix(), currentTime.Unix(), "time: %s after rekor time: %s", currentTime, tm) |
| 122 |
}) |
| 123 |
} |
| 124 |
} |