diff --git a/cmd/cozypkg/cmd/tap.go b/cmd/cozypkg/cmd/tap.go index 3f844bfc7..96e3bb9cc 100644 --- a/cmd/cozypkg/cmd/tap.go +++ b/cmd/cozypkg/cmd/tap.go @@ -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") } diff --git a/cmd/cozypkg/cmd/tap_test.go b/cmd/cozypkg/cmd/tap_test.go index d57e75b2e..f3041d312 100644 --- a/cmd/cozypkg/cmd/tap_test.go +++ b/cmd/cozypkg/cmd/tap_test.go @@ -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. diff --git a/pkg/apis/core/v1alpha1/tap_types.go b/pkg/apis/core/v1alpha1/tap_types.go index 573337bd9..076925271 100644 --- a/pkg/apis/core/v1alpha1/tap_types.go +++ b/pkg/apis/core/v1alpha1/tap_types.go @@ -52,6 +52,25 @@ type TapSpec struct { // Secret in cozy-system for a private repository. It is consumed when a Tap // is created and, being a credential reference, is never returned on read. SecretRef string `json:"secretRef,omitempty"` + // Verify is a connect-time input only: a signature-verification policy to + // set on the tap's Flux source (spec.verify). It is consumed when a Tap is + // created, preserved across repeat connects, and never returned on read. + Verify *TapVerify `json:"verify,omitempty"` +} + +// TapVerify is a connect-time signature-verification policy for a tap. It maps +// to the Flux OCIRepository spec.verify: keyless cosign identity matching +// (issuer + subject) is the supported form, so a moved tag cannot be followed +// unless it is also signed by the recorded identity. +type TapVerify struct { + // Provider is the signature technology: "cosign" (default) or "notation". + Provider string `json:"provider,omitempty"` + // Issuer is the OIDC issuer URL for keyless cosign verification, e.g. + // https://token.actions.githubusercontent.com. + Issuer string `json:"issuer,omitempty"` + // Subject is the certificate identity (subject) for keyless cosign + // verification; it is matched against the artifact's signing identity. + Subject string `json:"subject,omitempty"` } // TapSource identifies the Flux source backing a tap. It carries no credential. diff --git a/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go index 66219aaa8..8088e27fd 100644 --- a/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go @@ -231,6 +231,11 @@ func (in *TapSpec) DeepCopyInto(out *TapSpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.Verify != nil { + in, out := &in.Verify, &out.Verify + *out = new(TapVerify) + **out = **in + } return } @@ -244,6 +249,22 @@ func (in *TapSpec) DeepCopy() *TapSpec { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TapVerify) DeepCopyInto(out *TapVerify) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TapVerify. +func (in *TapVerify) DeepCopy() *TapVerify { + if in == nil { + return nil + } + out := new(TapVerify) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TenantModule) DeepCopyInto(out *TenantModule) { *out = *in diff --git a/pkg/registry/core/tap/compute.go b/pkg/registry/core/tap/compute.go index 9df414f76..af25ab1da 100644 --- a/pkg/registry/core/tap/compute.go +++ b/pkg/registry/core/tap/compute.go @@ -87,6 +87,25 @@ func parseConnectURL(url, tagOverride string) (connectTarget, error) { return t, nil } +// buildVerifySpec maps a connect-time TapVerify input to a Flux OCIRepository +// spec.verify. Only keyless cosign identity matching is exposed, so both the +// issuer and the subject are required: a verification policy that matched any +// signer would be worse than none. A key-based policy an admin sets by hand on +// the source is preserved across connects (see Create) but is not settable here. +func buildVerifySpec(v *corev1alpha1.TapVerify) (map[string]any, error) { + if v.Issuer == "" || v.Subject == "" { + return nil, fmt.Errorf("verify requires both issuer and subject (keyless cosign identity)") + } + provider := v.Provider + if provider == "" { + provider = "cosign" + } + return map[string]any{ + "provider": provider, + "matchOIDCIdentity": []any{map[string]any{"issuer": v.Issuer, "subject": v.Subject}}, + }, nil +} + var ( gvrPackageSources = schema.GroupVersionResource{Group: "cozystack.io", Version: "v1alpha1", Resource: "packagesources"} gvrPackages = schema.GroupVersionResource{Group: "cozystack.io", Version: "v1alpha1", Resource: "packages"} diff --git a/pkg/registry/core/tap/rest.go b/pkg/registry/core/tap/rest.go index b75fde3d9..64d81e8c7 100644 --- a/pkg/registry/core/tap/rest.go +++ b/pkg/registry/core/tap/rest.go @@ -244,6 +244,13 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation if in.Spec.SecretRef != "" { _ = unstructured.SetNestedMap(repo.Object, map[string]any{"name": in.Spec.SecretRef}, "spec", "secretRef") } + if in.Spec.Verify != nil { + verifySpec, err := buildVerifySpec(in.Spec.Verify) + if err != nil { + return nil, apierrors.NewBadRequest(err.Error()) + } + _ = unstructured.SetNestedMap(repo.Object, verifySpec, "spec", "verify") + } // Create the Flux source, idempotently: a repeat connect updates the // existing source (new tag/secret) rather than erroring. diff --git a/pkg/registry/core/tap/rest_test.go b/pkg/registry/core/tap/rest_test.go index 112381b0b..9525de1d6 100644 --- a/pkg/registry/core/tap/rest_test.go +++ b/pkg/registry/core/tap/rest_test.go @@ -376,6 +376,46 @@ func TestCreateRepeatPreservesVerifyAndResetsPin(t *testing.T) { } } +func TestCreateSetsVerify(t *testing.T) { + r := fakeREST() + in := &corev1alpha1.Tap{Spec: corev1alpha1.TapSpec{ + URL: "oci://ghcr.io/foo/bar:v1", + Verify: &corev1alpha1.TapVerify{ + Issuer: "https://token.actions.githubusercontent.com", + Subject: "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1", + }, + }} + if _, err := r.Create(context.Background(), in, nil, nil); err != nil { + t.Fatalf("create: %v", err) + } + u, err := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system").Get(context.Background(), "tap-foo-bar", metav1.GetOptions{}) + if err != nil { + t.Fatal(err) + } + if prov, _, _ := unstructured.NestedString(u.Object, "spec", "verify", "provider"); prov != "cosign" { + t.Errorf("verify.provider = %q, want cosign", prov) + } + ids, _, _ := unstructured.NestedSlice(u.Object, "spec", "verify", "matchOIDCIdentity") + if len(ids) != 1 { + t.Fatalf("expected one matchOIDCIdentity, got %v", ids) + } + id := ids[0].(map[string]any) + if id["issuer"] != "https://token.actions.githubusercontent.com" || id["subject"] == "" { + t.Errorf("unexpected identity matcher: %v", id) + } +} + +func TestCreateVerifyRequiresBothIdentityFields(t *testing.T) { + r := fakeREST() + in := &corev1alpha1.Tap{Spec: corev1alpha1.TapSpec{ + URL: "oci://ghcr.io/foo/bar:v1", + Verify: &corev1alpha1.TapVerify{Issuer: "https://token.actions.githubusercontent.com"}, + }} + if _, err := r.Create(context.Background(), in, nil, nil); !apierrors.IsBadRequest(err) { + t.Errorf("a keyless verify with only an issuer must be BadRequest, got %v", err) + } +} + func TestCreateRefusesConflictingURL(t *testing.T) { // A source with the same derived name but a different registry host must not // be silently retargeted.