Prepare configured secrets before loading
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user