Files
archie/internal/infrastructure/configuration/setup/step_chat.go
T
sam 89cfa44b5e feat(configuration): let setup reference a secret without carrying its value
A parameterised install could not configure a cloud provider without a terminal:
the only way a secret value entered was the prompt, so an unattended run was
limited to the keyless self-hosted provider. The missing piece is the reference
form.

A reference is not a secret. An engine name and a key name are not sensitive --
which is exactly why a reference may be a parameter while a value may not -- so
setup gains -forge-secret-ref, -provider-secret-ref and -telegram-secret-ref,
each taking engine:key, parsed by one new setup.ParseSecretRef. When a reference
is given the step writes it to TOML and asks nothing, and setup neither reads nor
stores the value, because the value is not its to hold. That is the difference
from the prompt path, where the operator supplies a value and setup writes it
into the env engine and references it there.

Engine names are identifiers, not paths: the shipped engines are registered as
age, sops and vault, so a reference like vault/openbao:KEY is refused rather than
written into a config that could never resolve it.

Two refusals guard the silent no-op. A reference naming a key for the keyless
self-hosted provider, or for a forge that is disabled, would sit in the config
looking configured while nothing read it -- the failure shape this flow exists to
remove -- so both are refused rather than written.

telegram keeps writing token_env for the prompt path, because that form names an
env var and the prompt path is the env engine; a supplied reference may name any
engine, which token_env cannot express, so the reference path writes the
preferred token = {engine, key} form. Noted as the one asymmetry.

Also folded stepCloudProvider's key handling into cloudKeyEdit and cloudModel.
That was a lint fix for nested blocks, but the split is the honest shape: the two
branches obtain a key edit by different means and then resolve a model the same
way.

Every new test was mutation-verified by reintroducing the absence: ignoring the
forge reference, making ParseSecretRef validate nothing, and dropping the keyless
-provider guard each fail the corresponding test.
2026-09-15 03:23:08 +10:00

111 lines
3.8 KiB
Go

package setup
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
)
// telegramTokenEnv is the env var Telegram's bot token is stored under.
// TelegramConfig has no secret.SecretRef field -- unlike the forge and
// provider steps, the config edit is a plain token_env string, not a
// {engine,key} inline table.
const telegramTokenEnv = "ARCHIE_TELEGRAM_TOKEN"
func stepChat(ctx context.Context, p Prompter, secrets SecretSink, params Params) (tableEdits, error) {
wantTelegram := len(params.TelegramUserIDs) > 0
if !wantTelegram {
var err error
wantTelegram, err = p.Confirm(ctx, "Configure a Telegram chat channel?", false)
if err != nil {
return nil, fmt.Errorf("setup: telegram: %w", err)
}
}
if !wantTelegram {
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
// allowlist answers nobody, which reads as broken rather than as the
// safe default it actually is. Require at least one ID rather than
// shipping a channel that silently does nothing.
ids := params.TelegramUserIDs
if len(ids) == 0 {
idsLine, err := p.ReadLine(ctx, "Allowed Telegram user IDs (comma-separated; required -- the bot answers nobody without this): ", "")
if err != nil {
return nil, fmt.Errorf("setup: telegram allowed user ids: %w", err)
}
ids, err = ParseTelegramUserIDs(idsLine)
if err != nil {
return nil, fmt.Errorf("setup: telegram allowed user ids: %w", err)
}
if len(ids) == 0 {
return nil, fmt.Errorf("setup: at least one allowed Telegram user ID is required, or the bot will answer nobody")
}
}
return tableEdits{
"chat.telegram": {
tokenForm: tokenValue,
"allowed_user_ids": intArrayLiteral(ids),
},
}, nil
}
// ParseTelegramUserIDs parses a comma-separated Telegram allowlist into
// int64 IDs, skipping empty parts. It is exported so cmd/archied setup can
// parse -telegram-user-ids with exactly the same rules the interactive
// prompt uses, rather than a second copy that could drift.
func ParseTelegramUserIDs(line string) ([]int64, error) {
var ids []int64
for part := range strings.SplitSeq(line, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
id, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return nil, fmt.Errorf("%q is not a valid Telegram user ID: %w", part, err)
}
ids = append(ids, id)
}
return ids, nil
}
// intArrayLiteral renders a single-line TOML integer array. tomlwrite only
// understands single-line values; a Telegram allowlist is short enough
// that this never needs to wrap.
func intArrayLiteral(ids []int64) string {
parts := make([]string, len(ids))
for i, id := range ids {
parts[i] = strconv.FormatInt(id, 10)
}
return "[" + strings.Join(parts, ", ") + "]"
}