181 lines
7.8 KiB
Go
181 lines
7.8 KiB
Go
|
|
package configkit_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"io/fs"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"sync"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
type startupHandoffConfig struct {
|
||
|
|
Main string `yaml:"main"`
|
||
|
|
Imported string `yaml:"imported"`
|
||
|
|
Underlay string `yaml:"underlay"`
|
||
|
|
Overlay string `yaml:"overlay"`
|
||
|
|
Asset string `yaml:"asset"`
|
||
|
|
Token configkit.Secret `yaml:"token"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestConfiguredSecretsStartupHandoff(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
other := t.TempDir()
|
||
|
|
t.Chdir(other)
|
||
|
|
write := func(name, contents string) string {
|
||
|
|
t.Helper()
|
||
|
|
file := filepath.Join(root, name)
|
||
|
|
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(file, []byte(contents), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return file
|
||
|
|
}
|
||
|
|
const tokenName = "CONFIGKIT_HANDOFF_TOKEN"
|
||
|
|
const sdkName = "CONFIGKIT_HANDOFF_SDK_ONLY"
|
||
|
|
const emptyName = "CONFIGKIT_HANDOFF_EMPTY"
|
||
|
|
t.Setenv(tokenName, "previous")
|
||
|
|
t.Setenv(sdkName, "previous-sdk")
|
||
|
|
t.Setenv(emptyName, "")
|
||
|
|
write("app/secrets/"+tokenName, "bootstrap-token\n")
|
||
|
|
write("app/secrets/"+sdkName, "bootstrap-sdk\n")
|
||
|
|
write("app/secrets/"+emptyName, "bootstrap-empty\n")
|
||
|
|
write("app/fragment.yml", "imported: before\n")
|
||
|
|
write("app/lower.yml", "underlay: companion\n")
|
||
|
|
write("app/overlays/dev.yml", "overlay: selected\n")
|
||
|
|
write("app/asset.txt", "resource-data")
|
||
|
|
main := write("app/main.yml", "config: {secrets_dir: secrets, secrets_overwrite: true, imports: [{path: fragment.yml}]}\nmain: captured\nasset: asset.txt\ntoken: "+tokenName+"\n")
|
||
|
|
candidate := filepath.Join(root, "app", "earlier.yml")
|
||
|
|
search := configkit.SearchConfig("app", configkit.SearchPaths(candidate, main))
|
||
|
|
report, prepared, err := configkit.LoadSecretsIntoEnvironmentFromConfig(search, configkit.SecretsFromConfigOptions{Filesystem: &configkit.FilesystemContext{Root: root}})
|
||
|
|
if err != nil || report == nil || len(report.Entries()) != 3 || os.Getenv(tokenName) != "bootstrap-token" || os.Getenv(sdkName) != "bootstrap-sdk" || os.Getenv(emptyName) != "bootstrap-empty" {
|
||
|
|
t.Fatalf("prepare report = %#v, error = %v", report, err)
|
||
|
|
}
|
||
|
|
for _, entry := range report.Entries() {
|
||
|
|
if entry.Status != configkit.SecretFileLoaded {
|
||
|
|
t.Fatalf("bootstrap entry = %#v", entry)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
options := configkit.Options[startupHandoffConfig]{Imports: true}
|
||
|
|
first, err := configkit.Load[startupHandoffConfig](prepared, options)
|
||
|
|
if err != nil || first.Config.Main != "captured" || first.Config.Imported != "before" || first.Config.Token.Value() != "bootstrap-token" {
|
||
|
|
t.Fatalf("first load = %#v, %v", first, err)
|
||
|
|
}
|
||
|
|
write("app/main.yml", "main: changed\n")
|
||
|
|
write("app/earlier.yml", "main: different-candidate\n")
|
||
|
|
write("app/fragment.yml", "imported: after\n")
|
||
|
|
if err := os.Setenv(tokenName, "later-token"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
second, err := configkit.Load[startupHandoffConfig](prepared, options)
|
||
|
|
if err != nil || second.Config.Main != "captured" || second.Config.Imported != "after" || second.Config.Token.Value() != "later-token" || first.Config.Imported != "before" || first.Config.Token.Value() != "bootstrap-token" {
|
||
|
|
t.Fatalf("subsequent load = %#v, %v", second, err)
|
||
|
|
}
|
||
|
|
sources := second.Sources()
|
||
|
|
canonicalMain, err := filepath.EvalSymlinks(main)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(sources) != 2 || sources[0].Kind != configkit.SourceKindFile || sources[0].Path != canonicalMain || sources[0].ID != "source-1" || sources[1].ID != "source-2" {
|
||
|
|
t.Fatalf("sources = %#v", sources)
|
||
|
|
}
|
||
|
|
sources[0].Label = "modified"
|
||
|
|
if second.Sources()[0].Label == "modified" || first.Sources()[0].Label == "modified" {
|
||
|
|
t.Fatal("source evidence is shared")
|
||
|
|
}
|
||
|
|
context, ok := second.SourceContext("source-1")
|
||
|
|
if !ok || context.BaseDir != "app" {
|
||
|
|
t.Fatalf("source context = %#v, %v", context, ok)
|
||
|
|
}
|
||
|
|
if data, err := fs.ReadFile(context.FS, "app/asset.txt"); err != nil || string(data) != "resource-data" {
|
||
|
|
t.Fatalf("resource = %q, %v", data, err)
|
||
|
|
}
|
||
|
|
underlay := configkit.Input{Source: configkit.Bytes("companion", []byte("config: {imports: [{path: lower.yml}]}\n")), Imports: true, MainRelative: configkit.MainRelative()}
|
||
|
|
layered, err := configkit.LoadLayered(configkit.Input{Source: prepared, Imports: true, Filesystem: &configkit.FilesystemContext{Root: root}}, configkit.LayeredOptions[startupHandoffConfig]{
|
||
|
|
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{underlay}}}, Overlay: "dev",
|
||
|
|
})
|
||
|
|
if err != nil || layered.Config.Underlay != "companion" || layered.Config.Overlay != "selected" || layered.Config.Imported != "after" {
|
||
|
|
t.Fatalf("layered load = %#v, %v", layered, err)
|
||
|
|
}
|
||
|
|
_, err = configkit.LoadLayered(configkit.Input{Source: prepared, Filesystem: &configkit.FilesystemContext{Root: filepath.Join(root, "app")}}, configkit.LayeredOptions[startupHandoffConfig]{})
|
||
|
|
var loadErr *configkit.LoadError
|
||
|
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) == 0 || loadErr.Diagnostics()[0].Code != configkit.CodeOptionsInvalid || os.Getenv(tokenName) != "later-token" {
|
||
|
|
t.Fatalf("incompatible context error = %v", err)
|
||
|
|
}
|
||
|
|
_, err = configkit.Load[startupHandoffConfig](prepared, configkit.Options[startupHandoffConfig]{Imports: true, Definition: configkit.Definition[startupHandoffConfig]{Validate: func(_ *startupHandoffConfig, validation *configkit.Validation) {
|
||
|
|
validation.Error(configkit.Keys("main"), "invalid startup setting")
|
||
|
|
}}})
|
||
|
|
if !errors.As(err, &loadErr) || os.Getenv(tokenName) != "later-token" || os.Getenv(sdkName) != "bootstrap-sdk" || len(report.Entries()) != 3 {
|
||
|
|
t.Fatalf("later validation error = %v, report = %#v", err, report)
|
||
|
|
}
|
||
|
|
var group sync.WaitGroup
|
||
|
|
errorsFound := make(chan error, 8)
|
||
|
|
for range 8 {
|
||
|
|
group.Go(func() {
|
||
|
|
loaded, err := configkit.Load[startupHandoffConfig](prepared, options)
|
||
|
|
if err != nil {
|
||
|
|
errorsFound <- err
|
||
|
|
} else if loaded.Config.Main != "captured" || loaded.Config.Token.Value() != "later-token" {
|
||
|
|
errorsFound <- errors.New("captured source produced unexpected values")
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
group.Wait()
|
||
|
|
close(errorsFound)
|
||
|
|
for err := range errorsFound {
|
||
|
|
t.Error(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestConfiguredSecretsPreservesExistingEmptyValue(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
if err := os.Mkdir(filepath.Join(root, "secrets"), 0o700); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
const name = "CONFIGKIT_HANDOFF_PRESERVED_EMPTY"
|
||
|
|
t.Setenv(name, "")
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "secrets", name), []byte("replacement"), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
main := filepath.Join(root, "main.yml")
|
||
|
|
if err := os.WriteFile(main, []byte("config: {secrets_dir: secrets}\nmain: value\n"), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
report, _, err := configkit.LoadSecretsIntoEnvironmentFromConfig(configkit.File(main))
|
||
|
|
if err != nil || len(report.Entries()) != 1 || report.Entries()[0].Status != configkit.SecretFileSkippedExisting || !report.Entries()[0].ExistingEmpty || os.Getenv(name) != "" {
|
||
|
|
t.Fatalf("report = %#v, error = %v", report, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOrdinaryLoadingNeverBootstrapsSecrets(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
if err := os.Mkdir(filepath.Join(root, "secrets"), 0o700); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
const name = "CONFIGKIT_NO_AUTOMATIC_BOOTSTRAP"
|
||
|
|
t.Setenv(name, "existing")
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "secrets", name), []byte("replacement"), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
main := filepath.Join(root, "main.yml")
|
||
|
|
if err := os.WriteFile(main, []byte("config: {secrets_dir: secrets, secrets_overwrite: true}\nmain: ordinary\n"), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
type ordinaryConfig struct {
|
||
|
|
Main string `yaml:"main"`
|
||
|
|
}
|
||
|
|
if _, err := configkit.Load[ordinaryConfig](configkit.File(main)); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := configkit.LoadLayered(configkit.Input{Source: configkit.File(main)}, configkit.LayeredOptions[ordinaryConfig]{}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if os.Getenv(name) != "existing" {
|
||
|
|
t.Fatal("ordinary loading changed the environment")
|
||
|
|
}
|
||
|
|
}
|