Skip to content

Commit

Permalink
Merge pull request #30917 from hashicorp/sebasslash/config-token-prec…
Browse files Browse the repository at this point in the history
…edence

Give the configuration token higher precedence over the CLI config file
  • Loading branch information
sebasslash committed Apr 26, 2022
2 parents 983e40b + c7bdd28 commit c557078
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ BUG FIXES:
* When rendering a diff, Terraform now quotes the name of any object attribute whose string representation is not a valid identifier. ([#30766](https://github.com/hashicorp/terraform/issues/30766))
* Terraform will prioritize local terraform variables over remote terraform variables in operations such as `import`, `plan`, `refresh` and `apply` for workspaces in local execution mode. This behavior applies to both `remote` backend and the `cloud` integration configuration. ([#29972](https://github.com/hashicorp/terraform/issues/29972))
* `terraform show -json`: JSON plan output now correctly maps aliased providers to their configurations, and includes the full provider source address alongside the short provider name. ([#30138](https://github.com/hashicorp/terraform/issues/30138))
* The local token configuration now has higher priority than the token specified in a CLI configuration. ([#30664](https://github.com/hashicorp/terraform/issues/30664))

## Previous Releases

Expand Down
31 changes: 16 additions & 15 deletions internal/backend/remote/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,25 @@ func (b *Remote) Configure(obj cty.Value) tfdiags.Diagnostics {
return diags
}

// Retrieve the token for this host as configured in the credentials
// section of the CLI Config File.
token, err := b.token()
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
// Get the token from the config.
var token string
if val := obj.GetAttr("token"); !val.IsNull() {
token = val.AsString()
}

// Get the token from the config if no token was configured for this
// host in credentials section of the CLI Config File.
// Retrieve the token for this host as configured in the credentials
// section of the CLI Config File if no token was configured for this
// host in the config.
if token == "" {
if val := obj.GetAttr("token"); !val.IsNull() {
token = val.AsString()
token, err = b.token()
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
}
}

Expand Down
30 changes: 15 additions & 15 deletions internal/cloud/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,24 @@ func (b *Cloud) Configure(obj cty.Value) tfdiags.Diagnostics {
return diags
}

// Retrieve the token for this host as configured in the credentials
// section of the CLI Config File.
token, err := b.token()
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
// First we'll retrieve the token from the configuration
var token string
if val := obj.GetAttr("token"); !val.IsNull() {
token = val.AsString()
}

// Get the token from the config if no token was configured for this
// host in credentials section of the CLI Config File.
// Get the token from the CLI Config File in the credentials section
// if no token was not set in the configuration
if token == "" {
if val := obj.GetAttr("token"); !val.IsNull() {
token = val.AsString()
token, err = b.token()
if err != nil {
diags = diags.Append(tfdiags.AttributeValue(
tfdiags.Error,
strings.ToUpper(err.Error()[:1])+err.Error()[1:],
"", // no description is needed here, the error is clear
cty.Path{cty.GetAttrStep{Name: "hostname"}},
))
return diags
}
}

Expand Down

0 comments on commit c557078

Please sign in to comment.