mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
A local KMS key's key_material can now be written as ${NAME}, so the key
can come from a Kubernetes Secret, or any secret store that hands it to the
process as an environment variable, instead of sitting in the config file.
This works in the S3 JSON config and in filer.toml.
A reference to a variable that is unset or empty, or one that isn't written
as ${NAME}, fails the load with an error that leaves the reference and the
material out. The expansion is the one static S3 credentials already use,
moved to weed/util so both share it.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
34 lines
969 B
Go
34 lines
969 B
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var envRef = regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)\}`)
|
|
|
|
// ExpandEnvRefs resolves ${VAR} references in a value from a config file. It
|
|
// reports false when any reference is malformed or names a variable that is
|
|
// unset or empty, leaving the reference in place for the caller to reject.
|
|
func ExpandEnvRefs(value string) (string, bool) {
|
|
if !strings.Contains(value, "${") {
|
|
return value, true
|
|
}
|
|
// Every ${ has to open a well-formed reference. A typo like ${MY-VAR} matches
|
|
// nothing, so it would otherwise survive substitution as a literal value.
|
|
if strings.Count(value, "${") != len(envRef.FindAllString(value, -1)) {
|
|
return value, false
|
|
}
|
|
resolved := true
|
|
expanded := envRef.ReplaceAllStringFunc(value, func(ref string) string {
|
|
env, found := os.LookupEnv(ref[2 : len(ref)-1])
|
|
if !found || env == "" {
|
|
resolved = false
|
|
return ref
|
|
}
|
|
return env
|
|
})
|
|
return expanded, resolved
|
|
}
|