Skip to content

Commit

Permalink
Merge pull request #2068 from carolynvs/fix-context-hierarchy-during-…
Browse files Browse the repository at this point in the history
…load

Fix context passed when resolving secrets in config file
  • Loading branch information
carolynvs authored May 19, 2022
1 parent 1327474 commit 23393bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (c *Config) GetBuildDriver() string {
// Load loads the configuration file, rendering any templating used in the config file
// such as ${secret.NAME} or ${env.NAME}.
// Pass nil for resolveSecret to skip resolving secrets.
func (c *Config) Load(ctx context.Context, resolveSecret func(secretKey string) (string, error)) error {
func (c *Config) Load(ctx context.Context, resolveSecret func(ctx context.Context, secretKey string) (string, error)) error {
ctx, log := tracing.StartSpan(ctx)
defer log.EndSpan()

Expand Down Expand Up @@ -345,7 +345,7 @@ func (c *Config) loadFirstPass(ctx context.Context) error {
return c.loadData(ctx, templateData)
}

func (c *Config) loadFinalPass(ctx context.Context, resolveSecret func(secretKey string) (string, error)) error {
func (c *Config) loadFinalPass(ctx context.Context, resolveSecret func(ctx context.Context, secretKey string) (string, error)) error {
ctx, log := tracing.StartSpan(ctx)
defer log.EndSpan()

Expand All @@ -366,9 +366,9 @@ func (c *Config) loadFinalPass(ctx context.Context, resolveSecret func(secretKey

secretKey := variable[len(secretPrefix):]

_, childLog := log.StartSpanWithName("resolveSecret", attribute.String("porter.config.secret.key", secretKey))
ctx, childLog := log.StartSpanWithName("resolveSecret", attribute.String("porter.config.secret.key", secretKey))
defer childLog.EndSpan()
secretValue, err := resolveSecret(secretKey)
secretValue, err := resolveSecret(ctx, secretKey)
if err != nil {
return childLog.Error(errors.Wrapf(err, "could not render config file because ${secret.%s} could not be resolved", secretKey))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestData_Marshal(t *testing.T) {
c.TestContext.AddTestFile("testdata/config.toml", "/home/myuser/.porter/config.toml")

c.DataLoader = LoadFromEnvironment()
resolveTestSecrets := func(secretKey string) (string, error) {
resolveTestSecrets := func(ctx context.Context, secretKey string) (string, error) {
return "topsecret-connectionstring", nil
}
err := c.Load(context.Background(), resolveTestSecrets)
Expand Down
4 changes: 2 additions & 2 deletions pkg/porter/porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func NewFor(c *config.Config, store storage.Store, secretStorage secrets.Store)
// It is the responsibility of the caller to also call Close when done with Porter.
func (p *Porter) Connect(ctx context.Context) error {
// Load the config file and replace any referenced secrets
return p.Config.Load(ctx, func(secret string) (string, error) {
value, err := p.Secrets.Resolve(ctx, "secret", secret)
return p.Config.Load(ctx, func(innerCtx context.Context, secret string) (string, error) {
value, err := p.Secrets.Resolve(innerCtx, "secret", secret)
if err != nil {
if strings.Contains(err.Error(), "invalid value source: secret") {
return "", errors.New("No secret store account is configured")
Expand Down

0 comments on commit 23393bf

Please sign in to comment.