|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/open-source-cloud/devstack/internal/secrets" |
| 11 | +) |
| 12 | + |
| 13 | +// newSecretsCmd wires `secrets keygen` (real) and `secrets login` (stub until the |
| 14 | +// keyring lands, S5). keygen generates the offline age identity that the SOPS+age |
| 15 | +// provider (S2) decrypts with — no account, fully local. |
| 16 | +func newSecretsCmd(g *GlobalOpts) *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "secrets", |
| 19 | + Short: "Secrets providers (age keygen, provider login)", |
| 20 | + } |
| 21 | + cmd.AddCommand( |
| 22 | + newSecretsKeygenCmd(g), |
| 23 | + stub("login", "Authenticate a secrets provider (keyring) — S5", "M4"), |
| 24 | + ) |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func newSecretsKeygenCmd(g *GlobalOpts) *cobra.Command { |
| 29 | + var output string |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "keygen", |
| 32 | + Short: "Generate an age keypair for SOPS+age (offline, no account)", |
| 33 | + Args: cobra.NoArgs, |
| 34 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 35 | + k, err := secrets.GenerateAgeKey() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if output == "" { |
| 40 | + // No file: print the key body to stdout (caller redirects) and the |
| 41 | + // public recipient to stderr so a pipe captures only the key. |
| 42 | + if g.JSON { |
| 43 | + return writeJSON(cmd, map[string]string{"recipient": k.Recipient, "identity": k.Identity}) |
| 44 | + } |
| 45 | + fmt.Fprint(cmd.OutOrStdout(), k.AgeKeyFileContents()) |
| 46 | + fmt.Fprintf(cmd.ErrOrStderr(), "public recipient: %s\n", k.Recipient) |
| 47 | + return nil |
| 48 | + } |
| 49 | + if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + if err := os.WriteFile(output, []byte(k.AgeKeyFileContents()), 0o600); err != nil { |
| 53 | + return fmt.Errorf("write age key %s: %w", output, err) |
| 54 | + } |
| 55 | + if g.JSON { |
| 56 | + return writeJSON(cmd, map[string]string{"path": output, "recipient": k.Recipient}) |
| 57 | + } |
| 58 | + fmt.Fprintf(cmd.OutOrStdout(), "wrote age key to %s\npublic recipient: %s\n", output, k.Recipient) |
| 59 | + fmt.Fprintf(cmd.OutOrStdout(), "→ point SOPS at it: export SOPS_AGE_KEY_FILE=%s\n", output) |
| 60 | + return nil |
| 61 | + }, |
| 62 | + } |
| 63 | + cmd.Flags().StringVarP(&output, "output", "o", "", "write the key to this file (0600) instead of stdout") |
| 64 | + return cmd |
| 65 | +} |
0 commit comments