Retain captured import policy independently of secrets bootstrap

This commit is contained in:
2026-12-05 20:52:29 +00:00
parent 4254b13ff6
commit 83b272c116
5 changed files with 112 additions and 11 deletions

View File

@@ -402,6 +402,11 @@ overlay paths and containment remain unchanged for existing valid loads.
## Stage 11 — Retain captured import policy without widening secrets bootstrap
Completed: captured mains retain the independent import boundary, preserve it
for omitted later roots and accept compatible explicit narrowing. Capture,
preparation, canonical alias/removal/retargeting and configured-secrets tests
pass; public bootstrap tests prove separate default and explicit anchors.
Extend `capturedHostSource` and `captureHostMain` in `prepared_source.go` to
retain the separated policy. Implement the captured compatibility table in
`resolveCapturedFilesystemContext`. Preserve canonical main identity, captured

View File

@@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strconv"
"sync"
"testing"
@@ -178,3 +179,52 @@ func TestOrdinaryLoadingNeverBootstrapsSecrets(t *testing.T) {
t.Fatal("ordinary loading changed the environment")
}
}
func TestConfiguredSecretsPreservesImportAndBootstrapScopes(t *testing.T) {
root := t.TempDir()
app := filepath.Join(root, "app")
if err := os.MkdirAll(filepath.Join(app, "secrets"), 0700); err != nil {
t.Fatal(err)
}
const name = "CONFIGKIT_EXTERNAL_IMPORT_BOOTSTRAP"
t.Setenv(name, "previous")
if err := os.WriteFile(filepath.Join(app, "secrets", name), []byte("disposable-token\n"), 0600); err != nil {
t.Fatal(err)
}
external := filepath.Join(root, "external.yml")
if err := os.WriteFile(external, []byte("imported: external\n"), 0600); err != nil {
t.Fatal(err)
}
main := filepath.Join(app, "main.yml")
contents := "config:\n secrets_dir: secrets\n secrets_overwrite: true\n imports: [{path: " + strconv.Quote(external) + "}]\ntoken: " + name + "\n"
if err := os.WriteFile(main, []byte(contents), 0600); err != nil {
t.Fatal(err)
}
report, prepared, err := configkit.LoadSecretsIntoEnvironmentFromConfig(configkit.File(main), configkit.SecretsFromConfigOptions{})
if err != nil || len(report.Entries()) != 1 {
t.Fatalf("bootstrap=%#v, %v", report, err)
}
loaded, err := configkit.Load[startupHandoffConfig](prepared, configkit.Options[startupHandoffConfig]{Imports: true})
if err != nil || loaded.Config.Imported != "external" || loaded.Config.Token.Value() != "disposable-token" {
t.Fatalf("load=%#v, %v", loaded, err)
}
// A relative declaration through an external symlink retains bootstrap's
// original anchor even though ordinary imports can reach that directory.
if err := os.Rename(filepath.Join(app, "secrets"), filepath.Join(root, "secrets")); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(root, "secrets"), filepath.Join(app, "secrets")); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
_, _, err = configkit.LoadSecretsIntoEnvironmentFromConfig(configkit.File(main), configkit.SecretsFromConfigOptions{})
if err == nil {
t.Fatal("default bootstrap escaped its anchor")
}
_, prepared, err = configkit.LoadSecretsIntoEnvironmentFromConfig(configkit.File(main), configkit.SecretsFromConfigOptions{Filesystem: &configkit.FilesystemContext{Root: root}})
if err != nil {
t.Fatal(err)
}
if _, err := configkit.Load[startupHandoffConfig](prepared, configkit.Options[startupHandoffConfig]{Imports: true}); err != nil {
t.Fatal(err)
}
}

View File

@@ -32,7 +32,7 @@ func resolveFilesystemContext(source Source, context *FilesystemContext, main pa
func resolveCapturedFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
captured := source.captured
resolved := resolvedFilesystemContext{hostToken: hostToken, rootPath: captured.rootPath, basePath: captured.basePath, mainPath: captured.mainPath}
resolved := resolvedFilesystemContext{hostToken: hostToken, rootPath: captured.rootPath, importRoot: captured.importRoot, basePath: captured.basePath, mainPath: captured.mainPath}
if context == nil {
return resolved, nil
}
@@ -40,13 +40,17 @@ func resolveCapturedFilesystemContext(source Source, context *FilesystemContext,
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
return resolvedFilesystemContext{}, &value
}
// The source record carries the captured canonical main path, so this does
// not follow the original filename again after preparation.
explicit, diagnostic := resolveHostFilesystemContext(source, context, main, hostToken)
if diagnostic != nil || !sameHostPath(explicit.rootPath, resolved.rootPath) || !sameHostPath(explicit.basePath, resolved.basePath) || !sameHostPath(explicit.mainPath, resolved.mainPath) {
if context.Root == "" {
return resolved, nil
}
// Check only the supplied root. Captured main identity and base remain
// usable after deletion or retargeting of the original main filename.
root, err := canonicalExistingPath(context.Root)
if err != nil || !sameHostPath(root, captured.rootPath) {
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
return resolvedFilesystemContext{}, &value
}
resolved.importRoot = root
return resolved, nil
}

View File

@@ -7,6 +7,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"strconv"
"testing"
"testing/fstest"
)
@@ -101,3 +102,43 @@ func TestCapturedMainUsesCanonicalTargetAfterSymlinkChanges(t *testing.T) {
t.Fatalf("load = %#v, %v", loaded, err)
}
}
func TestCapturedImportBoundaryCompatibility(t *testing.T) {
t.Parallel()
root := canonicalTestPath(t, t.TempDir())
external := writeImportFile(t, t.TempDir(), "external.yml", "items: {external: retained}\n")
main := writeImportFile(t, root, "app/main.yml", "config: {imports: [{path: "+strconv.Quote(external)+"}]}\n")
for _, bounded := range []bool{false, true} {
var initial *FilesystemContext
anchor := filepath.Dir(main)
if bounded {
initial = &FilesystemContext{Root: root}
anchor = root
}
prepared, _, diagnostics := captureHostMain(File(main), initial)
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
for _, later := range []*FilesystemContext{nil, {}, {Root: anchor}} {
loaded, err := testLoad[importTestConfig](prepared, Options[importTestConfig]{Imports: true, Filesystem: later})
wantBound := bounded || later != nil && later.Root != ""
if !wantBound {
if err != nil || loaded.Config.Items["external"] != "retained" {
t.Fatalf("unbounded capture=%#v, %v", loaded, err)
}
} else {
var failure *LoadError
if !errors.As(err, &failure) || failure.Diagnostics()[0].Code != CodeImportOutsideRoot {
t.Fatalf("bounded=%v, later=%#v: %v", bounded, later, err)
}
}
}
for _, later := range []*FilesystemContext{{Root: t.TempDir()}, {BaseDir: anchor}, {FS: filesystemPointer(NewFilesystem("native", fstest.MapFS{}))}} {
_, err := testLoad[importTestConfig](prepared, Options[importTestConfig]{Filesystem: later})
var failure *LoadError
if !errors.As(err, &failure) || failure.Diagnostics()[0].Code != CodeOptionsInvalid {
t.Fatalf("incompatible later=%#v: %v", later, err)
}
}
}
}

View File

@@ -4,11 +4,12 @@ import "go.maximumdirect.net/configkit/internal/document"
// capturedHostSource contains immutable, passive facts about one selected file.
type capturedHostSource struct {
document *document.Node
bytes int64
mainPath string
basePath string
rootPath string
document *document.Node
bytes int64
mainPath string
basePath string
rootPath string
importRoot string
}
// selectInputSource shares discovery and post-selection option checks with
@@ -67,7 +68,7 @@ func captureHostMain(source Source, context *FilesystemContext) (Source, []Sourc
}
selected.captured = &capturedHostSource{
document: parsed.document, bytes: parsed.bytes, mainPath: resolved.mainPath,
basePath: resolved.basePath, rootPath: resolved.rootPath,
basePath: resolved.basePath, rootPath: resolved.rootPath, importRoot: resolved.importRoot,
}
return selected, sourceInfos(parsed.source), nil
}