| 1 | // |
| 2 | // Copyright 2021 The Sigstore Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | package payloadscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/ |
| 17 | |
| 18 | import ( |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 21 | |
| 22 | "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/" |
| 23 | "github.com/google/go-cmp/cmp/cmpoptsscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp/cmpopts`/" |
| 24 | "github.com/google/go-containerregistry/pkg/namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/" |
| 25 | ) |
| 26 | |
| 27 | const validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest. = "sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f" |
| 28 | |
| 29 | func mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, digestStr string) namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Digestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest# { |
| 30 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 31 | digest, err := namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.NewDigestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/NewDigest().(digestStr) |
| 32 | if err != nil { |
| 33 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("could not parse digest %q: %v", digestStr, err) |
| 34 | } |
| 35 | return digest |
| 36 | } |
| 37 | |
| 38 | func TestMarshalCosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/TestMarshalCosign().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 39 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 40 | testCases := []struct { |
| 41 | desc string |
| 42 | imgPayload Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign# |
| 43 | expected string |
| 44 | }{ |
| 45 | { |
| 46 | desc: "no claims", |
| 47 | imgPayload: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 48 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 49 | }, |
| 50 | expected: `{"critical":{"identity":{"docker-reference":"example.com/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":null}`, |
| 51 | }, |
| 52 | { |
| 53 | desc: "standard atomic signature", |
| 54 | imgPayload: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 55 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/atomic/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 56 | Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations.: map[string]any{ |
| 57 | "creator": "atomic", |
| 58 | "timestamp": 1458239713, |
| 59 | }, |
| 60 | }, |
| 61 | expected: `{"critical":{"identity":{"docker-reference":"example.com/atomic/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":{"creator":"atomic","timestamp":1458239713}}`, |
| 62 | }, |
| 63 | { |
| 64 | desc: "arbitrary claims", |
| 65 | imgPayload: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 66 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/cosign/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 67 | Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations.: map[string]any{ |
| 68 | "creator": "anyone", |
| 69 | "some_struct": map[string]any{ |
| 70 | "foo": "bar", |
| 71 | "false": true, |
| 72 | "nothing": nil, |
| 73 | }, |
| 74 | "CamelCase WithSpace": 8.314, |
| 75 | }, |
| 76 | }, |
| 77 | expected: `{"critical":{"identity":{"docker-reference":"example.com/cosign/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":{"CamelCase WithSpace":8.314,"creator":"anyone","some_struct":{"false":true,"foo":"bar","nothing":null}}}`, |
| 78 | }, |
| 79 | { |
| 80 | desc: "custom identity", |
| 81 | imgPayload: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 82 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 83 | ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity.: "docker.io/library/test:1.2.3", |
| 84 | }, |
| 85 | expected: `{"critical":{"identity":{"docker-reference":"docker.io/library/test:1.2.3"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":null}`, |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | for _, tc := range testCases { |
| 90 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 91 | tc := tc |
| 92 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 93 | payload, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(tc.imgPayload) |
| 94 | if err != nil { |
| 95 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("json.Marshal returned error: %v", err) |
| 96 | } |
| 97 | |
| 98 | if tc.expected != string(payload) { |
| 99 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("marshaled payload was %q, wanted %q", string(payload), tc.expected) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestUnmarshalCosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/TestUnmarshalCosign().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 106 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 107 | testCases := []struct { |
| 108 | desc string |
| 109 | payload string |
| 110 | |
| 111 | expected Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign# |
| 112 | expectErr bool |
| 113 | }{ |
| 114 | { |
| 115 | desc: "no claims", |
| 116 | payload: `{"critical":{"identity":{"docker-reference":"example.com/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":null}`, |
| 117 | expected: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 118 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 119 | ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity.: "example.com/test/image", |
| 120 | }, |
| 121 | }, |
| 122 | { |
| 123 | desc: "arbitrary claims", |
| 124 | payload: `{"critical":{"identity":{"docker-reference":"example.com/cosign/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"cosign container image signature"},"optional":{"CamelCase WithSpace":8.314,"creator":"anyone","some_struct":{"false":true,"foo":"bar","nothing":null}}}`, |
| 125 | expected: Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{ |
| 126 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image.: mustParseDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/mustParseDigest().(t, "example.com/cosign/test/image@"+validDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/validDigest.), |
| 127 | ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity.: "example.com/cosign/test/image", |
| 128 | Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations.: map[string]any{ |
| 129 | "creator": "anyone", |
| 130 | "some_struct": map[string]any{ |
| 131 | "foo": "bar", |
| 132 | "false": true, |
| 133 | "nothing": nil, |
| 134 | }, |
| 135 | "CamelCase WithSpace": 8.314, |
| 136 | }, |
| 137 | }, |
| 138 | }, |
| 139 | { |
| 140 | desc: "unknown type", |
| 141 | payload: `{"critical":{"identity":{"docker-reference":"example.com/atomic/test/image"},"image":{"docker-manifest-digest":"sha256:d34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33fd34db33f"},"type":"atomic container signature"},"optional":{}}`, |
| 142 | expectErr: true, |
| 143 | }, |
| 144 | } |
| 145 | |
| 146 | for _, tc := range testCases { |
| 147 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tc.desc, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 148 | tc := tc |
| 149 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 150 | var imgPayload Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign# |
| 151 | err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().([]byte(tc.payload), &imgPayload) |
| 152 | if err != nil { |
| 153 | if !tc.expectErr { |
| 154 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("json.Unmarshal unexpectedly returned an error: %v", err) |
| 155 | } |
| 156 | return // operation failed successfully |
| 157 | } |
| 158 | if tc.expectErr { |
| 159 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("json.Unmarshal returned %v, expected an error", imgPayload) |
| 160 | } |
| 161 | // Use IgnoreUnexported to handle unexported fields in name.Digest and nested types |
| 162 | opts := cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/.Optionsscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/Options#{ |
| 163 | cmpoptsscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp/cmpopts`/.IgnoreUnexportedscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp/cmpopts`/IgnoreUnexported().(namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Registryscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Registry#{}, namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Repositoryscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Repository#{}, namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Digestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#{}), |
| 164 | cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/.Comparerscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/Comparer().(func(a, b namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Digestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#) bool { |
| 165 | return a.Stringscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#String().() == b.Stringscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#String().() |
| 166 | }), |
| 167 | } |
| 168 | if diff := cmpscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.7.0 `github.com/google/go-cmp/cmp`/Diff().(tc.expected, imgPayload, opts); diff != "" { |
| 169 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Cosign unmarshalled incorrectly (-want +got):\n%s", diff) |
| 170 | } |
| 171 | }) |
| 172 | } |
| 173 | } |