Skip to content

Commit

Permalink
azurerm_storage_account support for setting defaultToOAuthAuthentic…
Browse files Browse the repository at this point in the history
…ation (#17116)
  • Loading branch information
marc-sensenich authored Aug 16, 2022
1 parent 7e5b911 commit bb605de
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 6 deletions.
31 changes: 25 additions & 6 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ func resourceStorageAccount() *pluginsdk.Resource {
Default: true,
},

"default_to_oauth_authentication": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"network_rules": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -968,6 +974,7 @@ func resourceStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) e
nfsV3Enabled := d.Get("nfsv3_enabled").(bool)
allowBlobPublicAccess := d.Get("allow_nested_items_to_be_public").(bool)
allowSharedKeyAccess := d.Get("shared_access_key_enabled").(bool)
defaultToOAuthAuthentication := d.Get("default_to_oauth_authentication").(bool)
crossTenantReplication := d.Get("cross_tenant_replication_enabled").(bool)

accountTier := d.Get("account_tier").(string)
Expand All @@ -983,12 +990,13 @@ func resourceStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) e
Tags: tags.Expand(t),
Kind: storage.Kind(accountKind),
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{
EnableHTTPSTrafficOnly: &enableHTTPSTrafficOnly,
NetworkRuleSet: expandStorageAccountNetworkRules(d, tenantId),
IsHnsEnabled: &isHnsEnabled,
EnableNfsV3: &nfsV3Enabled,
AllowSharedKeyAccess: &allowSharedKeyAccess,
AllowCrossTenantReplication: &crossTenantReplication,
EnableHTTPSTrafficOnly: &enableHTTPSTrafficOnly,
NetworkRuleSet: expandStorageAccountNetworkRules(d, tenantId),
IsHnsEnabled: &isHnsEnabled,
EnableNfsV3: &nfsV3Enabled,
AllowSharedKeyAccess: &allowSharedKeyAccess,
DefaultToOAuthAuthentication: &defaultToOAuthAuthentication,
AllowCrossTenantReplication: &crossTenantReplication,
},
}

Expand Down Expand Up @@ -1317,6 +1325,11 @@ func resourceStorageAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) e
},
}

if d.HasChange("default_to_oauth_authentication") {
defaultToOAuthAuthentication := d.Get("default_to_oauth_authentication").(bool)
opts.AccountPropertiesUpdateParameters.DefaultToOAuthAuthentication = &defaultToOAuthAuthentication
}

if d.HasChange("cross_tenant_replication_enabled") {
crossTenantReplication := d.Get("cross_tenant_replication_enabled").(bool)
opts.AccountPropertiesUpdateParameters.AllowCrossTenantReplication = &crossTenantReplication
Expand Down Expand Up @@ -1818,6 +1831,12 @@ func resourceStorageAccountRead(d *pluginsdk.ResourceData, meta interface{}) err
}
d.Set("shared_access_key_enabled", allowSharedKeyAccess)

defaultToOAuthAuthentication := false
if props.DefaultToOAuthAuthentication != nil {
defaultToOAuthAuthentication = *props.DefaultToOAuthAuthentication
}
d.Set("default_to_oauth_authentication", defaultToOAuthAuthentication)

// Setting the encryption key type to "Service" in PUT. The following GET will not return the queue/table in the service list of its response.
// So defaults to setting the encryption key type to "Service" if it is absent in the GET response. Also, define the default value as "Service" in the schema.
var (
Expand Down
86 changes: 86 additions & 0 deletions internal/services/storage/storage_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,36 @@ func TestAccStorageAccount_allowSharedKeyAccess(t *testing.T) {
})
}

func TestAccStorageAccount_defaultToOAuthAuthentication(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")
r := StorageAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.defaultToOAuthAuthentication(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("account_tier").HasValue("Standard"),
check.That(data.ResourceName).Key("account_replication_type").HasValue("LRS"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.environment").HasValue("production"),
check.That(data.ResourceName).Key("default_to_oauth_authentication").HasValue("true"),
),
},
{
Config: r.defaultToOAuthAuthenticationUpdated(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("account_tier").HasValue("Standard"),
check.That(data.ResourceName).Key("account_replication_type").HasValue("LRS"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.environment").HasValue("production"),
check.That(data.ResourceName).Key("default_to_oauth_authentication").HasValue("false"),
),
},
})
}

func TestAccStorageAccount_encryptionKeyType(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")
r := StorageAccountResource{}
Expand Down Expand Up @@ -3259,6 +3289,62 @@ resource "azurerm_storage_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (r StorageAccountResource) defaultToOAuthAuthentication(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
storage_use_azuread = true
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
default_to_oauth_authentication = true
tags = {
environment = "production"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (r StorageAccountResource) defaultToOAuthAuthenticationUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
storage_use_azuread = true
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
default_to_oauth_authentication = false
tags = {
environment = "production"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (r StorageAccountResource) encryptionKeyType(data acceptance.TestData, t string) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ The following arguments are supported:

~> **Note:** Terraform uses Shared Key Authorisation to provision Storage Containers, Blobs and other items - when Shared Key Access is disabled, you will need to enable [the `storage_use_azuread` flag in the Provider block](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#storage_use_azuread) to use Azure AD for authentication, however not all Azure Storage services support Active Directory authentication.

* `default_to_oauth_authentication` - (Optional) Default to Azure Active Directory authorization in the Azure portal when accessing the Storage Account. The default value is `false`

* `is_hns_enabled` - (Optional) Is Hierarchical Namespace enabled? This can be used with Azure Data Lake Storage Gen 2 ([see here for more information](https://docs.microsoft.com/azure/storage/blobs/data-lake-storage-quickstart-create-account/)). Changing this forces a new resource to be created.

-> **NOTE:** This can only be `true` when `account_tier` is `Standard` or when `account_tier` is `Premium` *and* `account_kind` is `BlockBlobStorage`
Expand Down

0 comments on commit bb605de

Please sign in to comment.