2021-10-28 22:48:35 +00:00
|
|
|
package cosign
|
|
|
|
|
|
|
|
import (
|
2021-10-29 16:18:47 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-03 07:45:35 +00:00
|
|
|
"github.com/sigstore/cosign/pkg/oci"
|
|
|
|
|
2021-10-28 22:48:35 +00:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/sigstore/cosign/pkg/cosign"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const cosignPayload = `{
|
|
|
|
"critical": {
|
|
|
|
"identity": {
|
|
|
|
"docker-reference": "registry-v2.nirmata.io/pause"
|
|
|
|
},
|
|
|
|
"image": {
|
|
|
|
"docker-manifest-digest": "sha256:4a1c4b21597c1b4415bdbecb28a3296c6b5e23ca4f9feeb599860a1dac6a0108"
|
|
|
|
},
|
|
|
|
"type": "cosign container image signature"
|
|
|
|
},
|
2021-12-16 06:19:44 +00:00
|
|
|
"optional": {
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": "baz"
|
|
|
|
}
|
2021-10-28 22:48:35 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
const tektonPayload = `{
|
|
|
|
"Critical": {
|
|
|
|
"Identity": {
|
|
|
|
"docker-reference": "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/nop"
|
|
|
|
},
|
|
|
|
"Image": {
|
|
|
|
"Docker-manifest-digest": "sha256:6a037d5ba27d9c6be32a9038bfe676fb67d2e4145b4f53e9c61fb3e69f06e816"
|
|
|
|
},
|
|
|
|
"Type": "Tekton container signature"
|
|
|
|
},
|
2021-12-10 19:46:22 +00:00
|
|
|
"Optional": {
|
|
|
|
"Issuer": "https://github.com/login/oauth",
|
|
|
|
"Subject": "https://github.com/mycompany/demo/.github/workflows/ci.yml@refs/heads/main"
|
|
|
|
}
|
2021-10-28 22:48:35 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
func TestCosignPayload(t *testing.T) {
|
2022-01-22 12:26:53 +00:00
|
|
|
var log logr.Logger = logr.Discard()
|
2021-10-28 22:48:35 +00:00
|
|
|
image := "registry-v2.nirmata.io/pause"
|
2021-11-03 07:45:35 +00:00
|
|
|
signedPayloads := cosign.SignedPayload{Payload: []byte(cosignPayload)}
|
2022-01-28 05:13:23 +00:00
|
|
|
p, err := extractPayload([]oci.Signature{&sig{cosignPayload: signedPayloads}})
|
2021-12-10 19:46:22 +00:00
|
|
|
assert.NilError(t, err)
|
2021-12-16 06:19:44 +00:00
|
|
|
a := map[string]string{"foo": "bar"}
|
2022-01-28 05:13:23 +00:00
|
|
|
err = checkAnnotations(p, a)
|
2021-12-16 06:19:44 +00:00
|
|
|
assert.NilError(t, err)
|
2021-12-10 19:46:22 +00:00
|
|
|
d, err := extractDigest(image, p, log)
|
2021-10-28 22:48:35 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, d, "sha256:4a1c4b21597c1b4415bdbecb28a3296c6b5e23ca4f9feeb599860a1dac6a0108")
|
|
|
|
|
|
|
|
image2 := "gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/nop"
|
2021-11-03 07:45:35 +00:00
|
|
|
signedPayloads2 := cosign.SignedPayload{Payload: []byte(tektonPayload)}
|
2022-01-28 05:13:23 +00:00
|
|
|
signatures2 := []oci.Signature{&sig{cosignPayload: signedPayloads2}}
|
|
|
|
p2, err := extractPayload(signatures2)
|
2021-12-10 19:46:22 +00:00
|
|
|
assert.NilError(t, err)
|
2022-01-28 05:13:23 +00:00
|
|
|
|
2021-12-10 19:46:22 +00:00
|
|
|
d2, err := extractDigest(image2, p2, log)
|
2021-10-28 22:48:35 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, d2, "sha256:6a037d5ba27d9c6be32a9038bfe676fb67d2e4145b4f53e9c61fb3e69f06e816")
|
|
|
|
}
|
2022-01-28 06:50:29 +00:00
|
|
|
|
|
|
|
func TestCosignKeyless(t *testing.T) {
|
|
|
|
var log logr.Logger = logr.Discard()
|
|
|
|
opts := Options{
|
|
|
|
ImageRef: "ghcr.io/jimbugwadia/pause2",
|
|
|
|
Issuer: "https://github.com/",
|
|
|
|
Subject: "jim",
|
|
|
|
Log: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := VerifySignature(opts)
|
|
|
|
assert.Error(t, err, "subject mismatch: expected jim@nirmata.com, got jim")
|
|
|
|
|
|
|
|
opts.Subject = "jim@nirmata.com"
|
|
|
|
_, err = VerifySignature(opts)
|
|
|
|
assert.Error(t, err, "issuer mismatch: expected https://github.com/login/oauth, got https://github.com/")
|
|
|
|
|
|
|
|
opts.Issuer = "https://github.com/login/oauth"
|
|
|
|
_, err = VerifySignature(opts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
}
|