Files
archie/internal/app/archiemessaging/config.go
T

136 lines
4.0 KiB
Go

package archiemessaging
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
"github.com/samcharles93/archie-core/internal/secret"
)
// ResolvedConfig contains the resolved inputs needed by the Messaging Service.
type ResolvedConfig struct {
Options Options
// Identity is this process's certificate; nil is plaintext, loopback only.
Identity *rpctls.Identity
TelegramToken string
Telegram config.TelegramConfig
// WorkDir and BotUser are not channel transport settings. They locate
// this identity's release-announcement and update-report state files.
WorkDir string
BotUser string
ShowToolCalls bool
Logging config.Config
}
type projection struct {
gateway ServiceTarget
stateStore ServiceTarget
messaging config.ServiceConnection
tls config.TLS
telegram config.TelegramConfig
workDir string
botUser string
showToolCalls bool
logging config.Config
}
// Resolve loads the configuration, extracts the messaging projection and
// applies defaults. Credentials are not resolved here: they may name an
// extension engine that starts only after the State Store is dialed, so
// resolveTokens runs afterwards.
func Resolve(o Options, log *slog.Logger) (ResolvedConfig, error) {
var proj projection
if o.Config != "" {
p, found, err := readProjection(o.Config, o.Overlay, log)
if err != nil {
return ResolvedConfig{}, err
}
if found {
proj = p
o = merge(o, proj)
}
}
o = withDefaults(o)
if err := o.validate(); err != nil {
return ResolvedConfig{}, err
}
id, err := rpctls.Load(proj.tls.CAFile, proj.tls.CertDir, presence.Messaging)
if err != nil {
return ResolvedConfig{}, err
}
return ResolvedConfig{
Options: o,
Identity: id,
Telegram: proj.telegram,
WorkDir: proj.workDir,
BotUser: proj.botUser,
ShowToolCalls: proj.showToolCalls,
Logging: proj.logging,
}, nil
}
func readProjection(path, overlay string, log *slog.Logger) (projection, bool, error) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) && overlay == "" {
return projection{}, false, nil
}
doc, err := configuration.New(log).Resolve(path, overlay)
if err != nil {
return projection{}, false, fmt.Errorf("read messaging configuration: %w", err)
}
return project(doc.Config), true, nil
}
func project(cfg config.Config) projection {
return projection{
gateway: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameGateway).Target},
stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target},
messaging: cfg.Services.Get(config.ServiceNameMessaging),
tls: cfg.TLS,
telegram: cfg.Chat.Telegram,
workDir: cfg.WorkDir,
botUser: cfg.BotUser,
showToolCalls: cfg.Chat.ShowToolCalls,
logging: config.Config{StateDir: cfg.StateDir, Log: cfg.Log, Telemetry: cfg.Telemetry},
}
}
func merge(o Options, p projection) Options {
if o.Gateway.Target == "" {
o.Gateway.Target = p.gateway.Target
}
if o.StateStore.Target == "" {
o.StateStore.Target = p.stateStore.Target
}
if o.Listen == "" {
o.Listen = p.messaging.Listen
}
return o
}
// resolveTelegramToken resolves the configured bot token; an unset
// reference means the channel is off.
func resolveTelegramToken(cfg config.TelegramConfig, secrets *secret.Registry) (string, error) {
if cfg.Token == (secret.SecretRef{}) {
return "", nil
}
return secrets.Resolve(cfg.Token)
}
// resolveTokens fills the channel credentials from the file configuration.
func (c *ResolvedConfig) resolveTokens(secrets *secret.Registry) error {
token, err := resolveTelegramToken(c.Telegram, secrets)
if err != nil {
return fmt.Errorf("resolve telegram token: %w", err)
}
c.TelegramToken = token
return nil
}