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

New Resource: 'azurerm_storage_account_encryption_settings' to enable storage account encryption using key vault customer-managed keys #2046

Closed
Closed
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
27 changes: 0 additions & 27 deletions azurerm/data_source_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ func dataSourceArmStorageAccount() *schema.Resource {
Computed: true,
},

"account_encryption_source": {
Type: schema.TypeString,
Computed: true,
},

"custom_domain": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -61,16 +56,6 @@ func dataSourceArmStorageAccount() *schema.Resource {
},
},

"enable_blob_encryption": {
Type: schema.TypeBool,
Computed: true,
},

"enable_file_encryption": {
Type: schema.TypeBool,
Computed: true,
},

"enable_https_traffic_only": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -243,18 +228,6 @@ func dataSourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) e
}
}

if encryption := props.Encryption; encryption != nil {
if services := encryption.Services; services != nil {
if blob := services.Blob; blob != nil {
d.Set("enable_blob_encryption", blob.Enabled)
}
if file := services.File; file != nil {
d.Set("enable_file_encryption", file.Enabled)
}
}
d.Set("account_encryption_source", string(encryption.KeySource))
}

// Computed
d.Set("primary_location", props.PrimaryLocation)
d.Set("secondary_location", props.SecondaryLocation)
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_virtual_network_rule": resourceArmSqlVirtualNetworkRule(),
"azurerm_storage_account": resourceArmStorageAccount(),
"azurerm_storage_account_encryption_settings": resourceArmStorageAccountEncryptionSettings(),
"azurerm_storage_blob": resourceArmStorageBlob(),
"azurerm_storage_container": resourceArmStorageContainer(),
"azurerm_storage_queue": resourceArmStorageQueue(),
Expand Down
171 changes: 44 additions & 127 deletions azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -60,7 +61,7 @@ func resourceArmStorageAccount() *schema.Resource {
Computed: true,
Deprecated: "This field has been split into `account_tier` and `account_replication_type`",
ValidateFunc: validateArmStorageAccountType,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
},

"account_tier": {
Expand All @@ -71,7 +72,7 @@ func resourceArmStorageAccount() *schema.Resource {
"Standard",
"Premium",
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
},

"account_replication_type": {
Expand All @@ -83,7 +84,7 @@ func resourceArmStorageAccount() *schema.Resource {
"GRS",
"RAGRS",
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
},

// Only valid for BlobStorage & StorageV2 accounts, defaults to "Hot" in create function
Expand All @@ -97,17 +98,6 @@ func resourceArmStorageAccount() *schema.Resource {
}, true),
},

"account_encryption_source": {
Type: schema.TypeString,
Optional: true,
Default: string(storage.MicrosoftStorage),
ValidateFunc: validation.StringInSlice([]string{
string(storage.MicrosoftKeyvault),
string(storage.MicrosoftStorage),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"custom_domain": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -128,18 +118,6 @@ func resourceArmStorageAccount() *schema.Resource {
},
},

"enable_blob_encryption": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"enable_file_encryption": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"enable_https_traffic_only": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -309,7 +287,7 @@ func resourceArmStorageAccount() *schema.Resource {
"type": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
"SystemAssigned",
}, true),
Expand Down Expand Up @@ -382,15 +360,11 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
accountKind := d.Get("account_kind").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
tags := d.Get("tags").(map[string]interface{})
enableBlobEncryption := d.Get("enable_blob_encryption").(bool)
enableFileEncryption := d.Get("enable_file_encryption").(bool)
enableHTTPSTrafficOnly := d.Get("enable_https_traffic_only").(bool)

accountTier := d.Get("account_tier").(string)
replicationType := d.Get("account_replication_type").(string)
storageType := fmt.Sprintf("%s_%s", accountTier, replicationType)
storageAccountEncryptionSource := d.Get("account_encryption_source").(string)

networkRules := expandStorageAccountNetworkRules(d)

parameters := storage.AccountCreateParameters{
Expand All @@ -401,16 +375,6 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
Tags: expandTags(tags),
Kind: storage.Kind(accountKind),
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{
Encryption: &storage.Encryption{
Services: &storage.EncryptionServices{
Blob: &storage.EncryptionService{
Enabled: utils.Bool(enableBlobEncryption),
},
File: &storage.EncryptionService{
Enabled: utils.Bool(enableFileEncryption),
}},
KeySource: storage.KeySource(storageAccountEncryptionSource),
},
EnableHTTPSTrafficOnly: &enableHTTPSTrafficOnly,
NetworkRuleSet: networkRules,
},
Expand Down Expand Up @@ -540,41 +504,6 @@ func resourceArmStorageAccountUpdate(d *schema.ResourceData, meta interface{}) e
d.SetPartial("tags")
}

if d.HasChange("enable_blob_encryption") || d.HasChange("enable_file_encryption") {
encryptionSource := d.Get("account_encryption_source").(string)

opts := storage.AccountUpdateParameters{
AccountPropertiesUpdateParameters: &storage.AccountPropertiesUpdateParameters{
Encryption: &storage.Encryption{
Services: &storage.EncryptionServices{},
KeySource: storage.KeySource(encryptionSource),
},
},
}

if d.HasChange("enable_blob_encryption") {
enableEncryption := d.Get("enable_blob_encryption").(bool)
opts.Encryption.Services.Blob = &storage.EncryptionService{
Enabled: utils.Bool(enableEncryption),
}

d.SetPartial("enable_blob_encryption")
}

if d.HasChange("enable_file_encryption") {
enableEncryption := d.Get("enable_file_encryption").(bool)
opts.Encryption.Services.File = &storage.EncryptionService{
Enabled: utils.Bool(enableEncryption),
}
d.SetPartial("enable_file_encryption")
}

_, err := client.Update(ctx, resourceGroupName, storageAccountName, opts)
if err != nil {
return fmt.Errorf("Error updating Azure Storage Account Encryption %q: %+v", storageAccountName, err)
}
}

if d.HasChange("custom_domain") {
customDomain := expandStorageAccountCustomDomain(d)
opts := storage.AccountUpdateParameters{
Expand Down Expand Up @@ -686,18 +615,6 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err
}
}

if encryption := props.Encryption; encryption != nil {
if services := encryption.Services; services != nil {
if blob := services.Blob; blob != nil {
d.Set("enable_blob_encryption", blob.Enabled)
}
if file := services.File; file != nil {
d.Set("enable_file_encryption", file.Enabled)
}
}
d.Set("account_encryption_source", string(encryption.KeySource))
}

// Computed
d.Set("primary_location", props.PrimaryLocation)
d.Set("secondary_location", props.SecondaryLocation)
Expand Down Expand Up @@ -825,17 +742,6 @@ func expandStorageAccountCustomDomain(d *schema.ResourceData) *storage.CustomDom
}
}

func flattenStorageAccountCustomDomain(input *storage.CustomDomain) []interface{} {
domain := make(map[string]interface{})

if v := input.Name; v != nil {
domain["name"] = *v
}

// use_subdomain isn't returned
return []interface{}{domain}
}

func expandStorageAccountNetworkRules(d *schema.ResourceData) *storage.NetworkRuleSet {
networkRules := d.Get("network_rules").([]interface{})
if len(networkRules) == 0 {
Expand Down Expand Up @@ -898,6 +804,45 @@ func expandStorageAccountBypass(networkRule map[string]interface{}) storage.Bypa
return storage.Bypass(strings.Join(bypassValues, ", "))
}

func expandAzureRmStorageAccountIdentity(d *schema.ResourceData) *storage.Identity {
identities := d.Get("identity").([]interface{})
identity := identities[0].(map[string]interface{})
identityType := identity["type"].(string)
return &storage.Identity{
Type: &identityType,
}
}

func flattenAzureRmStorageAccountIdentity(identity *storage.Identity) []interface{} {
if identity == nil {
return make([]interface{}, 0)
}

result := make(map[string]interface{})
if identity.Type != nil {
result["type"] = *identity.Type
}
if identity.PrincipalID != nil {
result["principal_id"] = *identity.PrincipalID
}
if identity.TenantID != nil {
result["tenant_id"] = *identity.TenantID
}

return []interface{}{result}
}

func flattenStorageAccountCustomDomain(input *storage.CustomDomain) []interface{} {
domain := make(map[string]interface{})

if v := input.Name; v != nil {
domain["name"] = *v
}

// use_subdomain isn't returned
return []interface{}{domain}
}

func flattenStorageAccountNetworkRules(input *storage.NetworkRuleSet) []interface{} {
if len(*input.IPRules) == 0 && len(*input.VirtualNetworkRules) == 0 {
return []interface{}{}
Expand Down Expand Up @@ -971,34 +916,6 @@ func validateArmStorageAccountType(v interface{}, _ string) (warnings []string,
return warnings, errors
}

func expandAzureRmStorageAccountIdentity(d *schema.ResourceData) *storage.Identity {
identities := d.Get("identity").([]interface{})
identity := identities[0].(map[string]interface{})
identityType := identity["type"].(string)
return &storage.Identity{
Type: &identityType,
}
}

func flattenAzureRmStorageAccountIdentity(identity *storage.Identity) []interface{} {
if identity == nil {
return make([]interface{}, 0)
}

result := make(map[string]interface{})
if identity.Type != nil {
result["type"] = *identity.Type
}
if identity.PrincipalID != nil {
result["principal_id"] = *identity.PrincipalID
}
if identity.TenantID != nil {
result["tenant_id"] = *identity.TenantID
}

return []interface{}{result}
}

func getBlobConnectionString(blobEndpoint *string, acctName *string, acctKey *string) string {
var endpoint string
if blobEndpoint != nil {
Expand Down
Loading