Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow escaping $ in config files that use expand env #5576

Merged
merged 10 commits into from
Oct 17, 2024
3 changes: 2 additions & 1 deletion cmd/spire-agent/cli/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/spiffe/spire/pkg/common/bundleutil"
"github.com/spiffe/spire/pkg/common/catalog"
common_cli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/config"
"github.com/spiffe/spire/pkg/common/fflag"
"github.com/spiffe/spire/pkg/common/health"
"github.com/spiffe/spire/pkg/common/idutil"
Expand Down Expand Up @@ -301,7 +302,7 @@ func ParseFile(path string, expandEnv bool) (*Config, error) {

// If envTemplate flag is passed, substitute $VARIABLES in configuration file
if expandEnv {
data = os.ExpandEnv(data)
data = config.ExpandEnv(data)
}

if err := hcl.Decode(&c, data); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/spire-server/cli/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/spiffe/spire/pkg/common/bundleutil"
"github.com/spiffe/spire/pkg/common/catalog"
common_cli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/config"
"github.com/spiffe/spire/pkg/common/diskcertmanager"
"github.com/spiffe/spire/pkg/common/fflag"
"github.com/spiffe/spire/pkg/common/health"
Expand Down Expand Up @@ -316,7 +317,7 @@ func ParseFile(path string, expandEnv bool) (*Config, error) {

// If envTemplate flag is passed, substitute $VARIABLES in configuration file
if expandEnv {
data = os.ExpandEnv(data)
data = config.ExpandEnv(data)
}

if err := hcl.Decode(&c, data); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config

import (
"os"
)

func ExpandEnv(data string) string {
return os.Expand(data, func(key string) string {
if key == "$" {
return "$"
}
return os.Getenv(key)
})
}
Loading