|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "connectrpc.com/connect" |
| 9 | + v1 "github.com/furisto/construct/api/go/v1" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "google.golang.org/protobuf/types/known/durationpb" |
| 12 | +) |
| 13 | + |
| 14 | +type tokenCreateSetupOptions struct { |
| 15 | + CodeExpires string |
| 16 | + TokenExpires string |
| 17 | + RenderOptions RenderOptions |
| 18 | +} |
| 19 | + |
| 20 | +func NewDaemonTokenCreateSetupCmd() *cobra.Command { |
| 21 | + var options tokenCreateSetupOptions |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "create-setup <token-name> [flags]", |
| 25 | + Short: "Generate a short-lived setup code for secure token distribution", |
| 26 | + Args: cobra.ExactArgs(1), |
| 27 | + Long: `Generate a short-lived setup code for secure token distribution. |
| 28 | +
|
| 29 | +Setup codes provide a secure way to distribute tokens to remote clients without |
| 30 | +sending the token itself over insecure channels. The code expires quickly and |
| 31 | +can only be used once. |
| 32 | +
|
| 33 | +The client exchanges the setup code for a token using: |
| 34 | + construct context add <name> --endpoint <url> --setup-code <code>`, |
| 35 | + Example: ` # Create setup code with default settings |
| 36 | + construct daemon token create-setup remote-laptop |
| 37 | +
|
| 38 | + # Create setup code with custom expiry times |
| 39 | + construct daemon token create-setup staging-server \ |
| 40 | + --code-expires 1h \ |
| 41 | + --token-expires 30d |
| 42 | +
|
| 43 | + # Create setup code with JSON output |
| 44 | + construct daemon token create-setup prod-api --output json`, |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + tokenName := args[0] |
| 47 | + |
| 48 | + codeExpiresDuration, err := ParseDuration(options.CodeExpires) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("invalid code expiry duration: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := ValidateSetupCodeExpiry(codeExpiresDuration); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + tokenExpiresDuration, err := ParseDuration(options.TokenExpires) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("invalid token expiry duration: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + if err := ValidateTokenExpiry(tokenExpiresDuration); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + client := getAPIClient(cmd.Context()) |
| 67 | + |
| 68 | + req := &connect.Request[v1.CreateSetupCodeRequest]{ |
| 69 | + Msg: &v1.CreateSetupCodeRequest{ |
| 70 | + TokenName: tokenName, |
| 71 | + ExpiresIn: durationpb.New(codeExpiresDuration), |
| 72 | + TokenExpiresIn: durationpb.New(tokenExpiresDuration), |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + resp, err := client.Auth().CreateSetupCode(cmd.Context(), req) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to create setup code: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + display := &SetupCodeDisplay{ |
| 82 | + TokenName: tokenName, |
| 83 | + SetupCode: resp.Msg.SetupCode, |
| 84 | + ExpiresAt: resp.Msg.ExpiresAt.AsTime().Format(time.RFC3339), |
| 85 | + } |
| 86 | + |
| 87 | + if err := getRenderer(cmd.Context()).Render(display, &options.RenderOptions); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + if options.RenderOptions.Format == OutputFormatCard || options.RenderOptions.Format == "" { |
| 92 | + fmt.Fprintln(os.Stderr, "") |
| 93 | + fmt.Fprintln(os.Stderr, "Share this code securely with the user. They can exchange it for a token using:") |
| 94 | + fmt.Fprintln(os.Stderr, "") |
| 95 | + fmt.Fprintf(os.Stderr, " construct context add <name> \\\n") |
| 96 | + fmt.Fprintf(os.Stderr, " --endpoint <daemon-url> \\\n") |
| 97 | + fmt.Fprintf(os.Stderr, " --setup-code %s\n", resp.Msg.SetupCode) |
| 98 | + fmt.Fprintln(os.Stderr, "") |
| 99 | + fmt.Fprintln(os.Stderr, "⚠️ This code can only be used once and expires in", FormatRelativeTime(resp.Msg.ExpiresAt.AsTime())) |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + cmd.Flags().StringVar(&options.CodeExpires, "code-expires", "5m", "Setup code lifetime (default: 5m, max: 72h)") |
| 107 | + cmd.Flags().StringVar(&options.TokenExpires, "token-expires", "90d", "Resulting token lifetime (default: 90d, max: 365d)") |
| 108 | + addRenderOptions(cmd, &options.RenderOptions) |
| 109 | + WithCardFormat(&options.RenderOptions) |
| 110 | + |
| 111 | + return cmd |
| 112 | +} |
0 commit comments