Data & infrastructure/Configuration & Secrets

Configuration & Secrets

7-layer typed YAML config, AES-256-GCM encrypted credentials, validation, redaction, and AWS providers.

github.com/fletchhq/fletch/config· fletch docs show config

On this page

github.com/fletchhq/fletch/config loads typed Go configuration structs from layered YAML files, AES-256-GCM encrypted credentials, .env files, pluggable remote secret providers, and OS environment variables. It collects all validation failures in one pass and redacts sensitive fields before exposing reports to logs or the dev dashboard.

7-Layer Precedence

Layers merge from lowest to highest priority. Higher layers override values set by lower layers:

Layer Source
1 (lowest) Struct default tags on your Go config type.
2 Base YAML files: split config/*.yaml or a single config/app.yaml (--single-config).
3 Environment overlays: top-level <env>: block inside a YAML file or config/environments/<env>.yaml.
4 AES-256-GCM encrypted credentials (config/credentials.yml.enc / per-environment credentials).
5 .env file loaded by the pure-Go dotenv parser.
6 Pluggable config.Provider adapters (such as AWS SSM Parameter Store or Secrets Manager).
7 (highest) OS environment variables.

Loading Typed Config

Define a Go struct with YAML, environment, and default tags, then call config.Load[T] or config.LoadWithReport[T]:

package config

import (
	"errors"

	fconfig "github.com/fletchhq/fletch/config"
)

type AppConfig struct {
	Addr     string           `yaml:"addr" env:"FLETCH_ADDR" default:":8080"`
	Timeout  fconfig.Duration `yaml:"timeout" env:"REQUEST_TIMEOUT" default:"15s"`
	MaxBody  fconfig.Bytes    `yaml:"max_body" env:"MAX_BODY" default:"10MB"`
	Database fconfig.URL      `yaml:"database_url" env:"DATABASE_URL"`
	APIKey   fconfig.Secret   `yaml:"api_key" env:"API_KEY"`
}

func (c *AppConfig) Validate() error {
	if c.APIKey.IsEmpty() {
		return errors.New("api_key is required")
	}
	return nil
}

func Load() (AppConfig, *fconfig.Report, error) {
	return fconfig.LoadWithReport[AppConfig](
		fconfig.WithDir("config"),
		fconfig.WithEnv("production"),
		fconfig.WithEnvPrefix("APP_"),
	)
}

Implementing config.Validator (Validate() error) on your root struct or nested structs runs validation automatically after all layers merge.

Core Functions & Options

Function Purpose
config.Load[T](opts...) Loads, merges, interpolates, and validates config into T.
config.LoadWithReport[T](opts...) Loads T and returns a redacted *config.Report recording the winning source for every field.
config.Describe[T](opts...) []FieldSpec Reflects the config schema (FieldSpec metadata) without loading values.
config.Inspect(opts...) (*Report, error) Produces a redacted provenance report for CLI inspection and tooling.
Option Behavior
WithDir(path) Sets the directory containing split config/*.yaml files.
WithFile(path) Loads a single base YAML file instead of a directory.
WithFS(fsys) Reads YAML and credential files from an fs.FS (for embed.FS or tests).
WithEnv(name) Selects the active environment overlay (development, staging, production, etc.).
WithMasterKey(key) Passes the encryption key explicitly instead of reading the key file or env var.
WithDotEnvDir(path) Sets the directory searched for .env files.
WithNoDotEnv() Disables .env file loading.
WithEnvPrefix(prefix) Prepends prefix when looking up OS environment variables.
WithProviders(p...) Registers one or more config.Provider adapters at layer 6.
WithContext(ctx) Supplies a context for remote config.Provider lookups.

Interpolation, Helper Types, and Redaction

YAML values support environment interpolation inside the active section only, so inactive environment blocks do not fail when their variables are unset:

  • ${VAR:-default} substitutes default when VAR is unset or empty.
  • ${VAR:?message} fails loading with message when VAR is unset or empty.
  • $$ escapes a literal $ character.

Built-in helper types handle common operational values cleanly:

Type Behavior
config.Secret Holds sensitive strings and renders as redacted in logs, formatting, and reports. Read the value with Expose(); check for a missing value with IsEmpty().
config.Duration Parses Go duration strings ("30s", "5m", "24h").
config.Bytes Parses human-readable byte sizes ("64KB", "10MB", "2GB").
config.URL Parses URLs and automatically redacts embedded passwords in *config.Report.

Pass the *config.Report from LoadWithReport into fletch.WithConfigReport(report) to populate the read-only Config tab in the dev dashboard. Both config.Secret values and passwords inside config.URL are redacted before any report data reaches logs or the dashboard wire payload.

cfg, report, err := appconfig.Load()
if err != nil {
	return err
}

app := fletch.New(
	fletch.WithConfigReport(report),
)

Encrypted Credentials

Store production secrets in Git alongside your config using AES-256-GCM encryption while keeping only the master key out of version control.

The config package exposes the full set of credential primitives for custom tooling and rotation scripts:

Function Role
config.GenerateKey Generates a new random master key.
config.Encrypt / config.Decrypt Encrypts or decrypts payloads with AES-256-GCM.
config.ResolveKey Resolves the master key from options, environment, or local key file.
config.WriteKeyFile Writes a key file with 0600 permissions.
config.WriteEncryptedFile / config.ReadEncryptedFile Atomically writes or reads versioned encrypted credential envelopes.
config.ValidateCredentialsYAML Validates decrypted credentials YAML and rejects duplicate keys.

CLI Commands & Diagnostics

Manage configuration and encrypted credentials from the fletch CLI:

fletch config show
fletch config check
fletch config env-example

fletch credentials init
fletch credentials edit
fletch credentials show
  • fletch config show prints the merged, redacted configuration with layer provenance.
  • fletch config check validates config files and reports issues without starting the server.
  • fletch config env-example generates a .env.example template from your struct’s FieldSpec definitions.
  • fletch credentials init, edit, and show initialize the key and encrypted file, open decrypted credentials in $EDITOR with validation on save, and print decrypted values locally.

fletch check emits diagnostics W0601 and W0602 when configuration or credential hygiene checks fail (such as tracked key files or invalid config schemas).

Pluggable Providers & AWS Adapter

Implement the config.Provider interface to pull values from external secret stores at layer 6 (above .env and encrypted credentials, below OS environment variables).

For AWS deployments, github.com/fletchhq/fletch/config/aws ships ready-to-use adapters for AWS Systems Manager Parameter Store (by path prefix) and AWS Secrets Manager:

awsCfg, err := awsconfig.LoadDefaultConfig(ctx) // github.com/aws/aws-sdk-go-v2/config
if err != nil {
	return err
}
ssmProvider := configaws.NewSSMProvider(ssm.NewFromConfig(awsCfg), "/myapp/prod")
// or configaws.NewSecretsManagerProvider(secretsmanager.NewFromConfig(awsCfg), []string{"myapp/prod"})

cfg, report, err := config.LoadWithReport[AppConfig](
	config.WithDir("config"),
	config.WithEnv("production"),
	config.WithContext(ctx),
	config.WithProviders(ssmProvider),
)