mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai
Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).
CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.
Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).
CGO_ENABLED=0 go build ./... verified green.
* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)
Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.
No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.
* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)
Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).
Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.
Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).
* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy
Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.
Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).
CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.
Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.
---------
Co-authored-by: Hanzo Dev <[email protected]>
568 lines
19 KiB
Go
568 lines
19 KiB
Go
package commands
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"maps"
|
|
"os"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/hanzoai/deploy/cmd/argocd/commands/headless"
|
|
argocdclient "github.com/hanzoai/deploy/pkg/apiclient"
|
|
"github.com/hanzoai/deploy/pkg/apiclient/gpgkey"
|
|
projectpkg "github.com/hanzoai/deploy/pkg/apiclient/project"
|
|
"github.com/hanzoai/deploy/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/deploy/util/cli"
|
|
utilio "github.com/hanzoai/deploy/util/io"
|
|
"github.com/hanzoai/deploy/util/sourceintegrity"
|
|
"github.com/hanzoai/deploy/util/templates"
|
|
)
|
|
|
|
const (
|
|
msgNoGitPolicies = "no source integrity git policies defined for project %q"
|
|
msgExamples = `
|
|
# List git policies
|
|
argocd proj source-integrity git policies list PROJECT
|
|
|
|
# Add a new git policy
|
|
argocd proj source-integrity git policies add PROJECT ...
|
|
|
|
# Update a git policy
|
|
argocd proj source-integrity git policies update PROJECT POLICY_ID ...
|
|
|
|
# Delete a git policy
|
|
argocd proj source-integrity git policies delete PROJECT POLICY_ID
|
|
`
|
|
)
|
|
|
|
var (
|
|
// Indirections needed for a client lookup during tests. Mocks are injected here.
|
|
newProjectClient = func(clientOpts *argocdclient.ClientOptions, c *cobra.Command) (io.Closer, projectpkg.ProjectServiceClient) {
|
|
return headless.NewClientOrDie(clientOpts, c).NewProjectClientOrDie()
|
|
}
|
|
newGpgKeyClient = func(clientOpts *argocdclient.ClientOptions, c *cobra.Command) (io.Closer, gpgkey.GPGKeyServiceClient) {
|
|
return headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie()
|
|
}
|
|
)
|
|
|
|
// NewProjectSourceIntegrityCommand returns a new instance of an `argocd proj source-integrity` command
|
|
func NewProjectSourceIntegrityCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "source-integrity",
|
|
Short: "Manage criteria for source integrity",
|
|
Example: templates.Examples(msgExamples),
|
|
Run: func(c *cobra.Command, args []string) {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
},
|
|
}
|
|
command.AddCommand(NewProjectSourceIntegrityGitCommand(clientOpts))
|
|
return command
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitCommand returns a new instance of an `argocd proj source-integrity git` command
|
|
func NewProjectSourceIntegrityGitCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "git",
|
|
Short: "Manage policies for Git repositories",
|
|
Example: templates.Examples(msgExamples),
|
|
Run: func(c *cobra.Command, args []string) {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
},
|
|
}
|
|
command.AddCommand(NewProjectSourceIntegrityGitPoliciesCommand(clientOpts))
|
|
return command
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitPoliciesCommand returns a new instance of an `argocd proj source-integrity git policies` command
|
|
func NewProjectSourceIntegrityGitPoliciesCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "policies",
|
|
Short: "Manage git source integrity policies",
|
|
Example: templates.Examples(msgExamples),
|
|
Run: func(c *cobra.Command, args []string) {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
},
|
|
}
|
|
command.AddCommand(NewProjectSourceIntegrityGitPoliciesListCommand(clientOpts))
|
|
command.AddCommand(NewProjectSourceIntegrityGitPoliciesAddCommand(clientOpts))
|
|
command.AddCommand(NewProjectSourceIntegrityGitPoliciesDeleteCommand(clientOpts))
|
|
command.AddCommand(NewProjectSourceIntegrityGitPoliciesUpdateCommand(clientOpts))
|
|
return command
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitPoliciesListCommand returns a new instance of an `argocd proj source-integrity git policies list` command
|
|
func NewProjectSourceIntegrityGitPoliciesListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "list PROJECT",
|
|
Short: "List git source integrity policies",
|
|
Long: "List git source integrity policies.\n\nThe listed policy ID is used to identify given policy in delete and update commands.",
|
|
Example: templates.Examples(`
|
|
# List all git policies for project PROJECT.
|
|
argocd proj source-integrity git policies list PROJECT
|
|
`),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
projName := args[0]
|
|
cleanup, projects := newProjectClient(clientOpts, c)
|
|
defer utilio.Close(cleanup)
|
|
|
|
proj, err := projects.Get(ctx, &projectpkg.ProjectQuery{Name: projName})
|
|
if err != nil {
|
|
return fmt.Errorf("failed getting project %q: %w", projName, err)
|
|
}
|
|
|
|
if proj.Spec.SourceIntegrity == nil || proj.Spec.SourceIntegrity.Git == nil || len(proj.Spec.SourceIntegrity.Git.Policies) == 0 {
|
|
return fmt.Errorf(msgNoGitPolicies, projName)
|
|
}
|
|
|
|
listGitGpgPolicies(c.OutOrStdout(), proj)
|
|
return nil
|
|
},
|
|
}
|
|
return command
|
|
}
|
|
|
|
func listGitGpgPolicies(out io.Writer, proj *v1alpha1.AppProject) {
|
|
w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0)
|
|
_, _ = fmt.Fprintln(w, "ID\tGPG-MODE\tGPG-KEYS\tREPO-URLS")
|
|
for i, policy := range proj.Spec.SourceIntegrity.Git.Policies {
|
|
gpgMode := "<none>"
|
|
gpgKeys := "<none>"
|
|
if policy.GPG != nil {
|
|
gpgMode = string(policy.GPG.Mode)
|
|
if len(policy.GPG.Keys) > 0 {
|
|
gpgKeys = strings.Join(policy.GPG.Keys, ", ")
|
|
}
|
|
}
|
|
|
|
repoURLs := "<none>"
|
|
if len(policy.Repos) > 0 {
|
|
urls := make([]string, 0, len(policy.Repos))
|
|
for _, repo := range policy.Repos {
|
|
urls = append(urls, repo.URL)
|
|
}
|
|
repoURLs = strings.Join(urls, ", ")
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", i, gpgMode, gpgKeys, repoURLs)
|
|
}
|
|
_ = w.Flush()
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitPoliciesDeleteCommand returns a new instance of an `argocd proj source-integrity git policies delete` command
|
|
func NewProjectSourceIntegrityGitPoliciesDeleteCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var yes bool
|
|
command := &cobra.Command{
|
|
Use: "delete PROJECT POLICY_ID...",
|
|
Short: "Delete a git source integrity policy",
|
|
Example: templates.Examples(`
|
|
# Delete git policy at index 1 and 3 from project named PROJECT
|
|
argocd proj source-integrity git policies delete PROJECT 1 3
|
|
`),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
ctx := c.Context()
|
|
|
|
if len(args) < 2 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
|
|
projName := args[0]
|
|
cleanup, projects := newProjectClient(clientOpts, c)
|
|
defer utilio.Close(cleanup)
|
|
|
|
proj, err := projects.Get(ctx, &projectpkg.ProjectQuery{Name: projName})
|
|
if err != nil {
|
|
return fmt.Errorf("failed getting project %q: %w", projName, err)
|
|
}
|
|
|
|
if proj.Spec.SourceIntegrity == nil || proj.Spec.SourceIntegrity.Git == nil || len(proj.Spec.SourceIntegrity.Git.Policies) == 0 {
|
|
return fmt.Errorf(msgNoGitPolicies, projName)
|
|
}
|
|
|
|
originalPolicyCount := len(proj.Spec.SourceIntegrity.Git.Policies)
|
|
idsToDelete := make(map[int]any)
|
|
for _, policyId := range args[1:] {
|
|
index, err := strconv.Atoi(policyId)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid POLICY_ID '%s'", policyId)
|
|
}
|
|
|
|
if index < 0 || index >= originalPolicyCount {
|
|
return fmt.Errorf("the POLICY_ID %d is out of range (0-%d)", index, originalPolicyCount-1)
|
|
}
|
|
|
|
idsToDelete[index] = nil
|
|
}
|
|
|
|
if !yes {
|
|
idsStr := make([]string, 0, len(idsToDelete))
|
|
for i := range maps.Keys(idsToDelete) {
|
|
idsStr = append(idsStr, strconv.Itoa(i))
|
|
}
|
|
slices.Sort(idsStr)
|
|
prompt := fmt.Sprintf("Are you sure you want to delete policies %s from project %q? [y/N] ", strings.Join(idsStr, ", "), projName)
|
|
if cli.AskToProceedS(prompt) != "y" {
|
|
fmt.Fprintln(c.OutOrStdout(), "Aborted by user.")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Build a new slice with only policies whose indices are not in the set
|
|
newPolicies := make([]*v1alpha1.SourceIntegrityGitPolicy, 0, originalPolicyCount-len(idsToDelete))
|
|
for i, policy := range proj.Spec.SourceIntegrity.Git.Policies {
|
|
if _, shouldDelete := idsToDelete[i]; !shouldDelete {
|
|
newPolicies = append(newPolicies, policy)
|
|
}
|
|
}
|
|
proj.Spec.SourceIntegrity.Git.Policies = newPolicies
|
|
|
|
cleanupSourceIntegrityIfEmpty(proj)
|
|
|
|
_, err = projects.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj})
|
|
if err != nil {
|
|
return fmt.Errorf("failed updating project %q: %w", projName, err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
command.Flags().BoolVarP(&yes, "yes", "y", false, "Skip explicit confirmation")
|
|
return command
|
|
}
|
|
|
|
// cleanupSourceIntegrityIfEmpty removes spec.sourceIntegrity from a project if policies are emptied
|
|
func cleanupSourceIntegrityIfEmpty(proj *v1alpha1.AppProject) {
|
|
if proj.Spec.SourceIntegrity != nil && proj.Spec.SourceIntegrity.Git != nil && len(proj.Spec.SourceIntegrity.Git.Policies) == 0 {
|
|
proj.Spec.SourceIntegrity.Git = nil
|
|
proj.Spec.SourceIntegrity = nil
|
|
}
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitPoliciesAddCommand returns a new instance of an `argocd proj source-integrity git policies add` command
|
|
func NewProjectSourceIntegrityGitPoliciesAddCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
repoURLs []string
|
|
gpgMode string
|
|
gpgKeys []string
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "add PROJECT",
|
|
Short: "Add a git source integrity policy",
|
|
Example: templates.Examples(`
|
|
# Add a new git policy with repo URLs and GPG settings
|
|
argocd proj source-integrity git policies add PROJECT \
|
|
--repo-url 'https://github.com/foo/*' \
|
|
--gpg-mode strict \
|
|
--gpg-key D56C4FCA57A46444
|
|
`),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
cleanup, projects := newProjectClient(clientOpts, c)
|
|
defer utilio.Close(cleanup)
|
|
|
|
projName := args[0]
|
|
proj, err := projects.Get(ctx, &projectpkg.ProjectQuery{Name: projName})
|
|
if err != nil {
|
|
return fmt.Errorf("failed getting project %q: %w", projName, err)
|
|
}
|
|
|
|
mode, err := validateGpgMode(gpgMode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newPolicy := v1alpha1.SourceIntegrityGitPolicy{GPG: &v1alpha1.SourceIntegrityGitPolicyGPG{Mode: mode}}
|
|
for _, url := range repoURLs {
|
|
newPolicy.Repos = append(newPolicy.Repos, v1alpha1.SourceIntegrityGitPolicyRepo{URL: url})
|
|
}
|
|
for _, key := range gpgKeys {
|
|
key, err := sourceintegrity.KeyID(key)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid GPG key ID '%s': %w", key, err)
|
|
}
|
|
newPolicy.GPG.Keys = append(newPolicy.GPG.Keys, key)
|
|
}
|
|
|
|
closer, gpgKeyClient := newGpgKeyClient(clientOpts, c)
|
|
defer utilio.Close(closer)
|
|
if err := warnOnProblems(c, &newPolicy, gpgKeyClient); err != nil {
|
|
return err
|
|
}
|
|
|
|
if proj.Spec.SourceIntegrity == nil {
|
|
proj.Spec.SourceIntegrity = &v1alpha1.SourceIntegrity{}
|
|
}
|
|
if proj.Spec.SourceIntegrity.Git == nil {
|
|
proj.Spec.SourceIntegrity.Git = &v1alpha1.SourceIntegrityGit{}
|
|
}
|
|
proj.Spec.SourceIntegrity.Git.Policies = append(proj.Spec.SourceIntegrity.Git.Policies, &newPolicy)
|
|
|
|
_, err = projects.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj})
|
|
if err != nil {
|
|
return fmt.Errorf("failed updating project %q: %w", projName, err)
|
|
}
|
|
|
|
// Print resulting policies out of convenience
|
|
listGitGpgPolicies(c.OutOrStdout(), proj)
|
|
return nil
|
|
},
|
|
}
|
|
command.Flags().StringSliceVar(&repoURLs, "repo-url", []string{}, "Repository URL pattern (can be repeated)")
|
|
command.Flags().StringVar(&gpgMode, "gpg-mode", "", "GPG verification mode (strict, head, or none)")
|
|
command.Flags().StringSliceVar(&gpgKeys, "gpg-key", []string{}, "GPG key ID (can be repeated)")
|
|
return command
|
|
}
|
|
|
|
func validateGpgMode(gpgMode string) (v1alpha1.SourceIntegrityGitPolicyGPGMode, error) {
|
|
out := v1alpha1.SourceIntegrityGitPolicyGPGMode(gpgMode)
|
|
|
|
switch gpgMode {
|
|
case "strict", "head", "none":
|
|
return out, nil
|
|
case "":
|
|
return out, errors.New("gpg-mode must be set")
|
|
default:
|
|
return out, errors.New("gpg-mode must be one of: strict, head, none")
|
|
}
|
|
}
|
|
|
|
// NewProjectSourceIntegrityGitPoliciesUpdateCommand returns a new instance of an `argocd proj source-integrity git policies update` command
|
|
func NewProjectSourceIntegrityGitPoliciesUpdateCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
repoURLs []string
|
|
deleteRepoURLs []string
|
|
addRepoURLs []string
|
|
gpgMode string
|
|
gpgKeys []string
|
|
deleteGPGKeys []string
|
|
addGPGKeys []string
|
|
yes bool
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "update PROJECT POLICY_ID",
|
|
Short: "Update a git source integrity policy",
|
|
Example: templates.Examples(`
|
|
# Update policy at index to set specific repo URLs, removing the old ones
|
|
argocd proj source-integrity git policies update PROJECT POLICY_ID \
|
|
--repo-url 'https://github.com/foo/*'
|
|
|
|
# Update policy at index to add and remove repo URLs
|
|
argocd proj source-integrity git policies update PROJECT POLICY_ID \
|
|
--add-repo-url 'https://github.com/bar/*' \
|
|
--delete-repo-url 'https://github.com/foo/*'
|
|
|
|
# Update policy GPG mode and keys
|
|
argocd proj source-integrity git policies update PROJECT POLICY_ID \
|
|
--gpg-mode strict \
|
|
--add-gpg-key D56C4FCA57A46444
|
|
`),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 2 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
|
|
cleanup, projects := newProjectClient(clientOpts, c)
|
|
defer utilio.Close(cleanup)
|
|
|
|
projName := args[0]
|
|
proj, err := projects.Get(ctx, &projectpkg.ProjectQuery{Name: projName})
|
|
if err != nil {
|
|
return fmt.Errorf("failed getting project %q: %w", projName, err)
|
|
}
|
|
|
|
if proj.Spec.SourceIntegrity == nil || proj.Spec.SourceIntegrity.Git == nil || len(proj.Spec.SourceIntegrity.Git.Policies) == 0 {
|
|
return fmt.Errorf(msgNoGitPolicies, projName)
|
|
}
|
|
|
|
index, err := strconv.Atoi(args[1])
|
|
if err != nil {
|
|
return fmt.Errorf("invalid POLICY_ID '%s'", args[1])
|
|
}
|
|
originalPolicyCount := len(proj.Spec.SourceIntegrity.Git.Policies)
|
|
if index < 0 || index >= originalPolicyCount {
|
|
return fmt.Errorf("the POLICY_ID %d is out of range (0-%d)", index, originalPolicyCount-1)
|
|
}
|
|
policy := proj.Spec.SourceIntegrity.Git.Policies[index]
|
|
|
|
if len(gpgKeys) > 0 && (len(deleteGPGKeys) > 0 || len(addGPGKeys) > 0) {
|
|
return errors.New("option --gpg-key, cannot be combined with --add-gpg-key or --delete-gpg-key")
|
|
}
|
|
|
|
if len(repoURLs) > 0 && (len(deleteRepoURLs) > 0 || len(addRepoURLs) > 0) {
|
|
return errors.New("option --repo-url, cannot be combined with --add-repo-url or --delete-repo-url")
|
|
}
|
|
|
|
if len(repoURLs) > 0 {
|
|
var repos []v1alpha1.SourceIntegrityGitPolicyRepo
|
|
for _, url := range repoURLs {
|
|
repos = append(repos, v1alpha1.SourceIntegrityGitPolicyRepo{URL: url})
|
|
}
|
|
policy.Repos = repos
|
|
}
|
|
for _, url := range deleteRepoURLs {
|
|
for i := 0; i < len(policy.Repos); i++ {
|
|
if policy.Repos[i].URL == url {
|
|
policy.Repos = append(policy.Repos[:i], policy.Repos[i+1:]...)
|
|
i--
|
|
}
|
|
}
|
|
}
|
|
for _, url := range addRepoURLs {
|
|
found := false
|
|
for _, repo := range policy.Repos {
|
|
if repo.URL == url {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
policy.Repos = append(policy.Repos, v1alpha1.SourceIntegrityGitPolicyRepo{URL: url})
|
|
}
|
|
}
|
|
|
|
// There are no other types for now, so turning GPG on is the only sensible thing
|
|
if policy.GPG == nil {
|
|
policy.GPG = &v1alpha1.SourceIntegrityGitPolicyGPG{}
|
|
}
|
|
|
|
// Update gpg mode
|
|
if gpgMode != "" {
|
|
mode, err := validateGpgMode(gpgMode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
policy.GPG.Mode = mode
|
|
} else if policy.GPG.Mode == "" {
|
|
// The policy is updated to a gpg one, but this mandatory field is unset
|
|
return errors.New("gpg-mode must be set")
|
|
}
|
|
|
|
// Reset keys to a new set
|
|
if len(gpgKeys) > 0 {
|
|
policy.GPG.Keys = make([]string, len(gpgKeys))
|
|
for i, key := range gpgKeys {
|
|
key, err := sourceintegrity.KeyID(key)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid GPG key ID '%s': %w", key, err)
|
|
}
|
|
policy.GPG.Keys[i] = key
|
|
}
|
|
}
|
|
for _, key := range deleteGPGKeys {
|
|
key, err := sourceintegrity.KeyID(key)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid GPG key ID '%s': %w", key, err)
|
|
}
|
|
policy.GPG.Keys = slices.DeleteFunc(policy.GPG.Keys, func(k string) bool {
|
|
k, err := sourceintegrity.KeyID(k)
|
|
return err == nil && k == key
|
|
})
|
|
}
|
|
for _, key := range addGPGKeys {
|
|
key, err := sourceintegrity.KeyID(key)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid GPG key ID '%s': %w", key, err)
|
|
}
|
|
found := slices.ContainsFunc(policy.GPG.Keys, func(k string) bool {
|
|
k, err := sourceintegrity.KeyID(k)
|
|
return err == nil && k == key
|
|
})
|
|
if !found {
|
|
policy.GPG.Keys = append(policy.GPG.Keys, key)
|
|
}
|
|
}
|
|
|
|
closer, gpgKeyClient := newGpgKeyClient(clientOpts, c)
|
|
defer utilio.Close(closer)
|
|
if err := warnOnProblems(c, policy, gpgKeyClient); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Print resulting policies out of convenience
|
|
listGitGpgPolicies(c.OutOrStdout(), proj)
|
|
|
|
if !yes {
|
|
prompt := fmt.Sprintf("Are you sure you want to update policy %d from project %q? [y/N] ", index, projName)
|
|
if cli.AskToProceedS(prompt) != "y" {
|
|
fmt.Fprintln(c.OutOrStdout(), "Aborted by user.")
|
|
return nil
|
|
}
|
|
}
|
|
_, err = projects.Update(ctx, &projectpkg.ProjectUpdateRequest{Project: proj})
|
|
if err != nil {
|
|
return fmt.Errorf("failed updating project %q: %w", projName, err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
command.Flags().StringVar(&gpgMode, "gpg-mode", "", "Set GPG verification mode (strict, head, or none)")
|
|
command.Flags().StringSliceVar(&repoURLs, "repo-url", []string{}, "Set repository URL pattern (replaces existing)")
|
|
command.Flags().StringSliceVar(&addRepoURLs, "add-repo-url", []string{}, "Add repository URL pattern")
|
|
command.Flags().StringSliceVar(&deleteRepoURLs, "delete-repo-url", []string{}, "Delete repository URL pattern")
|
|
command.Flags().StringSliceVar(&gpgKeys, "gpg-key", []string{}, "Set GPG key ID (replaces existing)")
|
|
command.Flags().StringSliceVar(&addGPGKeys, "add-gpg-key", []string{}, "Add GPG key ID")
|
|
command.Flags().StringSliceVar(&deleteGPGKeys, "delete-gpg-key", []string{}, "Delete GPG key ID")
|
|
command.Flags().BoolVarP(&yes, "yes", "y", false, "Skip explicit confirmation")
|
|
return command
|
|
}
|
|
|
|
// warnOnProblems checks if a policy has empty repo URLs or GPG keys and prints warnings
|
|
func warnOnProblems(c *cobra.Command, policy *v1alpha1.SourceIntegrityGitPolicy, gpgKeyClient gpgkey.GPGKeyServiceClient) error {
|
|
stderr := c.ErrOrStderr()
|
|
if len(policy.Repos) == 0 {
|
|
_, _ = fmt.Fprintln(stderr, "Warning: Policy has no repository URLs and will never be used")
|
|
}
|
|
if policy.GPG != nil {
|
|
if len(policy.GPG.Keys) == 0 {
|
|
_, _ = fmt.Fprintln(stderr, "Warning: Policy has no GPG keys and will never validate any revision")
|
|
} else {
|
|
absent := make(map[string]any)
|
|
for _, key := range policy.GPG.Keys {
|
|
absent[key] = nil
|
|
}
|
|
|
|
keyring, err := gpgKeyClient.List(c.Context(), &gpgkey.GnuPGPublicKeyQuery{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed listing GPG keys: %w", err)
|
|
}
|
|
for _, keyringKey := range keyring.Items {
|
|
delete(absent, keyringKey.KeyID)
|
|
}
|
|
|
|
if len(absent) != 0 {
|
|
absentKeys := slices.Collect(maps.Keys(absent))
|
|
slices.Sort(absentKeys)
|
|
_, _ = fmt.Fprintf(stderr,
|
|
"Warning: Following GPG keys are not in repo-server keyring: %s\n",
|
|
strings.Join(absentKeys, ", "),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|