Prepare configured secrets before loading
This commit is contained in:
@@ -283,6 +283,8 @@ ordinary and layered composition, metadata, and main-relative bindings.
|
|||||||
|
|
||||||
## Stage 6 — Implement configured bootstrap orchestration
|
## Stage 6 — Implement configured bootstrap orchestration
|
||||||
|
|
||||||
|
**Completed.**
|
||||||
|
|
||||||
Add a focused engine companion in `secrets_from_config.go`, using the capture
|
Add a focused engine companion in `secrets_from_config.go`, using the capture
|
||||||
and bootstrap helpers from earlier stages.
|
and bootstrap helpers from earlier stages.
|
||||||
|
|
||||||
|
|||||||
@@ -137,7 +137,8 @@ func LoadSecretsDir(directory string) (*SecretsReport, error) {
|
|||||||
|
|
||||||
// SecretsDirOptions controls how directory values are applied.
|
// SecretsDirOptions controls how directory values are applied.
|
||||||
type SecretsDirOptions struct {
|
type SecretsDirOptions struct {
|
||||||
Overwrite bool
|
Overwrite bool
|
||||||
|
containmentRoot string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadSecretsDirWithOptions loads directory values with the selected policy.
|
// LoadSecretsDirWithOptions loads directory values with the selected policy.
|
||||||
@@ -159,6 +160,9 @@ func loadSecretsDir(directory string, options SecretsDirOptions, environment sec
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return report, secretsFailure(classifySecretsFilesystemError(err), "secrets directory could not be canonicalized: "+strconv.Quote(cleanAbsolutePath(directory)))
|
return report, secretsFailure(classifySecretsFilesystemError(err), "secrets directory could not be canonicalized: "+strconv.Quote(cleanAbsolutePath(directory)))
|
||||||
}
|
}
|
||||||
|
if options.containmentRoot != "" && !containedPath(options.containmentRoot, canonical) {
|
||||||
|
return report, secretsFailure(CodeSecretsInvalidPath, "secrets directory is outside the configuration root")
|
||||||
|
}
|
||||||
info, err := os.Stat(canonical)
|
info, err := os.Stat(canonical)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return report, secretsFailure(classifySecretsFilesystemError(err), "secrets directory could not be inspected: "+strconv.Quote(canonical))
|
return report, secretsFailure(classifySecretsFilesystemError(err), "secrets directory could not be inspected: "+strconv.Quote(canonical))
|
||||||
|
|||||||
67
internal/engine/secrets_from_config.go
Normal file
67
internal/engine/secrets_from_config.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SecretsFromConfigOptions controls main preparation and bootstrap precedence.
|
||||||
|
type SecretsFromConfigOptions struct {
|
||||||
|
Filesystem *FilesystemContext
|
||||||
|
Overwrite *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadSecretsDirFromConfig prepares a host main source and explicitly loads
|
||||||
|
// its declared secrets directory before a later configuration load.
|
||||||
|
func LoadSecretsDirFromConfig(main Source, supplied ...SecretsFromConfigOptions) (*SecretsReport, Source, error) {
|
||||||
|
return loadSecretsFromConfig(main, supplied, secretsEnvironment{lookup: os.LookupEnv, set: os.Setenv}, secretsAccess{
|
||||||
|
openDirectory: os.Open, lstat: os.Lstat, openFile: os.Open,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadSecretsFromConfig(main Source, supplied []SecretsFromConfigOptions, environment secretsEnvironment, access secretsAccess) (*SecretsReport, Source, error) {
|
||||||
|
emptyReport := &SecretsReport{}
|
||||||
|
if len(supplied) > 1 {
|
||||||
|
return emptyReport, Source{}, (&diagnosticAccumulator{diagnostics: []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "secrets preparation accepts at most one options value")}}).loadError()
|
||||||
|
}
|
||||||
|
var context *FilesystemContext
|
||||||
|
var overwrite *bool
|
||||||
|
if len(supplied) == 1 {
|
||||||
|
if supplied[0].Filesystem != nil {
|
||||||
|
snapshot := *supplied[0].Filesystem
|
||||||
|
if snapshot.FS != nil {
|
||||||
|
filesystem := *snapshot.FS
|
||||||
|
snapshot.FS = &filesystem
|
||||||
|
}
|
||||||
|
context = &snapshot
|
||||||
|
}
|
||||||
|
if supplied[0].Overwrite != nil {
|
||||||
|
value := *supplied[0].Overwrite
|
||||||
|
overwrite = &value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prepared, sources, diagnostics := captureHostMain(main, context)
|
||||||
|
if len(diagnostics) != 0 {
|
||||||
|
return emptyReport, Source{}, (&diagnosticAccumulator{sources: sources, diagnostics: diagnostics}).loadError()
|
||||||
|
}
|
||||||
|
if prepared.kind == sourceNone {
|
||||||
|
return emptyReport, prepared, nil
|
||||||
|
}
|
||||||
|
settings, diagnostics := extractBootstrapSettings(prepared.captured.document)
|
||||||
|
if len(diagnostics) != 0 {
|
||||||
|
return emptyReport, Source{}, (&diagnosticAccumulator{sources: sources, diagnostics: diagnostics}).loadError()
|
||||||
|
}
|
||||||
|
if !settings.directoryDeclared {
|
||||||
|
return emptyReport, prepared, nil
|
||||||
|
}
|
||||||
|
selectedOverwrite := settings.overwrite
|
||||||
|
if overwrite != nil {
|
||||||
|
selectedOverwrite = *overwrite
|
||||||
|
}
|
||||||
|
directory := filepath.Join(prepared.captured.basePath, settings.directory)
|
||||||
|
report, err := loadSecretsDir(directory, SecretsDirOptions{Overwrite: selectedOverwrite, containmentRoot: prepared.captured.rootPath}, environment, access)
|
||||||
|
if err != nil {
|
||||||
|
return report, Source{}, err
|
||||||
|
}
|
||||||
|
return report, prepared, nil
|
||||||
|
}
|
||||||
172
internal/engine/secrets_from_config_test.go
Normal file
172
internal/engine/secrets_from_config_test.go
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSecretsFromConfigPreparesWithoutDirectory(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
for _, body := range []string{"value: retained\n", "config: {secrets_overwrite: true, imports: invalid}\nvalue: retained\n"} {
|
||||||
|
main := writeImportFile(t, root, "main.yml", body)
|
||||||
|
values := map[string]string{"EXISTING": "kept"}
|
||||||
|
report, prepared, err := loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
if err != nil || report == nil || len(report.Entries()) != 0 || prepared.captured == nil || values["EXISTING"] != "kept" {
|
||||||
|
t.Fatalf("report = %#v, source = %#v, error = %v", report, prepared, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, source := range []Source{NoSource(), SearchConfig("app", Paths(filepath.Join(root, "missing.yml")))} {
|
||||||
|
report, prepared, err := loadSecretsFromConfig(source, nil, mapSecretsEnvironment(nil), testSecretsAccess())
|
||||||
|
if err != nil || report == nil || len(report.Entries()) != 0 || prepared.kind != sourceNone {
|
||||||
|
t.Fatalf("absent: report = %#v, source = %#v, error = %v", report, prepared, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSecretsFromConfigSelectsOverwritePolicy(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeImportFile(t, root, "secrets/EXISTING", "replacement\n")
|
||||||
|
writeImportFile(t, root, "secrets/NEW", "new\n")
|
||||||
|
for _, test := range []struct {
|
||||||
|
name, yaml, want string
|
||||||
|
overwrite *bool
|
||||||
|
}{
|
||||||
|
{"default preserves", "", "old", nil},
|
||||||
|
{"yaml enables", " secrets_overwrite: true\n", "replacement", nil},
|
||||||
|
{"explicit false wins", " secrets_overwrite: true\n", "old", boolPointer(false)},
|
||||||
|
{"explicit true wins", " secrets_overwrite: false\n", "replacement", boolPointer(true)},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
main := writeImportFile(t, root, "main.yml", "config:\n secrets_dir: secrets\n"+test.yaml+"value: fixed\n")
|
||||||
|
values := map[string]string{"EXISTING": "old", "OTHER": "kept"}
|
||||||
|
report, prepared, err := loadSecretsFromConfig(File(main), []SecretsFromConfigOptions{{Overwrite: test.overwrite}}, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
if err != nil || prepared.captured == nil || report == nil || len(report.Entries()) != 2 || values["EXISTING"] != test.want || values["NEW"] != "new" || values["OTHER"] != "kept" {
|
||||||
|
t.Fatalf("report = %#v, source = %#v, values = %#v, error = %v", report, prepared, values, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSecretsFromConfigAcceptsPreparedMainAgain(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
main := writeImportFile(t, root, "main.yml", "config: {secrets_dir: secrets}\nvalue: captured\n")
|
||||||
|
writeImportFile(t, root, "secrets/SECRET", "value")
|
||||||
|
values := map[string]string{}
|
||||||
|
_, prepared, err := loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(main, []byte("config: {secrets_dir: missing}\n"), 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
delete(values, "SECRET")
|
||||||
|
report, again, err := loadSecretsFromConfig(prepared, nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
if err != nil || again.captured == nil || len(report.Entries()) != 1 || values["SECRET"] != "value" {
|
||||||
|
t.Fatalf("report = %#v, source = %#v, error = %v", report, again, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSecretsFromConfigReportsPreparationFailures(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
malformed := writeImportFile(t, root, "bad.yml", "config: {secrets_overwrite: wrong}\n")
|
||||||
|
badRoot := writeImportFile(t, root, "bad-root.yml", "config: invalid\n")
|
||||||
|
missing := filepath.Join(root, "missing.yml")
|
||||||
|
native := NewFilesystem("native", fstest.MapFS{})
|
||||||
|
for _, test := range []struct {
|
||||||
|
name string
|
||||||
|
main Source
|
||||||
|
opts []SecretsFromConfigOptions
|
||||||
|
code Code
|
||||||
|
}{
|
||||||
|
{"malformed even with override", File(malformed), []SecretsFromConfigOptions{{Overwrite: boolPointer(true)}}, CodeSecretsInvalidValue},
|
||||||
|
{"malformed root", File(badRoot), nil, CodeImportInvalid},
|
||||||
|
{"missing file", File(missing), nil, CodeSourceMissing},
|
||||||
|
{"required search", SearchConfig("app", Paths(missing), SearchOptions{Required: true}), nil, CodeSourceMissing},
|
||||||
|
{"unsupported source", Bytes("main", []byte("{}\n")), nil, CodeOptionsInvalid},
|
||||||
|
{"native context", File(malformed), []SecretsFromConfigOptions{{Filesystem: &FilesystemContext{Root: ".", FS: &native}}}, CodeOptionsInvalid},
|
||||||
|
{"multiple options", File(malformed), []SecretsFromConfigOptions{{}, {}}, CodeOptionsInvalid},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
report, prepared, err := loadSecretsFromConfig(test.main, test.opts, mapSecretsEnvironment(nil), testSecretsAccess())
|
||||||
|
var failure *LoadError
|
||||||
|
if report == nil || len(report.Entries()) != 0 || prepared.kind != sourceInvalid || !errors.As(err, &failure) || len(failure.Diagnostics()) == 0 || failure.Diagnostics()[0].Code != test.code {
|
||||||
|
t.Fatalf("report = %#v, source = %#v, error = %#v", report, prepared, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSecretsFromConfigChecksDirectoryContainment(t *testing.T) {
|
||||||
|
parent := t.TempDir()
|
||||||
|
root := filepath.Join(parent, "root")
|
||||||
|
writeImportFile(t, root, "inside/SECRET", "safe")
|
||||||
|
writeImportFile(t, parent, "outside/SECRET", "outside")
|
||||||
|
main := writeImportFile(t, root, "main.yml", "config: {secrets_dir: ../outside}\n")
|
||||||
|
values := map[string]string{}
|
||||||
|
report, prepared, err := loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
requireSecretsCode(t, err, CodeSecretsInvalidPath)
|
||||||
|
if report == nil || len(report.Entries()) != 0 || prepared.kind != sourceInvalid || len(values) != 0 {
|
||||||
|
t.Fatalf("outside: report = %#v, source = %#v, values = %#v", report, prepared, values)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(main, []byte("config: {secrets_dir: missing}\n"), 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
report, prepared, err = loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
requireSecretsCode(t, err, CodeSecretsMissing)
|
||||||
|
if report == nil || prepared.kind != sourceInvalid || len(values) != 0 {
|
||||||
|
t.Fatalf("missing: report = %#v, source = %#v, values = %#v", report, prepared, values)
|
||||||
|
}
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := os.Symlink(filepath.Join(root, "inside"), filepath.Join(root, "linked")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(main, []byte("config: {secrets_dir: linked}\n"), 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
report, prepared, err = loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(values), testSecretsAccess())
|
||||||
|
if err != nil || prepared.captured == nil || len(report.Entries()) != 1 || values["SECRET"] != "safe" {
|
||||||
|
t.Fatalf("contained link: report = %#v, source = %#v, error = %v", report, prepared, err)
|
||||||
|
}
|
||||||
|
if err := os.Remove(filepath.Join(root, "linked")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.Symlink(filepath.Join(parent, "outside"), filepath.Join(root, "linked")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
report, prepared, err = loadSecretsFromConfig(File(main), nil, mapSecretsEnvironment(map[string]string{}), testSecretsAccess())
|
||||||
|
requireSecretsCode(t, err, CodeSecretsInvalidPath)
|
||||||
|
if report == nil || len(report.Entries()) != 0 || prepared.kind != sourceInvalid {
|
||||||
|
t.Fatalf("outside link: report = %#v, source = %#v", report, prepared)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSecretsFromConfigKeepsPartialReportSafe(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
main := writeImportFile(t, root, "main.yml", "config: {secrets_dir: secrets}\n")
|
||||||
|
writeImportFile(t, root, "secrets/A", "FIRST-CANARY")
|
||||||
|
writeImportFile(t, root, "secrets/B", "SECOND-CANARY")
|
||||||
|
writeImportFile(t, root, "secrets/C", "THIRD-CANARY")
|
||||||
|
values := map[string]string{}
|
||||||
|
environment := mapSecretsEnvironment(values)
|
||||||
|
environment.set = func(name, value string) error {
|
||||||
|
if name == "B" {
|
||||||
|
return errors.New("unsafe canary failure")
|
||||||
|
}
|
||||||
|
values[name] = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
report, prepared, err := loadSecretsFromConfig(File(main), nil, environment, testSecretsAccess())
|
||||||
|
requireSecretsCode(t, err, CodeSecretsApplyFailed)
|
||||||
|
entries := report.Entries()
|
||||||
|
if prepared.kind != sourceInvalid || len(entries) != 3 || entries[0].Status != SecretFileLoaded || entries[1].Status != SecretFileFailed || entries[2].Status != SecretFileNotApplied || values["A"] != "FIRST-CANARY" || strings.Contains(err.Error()+fmt.Sprintf("%#v", entries), "CANARY") {
|
||||||
|
t.Fatalf("report = %#v, source = %#v, error = %v", report, prepared, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user