mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 12:46:14 +09:00
feat(marketplace): accept a signature-verification policy at tap connect
A tap can now carry a keyless cosign verification policy (spec.verify), so a
reference is only pulled when signed by the recorded identity. The CLI takes
--certificate-identity and --certificate-oidc-issuer (both required together,
mirroring validate); the API takes spec.verify.{issuer,subject} on the Tap
connect object. Both map to a Flux OCIRepository verify block with provider
cosign and a matchOIDCIdentity entry. A half-configured policy (only one of
the identity pair) is refused rather than set, since a policy that matched any
signer is worse than none. A key-based policy an admin sets on the source by
hand is preserved across repeat connects.
Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
(cherry picked from commit c56b9876f4)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
b326e0badd
commit
9fb9ae4000
7 files changed
+177
-6
No files matched your search
+31
-2
@@ -55,6 +55,8 @@ var tapCmdFlags struct {
|
||||
kubeconfig string
|
||||
index string
|
||||
skipValidate bool
|
||||
certIdentity string
|
||||
certIssuer string
|
||||
}
|
||||
|
||||
// ociRef is the parsed form of an oci:// reference.
|
||||
@@ -148,7 +150,7 @@ func fluxSourceName(r ociRef) string {
|
||||
// optional pull-credential secretRef (the private-tap credential, symmetric
|
||||
// with #2472). A tag-only reference is pinned to a digest later by the tap
|
||||
// materializer; an explicit digest is pinned here at connect time.
|
||||
func buildTapOCIRepository(name string, r ociRef, secret string) *sourcev1.OCIRepository {
|
||||
func buildTapOCIRepository(name string, r ociRef, secret string, verify *sourcev1.OCIRepositoryVerification) *sourcev1.OCIRepository {
|
||||
obj := &sourcev1.OCIRepository{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: sourcev1.GroupVersion.String(),
|
||||
@@ -172,9 +174,30 @@ func buildTapOCIRepository(name string, r ociRef, secret string) *sourcev1.OCIRe
|
||||
if secret != "" {
|
||||
obj.Spec.SecretRef = &fluxmeta.LocalObjectReference{Name: secret}
|
||||
}
|
||||
if verify != nil {
|
||||
obj.Spec.Verify = verify
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// tapVerification builds a keyless cosign verification policy from the connect
|
||||
// flags, or nil when neither is set. Both the certificate identity (subject)
|
||||
// and the OIDC issuer are required together: a policy that verified a signature
|
||||
// without pinning who signed it would be worse than none. Both are Go regular
|
||||
// expressions at the Flux layer, so an exact identity should be anchored.
|
||||
func tapVerification(identity, issuer string) (*sourcev1.OCIRepositoryVerification, error) {
|
||||
if identity == "" && issuer == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if identity == "" || issuer == "" {
|
||||
return nil, fmt.Errorf("keyless signature verification needs both --certificate-identity and --certificate-oidc-issuer")
|
||||
}
|
||||
return &sourcev1.OCIRepositoryVerification{
|
||||
Provider: "cosign",
|
||||
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: issuer, Subject: identity}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// rewritePackageSourceForTap prepares a PackageSource read from the artifact for
|
||||
// apply: it keeps the repository's own declared name (no community rename),
|
||||
// clears server-set metadata, stamps the tap marker (label + source annotation)
|
||||
@@ -303,6 +326,10 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
if tapCmdFlags.tag != "" {
|
||||
ref.Tag = tapCmdFlags.tag
|
||||
}
|
||||
verify, err := tapVerification(tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Pull first: an unreachable/invalid ref must fail before any cluster
|
||||
// resource is created (no half-created source).
|
||||
@@ -332,7 +359,7 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
}
|
||||
|
||||
srcName := fluxSourceName(ref)
|
||||
ociRepo := buildTapOCIRepository(srcName, ref, tapCmdFlags.secret)
|
||||
ociRepo := buildTapOCIRepository(srcName, ref, tapCmdFlags.secret, verify)
|
||||
|
||||
toApply := make([]client.Object, 0, len(sources)+1)
|
||||
toApply = append(toApply, ociRepo)
|
||||
@@ -555,6 +582,8 @@ func init() {
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.kubeconfig, "kubeconfig", "", "Path to kubeconfig file")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.index, "index", "", "Index location for resolving a short name (local dir or oci:// ref; defaults to COZYPKG_INDEX)")
|
||||
tapCmd.Flags().BoolVar(&tapCmdFlags.skipValidate, "skip-validate", false, "Skip validating the artifact before tapping")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.certIdentity, "certificate-identity", "", "Keyless cosign certificate identity (subject regex) to verify the artifact's signature against at pull time; requires --certificate-oidc-issuer")
|
||||
tapCmd.Flags().StringVar(&tapCmdFlags.certIssuer, "certificate-oidc-issuer", "", "Keyless cosign OIDC issuer (regex) to verify the artifact's signature against at pull time; requires --certificate-identity")
|
||||
untapCmd.Flags().StringVar(&tapCmdFlags.kubeconfig, "kubeconfig", "", "Path to kubeconfig file")
|
||||
untapCmd.Flags().BoolVar(&untapConfirmFlag, "yes", false, "Untap even if a Package from this source is still installed")
|
||||
}
|
||||
@@ -126,7 +126,7 @@ func TestObjectExists(t *testing.T) {
|
||||
|
||||
func TestBuildTapOCIRepository(t *testing.T) {
|
||||
r := ociRef{URL: "oci://ghcr.io/foo/bar", Org: "foo", Repo: "bar", Tag: "v1"}
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "")
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "", nil)
|
||||
if obj.Namespace != "cozy-system" || obj.Spec.URL != "oci://ghcr.io/foo/bar" {
|
||||
t.Fatalf("unexpected OCIRepository: %+v", obj.Spec)
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func TestBuildTapOCIRepository(t *testing.T) {
|
||||
if obj.Spec.SecretRef != nil {
|
||||
t.Fatalf("expected no secretRef, got %+v", obj.Spec.SecretRef)
|
||||
}
|
||||
withSecret := buildTapOCIRepository("n", r, "pull-creds")
|
||||
withSecret := buildTapOCIRepository("n", r, "pull-creds", nil)
|
||||
if withSecret.Spec.SecretRef == nil || withSecret.Spec.SecretRef.Name != "pull-creds" {
|
||||
t.Fatalf("expected secretRef pull-creds, got %+v", withSecret.Spec.SecretRef)
|
||||
}
|
||||
@@ -156,7 +156,7 @@ func TestBuildTapOCIRepository(t *testing.T) {
|
||||
// the user is written by the CLI itself (the operator only pins tag refs).
|
||||
const digest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
|
||||
rd := ociRef{URL: "oci://ghcr.io/foo/bar", Org: "foo", Repo: "bar", Digest: digest}
|
||||
objd := buildTapOCIRepository("tap-foo-bar", rd, "")
|
||||
objd := buildTapOCIRepository("tap-foo-bar", rd, "", nil)
|
||||
if objd.Spec.Reference == nil || objd.Spec.Reference.Digest != digest {
|
||||
t.Fatalf("expected digest pin %q, got %+v", digest, objd.Spec.Reference)
|
||||
}
|
||||
@@ -165,7 +165,7 @@ func TestBuildTapOCIRepository(t *testing.T) {
|
||||
}
|
||||
// A tag may accompany the digest and is then informational.
|
||||
rtd := ociRef{URL: "oci://ghcr.io/foo/bar", Repo: "bar", Tag: "v1", Digest: digest}
|
||||
objtd := buildTapOCIRepository("n", rtd, "")
|
||||
objtd := buildTapOCIRepository("n", rtd, "", nil)
|
||||
if objtd.Spec.Reference.Digest != digest || objtd.Spec.Reference.Tag != "v1" {
|
||||
t.Fatalf("expected tag v1 + digest, got %+v", objtd.Spec.Reference)
|
||||
}
|
||||
@@ -183,6 +183,42 @@ func TestOCIRefPullRef(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTapVerification(t *testing.T) {
|
||||
// No identity and no issuer: no verification policy.
|
||||
if v, err := tapVerification("", ""); err != nil || v != nil {
|
||||
t.Fatalf("empty flags should yield no policy, got %+v err %v", v, err)
|
||||
}
|
||||
// Only one of the pair: an error, never a half-configured policy.
|
||||
if _, err := tapVerification("subj", ""); err == nil {
|
||||
t.Error("identity without issuer must error")
|
||||
}
|
||||
if _, err := tapVerification("", "iss"); err == nil {
|
||||
t.Error("issuer without identity must error")
|
||||
}
|
||||
v, err := tapVerification("https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1", "https://token.actions.githubusercontent.com")
|
||||
if err != nil {
|
||||
t.Fatalf("valid pair: %v", err)
|
||||
}
|
||||
if v == nil || v.Provider != "cosign" || len(v.MatchOIDCIdentity) != 1 {
|
||||
t.Fatalf("unexpected verification: %+v", v)
|
||||
}
|
||||
if v.MatchOIDCIdentity[0].Issuer != "https://token.actions.githubusercontent.com" || v.MatchOIDCIdentity[0].Subject == "" {
|
||||
t.Errorf("identity matcher not set: %+v", v.MatchOIDCIdentity[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTapOCIRepositoryWithVerify(t *testing.T) {
|
||||
r := ociRef{URL: "oci://ghcr.io/foo/bar", Repo: "bar", Tag: "v1"}
|
||||
v := &sourcev1.OCIRepositoryVerification{
|
||||
Provider: "cosign",
|
||||
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: "iss", Subject: "subj"}},
|
||||
}
|
||||
obj := buildTapOCIRepository("tap-foo-bar", r, "", v)
|
||||
if obj.Spec.Verify == nil || obj.Spec.Verify.Provider != "cosign" || len(obj.Spec.Verify.MatchOIDCIdentity) != 1 {
|
||||
t.Fatalf("verify not set on the source: %+v", obj.Spec.Verify)
|
||||
}
|
||||
}
|
||||
|
||||
// rewritePackageSourceForTap must NOT rename the PackageSource: a tapped
|
||||
// repository keeps its own declared name. It only clears server-set metadata,
|
||||
// stamps the tap marker (label + source annotation), and repoints the sourceRef.
|
||||
|
||||
Reference in new issue
Block a user