Support overwriting secrets from a directory
This commit is contained in:
@@ -132,14 +132,24 @@ type stagedSecret struct {
|
||||
// once during startup, before concurrent work; Load never calls it.
|
||||
// Dot-prefixed entries are ignored and omitted from the report.
|
||||
func LoadSecretsDir(directory string) (*SecretsReport, error) {
|
||||
return loadSecretsDir(directory, secretsEnvironment{lookup: os.LookupEnv, set: os.Setenv}, secretsAccess{
|
||||
return LoadSecretsDirWithOptions(directory, SecretsDirOptions{})
|
||||
}
|
||||
|
||||
// SecretsDirOptions controls how directory values are applied.
|
||||
type SecretsDirOptions struct {
|
||||
Overwrite bool
|
||||
}
|
||||
|
||||
// LoadSecretsDirWithOptions loads directory values with the selected policy.
|
||||
func LoadSecretsDirWithOptions(directory string, options SecretsDirOptions) (*SecretsReport, error) {
|
||||
return loadSecretsDir(directory, options, secretsEnvironment{lookup: os.LookupEnv, set: os.Setenv}, secretsAccess{
|
||||
openDirectory: os.Open,
|
||||
lstat: os.Lstat,
|
||||
openFile: os.Open,
|
||||
})
|
||||
}
|
||||
|
||||
func loadSecretsDir(directory string, environment secretsEnvironment, access secretsAccess) (*SecretsReport, error) {
|
||||
func loadSecretsDir(directory string, options SecretsDirOptions, environment secretsEnvironment, access secretsAccess) (*SecretsReport, error) {
|
||||
report := &SecretsReport{}
|
||||
if strings.TrimSpace(directory) == "" {
|
||||
return report, secretsFailure(CodeSecretsInvalidPath, "secrets directory path is required")
|
||||
@@ -204,7 +214,7 @@ func loadSecretsDir(directory string, environment secretsEnvironment, access sec
|
||||
var consumed int64
|
||||
for index := range report.entries {
|
||||
entry := &report.entries[index]
|
||||
if existing, present := environment.lookup(entry.Name); present {
|
||||
if existing, present := environment.lookup(entry.Name); present && !options.Overwrite {
|
||||
entry.Status = SecretFileSkippedExisting
|
||||
entry.ExistingEmpty = existing == ""
|
||||
continue
|
||||
@@ -221,7 +231,7 @@ func loadSecretsDir(directory string, environment secretsEnvironment, access sec
|
||||
|
||||
for _, secret := range staged {
|
||||
entry := &report.entries[secret.entry]
|
||||
if existing, present := environment.lookup(entry.Name); present {
|
||||
if existing, present := environment.lookup(entry.Name); present && !options.Overwrite {
|
||||
entry.Status = SecretFileSkippedExisting
|
||||
entry.ExistingEmpty = existing == ""
|
||||
continue
|
||||
|
||||
@@ -84,12 +84,12 @@ func TestLoadSecretsDirIgnoresDotEntries(t *testing.T) {
|
||||
return os.Lstat(path)
|
||||
}
|
||||
environment := map[string]string{}
|
||||
report, err := loadSecretsDir(directory, mapSecretsEnvironment(environment), access)
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, mapSecretsEnvironment(environment), access)
|
||||
if err != nil || len(report.Entries()) != 0 || len(environment) != 0 {
|
||||
t.Fatalf("hidden-only directory: error = %v, report = %#v, environment = %#v", err, report.Entries(), environment)
|
||||
}
|
||||
writeSecretTestFile(t, directory, "SECRET", "value\n")
|
||||
report, err = loadSecretsDir(directory, mapSecretsEnvironment(environment), access)
|
||||
report, err = loadSecretsDir(directory, SecretsDirOptions{}, mapSecretsEnvironment(environment), access)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func TestLoadSecretsDirPreservesExistingWithoutReading(t *testing.T) {
|
||||
return nil, errors.New("must not open")
|
||||
}
|
||||
|
||||
report, err := loadSecretsDir(directory, mapSecretsEnvironment(environment), access)
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, mapSecretsEnvironment(environment), access)
|
||||
if err != nil {
|
||||
t.Fatalf("loadSecretsDir() error = %v", err)
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func TestLoadSecretsDirValidatesAllMetadataBeforeReadingOrWriting(t *testing.T)
|
||||
access := testSecretsAccess()
|
||||
opened := 0
|
||||
access.openFile = func(path string) (*os.File, error) { opened++; return os.Open(path) }
|
||||
report, err := loadSecretsDir(directory, secretsEnvironment{
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, secretsEnvironment{
|
||||
lookup: func(string) (string, bool) { return "", false },
|
||||
set: func(string, string) error { writes++; return nil },
|
||||
}, access)
|
||||
@@ -186,7 +186,7 @@ func TestLoadSecretsDirPreflightFailuresDoNotWrite(t *testing.T) {
|
||||
writeSecretTestFile(t, directory, "A_FIRST", "first")
|
||||
writeSecretTestFile(t, directory, "Z_FAILED", test.body)
|
||||
writes := 0
|
||||
report, err := loadSecretsDir(directory, secretsEnvironment{
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, secretsEnvironment{
|
||||
lookup: func(string) (string, bool) { return "", false },
|
||||
set: func(string, string) error { writes++; return nil },
|
||||
}, testSecretsAccess())
|
||||
@@ -218,7 +218,7 @@ func TestLoadSecretsDirClassifiesReadFailuresWithoutLeakingThem(t *testing.T) {
|
||||
access := testSecretsAccess()
|
||||
access.openFile = func(string) (*os.File, error) { return nil, test.err }
|
||||
writes := 0
|
||||
report, err := loadSecretsDir(directory, secretsEnvironment{
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, secretsEnvironment{
|
||||
lookup: func(string) (string, bool) { return "", false },
|
||||
set: func(string, string) error { writes++; return nil },
|
||||
}, access)
|
||||
@@ -239,7 +239,7 @@ func TestLoadSecretsDirEnforcesEnumerationAndTotalBounds(t *testing.T) {
|
||||
for index := 0; index <= secretsEntryLimit; index++ {
|
||||
writeSecretTestFile(t, directory, fmt.Sprintf("SECRET_%04d", index), "")
|
||||
}
|
||||
report, err := loadSecretsDir(directory, mapSecretsEnvironment(map[string]string{}), testSecretsAccess())
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, mapSecretsEnvironment(map[string]string{}), testSecretsAccess())
|
||||
requireSecretsCode(t, err, CodeSecretsLimitExceeded)
|
||||
if len(report.Entries()) != secretsEntryLimit+1 {
|
||||
t.Fatalf("entries = %d, want %d bounded enumerated entries", len(report.Entries()), secretsEntryLimit+1)
|
||||
@@ -252,7 +252,7 @@ func TestLoadSecretsDirEnforcesEnumerationAndTotalBounds(t *testing.T) {
|
||||
writeSecretTestFile(t, directory, fmt.Sprintf("SECRET_%02d", index), strings.Repeat("x", int(secretFileByteLimit)))
|
||||
}
|
||||
writes := 0
|
||||
report, err := loadSecretsDir(directory, secretsEnvironment{
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, secretsEnvironment{
|
||||
lookup: func(string) (string, bool) { return "", false },
|
||||
set: func(string, string) error { writes++; return nil },
|
||||
}, testSecretsAccess())
|
||||
@@ -288,7 +288,7 @@ func TestLoadSecretsDirApplyReportsPartialFailureAndPresenceRace(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
report, err := loadSecretsDir(directory, environment, testSecretsAccess())
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, environment, testSecretsAccess())
|
||||
requireSecretsCode(t, err, CodeSecretsApplyFailed)
|
||||
entries := report.Entries()
|
||||
wantStatuses := []SecretFileStatus{SecretFileLoaded, SecretFileSkippedExisting, SecretFileFailed, SecretFileNotApplied}
|
||||
@@ -305,10 +305,79 @@ func TestLoadSecretsDirApplyReportsPartialFailureAndPresenceRace(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSecretsDirOverwrite(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
writeSecretTestFile(t, directory, "CK_OVERWRITE_NONEMPTY", "replacement\n")
|
||||
writeSecretTestFile(t, directory, "CK_OVERWRITE_EMPTY", "filled")
|
||||
writeSecretTestFile(t, directory, "CK_OVERWRITE_WITH_EMPTY", "")
|
||||
t.Setenv("CK_OVERWRITE_NONEMPTY", "old")
|
||||
t.Setenv("CK_OVERWRITE_EMPTY", "")
|
||||
t.Setenv("CK_OVERWRITE_WITH_EMPTY", "old")
|
||||
t.Setenv("CK_OVERWRITE_UNRELATED", "untouched")
|
||||
|
||||
report, err := LoadSecretsDirWithOptions(directory, SecretsDirOptions{Overwrite: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, want := range map[string]string{
|
||||
"CK_OVERWRITE_NONEMPTY": "replacement", "CK_OVERWRITE_EMPTY": "filled",
|
||||
"CK_OVERWRITE_WITH_EMPTY": "", "CK_OVERWRITE_UNRELATED": "untouched",
|
||||
} {
|
||||
if got, ok := os.LookupEnv(name); !ok || got != want {
|
||||
t.Errorf("%s = %q, present %v; want %q", name, got, ok, want)
|
||||
}
|
||||
}
|
||||
for _, entry := range report.Entries() {
|
||||
if entry.Status != SecretFileLoaded || entry.ExistingEmpty {
|
||||
t.Errorf("unexpected result: %#v", entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSecretsDirOverwritePresenceRaceAndPartialFailure(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
for _, name := range []string{"A_LOADED", "B_RACED", "C_FAILED", "D_WAITING"} {
|
||||
writeSecretTestFile(t, directory, name, "value-"+name)
|
||||
}
|
||||
values := map[string]string{"A_LOADED": "old"}
|
||||
lookups := map[string]int{}
|
||||
environment := secretsEnvironment{
|
||||
lookup: func(name string) (string, bool) {
|
||||
lookups[name]++
|
||||
if name == "B_RACED" && lookups[name] == 2 {
|
||||
values[name] = "raced"
|
||||
}
|
||||
value, present := values[name]
|
||||
return value, present
|
||||
},
|
||||
set: func(name, value string) error {
|
||||
if name == "C_FAILED" {
|
||||
return errors.New("set failure")
|
||||
}
|
||||
values[name] = value
|
||||
return nil
|
||||
},
|
||||
}
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{Overwrite: true}, environment, testSecretsAccess())
|
||||
requireSecretsCode(t, err, CodeSecretsApplyFailed)
|
||||
want := []SecretFileStatus{SecretFileLoaded, SecretFileLoaded, SecretFileFailed, SecretFileNotApplied}
|
||||
for i, entry := range report.Entries() {
|
||||
if entry.Status != want[i] || entry.ExistingEmpty {
|
||||
t.Errorf("entry %d = %#v, want status %s", i, entry, want[i])
|
||||
}
|
||||
}
|
||||
if values["A_LOADED"] != "value-A_LOADED" || values["B_RACED"] != "value-B_RACED" {
|
||||
t.Fatalf("unexpected applied values: %#v", values)
|
||||
}
|
||||
if _, present := values["D_WAITING"]; present {
|
||||
t.Fatal("entry after apply failure was written")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSecretsDirFailuresAreSafeAndDetached(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
writeSecretTestFile(t, directory, "SECRET", "CREDENTIAL-CANARY\x00")
|
||||
report, err := loadSecretsDir(directory, mapSecretsEnvironment(map[string]string{}), testSecretsAccess())
|
||||
report, err := loadSecretsDir(directory, SecretsDirOptions{}, mapSecretsEnvironment(map[string]string{}), testSecretsAccess())
|
||||
secretsErr := requireSecretsCode(t, err, CodeSecretsInvalidValue)
|
||||
diagnostics := secretsErr.Diagnostics()
|
||||
diagnostics[0].Message = "mutated"
|
||||
|
||||
Reference in New Issue
Block a user