Parse main-scoped secrets bootstrap controls
This commit is contained in:
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/configkit/internal/document"
|
||||
@@ -24,15 +25,71 @@ type rootDirectiveExtraction struct {
|
||||
sequences []sequenceDeclarationSpec
|
||||
}
|
||||
|
||||
func splitImportDirectives(root *document.Node) (*document.Node, []importDeclarationSpec, []Diagnostic) {
|
||||
extracted, diagnostics := extractRootDirectives(root, rootDirectiveContextImportsOnly)
|
||||
// bootstrapSettings retains only source-local control facts, never credentials.
|
||||
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 {
|
||||
return nil, nil, diagnostics
|
||||
}
|
||||
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 {
|
||||
return rootDirectiveExtraction{}, nil
|
||||
}
|
||||
@@ -43,7 +100,7 @@ func extractRootDirectives(root *document.Node, context rootDirectiveContext) (r
|
||||
continue
|
||||
}
|
||||
directiveIndex = index
|
||||
extracted, diagnostics := parseRootDirectiveBlock(entry.Value, Keys(directiveKey), context)
|
||||
extracted, diagnostics := parseRootDirectiveBlock(entry.Value, Keys(directiveKey), context, role)
|
||||
if len(diagnostics) != 0 {
|
||||
return rootDirectiveExtraction{}, diagnostics
|
||||
}
|
||||
@@ -56,7 +113,7 @@ func extractRootDirectives(root *document.Node, context rootDirectiveContext) (r
|
||||
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 {
|
||||
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)
|
||||
extracted.sequences = append(extracted.sequences, parsed...)
|
||||
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:
|
||||
diagnostics = append(diagnostics, nodeImportDiagnostic(CodeImportInvalid, fieldPath, "configuration directive field is not recognized", entry.Value))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user