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

Fix panic when tenant_id and subscription are specified #1391

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.4.1 (Unreleased)
BUGS:
* `data/azure_access_credentials`: Fix panic when `tenant_id` and `subscription_id` are specified together; add new `environment` override field
([#1391](https://github.com/terraform-providers/terraform-provider-vault/pull/1391)).

## 3.4.0 (March 24, 2022)
FEATURES:
* `data/azure_access_credentials` Add `subscription_id` and `tenant_id` fields to used during credential validation ([#1384](https://github.com/terraform-providers/terraform-provider-vault/pull/1384)).
Expand Down
30 changes: 26 additions & 4 deletions vault/data_source_azure_access_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func azureAccessCredentialsDataSource() *schema.Resource {
Description: "The tenant ID to use during credential validation. " +
"Defaults to the tenant ID configured in the Vault backend",
},
"environment": {
Type: schema.TypeString,
Optional: true,
Description: `The Azure environment to use during credential validation.
Defaults to the environment configured in the Vault backend.
Some possible values: AzurePublicCloud, AzureGovernmentCloud`,
},
},
}
}
Expand Down Expand Up @@ -197,13 +204,28 @@ func azureAccessCredentialsDataSourceRead(d *schema.ResourceData, meta interface
}

clientOptions := &arm.ClientOptions{}
if e, ok := config.Data["environment"]; ok && e.(string) != "" {
env, err := azure.EnvironmentFromName(e.(string))
var e string
if v, ok := d.GetOk("environment"); ok {
e = v.(string)
} else {
data, err := getConfigData()
if err != nil {
return err
}
if v, ok := data["environment"]; ok && v.(string) != "" {
e = v.(string)
}
}

if e != "" {
env, err := azure.EnvironmentFromName(e)
if err != nil {
return err
}

switch env.Name {
case "AzurePublicCloud":
clientOptions.Endpoint = arm.AzurePublicCloud
case "AzureChinaCloud":
clientOptions.Endpoint = arm.AzureChina
case "AzureGovernmentCloud":
Expand Down Expand Up @@ -233,7 +255,7 @@ func azureAccessCredentialsDataSourceRead(d *schema.ResourceData, meta interface
if pr.RawResponse.StatusCode == http.StatusUnauthorized {
return fmt.Errorf("validation failed, unauthorized credentials from Vault, err=%w", pager.Err())
}
log.Printf("[DEBUG] Provider Client List response %+v", pr)
log.Printf("[DEBUG] Provider Client List response %+v", pr.RawResponse)
}

if pager.Err() == nil {
Expand All @@ -247,7 +269,7 @@ func azureAccessCredentialsDataSourceRead(d *schema.ResourceData, meta interface
return fmt.Errorf("validation failed, giving up err=%w", pager.Err())
}

log.Printf("[ERROR] Credential validation failed with %v, retrying in %s", pager.Err(), delay)
log.Printf("[WARN] Credential validation failed with %v, retrying in %s", pager.Err(), delay)
successCount = 0
}
time.Sleep(delay)
Expand Down
4 changes: 4 additions & 0 deletions website/docs/d/azure_access_credentials.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ to 300.
* `tenant_id` - (Optional) The tenant ID to use during credential validation.
Defaults to the tenant ID configured in the Vault `backend`.

* `environment` - (Optional) The Azure environment to use during credential validation.
Defaults to the environment configured in the Vault backend.
Some possible values: `AzurePublicCloud`, `AzureGovernmentCloud`

## Attributes Reference

In addition to the arguments above, the following attributes are exported:
Expand Down