Parse main-scoped secrets bootstrap controls
This commit is contained in:
@@ -179,6 +179,8 @@ new `LoadSecretsDir` tests once callers compile.
|
|||||||
|
|
||||||
## Stage 2 — Add main-scoped bootstrap control parsing
|
## Stage 2 — Add main-scoped bootstrap control parsing
|
||||||
|
|
||||||
|
**Completed.**
|
||||||
|
|
||||||
Work primarily in `internal/engine/directives.go`, `acquisition.go`, and the
|
Work primarily in `internal/engine/directives.go`, `acquisition.go`, and the
|
||||||
directive/version parsing handoffs in `source_version.go`.
|
directive/version parsing handoffs in `source_version.go`.
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package configkit_test
|
package configkit_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/fstest"
|
"testing/fstest"
|
||||||
@@ -8,6 +10,72 @@ import (
|
|||||||
configkit "go.maximumdirect.net/configkit"
|
configkit "go.maximumdirect.net/configkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMainBootstrapControlsAreConsumedWithoutEnvironmentWrites(t *testing.T) {
|
||||||
|
t.Setenv("CONFIGKIT_CONTROL_SENTINEL", "unchanged")
|
||||||
|
type nested struct {
|
||||||
|
Config struct {
|
||||||
|
Directory string `yaml:"secrets_dir"`
|
||||||
|
} `yaml:"config"`
|
||||||
|
}
|
||||||
|
type app struct {
|
||||||
|
Service nested `yaml:"service"`
|
||||||
|
}
|
||||||
|
for _, imports := range []bool{false, true} {
|
||||||
|
body := "config: {secrets_dir: missing, secrets_overwrite: true}\nservice: {config: {secrets_dir: ordinary}}\n"
|
||||||
|
fs := configkit.NewFilesystem("bundle", fstest.MapFS{"main.yml": {Data: []byte(body)}})
|
||||||
|
result, err := configkit.Load[app](fs.File("main.yml"), configkit.Options[app]{Imports: imports})
|
||||||
|
if err != nil || result.Config.Service.Config.Directory != "ordinary" || os.Getenv("CONFIGKIT_CONTROL_SENTINEL") != "unchanged" {
|
||||||
|
t.Fatalf("imports %v: result = %#v, error = %v", imports, result, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBootstrapControlsRejectMisplacedAndMalformedValues(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
name, body string
|
||||||
|
code configkit.Code
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{"directory", "config: {secrets_dir: null}\n", configkit.CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||||
|
{"overwrite", "config: {secrets_overwrite: 'false'}\n", configkit.CodeSecretsInvalidValue, "/config/secrets_overwrite"},
|
||||||
|
} {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
_, err := configkit.Load[struct{}](configkit.Bytes("main", []byte(test.body)))
|
||||||
|
var loadErr *configkit.LoadError
|
||||||
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != test.code || loadErr.Diagnostics()[0].Path.String() != test.path || loadErr.Diagnostics()[0].Primary == nil {
|
||||||
|
t.Fatalf("error = %#v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fs := configkit.NewFilesystem("bundle", fstest.MapFS{
|
||||||
|
"main.yml": {Data: []byte("config: {imports: [{path: other.yml}]}\n")},
|
||||||
|
"other.yml": {Data: []byte("config: {secrets_overwrite: false}\n")},
|
||||||
|
})
|
||||||
|
_, err := configkit.Load[struct{}](fs.File("main.yml"), configkit.Options[struct{}]{Imports: true})
|
||||||
|
var loadErr *configkit.LoadError
|
||||||
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportInvalid || loadErr.Diagnostics()[0].Path.String() != "/config/secrets_overwrite" {
|
||||||
|
t.Fatalf("import error = %#v", err)
|
||||||
|
}
|
||||||
|
_, err = configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("main", []byte("{}\n"))}, configkit.LayeredOptions[struct{}]{
|
||||||
|
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: configkit.Bytes("underlay", []byte("config: {secrets_dir: secrets}\n"))}}}},
|
||||||
|
})
|
||||||
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportInvalid {
|
||||||
|
t.Fatalf("underlay error = %#v", err)
|
||||||
|
}
|
||||||
|
overlays := configkit.NewFilesystem("overlays", fstest.MapFS{
|
||||||
|
"main.yml": {Data: []byte("{}\n")},
|
||||||
|
"overlays/dev.yml": {Data: []byte("config: {secrets_overwrite: false}\n")},
|
||||||
|
})
|
||||||
|
_, err = configkit.Load[struct{}](overlays.File("main.yml"), configkit.Options[struct{}]{Overlay: "dev"})
|
||||||
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeOverlayDirective || loadErr.Diagnostics()[0].Primary == nil {
|
||||||
|
t.Fatalf("overlay error = %#v", err)
|
||||||
|
}
|
||||||
|
_, err = configkit.Load[struct{}](configkit.Bytes("main", []byte("config: {imports: [], secrets_overwrite: false}\n")))
|
||||||
|
if !errors.As(err, &loadErr) || len(loadErr.Diagnostics()) != 1 || loadErr.Diagnostics()[0].Code != configkit.CodeImportDisabled {
|
||||||
|
t.Fatalf("disabled imports error = %#v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDocumentRootControlsAndNestedApplicationConfig(t *testing.T) {
|
func TestDocumentRootControlsAndNestedApplicationConfig(t *testing.T) {
|
||||||
type settings struct {
|
type settings struct {
|
||||||
Imports string `yaml:"imports"`
|
Imports string `yaml:"imports"`
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func acquireContextlessInput(input assembledInput, request inputAcquisitionReque
|
|||||||
body, declarations = processing.payload, processing.imports
|
body, declarations = processing.payload, processing.imports
|
||||||
sequences = cloneSequenceDeclarations(processing.sequences)
|
sequences = cloneSequenceDeclarations(processing.sequences)
|
||||||
} else {
|
} else {
|
||||||
body, declarations, diagnostics = splitImportDirectives(parsed.document)
|
body, declarations, diagnostics = splitImportDirectives(parsed.document, request.role)
|
||||||
}
|
}
|
||||||
destination := request.input.destination
|
destination := request.input.destination
|
||||||
localDestination := destination
|
localDestination := destination
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ parent: true
|
|||||||
betaPath := writeImportFile(t, directory, "fragments/b.yml", "beta: true\n")
|
betaPath := writeImportFile(t, directory, "fragments/b.yml", "beta: true\n")
|
||||||
|
|
||||||
main := parseImportMain(t, mainPath)
|
main := parseImportMain(t, mainPath)
|
||||||
_, declarations, diagnostics := splitImportDirectives(main.document)
|
_, declarations, diagnostics := splitImportDirectives(main.document, sourceVersionMain)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
t.Fatalf("main directive diagnostics = %#v", diagnostics)
|
t.Fatalf("main directive diagnostics = %#v", diagnostics)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ records:
|
|||||||
configkit: {imports: [{path: absent.yaml}]}
|
configkit: {imports: [{path: absent.yaml}]}
|
||||||
_configkit: ordinary
|
_configkit: ordinary
|
||||||
`)
|
`)
|
||||||
extracted, diagnostics := extractRootDirectives(root, rootDirectiveContextSource)
|
extracted, diagnostics := extractRootDirectives(root, rootDirectiveContextSource, sourceVersionContribution)
|
||||||
if len(diagnostics) != 0 || len(extracted.imports) != 1 {
|
if len(diagnostics) != 0 || len(extracted.imports) != 1 {
|
||||||
t.Fatalf("extraction = %#v, diagnostics = %#v", extracted, diagnostics)
|
t.Fatalf("extraction = %#v, diagnostics = %#v", extracted, diagnostics)
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ func TestDocumentRootControlValidation(t *testing.T) {
|
|||||||
{"bad imports", "config: {imports: wrong}\n", CodeImportInvalid, "/config/imports"},
|
{"bad imports", "config: {imports: wrong}\n", CodeImportInvalid, "/config/imports"},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, test.input), rootDirectiveContextSource)
|
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, test.input), rootDirectiveContextSource, sourceVersionContribution)
|
||||||
if len(diagnostics) == 0 || diagnostics[0].Code != test.code || diagnostics[0].Path.String() != test.path {
|
if len(diagnostics) == 0 || diagnostics[0].Code != test.code || diagnostics[0].Path.String() != test.path {
|
||||||
t.Fatalf("diagnostics = %#v", diagnostics)
|
t.Fatalf("diagnostics = %#v", diagnostics)
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,88 @@ func TestDocumentRootControlGuards(t *testing.T) {
|
|||||||
if got := overlayDirectiveDiagnostics(sequences, Path{}, true); len(got) != 0 {
|
if got := overlayDirectiveDiagnostics(sequences, Path{}, true); len(got) != 0 {
|
||||||
t.Fatalf("overlay sequences diagnostics = %#v", got)
|
t.Fatalf("overlay sequences diagnostics = %#v", got)
|
||||||
}
|
}
|
||||||
if _, got := extractRootDirectives(sequences, rootDirectiveContextImportsOnly); len(got) == 0 || got[0].Code != CodeSequenceInvalid {
|
if _, got := extractRootDirectives(sequences, rootDirectiveContextImportsOnly, sourceVersionContribution); len(got) == 0 || got[0].Code != CodeSequenceInvalid {
|
||||||
t.Fatalf("imports-only extraction diagnostics = %#v", got)
|
t.Fatalf("imports-only extraction diagnostics = %#v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBootstrapControlExtraction(t *testing.T) {
|
||||||
|
root := parseRootDirectiveDocument(t, "config:\n imports: invalid\n secrets_dir: ../secrets\n secrets_overwrite: false\n")
|
||||||
|
settings, diagnostics := extractBootstrapSettings(root)
|
||||||
|
if len(diagnostics) != 0 || !settings.directoryDeclared || settings.directory != "../secrets" || !settings.overwriteDeclared || settings.overwrite || settings.directoryLocation.Line != 3 || settings.overwriteLocation.Line != 4 {
|
||||||
|
t.Fatalf("settings = %#v, diagnostics = %#v", settings, diagnostics)
|
||||||
|
}
|
||||||
|
if _, diagnostics := extractRootDirectives(root, rootDirectiveContextSource, sourceVersionMain); len(diagnostics) == 0 {
|
||||||
|
t.Fatal("normal extraction did not validate unrelated import grammar")
|
||||||
|
}
|
||||||
|
for _, test := range []struct {
|
||||||
|
input string
|
||||||
|
code Code
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{"config: {secrets_dir: ''}", CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||||
|
{"config: {secrets_dir: /absolute}", CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||||
|
{"config: {secrets_dir: null}", CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||||
|
{"config: {secrets_dir: 123}", CodeSecretsInvalidPath, "/config/secrets_dir"},
|
||||||
|
{"config: {secrets_overwrite: null}", CodeSecretsInvalidValue, "/config/secrets_overwrite"},
|
||||||
|
{"config: {secrets_overwrite: 'true'}", CodeSecretsInvalidValue, "/config/secrets_overwrite"},
|
||||||
|
{"config: invalid", CodeImportInvalid, "/config"},
|
||||||
|
} {
|
||||||
|
t.Run(test.input, func(t *testing.T) {
|
||||||
|
root := parseRootDirectiveDocument(t, test.input+"\n")
|
||||||
|
_, preparation := extractBootstrapSettings(root)
|
||||||
|
_, normal := extractRootDirectives(root, rootDirectiveContextSource, sourceVersionMain)
|
||||||
|
for _, got := range [][]Diagnostic{preparation, normal} {
|
||||||
|
if len(got) != 1 || got[0].Code != test.code || got[0].Path.String() != test.path || got[0].Primary == nil || got[0].Primary.Line != 1 {
|
||||||
|
t.Fatalf("diagnostics = %#v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBootstrapControlsRequireMainSource(t *testing.T) {
|
||||||
|
root := parseRootDirectiveDocument(t, "config: {secrets_dir: secrets, secrets_overwrite: true}\nvalue: ordinary\n")
|
||||||
|
for _, context := range []rootDirectiveContext{rootDirectiveContextSource, rootDirectiveContextLayered} {
|
||||||
|
main, diagnostics := extractRootDirectives(root, context, sourceVersionMain)
|
||||||
|
if len(diagnostics) != 0 || len(main.body.Mapping) != 1 || main.body.Mapping[0].Key != "value" {
|
||||||
|
t.Fatalf("main extraction = %#v, diagnostics = %#v", main, diagnostics)
|
||||||
|
}
|
||||||
|
_, diagnostics = extractRootDirectives(root, context, sourceVersionContribution)
|
||||||
|
if len(diagnostics) != 2 || diagnostics[0].Code != CodeImportInvalid || diagnostics[1].Code != CodeImportInvalid {
|
||||||
|
t.Fatalf("contribution diagnostics = %#v", diagnostics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, diagnostics := extractRootDirectives(root, rootDirectiveContextOverlay, sourceVersionContribution)
|
||||||
|
if len(diagnostics) != 2 || diagnostics[0].Code != CodeOverlayDirective || diagnostics[1].Code != CodeOverlayDirective {
|
||||||
|
t.Fatalf("overlay diagnostics = %#v", diagnostics)
|
||||||
|
}
|
||||||
|
for _, name := range []string{"secrets_dir", "secrets_overwrite"} {
|
||||||
|
if !sourceVersionControlOverlap(root, Keys("config", name)) {
|
||||||
|
t.Errorf("version overlap not detected for %s", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBootstrapControlsKeepSourceVersionChecksInLoad(t *testing.T) {
|
||||||
|
root := parseRootDirectiveDocument(t, "config: {secrets_dir: secrets, secrets_overwrite: false}\nversion: 1\n")
|
||||||
|
called := false
|
||||||
|
rule := &VersionRule{Path: Keys("version"), Check: func(Version, *Validation) error {
|
||||||
|
called = true
|
||||||
|
return nil
|
||||||
|
}}
|
||||||
|
if _, diagnostics := extractBootstrapSettings(root); len(diagnostics) != 0 || called {
|
||||||
|
t.Fatalf("preparation diagnostics = %#v, checker called = %v", diagnostics, called)
|
||||||
|
}
|
||||||
|
result := processSourceVersionWithDirectiveContext(root, rule, sourceVersionMain, Path{}, sourceVersionModeRequired, rootDirectiveContextSource)
|
||||||
|
if len(result.diagnostics) != 0 || !called {
|
||||||
|
t.Fatalf("load diagnostics = %#v, checker called = %v", result.diagnostics, called)
|
||||||
|
}
|
||||||
|
for _, name := range []string{"secrets_dir", "secrets_overwrite"} {
|
||||||
|
rule.Path = Keys("config", name)
|
||||||
|
result = processSourceVersionWithDirectiveContext(root, rule, sourceVersionMain, Path{}, sourceVersionModeRequired, rootDirectiveContextSource)
|
||||||
|
if len(result.diagnostics) != 1 || result.diagnostics[0].Code != CodeVersionError {
|
||||||
|
t.Errorf("overlap %s diagnostics = %#v", name, result.diagnostics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package engine
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.maximumdirect.net/configkit/internal/document"
|
"go.maximumdirect.net/configkit/internal/document"
|
||||||
@@ -24,15 +25,71 @@ type rootDirectiveExtraction struct {
|
|||||||
sequences []sequenceDeclarationSpec
|
sequences []sequenceDeclarationSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitImportDirectives(root *document.Node) (*document.Node, []importDeclarationSpec, []Diagnostic) {
|
// bootstrapSettings retains only source-local control facts, never credentials.
|
||||||
extracted, diagnostics := extractRootDirectives(root, rootDirectiveContextImportsOnly)
|
type bootstrapSettings struct {
|
||||||
|
directoryDeclared bool
|
||||||
|
directory string
|
||||||
|
directoryLocation document.Location
|
||||||
|
overwriteDeclared bool
|
||||||
|
overwrite bool
|
||||||
|
overwriteLocation document.Location
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractBootstrapSettings validates only the controls needed for preparation;
|
||||||
|
// normal loading remains responsible for the rest of the directive grammar.
|
||||||
|
func extractBootstrapSettings(root *document.Node) (bootstrapSettings, []Diagnostic) {
|
||||||
|
var settings bootstrapSettings
|
||||||
|
if root == nil || root.Kind != document.Mapping {
|
||||||
|
return settings, nil
|
||||||
|
}
|
||||||
|
for _, entry := range root.Mapping {
|
||||||
|
if entry.Key != directiveKey {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if entry.Value == nil || entry.Value.Kind != document.Mapping {
|
||||||
|
return settings, []Diagnostic{nodeImportDiagnostic(CodeImportInvalid, Keys(directiveKey), "configuration directive must be a mapping", entry.Value)}
|
||||||
|
}
|
||||||
|
var diagnostics []Diagnostic
|
||||||
|
for _, field := range entry.Value.Mapping {
|
||||||
|
if field.Key == "secrets_dir" || field.Key == "secrets_overwrite" {
|
||||||
|
if diagnostic := parseBootstrapField(&settings, field); diagnostic != nil {
|
||||||
|
diagnostics = append(diagnostics, *diagnostic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return settings, canonicalDiagnostics(diagnostics)
|
||||||
|
}
|
||||||
|
return settings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseBootstrapField(settings *bootstrapSettings, entry document.Entry) *Diagnostic {
|
||||||
|
value := entry.Value
|
||||||
|
fieldPath := Keys(directiveKey, entry.Key)
|
||||||
|
if entry.Key == "secrets_dir" {
|
||||||
|
if value == nil || value.Kind != document.String || value.Text == "" || filepath.IsAbs(value.Text) || filepath.VolumeName(value.Text) != "" {
|
||||||
|
diagnostic := nodeImportDiagnostic(CodeSecretsInvalidPath, fieldPath, "secrets directory must be a nonempty relative host path", value)
|
||||||
|
return &diagnostic
|
||||||
|
}
|
||||||
|
settings.directoryDeclared, settings.directory, settings.directoryLocation = true, value.Text, value.Location
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if value == nil || value.Kind != document.Boolean {
|
||||||
|
diagnostic := nodeImportDiagnostic(CodeSecretsInvalidValue, fieldPath, "secrets overwrite setting must be a Boolean", value)
|
||||||
|
return &diagnostic
|
||||||
|
}
|
||||||
|
settings.overwriteDeclared, settings.overwrite, settings.overwriteLocation = true, value.Text == "true", value.Location
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitImportDirectives(root *document.Node, role sourceVersionRole) (*document.Node, []importDeclarationSpec, []Diagnostic) {
|
||||||
|
extracted, diagnostics := extractRootDirectives(root, rootDirectiveContextImportsOnly, role)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return nil, nil, diagnostics
|
return nil, nil, diagnostics
|
||||||
}
|
}
|
||||||
return extracted.body, extracted.imports, nil
|
return extracted.body, extracted.imports, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractRootDirectives(root *document.Node, context rootDirectiveContext) (rootDirectiveExtraction, []Diagnostic) {
|
func extractRootDirectives(root *document.Node, context rootDirectiveContext, role sourceVersionRole) (rootDirectiveExtraction, []Diagnostic) {
|
||||||
if root == nil {
|
if root == nil {
|
||||||
return rootDirectiveExtraction{}, nil
|
return rootDirectiveExtraction{}, nil
|
||||||
}
|
}
|
||||||
@@ -43,7 +100,7 @@ func extractRootDirectives(root *document.Node, context rootDirectiveContext) (r
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
directiveIndex = index
|
directiveIndex = index
|
||||||
extracted, diagnostics := parseRootDirectiveBlock(entry.Value, Keys(directiveKey), context)
|
extracted, diagnostics := parseRootDirectiveBlock(entry.Value, Keys(directiveKey), context, role)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return rootDirectiveExtraction{}, diagnostics
|
return rootDirectiveExtraction{}, diagnostics
|
||||||
}
|
}
|
||||||
@@ -56,7 +113,7 @@ func extractRootDirectives(root *document.Node, context rootDirectiveContext) (r
|
|||||||
return rootDirectiveExtraction{body: body}, nil
|
return rootDirectiveExtraction{body: body}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRootDirectiveBlock(node *document.Node, directivePath Path, context rootDirectiveContext) (rootDirectiveExtraction, []Diagnostic) {
|
func parseRootDirectiveBlock(node *document.Node, directivePath Path, context rootDirectiveContext, role sourceVersionRole) (rootDirectiveExtraction, []Diagnostic) {
|
||||||
if node == nil || node.Kind != document.Mapping {
|
if node == nil || node.Kind != document.Mapping {
|
||||||
return rootDirectiveExtraction{}, []Diagnostic{nodeImportDiagnostic(CodeImportInvalid, directivePath, "configuration directive must be a mapping", node)}
|
return rootDirectiveExtraction{}, []Diagnostic{nodeImportDiagnostic(CodeImportInvalid, directivePath, "configuration directive must be a mapping", node)}
|
||||||
}
|
}
|
||||||
@@ -89,6 +146,19 @@ func parseRootDirectiveBlock(node *document.Node, directivePath Path, context ro
|
|||||||
parsed, issues := parseSequenceDeclarationDirective(entry.Value, fieldPath)
|
parsed, issues := parseSequenceDeclarationDirective(entry.Value, fieldPath)
|
||||||
extracted.sequences = append(extracted.sequences, parsed...)
|
extracted.sequences = append(extracted.sequences, parsed...)
|
||||||
diagnostics = append(diagnostics, issues...)
|
diagnostics = append(diagnostics, issues...)
|
||||||
|
case "secrets_dir", "secrets_overwrite":
|
||||||
|
if role != sourceVersionMain || context == rootDirectiveContextOverlay {
|
||||||
|
code := CodeImportInvalid
|
||||||
|
if context == rootDirectiveContextOverlay {
|
||||||
|
code = CodeOverlayDirective
|
||||||
|
}
|
||||||
|
diagnostics = append(diagnostics, nodeImportDiagnostic(code, fieldPath, "secrets bootstrap controls are only permitted in the main document", entry.Value))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var settings bootstrapSettings
|
||||||
|
if diagnostic := parseBootstrapField(&settings, entry); diagnostic != nil {
|
||||||
|
diagnostics = append(diagnostics, *diagnostic)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
diagnostics = append(diagnostics, nodeImportDiagnostic(CodeImportInvalid, fieldPath, "configuration directive field is not recognized", entry.Value))
|
diagnostics = append(diagnostics, nodeImportDiagnostic(CodeImportInvalid, fieldPath, "configuration directive field is not recognized", entry.Value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ func acquireImportedWithAccess(request importAcquisitionRequest, policy *acquisi
|
|||||||
declarations = processing.imports
|
declarations = processing.imports
|
||||||
sequenceDeclarations = cloneSequenceDeclarations(processing.sequences)
|
sequenceDeclarations = cloneSequenceDeclarations(processing.sequences)
|
||||||
} else {
|
} else {
|
||||||
body, declarations, diagnostics = splitImportDirectives(main.document)
|
body, declarations, diagnostics = splitImportDirectives(main.document, request.input.role)
|
||||||
}
|
}
|
||||||
root := acquiredContribution{
|
root := acquiredContribution{
|
||||||
source: cloneSourceInfo(mainInfo), payload: body, identity: mainNodeKey,
|
source: cloneSourceInfo(mainInfo), payload: body, identity: mainNodeKey,
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ func parseImportSpecsYAML(t *testing.T, declarations string) ([]importDeclaratio
|
|||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return nil, diagnostics
|
return nil, diagnostics
|
||||||
}
|
}
|
||||||
extracted, diagnostics := extractRootDirectives(parsed.document, rootDirectiveContextSource)
|
extracted, diagnostics := extractRootDirectives(parsed.document, rootDirectiveContextSource, sourceVersionContribution)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return nil, diagnostics
|
return nil, diagnostics
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1558,6 +1558,6 @@ func parseImportSpecificationYAML(input string) ([]importDeclarationSpec, []Diag
|
|||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return nil, diagnostics
|
return nil, diagnostics
|
||||||
}
|
}
|
||||||
_, declarations, diagnostics := splitImportDirectives(parsed.document)
|
_, declarations, diagnostics := splitImportDirectives(parsed.document, sourceVersionMain)
|
||||||
return declarations, diagnostics
|
return declarations, diagnostics
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,14 +81,14 @@ func TestExtractRootDirectivesAllowsSequencesButRejectsOverlayImports(t *testing
|
|||||||
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, `config:
|
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, `config:
|
||||||
imports: [{path: fragments/base.yaml}]
|
imports: [{path: fragments/base.yaml}]
|
||||||
sequences: [{destination: [prompts], key: id}]
|
sequences: [{destination: [prompts], key: id}]
|
||||||
`), rootDirectiveContextOverlay)
|
`), rootDirectiveContextOverlay, sourceVersionContribution)
|
||||||
if len(diagnostics) != 1 || diagnostics[0].Code != CodeOverlayDirective || diagnostics[0].Path.String() != "/config/imports" {
|
if len(diagnostics) != 1 || diagnostics[0].Code != CodeOverlayDirective || diagnostics[0].Path.String() != "/config/imports" {
|
||||||
t.Fatalf("overlay diagnostics = %#v, want imports-only rejection", diagnostics)
|
t.Fatalf("overlay diagnostics = %#v, want imports-only rejection", diagnostics)
|
||||||
}
|
}
|
||||||
|
|
||||||
extracted, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, `config:
|
extracted, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, `config:
|
||||||
sequences: [{destination: [prompts], key: id}]
|
sequences: [{destination: [prompts], key: id}]
|
||||||
`), rootDirectiveContextOverlay)
|
`), rootDirectiveContextOverlay, sourceVersionContribution)
|
||||||
if len(diagnostics) != 0 || len(extracted.sequences) != 1 {
|
if len(diagnostics) != 0 || len(extracted.sequences) != 1 {
|
||||||
t.Fatalf("overlay sequence extraction = %#v, %#v", extracted, diagnostics)
|
t.Fatalf("overlay sequence extraction = %#v, %#v", extracted, diagnostics)
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ func TestParseSequenceDeclarationRejectsMalformedForms(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, test.body), rootDirectiveContextSource)
|
_, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, test.body), rootDirectiveContextSource, sourceVersionContribution)
|
||||||
if len(diagnostics) == 0 || diagnostics[0].Code != CodeSequenceInvalid || diagnostics[0].Path.String() != test.path {
|
if len(diagnostics) == 0 || diagnostics[0].Code != CodeSequenceInvalid || diagnostics[0].Path.String() != test.path {
|
||||||
t.Fatalf("diagnostics = %#v, want %s at %s", diagnostics, CodeSequenceInvalid, test.path)
|
t.Fatalf("diagnostics = %#v, want %s at %s", diagnostics, CodeSequenceInvalid, test.path)
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ version: v1
|
|||||||
|
|
||||||
func extractSequenceDeclarationsYAML(t *testing.T, input string, context rootDirectiveContext) rootDirectiveExtraction {
|
func extractSequenceDeclarationsYAML(t *testing.T, input string, context rootDirectiveContext) rootDirectiveExtraction {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
extracted, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, input), context)
|
extracted, diagnostics := extractRootDirectives(parseRootDirectiveDocument(t, input), context, sourceVersionContribution)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
t.Fatalf("root directive diagnostics = %#v", diagnostics)
|
t.Fatalf("root directive diagnostics = %#v", diagnostics)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ func processSourceVersionWithDirectiveContext(root *document.Node, rule *Version
|
|||||||
return sourceVersionProcessingResult{}
|
return sourceVersionProcessingResult{}
|
||||||
}
|
}
|
||||||
|
|
||||||
extracted, diagnostics := extractRootDirectives(root, directiveContext)
|
extracted, diagnostics := extractRootDirectives(root, directiveContext, role)
|
||||||
if len(diagnostics) != 0 {
|
if len(diagnostics) != 0 {
|
||||||
return sourceVersionProcessingResult{diagnostics: diagnostics}
|
return sourceVersionProcessingResult{diagnostics: diagnostics}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user