Skip to content

Commit ea7bd24

Browse files
simonbairdclaude
andcommitted
Assume cosign.key as key field in signing secret
I want to use this and have it dwim: --vsa-signing-key k8s://conforma/vsa-signing-key Ref: https://issues.redhat.com/browse/EC-1589 Co-authored-by: Claude Code <[email protected]>
1 parent 07acfa9 commit ea7bd24

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

internal/utils/private_key.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package utils
1818

1919
import (
2020
"context"
21+
"fmt"
22+
"strings"
2123

2224
"github.com/spf13/afero"
2325
)
@@ -26,7 +28,16 @@ import (
2628
// This follows the same pattern as cosignSig.PublicKeyFromKeyRef but for private keys.
2729
// Supported formats:
2830
// - File path: "/path/to/private-key.pem"
31+
// - Kubernetes secret: "k8s://namespace/secret-name"
2932
// - Kubernetes secret: "k8s://namespace/secret-name/key-field"
3033
func PrivateKeyFromKeyRef(ctx context.Context, keyRef string, fs afero.Fs) ([]byte, error) {
31-
return KeyFromKeyRef(ctx, keyRef, fs)
34+
// If the key-field is not specified assume it is "cosign.key"
35+
adjustedKeyRef := keyRef
36+
if strings.HasPrefix(keyRef, "k8s://") {
37+
parts := strings.Split(strings.TrimPrefix(keyRef, "k8s://"), "/")
38+
if len(parts) == 2 {
39+
adjustedKeyRef = fmt.Sprintf("%s/cosign.key", keyRef)
40+
}
41+
}
42+
return KeyFromKeyRef(ctx, adjustedKeyRef, fs)
3243
}

internal/utils/private_key_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,29 @@ func TestPrivateKeyFromKeyRef(t *testing.T) {
6363
expectErr: false,
6464
},
6565
{
66-
name: "k8s secret with multiple keys (no key field specified)",
66+
name: "k8s secret with multiple keys (no key field specified, defaults to cosign.key)",
6767
keyRef: "k8s://test-namespace/multi-key-secret",
6868
setup: func(fs afero.Fs, ctx context.Context) {
6969
// This will be handled in the test loop
7070
},
7171
expectErr: true,
72-
errMsg: "contains multiple keys, please specify the key field",
72+
errMsg: "key field \"cosign.key\" not found in secret",
73+
},
74+
{
75+
name: "k8s secret with default cosign.key field",
76+
keyRef: "k8s://test-namespace/cosign-key-secret",
77+
setup: func(fs afero.Fs, ctx context.Context) {
78+
// This will be handled in the test loop
79+
},
80+
expectErr: false,
81+
},
82+
{
83+
name: "k8s secret with cosign.key among multiple keys (defaults to cosign.key)",
84+
keyRef: "k8s://test-namespace/mixed-secret",
85+
setup: func(fs afero.Fs, ctx context.Context) {
86+
// This will be handled in the test loop
87+
},
88+
expectErr: false,
7389
},
7490
{
7591
name: "invalid k8s format",
@@ -127,6 +143,28 @@ func TestPrivateKeyFromKeyRef(t *testing.T) {
127143
"key2": []byte("key2 content"),
128144
},
129145
})
146+
} else if tt.keyRef == "k8s://test-namespace/cosign-key-secret" {
147+
secrets = append(secrets, &v1.Secret{
148+
ObjectMeta: metav1.ObjectMeta{
149+
Name: "cosign-key-secret",
150+
Namespace: "test-namespace",
151+
},
152+
Data: map[string][]byte{
153+
"cosign.key": []byte("default cosign key content"),
154+
},
155+
})
156+
} else if tt.keyRef == "k8s://test-namespace/mixed-secret" {
157+
secrets = append(secrets, &v1.Secret{
158+
ObjectMeta: metav1.ObjectMeta{
159+
Name: "mixed-secret",
160+
Namespace: "test-namespace",
161+
},
162+
Data: map[string][]byte{
163+
"cosign.key": []byte("mixed secret cosign key content"),
164+
"other-key": []byte("other key content"),
165+
"another-key": []byte("another key content"),
166+
},
167+
})
130168
}
131169

132170
if len(secrets) > 0 {
@@ -158,6 +196,10 @@ func TestPrivateKeyFromKeyRef(t *testing.T) {
158196
assert.Equal(t, []byte("single key content"), keyBytes)
159197
} else if tt.keyRef == "k8s://test-namespace/test-secret/private-key" {
160198
assert.Equal(t, []byte("test private key content"), keyBytes)
199+
} else if tt.keyRef == "k8s://test-namespace/cosign-key-secret" {
200+
assert.Equal(t, []byte("default cosign key content"), keyBytes)
201+
} else if tt.keyRef == "k8s://test-namespace/mixed-secret" {
202+
assert.Equal(t, []byte("mixed secret cosign key content"), keyBytes)
161203
}
162204
}
163205
})

0 commit comments

Comments
 (0)