Document and validate configured secrets bootstrap
This commit is contained in:
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/configkit/internal/document"
|
||||
@@ -127,6 +128,19 @@ func TestBootstrapControlExtraction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapDirectoryRejectsWindowsRootedPaths(t *testing.T) {
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("host path syntax differs")
|
||||
}
|
||||
for _, path := range []string{`C:relative`, `\rooted`} {
|
||||
root := parseRootDirectiveDocument(t, "config: {secrets_dir: '"+path+"'}\n")
|
||||
_, diagnostics := extractBootstrapSettings(root)
|
||||
if len(diagnostics) != 1 || diagnostics[0].Code != CodeSecretsInvalidPath {
|
||||
t.Fatalf("path %q: %#v", path, diagnostics)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapControlsRequireMainSource(t *testing.T) {
|
||||
root := parseRootDirectiveDocument(t, "config: {secrets_dir: secrets, secrets_overwrite: true}\nvalue: ordinary\n")
|
||||
for _, context := range []rootDirectiveContext{rootDirectiveContextSource, rootDirectiveContextLayered} {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -66,7 +67,7 @@ func parseBootstrapField(settings *bootstrapSettings, entry document.Entry) *Dia
|
||||
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) != "" {
|
||||
if value == nil || value.Kind != document.String || value.Text == "" || os.IsPathSeparator(value.Text[0]) || filepath.IsAbs(value.Text) || filepath.VolumeName(value.Text) != "" {
|
||||
diagnostic := nodeImportDiagnostic(CodeSecretsInvalidPath, fieldPath, "secrets directory must be a nonempty relative host path", value)
|
||||
return &diagnostic
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ func captureHostMain(source Source, context *FilesystemContext) (Source, []Sourc
|
||||
if context.FS != nil {
|
||||
return Source{}, nil, []Diagnostic{optionDiagnostic(CodeOptionsInvalid, "main preparation requires a host filesystem context")}
|
||||
}
|
||||
if diagnostics := filesystemOptionDiagnostics(source, context); len(diagnostics) != 0 {
|
||||
return Source{}, nil, diagnostics
|
||||
}
|
||||
}
|
||||
if _, diagnostics := normalizeInput(loadInput{source: source, filesystem: context}, ""); len(diagnostics) != 0 {
|
||||
return Source{}, nil, diagnostics
|
||||
}
|
||||
selected, failed, diagnostics := selectInputSource(source, sourceID, context)
|
||||
if len(diagnostics) != 0 {
|
||||
|
||||
@@ -89,6 +89,7 @@ func TestSecretsFromConfigReportsPreparationFailures(t *testing.T) {
|
||||
{"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},
|
||||
{"invalid search options", SearchConfig("app", Paths(malformed), SearchOptions{}, SearchOptions{}), nil, CodeOptionsInvalid},
|
||||
{"native context", File(malformed), []SecretsFromConfigOptions{{Filesystem: &FilesystemContext{Root: ".", FS: &native}}}, CodeOptionsInvalid},
|
||||
{"multiple options", File(malformed), []SecretsFromConfigOptions{{}, {}}, CodeOptionsInvalid},
|
||||
} {
|
||||
@@ -98,6 +99,9 @@ func TestSecretsFromConfigReportsPreparationFailures(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
if test.code == CodeOptionsInvalid && len(failure.Sources()) != 0 {
|
||||
t.Fatalf("option error acquired sources: %#v", failure.Sources())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user