diff --git a/internal/app/archied/setup.go b/internal/app/archied/setup.go index 77282e3da..cffa8c6dd 100644 --- a/internal/app/archied/setup.go +++ b/internal/app/archied/setup.go @@ -12,6 +12,7 @@ import ( "syscall" configtemplate "github.com/samcharles93/archie-core" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" @@ -50,15 +51,18 @@ func IsSetupArgs(args []string) bool { // parameters and prompts are the only two ways an answer arrives, and no // environment variable is consulted. type setupFlags struct { - cfgPath string - defaults bool - botUser string - operator string - forgeType string - forgeHost string - provider string - model string - telegramUserIDs string + cfgPath string + defaults bool + botUser string + operator string + forgeType string + forgeHost string + provider string + model string + telegramUserIDs string + forgeSecretRef string + providerSecretRef string + telegramSecretRef string } func bindSetupFlags(fs *flag.FlagSet) *setupFlags { @@ -72,6 +76,11 @@ func bindSetupFlags(fs *flag.FlagSet) *setupFlags { fs.StringVar(&f.provider, "provider", "", "LLM provider class: openai | anthropic | openrouter | gemini | groq | deepseek | mistral | ollama") fs.StringVar(&f.model, "model", "", "bare model name; the provider class prefix is added automatically") fs.StringVar(&f.telegramUserIDs, "telegram-user-ids", "", "comma-separated allowed Telegram user IDs; configuring Telegram requires at least one") + // References, not values: engine:key names where a secret already lives, and + // neither part is sensitive. A value has no flag, deliberately -- see Params. + fs.StringVar(&f.forgeSecretRef, "forge-secret-ref", "", "engine:key naming where the forge token is stored (e.g. bws:ARCHIE_GITHUB_TOKEN); that engine must be configured") + fs.StringVar(&f.providerSecretRef, "provider-secret-ref", "", "engine:key naming where the provider API key is stored (e.g. bws:OPENAI_API_KEY); that engine must be configured") + fs.StringVar(&f.telegramSecretRef, "telegram-secret-ref", "", "engine:key naming where the Telegram bot token is stored (e.g. bws:TELEGRAM_BOT_TOKEN); that engine must be configured") return f } @@ -101,6 +110,27 @@ func (f *setupFlags) params() (setup.Params, error) { if f.model != "" { params.Model = f.model } + // Parsed at the boundary, so a malformed reference fails before anything is + // rendered or written. + for _, ref := range []struct { + flag string + value string + dst *config.SecretRef + }{ + {"-forge-secret-ref", f.forgeSecretRef, ¶ms.ForgeTokenRef}, + {"-provider-secret-ref", f.providerSecretRef, ¶ms.ProviderAPIKeyRef}, + {"-telegram-secret-ref", f.telegramSecretRef, ¶ms.TelegramTokenRef}, + } { + if ref.value == "" { + continue + } + parsed, err := setup.ParseSecretRef(ref.value) + if err != nil { + return setup.Params{}, fmt.Errorf("%s: %w", ref.flag, err) + } + *ref.dst = parsed + } + if f.telegramUserIDs != "" { ids, err := setup.ParseTelegramUserIDs(f.telegramUserIDs) if err != nil { diff --git a/internal/app/archied/setup_test.go b/internal/app/archied/setup_test.go index 14509efc2..a9eb6b537 100644 --- a/internal/app/archied/setup_test.go +++ b/internal/app/archied/setup_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/setup" ) @@ -163,3 +164,76 @@ func TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret(t *testing.T) { t.Errorf("secret committed even though the config was never written: %v", statErr) } } + +// TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile pins the reference +// form end to end: naming where a secret is stored writes the reference, and +// setup stores nothing, so no env file appears. The engine here (bws) is one +// setup has no writer for -- which is the point, the value is not setup's. +func TestRunSetup_SecretRefFlagsWriteReferencesWithoutEnvFile(t *testing.T) { + dir := t.TempDir() + cfgPath := filepath.Join(dir, "config.toml") + devNull, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + defer func() { _ = devNull.Close() }() + + var stdout, stderr strings.Builder + code := RunSetup([]string{ + "--defaults", "-config", cfgPath, + "-provider", "openai", "-model", "gpt-5.4", + "-provider-secret-ref", "bws:OPENAI_API_KEY", + "-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN", + }, devNull, &stdout, &stderr) + if code != 0 { + t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String()) + } + + doc, err := configuration.New(nil).File(cfgPath) + if err != nil { + t.Fatalf("generated config does not load: %v", err) + } + if got, want := doc.Config.Providers["openai"].APIKey, (config.SecretRef{Engine: "bws", Key: "OPENAI_API_KEY"}); got != want { + t.Errorf("providers.openai.api_key = %+v, want %+v", got, want) + } + if got, want := doc.Config.Forge.Token, (config.SecretRef{Engine: "bws", Key: "ARCHIE_GITHUB_TOKEN"}); got != want { + t.Errorf("forge.token = %+v, want %+v", got, want) + } + if _, err := os.Stat(filepath.Join(dir, "env")); !os.IsNotExist(err) { + t.Errorf("env file written: a reference means setup stores no value (stat err: %v)", err) + } +} + +// TestRunSetup_RejectsReferencesNothingWouldUse pins the no-silent-no-op rule. +// A reference for a keyless provider, or for a disabled forge, would sit in the +// config looking configured while nothing read it -- the failure shape this +// whole flow exists to remove -- so it is refused instead. +func TestRunSetup_RejectsReferencesNothingWouldUse(t *testing.T) { + tests := []struct { + name string + args []string + }{ + {name: "provider ref with the keyless self-hosted provider", args: []string{"-provider-secret-ref", "bws:OPENAI_API_KEY"}}, + {name: "forge ref with the forge disabled", args: []string{"-forge-type", "none", "-forge-secret-ref", "bws:ARCHIE_GITHUB_TOKEN"}}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + cfgPath := filepath.Join(dir, "config.toml") + devNull, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + defer func() { _ = devNull.Close() }() + + args := append([]string{"--defaults", "-config", cfgPath}, tc.args...) + var stdout, stderr strings.Builder + if code := RunSetup(args, devNull, &stdout, &stderr); code == 0 { + t.Fatalf("RunSetup = exit 0, want a refusal; stderr: %s", stderr.String()) + } + if _, err := os.Stat(cfgPath); !os.IsNotExist(err) { + t.Errorf("config written despite the refusal: %v", err) + } + }) + } +} diff --git a/internal/infrastructure/configuration/setup/params_test.go b/internal/infrastructure/configuration/setup/params_test.go index fac407d0b..1cfbd73ff 100644 --- a/internal/infrastructure/configuration/setup/params_test.go +++ b/internal/infrastructure/configuration/setup/params_test.go @@ -3,6 +3,8 @@ package setup import ( "context" "testing" + + "github.com/samcharles93/archie-core/internal/config" ) // rejectingPrompter fails the test if any question is asked, proving that a @@ -204,3 +206,45 @@ func TestRun_DefaultsBaselineLoads(t *testing.T) { t.Errorf("committed secrets = %v, want none: the defaults baseline is keyless", secrets.committed) } } + +// TestRunParams_SecretRefsWriteReferencesWithoutStoring pins what a reference +// means: the value already lives in a secret engine, so setup writes the +// reference, reads nothing from the prompt surface, and stores nothing of its +// own. `vault/openbao` is deliberate -- an arbitrary engine name must survive +// to TOML, which the legacy token_env form could not express. +func TestRunParams_SecretRefsWriteReferencesWithoutStoring(t *testing.T) { + params := Params{ + BotUser: "archie-bot", + Operator: "Ada", + ForgeType: "github", + ForgeHost: "https://github.com", + ForgeTokenRef: config.SecretRef{Engine: "bws", Key: "ARCHIE_GITHUB_TOKEN"}, + Provider: "openai", + Model: "gpt-5.4", + ProviderAPIKeyRef: config.SecretRef{Engine: "vault", Key: "OPENAI_API_KEY"}, + TelegramTokenRef: config.SecretRef{Engine: "bws", Key: "TELEGRAM_BOT_TOKEN"}, + TelegramUserIDs: []int64{111}, + } + // No secrets are scripted: any ReadSecret call fails the test, because a + // reference means nothing may be prompted for. + p := &scriptedSecretPrompter{t: t} + secrets := newFakeSecrets() + edits, err := RunParams(context.Background(), p, nil, secrets, ExistingValues{}, params) + if err != nil { + t.Fatalf("RunParams: %v", err) + } + if len(secrets.pending) != 0 { + t.Errorf("pending secrets = %v, want none: setup does not own a referenced value", secrets.pending) + } + + cfg := generateAndLoad(t, edits) + if got := cfg.Forge.Token; got != params.ForgeTokenRef { + t.Errorf("forge.token = %+v, want %+v", got, params.ForgeTokenRef) + } + if got := cfg.Providers["openai"].APIKey; got != params.ProviderAPIKeyRef { + t.Errorf("providers.openai.api_key = %+v, want %+v", got, params.ProviderAPIKeyRef) + } + if got := cfg.Chat.Telegram.Token; got != params.TelegramTokenRef { + t.Errorf("chat.telegram.token = %+v, want %+v", got, params.TelegramTokenRef) + } +} diff --git a/internal/infrastructure/configuration/setup/secretref.go b/internal/infrastructure/configuration/setup/secretref.go new file mode 100644 index 000000000..54d634a24 --- /dev/null +++ b/internal/infrastructure/configuration/setup/secretref.go @@ -0,0 +1,39 @@ +package setup + +import ( + "fmt" + "regexp" + "strings" + + "github.com/samcharles93/archie-core/internal/config" +) + +// engineNameRe is the shape a secret engine name must have. Engine names are +// identifiers -- "env", "bws", "builtin" -- and for operator-supplied engines +// they are also Yaegi plugin filenames, so this is deliberately no tighter than +// an identifier. +var engineNameRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) + +// ParseSecretRef parses an "engine:key" secret reference. +// +// A reference is not a secret. An engine name and a key name are not sensitive, +// which is exactly why a reference may be given as a parameter while a value may +// not: the value lives in the engine, and this flow never sees it. Setup writes +// the reference to TOML and stops there. +func ParseSecretRef(s string) (config.SecretRef, error) { + engine, key, ok := strings.Cut(s, ":") + if !ok { + return config.SecretRef{}, fmt.Errorf("secret reference %q must be engine:key, e.g. env:ARCHIE_GITHUB_TOKEN", s) + } + engine, key = strings.TrimSpace(engine), strings.TrimSpace(key) + if engine == "" || key == "" { + return config.SecretRef{}, fmt.Errorf("secret reference %q must name both an engine and a key, e.g. env:ARCHIE_GITHUB_TOKEN", s) + } + if !engineNameRe.MatchString(engine) { + return config.SecretRef{}, fmt.Errorf("secret reference engine %q is not a valid engine name", engine) + } + if strings.ContainsAny(key, ": \t\n") { + return config.SecretRef{}, fmt.Errorf("secret reference key %q must not contain whitespace or a colon", key) + } + return config.SecretRef{Engine: engine, Key: key}, nil +} diff --git a/internal/infrastructure/configuration/setup/secretref_test.go b/internal/infrastructure/configuration/setup/secretref_test.go new file mode 100644 index 000000000..10cb2943d --- /dev/null +++ b/internal/infrastructure/configuration/setup/secretref_test.go @@ -0,0 +1,48 @@ +package setup + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +func TestParseSecretRef(t *testing.T) { + tests := []struct { + name string + in string + want config.SecretRef + wantErr bool + }{ + {name: "env engine", in: "env:ARCHIE_GITHUB_TOKEN", want: config.SecretRef{Engine: "env", Key: "ARCHIE_GITHUB_TOKEN"}}, + {name: "bws engine", in: "bws:OPENAI_API_KEY", want: config.SecretRef{Engine: "bws", Key: "OPENAI_API_KEY"}}, + {name: "operator-supplied engine", in: "vault:TELEGRAM_BOT_TOKEN", want: config.SecretRef{Engine: "vault", Key: "TELEGRAM_BOT_TOKEN"}}, + {name: "hyphenated engine", in: "my-engine:KEY", want: config.SecretRef{Engine: "my-engine", Key: "KEY"}}, + {name: "surrounding space is trimmed", in: " env : ARCHIE_TOKEN ", want: config.SecretRef{Engine: "env", Key: "ARCHIE_TOKEN"}}, + {name: "no colon", in: "ARCHIE_GITHUB_TOKEN", wantErr: true}, + {name: "empty engine", in: ":ARCHIE_TOKEN", wantErr: true}, + {name: "empty key", in: "env:", wantErr: true}, + {name: "engine with a space", in: "my engine:KEY", wantErr: true}, + // Engine names are Yaegi plugin filenames and the shipped ones are bare + // identifiers (age, sops, vault), so a path separator is never valid. + {name: "engine with a slash", in: "vault/openbao:KEY", wantErr: true}, + {name: "key with a colon", in: "env:A:B", wantErr: true}, + {name: "empty", in: "", wantErr: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got, err := ParseSecretRef(tc.in) + if tc.wantErr { + if err == nil { + t.Fatalf("ParseSecretRef(%q) = %+v, want an error", tc.in, got) + } + return + } + if err != nil { + t.Fatalf("ParseSecretRef(%q): %v", tc.in, err) + } + if got != tc.want { + t.Errorf("ParseSecretRef(%q) = %+v, want %+v", tc.in, got, tc.want) + } + }) + } +} diff --git a/internal/infrastructure/configuration/setup/setup.go b/internal/infrastructure/configuration/setup/setup.go index b03c65c61..fd9d4fdb3 100644 --- a/internal/infrastructure/configuration/setup/setup.go +++ b/internal/infrastructure/configuration/setup/setup.go @@ -12,6 +12,7 @@ import ( "fmt" "strings" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" ) @@ -75,6 +76,16 @@ type Params struct { // TelegramUserIDs is an access policy, not a credential: IDs are not secret. // Non-empty means "configure Telegram". + // The three reference fields below name where an already-stored secret + // lives, as engine:key. A reference is not a secret -- an engine name and a + // key name are not sensitive -- which is why a reference may be + // parameterised while a value may not. When one is set the step writes the + // reference to TOML and asks nothing, because the value is already in the + // engine and setup does not own it. + ForgeTokenRef config.SecretRef + ProviderAPIKeyRef config.SecretRef + TelegramTokenRef config.SecretRef + TelegramUserIDs []int64 // There is deliberately no secret-valued field here, for forge tokens, diff --git a/internal/infrastructure/configuration/setup/step_chat.go b/internal/infrastructure/configuration/setup/step_chat.go index a984eddc3..63d2dff33 100644 --- a/internal/infrastructure/configuration/setup/step_chat.go +++ b/internal/infrastructure/configuration/setup/step_chat.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" ) @@ -28,13 +29,26 @@ func stepChat(ctx context.Context, p Prompter, secrets SecretSink, params Params return nil, nil } - // Always prompted, never parameterised; see stepForgeWithToken. - token, err := p.ReadSecret(ctx, "Telegram bot token (from @BotFather): ") - if err != nil { - return nil, fmt.Errorf("setup: telegram token: %w", err) - } - if strings.TrimSpace(token) == "" { - return nil, nil + // A reference means the value already lives in a secret engine: write the + // reference and ask nothing. A supplied reference may name any engine, which + // the legacy token_env form cannot express, so it writes the preferred + // secret-reference form instead. Otherwise the token comes from a prompt, and + // only from a prompt; see stepForgeWithToken. + tokenForm := "token_env" + tokenValue := tomlwrite.String(telegramTokenEnv) + if ref := params.TelegramTokenRef; ref != (config.SecretRef{}) { + tokenForm, tokenValue = "token", tomlwrite.Ref(ref.Engine, ref.Key) + } else { + token, err := p.ReadSecret(ctx, "Telegram bot token (from @BotFather): ") + if err != nil { + return nil, fmt.Errorf("setup: telegram token: %w", err) + } + if strings.TrimSpace(token) == "" { + return nil, nil + } + if err := secrets.Put("env", telegramTokenEnv, token); err != nil { + return nil, fmt.Errorf("setup: store telegram token: %w", err) + } } // TelegramConfig.AllowedUserIDs is deny-by-default: a bot token with no @@ -56,13 +70,9 @@ func stepChat(ctx context.Context, p Prompter, secrets SecretSink, params Params } } - if err := secrets.Put("env", telegramTokenEnv, token); err != nil { - return nil, fmt.Errorf("setup: store telegram token: %w", err) - } - return tableEdits{ "chat.telegram": { - "token_env": tomlwrite.String(telegramTokenEnv), + tokenForm: tokenValue, "allowed_user_ids": intArrayLiteral(ids), }, }, nil diff --git a/internal/infrastructure/configuration/setup/step_forge.go b/internal/infrastructure/configuration/setup/step_forge.go index 11f4ff2f3..d1c44cb0e 100644 --- a/internal/infrastructure/configuration/setup/step_forge.go +++ b/internal/infrastructure/configuration/setup/step_forge.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" ) @@ -40,7 +41,7 @@ func stepForge(ctx context.Context, p Prompter, secrets SecretSink, existingHost if strings.TrimSpace(host) == "" { host = "https://github.com" } - return stepForgeWithToken(ctx, p, secrets, "github", host, "GitHub token (leave blank to configure later): ") + return stepForgeWithToken(ctx, p, secrets, "github", host, "GitHub token (leave blank to configure later): ", params.ForgeTokenRef) case 1: defaultHost := existingHost if defaultHost == "" { @@ -54,12 +55,15 @@ func stepForge(ctx context.Context, p Prompter, secrets SecretSink, existingHost return nil, fmt.Errorf("setup: gitea host: %w", err) } } - return stepForgeWithToken(ctx, p, secrets, "gitea", host, "Gitea token (leave blank to configure later): ") + return stepForgeWithToken(ctx, p, secrets, "gitea", host, "Gitea token (leave blank to configure later): ", params.ForgeTokenRef) default: // The template's default [forge] block is active (type = "github" // with a host and token already set). Selecting "none" must // overwrite host and token too, not just type -- otherwise a // disabled forge still shows a token reference nobody configured + if params.ForgeTokenRef != (config.SecretRef{}) { + return nil, fmt.Errorf("setup: a forge token reference was given, but the forge is disabled (none), so nothing would use it") + } // and that resolveForge never reads, which is exactly the kind of // stale/misleading value this feature exists to stop shipping. return tableEdits{"forge": { @@ -94,11 +98,23 @@ func forgeChoice(name string) (int, error) { // as a parameter: a value passed in would arrive from a command line, where it // lands in shell history and every process listing, and it would be a second // way for a secret to be set that no secret engine knows about. -func stepForgeWithToken(ctx context.Context, p Prompter, secrets SecretSink, forgeType, host, tokenPrompt string) (tableEdits, error) { +// stepForgeWithToken records the forge type and host and writes a reference to +// the token when one is configured. +// +// A tokenRef means the value already lives in a secret engine, so setup writes +// the reference and asks nothing. Otherwise the token comes from a prompt, and +// only from a prompt: a value supplied as a parameter would arrive from a +// command line, where it lands in shell history and every process listing, and +// would be a second way to set a secret that no engine knows about. +func stepForgeWithToken(ctx context.Context, p Prompter, secrets SecretSink, forgeType, host, tokenPrompt string, tokenRef config.SecretRef) (tableEdits, error) { edits := tableEdits{"forge": { "type": tomlwrite.String(forgeType), "host": tomlwrite.String(host), }} + if tokenRef != (config.SecretRef{}) { + edits["forge"]["token"] = tomlwrite.Ref(tokenRef.Engine, tokenRef.Key) + return edits, nil + } token, err := p.ReadSecret(ctx, tokenPrompt) if err != nil { return nil, fmt.Errorf("setup: %s token: %w", forgeType, err) diff --git a/internal/infrastructure/configuration/setup/step_provider.go b/internal/infrastructure/configuration/setup/step_provider.go index 6421e21c9..378268cfc 100644 --- a/internal/infrastructure/configuration/setup/step_provider.go +++ b/internal/infrastructure/configuration/setup/step_provider.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" ) @@ -70,9 +71,12 @@ func stepProvider(ctx context.Context, p Prompter, discovery ModelDiscovery, sec var model string var err error if choice == len(cloudProviders) { + if params.ProviderAPIKeyRef != (config.SecretRef{}) { + return nil, "", fmt.Errorf("setup: a provider key reference was given, but the self-hosted provider is keyless, so nothing would use it") + } edits, model, err = stepSelfHostedModel(ctx, p, discovery, params.Model) } else { - edits, model, err = stepCloudProvider(ctx, p, secrets, cloudProviders[choice], params.Model) + edits, model, err = stepCloudProvider(ctx, p, secrets, cloudProviders[choice], params.Model, params.ProviderAPIKeyRef) } if err != nil { return nil, "", err @@ -125,40 +129,72 @@ func stepSelfHostedModel(ctx context.Context, p Prompter, discovery ModelDiscove return edits, "ollama/" + model, nil } -func stepCloudProvider(ctx context.Context, p Prompter, secrets SecretSink, cp cloudProvider, modelParam string) (tableEdits, string, error) { - // A key supplied as a parameter must not prompt. Without this the only - // provider an unattended install could configure was the keyless - // self-hosted one, so every cloud setup needed a terminal. - // Always prompted, never parameterised: a secret supplied as a value would - // come from a command line and bypass the secret engine entirely. - key, err := p.ReadSecret(ctx, fmt.Sprintf("%s API key: ", cp.name)) - if err != nil { - return nil, "", fmt.Errorf("setup: %s api key: %w", cp.name, err) - } +func stepCloudProvider(ctx context.Context, p Prompter, secrets SecretSink, cp cloudProvider, modelParam string, keyRef config.SecretRef) (tableEdits, string, error) { table := "providers." + cp.class edits := tableEdits{table: {"class": tomlwrite.String(cp.class)}} - if strings.TrimSpace(key) != "" { - if err := secrets.Put("env", cp.apiKeyEnv, key); err != nil { - return nil, "", fmt.Errorf("setup: store %s api key: %w", cp.name, err) + + if keyRef != (config.SecretRef{}) { + // A reference means the value already lives in a secret engine: write + // the reference and ask nothing. Setup neither reads nor stores it, + // because it does not own it. + edits[table]["api_key"] = tomlwrite.Ref(keyRef.Engine, keyRef.Key) + } else { + keyEdit, err := cloudKeyEdit(ctx, p, secrets, cp) + if err != nil { + return nil, "", err + } + if keyEdit != "" { + edits[table]["api_key"] = keyEdit } - edits[table]["api_key"] = tomlwrite.Ref("env", cp.apiKeyEnv) - } else if cp.class == templateDefaultActiveProvider { - // Skipping the key normally means "configure later" and leaves - // api_key untouched -- but for openai specifically that would - // leave the template's own unresolvable bws default active. See - // templateDefaultActiveProvider. - edits[table]["api_key"] = tomlwrite.Ref("", "") } - model := modelParam - if strings.TrimSpace(model) == "" { - model, err = p.ReadLine(ctx, fmt.Sprintf("Model for %s (e.g. gpt-5.4): ", cp.name), "") - if err != nil { - return nil, "", fmt.Errorf("setup: model name: %w", err) - } - } - if strings.TrimSpace(model) == "" { - return nil, "", fmt.Errorf("setup: a model name is required for %s", cp.name) + model, err := cloudModel(ctx, p, cp, modelParam) + if err != nil { + return nil, "", err } return edits, cp.class + "/" + model, nil } + +// cloudKeyEdit obtains the api_key edit by prompting. An empty return means +// "write no api_key and leave whatever the template has", which is the +// configure-later case. +// +// There is no key parameter. A value supplied as one would arrive from a +// command line and bypass the secret engine entirely; see Params. +func cloudKeyEdit(ctx context.Context, p Prompter, secrets SecretSink, cp cloudProvider) (string, error) { + key, err := p.ReadSecret(ctx, fmt.Sprintf("%s API key: ", cp.name)) + if err != nil { + return "", fmt.Errorf("setup: %s api key: %w", cp.name, err) + } + if strings.TrimSpace(key) == "" { + if cp.class == templateDefaultActiveProvider { + // Skipping the key normally means "configure later" and leaves + // api_key untouched -- but for openai specifically that would leave + // the template's own unresolvable bws default active. See + // templateDefaultActiveProvider. + return tomlwrite.Ref("", ""), nil + } + return "", nil + } + if err := secrets.Put("env", cp.apiKeyEnv, key); err != nil { + return "", fmt.Errorf("setup: store %s api key: %w", cp.name, err) + } + return tomlwrite.Ref("env", cp.apiKeyEnv), nil +} + +// cloudModel resolves the model name: the parameter when given, otherwise a +// prompt, and an error when neither yields one. +func cloudModel(ctx context.Context, p Prompter, cp cloudProvider, modelParam string) (string, error) { + model := modelParam + if strings.TrimSpace(model) == "" { + var err error + model, err = p.ReadLine(ctx, fmt.Sprintf("Model for %s (e.g. gpt-5.4): ", cp.name), "") + if err != nil { + return "", fmt.Errorf("setup: model name: %w", err) + } + } + if strings.TrimSpace(model) == "" { + return "", fmt.Errorf("setup: a model name is required for %s", cp.name) + } + return model, nil +}