Skip to content

Commit 44b8288

Browse files
committed
Add basic cosign verification tests
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 7c72acc commit 44b8288

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

controllers/ocirepository_controller_test.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
1617
package controllers
1718

1819
import (
@@ -24,9 +25,6 @@ import (
2425
"encoding/pem"
2526
"errors"
2627
"fmt"
27-
coptions "github.com/sigstore/cosign/cmd/cosign/cli/options"
28-
"github.com/sigstore/cosign/cmd/cosign/cli/sign"
29-
"github.com/sigstore/cosign/pkg/cosign"
3028
"math/big"
3129
"net"
3230
"net/http"
@@ -55,6 +53,9 @@ import (
5553
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
5654
"github.com/google/go-containerregistry/pkg/v1/mutate"
5755
. "github.com/onsi/gomega"
56+
coptions "github.com/sigstore/cosign/cmd/cosign/cli/options"
57+
"github.com/sigstore/cosign/cmd/cosign/cli/sign"
58+
"github.com/sigstore/cosign/pkg/cosign"
5859
corev1 "k8s.io/api/core/v1"
5960
apierrors "k8s.io/apimachinery/pkg/api/errors"
6061
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -1231,7 +1232,7 @@ func TestOCIRepository_verifyOCISourceSignature(t *testing.T) {
12311232
url string
12321233
reference *sourcev1.OCIRepositoryRef
12331234
shouldSign bool
1234-
wantErr bool
1235+
wantErrMsg string
12351236
}{
12361237
{
12371238
name: "signed image should pass verification",
@@ -1246,6 +1247,7 @@ func TestOCIRepository_verifyOCISourceSignature(t *testing.T) {
12461247
Tag: "6.1.5",
12471248
},
12481249
shouldSign: false,
1250+
wantErrMsg: "no matching signatures were found",
12491251
},
12501252
}
12511253

@@ -1256,6 +1258,29 @@ func TestOCIRepository_verifyOCISourceSignature(t *testing.T) {
12561258
Storage: testStorage,
12571259
}
12581260

1261+
pf := func(b bool) ([]byte, error) {
1262+
return []byte("cosign-password"), nil
1263+
}
1264+
1265+
keys, err := cosign.GenerateKeyPair(pf)
1266+
g.Expect(err).ToNot(HaveOccurred())
1267+
1268+
err = os.WriteFile(path.Join(tmpDir, "cosign.key"), keys.PrivateBytes, 0600)
1269+
g.Expect(err).ToNot(HaveOccurred())
1270+
1271+
secret := &corev1.Secret{
1272+
ObjectMeta: metav1.ObjectMeta{
1273+
Name: "cosign-key",
1274+
},
1275+
Data: map[string][]byte{
1276+
"cosign.pub": keys.PublicBytes,
1277+
}}
1278+
1279+
err = r.Create(ctx, secret)
1280+
if err != nil {
1281+
g.Expect(err).NotTo(HaveOccurred())
1282+
}
1283+
12591284
for _, tt := range tests {
12601285
t.Run(tt.name, func(t *testing.T) {
12611286
obj := &sourcev1.OCIRepository{
@@ -1273,69 +1298,44 @@ func TestOCIRepository_verifyOCISourceSignature(t *testing.T) {
12731298
},
12741299
}
12751300

1276-
pf := func(b bool) ([]byte, error) {
1277-
return []byte("foo"), nil
1278-
}
1279-
1280-
keys, err := cosign.GenerateKeyPair(pf)
1281-
if err != nil {
1282-
g.Expect(err).ToNot(HaveOccurred())
1283-
}
1284-
1285-
err = os.WriteFile("cosign.key", keys.PrivateBytes, 0600)
1286-
if err != nil {
1287-
g.Expect(err).ToNot(HaveOccurred())
1288-
}
1289-
1290-
secret := &corev1.Secret{
1291-
ObjectMeta: metav1.ObjectMeta{
1292-
Name: "cosign-key",
1293-
},
1294-
Data: map[string][]byte{
1295-
"cosign.pub": keys.PublicBytes,
1296-
}}
1297-
1298-
err = r.Create(ctx, secret)
1299-
if err != nil {
1300-
g.Expect(err).NotTo(HaveOccurred())
1301-
}
1302-
13031301
keychain, err := r.keychain(ctx, obj)
13041302
if err != nil {
13051303
g.Expect(err).ToNot(HaveOccurred())
13061304
}
13071305

13081306
options := r.craneOptions(ctx, obj.Spec.Insecure)
13091307
options = append(options, crane.WithAuthFromKeychain(keychain))
1310-
url, err := r.getArtifactURL(obj, options)
1308+
artifactURL, err := r.getArtifactURL(obj, options)
13111309
if err != nil {
13121310
g.Expect(err).ToNot(HaveOccurred())
13131311
}
13141312

13151313
if tt.shouldSign {
1316-
13171314
ko := coptions.KeyOpts{
1318-
KeyRef: "cosign.key",
1315+
KeyRef: path.Join(tmpDir, "cosign.key"),
13191316
PassFunc: pf,
13201317
}
13211318

1322-
t.Logf("url: %s", url)
1323-
1324-
ro := &coptions.RootOptions{}
1325-
err = sign.SignCmd(ro, ko, coptions.RegistryOptions{Keychain: keychain}, nil, []string{url}, "", "", false, "", "", "", false, false, "", false)
1326-
if err != nil {
1327-
g.Expect(err).ToNot(HaveOccurred())
1319+
ro := &coptions.RootOptions{
1320+
Timeout: timeout,
13281321
}
1322+
err = sign.SignCmd(ro, ko, coptions.RegistryOptions{Keychain: keychain},
1323+
nil, []string{artifactURL}, "",
1324+
"", true, "",
1325+
"", "", false,
1326+
false, "", false)
1327+
g.Expect(err).ToNot(HaveOccurred())
13291328
}
13301329

1331-
err = r.verifyOCISourceSignature(ctx, obj, url, keychain)
1332-
if tt.wantErr {
1333-
g.Expect(err).To(HaveOccurred())
1334-
return
1330+
err = r.verifyOCISourceSignature(ctx, obj, artifactURL, keychain)
1331+
if tt.wantErrMsg != "" {
1332+
g.Expect(err).ToNot(BeNil())
1333+
g.Expect(err.Error()).To(ContainSubstring(tt.wantErrMsg))
1334+
} else {
1335+
g.Expect(err).ToNot(HaveOccurred())
13351336
}
13361337
})
13371338
}
1338-
13391339
}
13401340

13411341
func TestOCIRepository_stalled(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ require (
6060
github.com/prometheus/client_golang v1.13.0
6161
github.com/sigstore/cosign v1.11.1
6262
github.com/sigstore/sigstore v1.4.0
63+
github.com/sirupsen/logrus v1.9.0
6364
github.com/spf13/pflag v1.0.5
6465
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503
6566
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c
@@ -292,7 +293,6 @@ require (
292293
github.com/shopspring/decimal v1.2.0 // indirect
293294
github.com/sigstore/fulcio v0.5.3 // indirect
294295
github.com/sigstore/rekor v0.11.0 // indirect
295-
github.com/sirupsen/logrus v1.9.0 // indirect
296296
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
297297
github.com/soheilhy/cmux v0.1.5 // indirect
298298
github.com/spf13/afero v1.8.2 // indirect

0 commit comments

Comments
 (0)