Skip to content

Commit

Permalink
azurerm_batch_account - allow versionless keys for CMK
Browse files Browse the repository at this point in the history
  • Loading branch information
alfpark committed May 9, 2023
1 parent c8d8c3c commit 6882862
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/services/batch/batch_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func dataSourceBatchAccount() *pluginsdk.Resource {
"key_vault_key_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: keyVaultValidate.NestedItemId,
ValidateFunc: keyVaultValidate.NestedItemIdWithOptionalVersion,
},
},
},
Expand Down
136 changes: 133 additions & 3 deletions internal/services/batch/batch_account_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,32 @@ func TestAccBatchAccountDataSource_userSubscription(t *testing.T) {
})
}

func TestAccBatchAccountDataSource_cmk(t *testing.T) {
func TestAccBatchAccountDataSource_cmkVersionedKey(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_batch_account", "test")
r := BatchAccountDataSource{}

tenantID := os.Getenv("ARM_TENANT_ID")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.cmkData(data, tenantID),
Config: r.cmkVersionedKeyData(data, tenantID),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").HasValue(azure.NormalizeLocation(data.Locations.Primary)),
check.That(data.ResourceName).Key("encryption.0.key_vault_key_id").IsSet(),
),
},
})
}

func TestAccBatchAccountDataSource_cmkVersionlessKey(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_batch_account", "test")
r := BatchAccountDataSource{}

tenantID := os.Getenv("ARM_TENANT_ID")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.cmkVersionlessKeyData(data, tenantID),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").HasValue(azure.NormalizeLocation(data.Locations.Primary)),
check.That(data.ResourceName).Key("encryption.0.key_vault_key_id").IsSet(),
Expand Down Expand Up @@ -213,7 +230,7 @@ data "azurerm_batch_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, tenantID, tenantID, subscriptionID, data.RandomString)
}

func (BatchAccountDataSource) cmkData(data acceptance.TestData, tenantID string) string {
func (BatchAccountDataSource) cmkVersionedKeyData(data acceptance.TestData, tenantID string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
Expand Down Expand Up @@ -325,3 +342,116 @@ data "azurerm_batch_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomString, tenantID, tenantID, tenantID, data.RandomInteger)
}

func (BatchAccountDataSource) cmkVersionlessKeyData(data acceptance.TestData, tenantID string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
key_vault {
purge_soft_delete_on_destroy = false
purge_soft_deleted_keys_on_destroy = false
}
}
}
data "azurerm_client_config" "current" {
}
resource "azurerm_resource_group" "test" {
name = "testaccRG-batch-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "testaccsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_user_assigned_identity" "test" {
name = "acctest%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}
resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
pool_allocation_mode = "BatchService"
storage_account_id = azurerm_storage_account.test.id
storage_account_authentication_mode = "StorageKeys"
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}
encryption {
key_vault_key_id = "${azurerm_key_vault.test.vault_uri}keys/${azurerm_key_vault_key.test.name}"
}
}
resource "azurerm_key_vault" "test" {
name = "batchkv%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
enabled_for_disk_encryption = true
enabled_for_deployment = true
enabled_for_template_deployment = true
purge_protection_enabled = true
tenant_id = "%s"
sku_name = "standard"
access_policy {
tenant_id = "%s"
object_id = "${data.azurerm_client_config.current.object_id}"
key_permissions = [
"Get",
"Create",
"Delete",
"WrapKey",
"UnwrapKey",
"GetRotationPolicy",
"SetRotationPolicy",
]
}
access_policy {
tenant_id = "%s"
object_id = "${azurerm_user_assigned_identity.test.principal_id}"
key_permissions = [
"Get",
"WrapKey",
"UnwrapKey"
]
}
}
resource "azurerm_key_vault_key" "test" {
name = "enckey%d"
key_vault_id = "${azurerm_key_vault.test.id}"
key_type = "RSA"
key_size = 2048
key_opts = [
"decrypt",
"encrypt",
"sign",
"unwrapKey",
"verify",
"wrapKey",
]
}
data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomString, tenantID, tenantID, tenantID, data.RandomInteger)
}
2 changes: 1 addition & 1 deletion internal/services/batch/batch_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func resourceBatchAccount() *pluginsdk.Resource {
"key_vault_key_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: keyVaultValidate.NestedItemId,
ValidateFunc: keyVaultValidate.NestedItemIdWithOptionalVersion,
},
},
},
Expand Down
130 changes: 127 additions & 3 deletions internal/services/batch/batch_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,30 @@ func TestAccBatchAccount_identity(t *testing.T) {
})
}

func TestAccBatchAccount_cmk(t *testing.T) {
func TestAccBatchAccount_cmkVersionedKey(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_batch_account", "test")
r := BatchAccountResource{}
tenantID := os.Getenv("ARM_TENANT_ID")

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.cmk(data, tenantID),
Config: r.cmkVersionedKey(data, tenantID),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("encryption.0.key_vault_key_id").IsSet(),
),
},
})
}

func TestAccBatchAccount_cmkVersionlessKey(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_batch_account", "test")
r := BatchAccountResource{}
tenantID := os.Getenv("ARM_TENANT_ID")

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.cmkVersionlessKey(data, tenantID),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("encryption.0.key_vault_key_id").IsSet(),
Expand Down Expand Up @@ -543,7 +559,7 @@ resource "azurerm_batch_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString)
}

func (BatchAccountResource) cmk(data acceptance.TestData, tenantID string) string {
func (BatchAccountResource) cmkVersionedKey(data acceptance.TestData, tenantID string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
Expand Down Expand Up @@ -651,6 +667,114 @@ resource "azurerm_key_vault_key" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomString, tenantID, tenantID, tenantID, data.RandomInteger)
}

func (BatchAccountResource) cmkVersionlessKey(data acceptance.TestData, tenantID string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
key_vault {
purge_soft_delete_on_destroy = false
purge_soft_deleted_keys_on_destroy = false
}
}
}
data "azurerm_client_config" "current" {
}
resource "azurerm_resource_group" "test" {
name = "testaccRG-batch-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "testaccsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_user_assigned_identity" "test" {
name = "acctest%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}
resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
pool_allocation_mode = "BatchService"
storage_account_id = azurerm_storage_account.test.id
storage_account_authentication_mode = "StorageKeys"
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}
encryption {
key_vault_key_id = "${azurerm_key_vault.test.vault_uri}keys/${azurerm_key_vault_key.test.name}"
}
}
resource "azurerm_key_vault" "test" {
name = "batchkv%s"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
enabled_for_disk_encryption = true
enabled_for_deployment = true
enabled_for_template_deployment = true
purge_protection_enabled = true
tenant_id = "%s"
sku_name = "standard"
access_policy {
tenant_id = "%s"
object_id = "${data.azurerm_client_config.current.object_id}"
key_permissions = [
"Get",
"Create",
"Delete",
"WrapKey",
"UnwrapKey",
"GetRotationPolicy",
"SetRotationPolicy",
]
}
access_policy {
tenant_id = "%s"
object_id = "${azurerm_user_assigned_identity.test.principal_id}"
key_permissions = [
"Get",
"WrapKey",
"UnwrapKey"
]
}
}
resource "azurerm_key_vault_key" "test" {
name = "enckey%d"
key_vault_id = "${azurerm_key_vault.test.id}"
key_type = "RSA"
key_size = 2048
key_opts = [
"decrypt",
"encrypt",
"sign",
"unwrapKey",
"verify",
"wrapKey",
]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString, data.RandomString, tenantID, tenantID, tenantID, data.RandomInteger)
}

func (BatchAccountResource) removeStorageAccount(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
10 changes: 9 additions & 1 deletion website/docs/d/batch_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,28 @@ The following attributes are exported:

* `key_vault_reference` - The `key_vault_reference` block that describes the Azure KeyVault reference to use when deploying the Azure Batch account using the `UserSubscription` pool allocation mode.

* `encryption` - The `encryption` block that describes the Azure KeyVault key reference used to encrypt data for the Azure Batch account.

* `tags` - A map of tags assigned to the Batch account.

~> **Note:** Primary and secondary access keys are only available when `pool_allocation_mode` is set to `BatchService`. See [documentation](https://docs.microsoft.com/azure/batch/batch-api-basics) for more information.

---

A `key_vault_reference` block have the following properties:
A `key_vault_reference` block has the following properties:

* `id` - The Azure identifier of the Azure KeyVault reference.

* `url` - The HTTPS URL of the Azure KeyVault reference.

---

An `encryption` block has the following properties:

* `key_vault_key_id` - The Azure identifier of the Azure KeyVault key reference.

---

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/batch_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ A `key_vault_reference` block supports the following:

A `encryption` block supports the following:

* `key_vault_key_id` - (Required) The Azure key vault reference id with version that should be used to encrypt data, as documented [here](https://docs.microsoft.com/azure/batch/batch-customer-managed-key). Key rotation is not yet supported.
* `key_vault_key_id` - (Required) The Azure key vault reference id that should be used to encrypt data, as documented [here](https://docs.microsoft.com/azure/batch/batch-customer-managed-key). Both versioned and versionless keys are supported.

## Attributes Reference

Expand Down

0 comments on commit 6882862

Please sign in to comment.