Skip to content

Commit

Permalink
Allow escaping $ in config files that use expand env (spiffe#5576)
Browse files Browse the repository at this point in the history
* Allow escaping $ in config files that use expand env

Signed-off-by: Kevin Fox <[email protected]>
  • Loading branch information
kfox1111 authored Oct 17, 2024
1 parent d7d1ccd commit 2d784fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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)
})
}

0 comments on commit 2d784fc

Please sign in to comment.