Merge pull request #30917 from hashicorp/sebasslash/config-token-precedence

Give the configuration token higher precedence over the CLI config file
This commit is contained in:
Sebastian Rivera 2022-04-26 10:23:02 -04:00 committed by GitHub
commit c557078704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 30 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -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
}
}