Bootstrap secrets for shared configuration acquisition
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
package appconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||||
@@ -58,10 +60,21 @@ func Definition() ck.Definition[Config] {
|
||||
}
|
||||
|
||||
// Load composes built-in resources and optional companions with the selected input.
|
||||
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
||||
func Load(ctx context.Context, input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], *ck.SecretsReport, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
report, prepared, err := ck.LoadSecretsIntoEnvironmentFromConfig(input.Source, ck.SecretsFromConfigOptions{Filesystem: input.Filesystem})
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return nil, report, errors.Join(err, canceled)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, report, err
|
||||
}
|
||||
input.Source = prepared
|
||||
embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("discover embedded prompts: %w", err)
|
||||
return nil, report, fmt.Errorf("discover embedded prompts: %w", err)
|
||||
}
|
||||
for i := range embedded {
|
||||
embedded[i].Destination = ck.Keys("promptkit")
|
||||
@@ -73,7 +86,7 @@ func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[
|
||||
Filesystem: &ck.FilesystemContext{FS: &schemas, Root: ".", BaseDir: "."},
|
||||
})
|
||||
input.Imports = true
|
||||
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||
result, err := ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||
Definition: Definition(),
|
||||
Overlay: overlay,
|
||||
Overrides: overrides,
|
||||
@@ -86,4 +99,8 @@ func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[
|
||||
}},
|
||||
},
|
||||
})
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return nil, report, errors.Join(err, canceled)
|
||||
}
|
||||
return result, report, err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestLoadCombinesApplicationAndPromptSettings(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -62,7 +62,7 @@ func TestLoadWithoutMainDoesNotDiscoverCompanions(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir(dir)
|
||||
result, err := Load(ck.Input{Source: ck.NoSource()}, "")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package appconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -48,7 +49,7 @@ party:
|
||||
`)
|
||||
write("shared/extra.yml", "lore:\n entries:\n - id: hero\n name: Ancient\n category: lore\n")
|
||||
write("glossary/nested/spells.yaml", "spells:\n entries:\n - id: bolt\n name: Bolt\n category: spell\n aliases: [Spark]\n plural: Bolts\n extra: {rank: 2}\n")
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "")
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
||||
}
|
||||
@@ -62,7 +63,7 @@ party:
|
||||
if got := result.Config.Application.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" {
|
||||
t.Fatalf("lore=%+v", got)
|
||||
}
|
||||
discovered, err := Load(ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
|
||||
discovered, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
|
||||
if err != nil || len(discovered.Config.Application.GlossaryCollections["party"].Entries) != 2 {
|
||||
t.Fatalf("discovered glossary: %+v %v", discovered, err)
|
||||
}
|
||||
@@ -97,7 +98,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
|
||||
"glossary_collections:\n x:\n entries:\n - {id: 'id with spaces', name: N, category: C}\n",
|
||||
} {
|
||||
write(main, data)
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err != nil {
|
||||
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err != nil {
|
||||
t.Fatalf("valid %q: %v", data, err)
|
||||
}
|
||||
}
|
||||
@@ -108,7 +109,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
|
||||
"glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, plural: ''}\n",
|
||||
} {
|
||||
write(main, data)
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
t.Fatalf("accepted %q", data)
|
||||
}
|
||||
}
|
||||
@@ -118,7 +119,7 @@ func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write(filepath.Join(dir, "glossary", "other.yml"), "x:\n entries:\n - {id: same, name: Two, category: C}\n")
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
t.Fatal("peer conflict accepted")
|
||||
}
|
||||
}
|
||||
@@ -129,7 +130,7 @@ func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir(dir)
|
||||
result, err := Load(ck.Input{Source: ck.NoSource()}, "")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
75
internal/appconfig/secrets_test.go
Normal file
75
internal/appconfig/secrets_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package appconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestLoadBootstrapsSecrets(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
secrets := filepath.Join(dir, "secrets")
|
||||
if err := os.Mkdir(secrets, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("AUDITA_TEST_BOOTSTRAP", "original")
|
||||
if err := os.WriteFile(filepath.Join(secrets, "AUDITA_TEST_BOOTSTRAP"), []byte("synthetic"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: secrets\n secrets_overwrite: true\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result == nil || os.Getenv("AUDITA_TEST_BOOTSTRAP") != "synthetic" || len(report.Entries()) != 1 || report.Entries()[0].Status != ck.SecretFileLoaded {
|
||||
t.Fatalf("result=%v entries=%+v", result, report.Entries())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRetainsBootstrapErrorAndCancellation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\nconfig:\n secrets_dir: missing\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, "")
|
||||
var secretsErr *ck.SecretsError
|
||||
if result != nil || report == nil || !errors.As(err, &secretsErr) {
|
||||
t.Fatalf("result=%v report=%v error=%v", result, report, err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
result, _, err = Load(ctx, ck.Input{Source: ck.File(main)}, "")
|
||||
if result != nil || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("result=%v error=%v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadReusesPreparedMainSnapshot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\ncontext:\n description: captured\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, prepared, err := ck.LoadSecretsIntoEnvironmentFromConfig(ck.File(main), ck.SecretsFromConfigOptions{Filesystem: &ck.FilesystemContext{Root: dir}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(main, []byte("version: 3\ncontext:\n description: changed\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: prepared, Filesystem: &ck.FilesystemContext{Root: dir}}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Application.Context.Description != "captured" {
|
||||
t.Fatalf("loaded description = %q", result.Config.Application.Context.Description)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package appconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
"io/fs"
|
||||
"os"
|
||||
@@ -23,7 +24,7 @@ func TestMainContextImportsAndOverlay(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -50,10 +51,10 @@ func TestMainContextImportsAndOverlay(t *testing.T) {
|
||||
if !found {
|
||||
t.Fatal("main source missing")
|
||||
}
|
||||
if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
|
||||
if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
|
||||
t.Fatal("missing overlay accepted")
|
||||
}
|
||||
if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
|
||||
if _, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
|
||||
t.Fatal("overlay without context accepted")
|
||||
}
|
||||
}
|
||||
@@ -62,7 +63,8 @@ func TestDiscoveryAndSelectedFailure(t *testing.T) {
|
||||
first, second := t.TempDir(), t.TempDir()
|
||||
search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second))
|
||||
load := func() (*ck.Result[Config], error) {
|
||||
return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.SearchConfig("audita", search)}, "")
|
||||
return result, err
|
||||
}
|
||||
result, err := load()
|
||||
if err != nil {
|
||||
@@ -103,7 +105,7 @@ func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) {
|
||||
if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
|
||||
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user