Skip to content

Commit 3a3b0b2

Browse files
authored
r/azurerm_app_service: Make key_vault_reference_identity_id configurable (#13720)
Solves #13388 by making the user assigned identity id for looking up key vault secrets configurable. The attribute is computed to read the default value (at the moment: SystemAssigned ) if no value is specified . The default value is returned by the API even if no SystemAssigned identity is set for an AppService. Therefore this behaviour should be fine. Test case succeeded:
1 parent 6b4e926 commit 3a3b0b2

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

internal/services/web/app_service_resource.go

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
1111
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1212
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
13+
msivalidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/msi/validate"
1314
"github.com/hashicorp/terraform-provider-azurerm/internal/services/web/parse"
1415
"github.com/hashicorp/terraform-provider-azurerm/internal/services/web/validate"
1516
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
@@ -134,6 +135,13 @@ func resourceAppService() *pluginsdk.Resource {
134135
Default: false,
135136
},
136137

138+
"key_vault_reference_identity_id": {
139+
Type: pluginsdk.TypeString,
140+
Optional: true,
141+
Computed: true,
142+
ValidateFunc: msivalidate.UserAssignedIdentityID,
143+
},
144+
137145
"logs": schemaAppServiceLogsConfig(),
138146

139147
"site_config": schemaAppServiceSiteConfig(),
@@ -274,6 +282,10 @@ func resourceAppServiceCreate(d *pluginsdk.ResourceData, meta interface{}) error
274282
},
275283
}
276284

285+
if v, ok := d.GetOk("key_vault_reference_identity_id"); ok {
286+
siteEnvelope.SiteProperties.KeyVaultReferenceIdentity = utils.String(v.(string))
287+
}
288+
277289
if _, ok := d.GetOk("identity"); ok {
278290
appServiceIdentityRaw := d.Get("identity").([]interface{})
279291
appServiceIdentity := expandAppServiceIdentity(appServiceIdentityRaw)
@@ -398,6 +410,10 @@ func resourceAppServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) error
398410
},
399411
}
400412

413+
if v, ok := d.GetOk("key_vault_reference_identity_id"); ok {
414+
siteEnvelope.SiteProperties.KeyVaultReferenceIdentity = utils.String(v.(string))
415+
}
416+
401417
siteEnvelope.SiteProperties.ClientCertEnabled = utils.Bool(d.Get("client_cert_enabled").(bool))
402418

403419
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SiteName, siteEnvelope)
@@ -684,6 +700,10 @@ func resourceAppServiceRead(d *pluginsdk.ResourceData, meta interface{}) error {
684700
d.Set("possible_outbound_ip_address_list", strings.Split(*props.PossibleOutboundIPAddresses, ","))
685701
}
686702
d.Set("custom_domain_verification_id", props.CustomDomainVerificationID)
703+
704+
if props.KeyVaultReferenceIdentity != nil {
705+
d.Set("key_vault_reference_identity_id", props.KeyVaultReferenceIdentity)
706+
}
687707
}
688708

689709
appSettings := flattenAppServiceAppSettings(appSettingsResp.Properties)

internal/services/web/app_service_resource_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,21 @@ func TestAccAppServiceEnvironment_scopeNameCheck(t *testing.T) {
19281928
})
19291929
}
19301930

1931+
func TestAccAppService_keyVaultUserAssignedIdentity(t *testing.T) {
1932+
data := acceptance.BuildTestData(t, "azurerm_app_service", "test")
1933+
r := AppServiceResource{}
1934+
1935+
data.ResourceTest(t, r, []acceptance.TestStep{
1936+
{
1937+
Config: r.KeyVaultUserAssignedIdentity(data),
1938+
Check: acceptance.ComposeTestCheckFunc(
1939+
check.That(data.ResourceName).ExistsInAzure(r),
1940+
),
1941+
},
1942+
data.ImportStep(),
1943+
})
1944+
}
1945+
19311946
func (r AppServiceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
19321947
id, err := parse.AppServiceID(state.ID)
19331948
if err != nil {
@@ -5591,3 +5606,47 @@ resource "azurerm_app_service" "test" {
55915606
}
55925607
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
55935608
}
5609+
5610+
func (r AppServiceResource) KeyVaultUserAssignedIdentity(data acceptance.TestData) string {
5611+
return fmt.Sprintf(`
5612+
provider "azurerm" {
5613+
features {}
5614+
}
5615+
5616+
resource "azurerm_resource_group" "test" {
5617+
name = "acctestRG-%d"
5618+
location = "%s"
5619+
}
5620+
5621+
resource "azurerm_user_assigned_identity" "test" {
5622+
name = "acct-%d"
5623+
resource_group_name = azurerm_resource_group.test.name
5624+
location = azurerm_resource_group.test.location
5625+
}
5626+
5627+
resource "azurerm_app_service_plan" "test" {
5628+
name = "acctestASP-%d"
5629+
location = azurerm_resource_group.test.location
5630+
resource_group_name = azurerm_resource_group.test.name
5631+
5632+
sku {
5633+
tier = "Standard"
5634+
size = "S1"
5635+
}
5636+
}
5637+
5638+
resource "azurerm_app_service" "test" {
5639+
name = "acctestAS-%d"
5640+
location = azurerm_resource_group.test.location
5641+
resource_group_name = azurerm_resource_group.test.name
5642+
app_service_plan_id = azurerm_app_service_plan.test.id
5643+
5644+
key_vault_reference_identity_id = azurerm_user_assigned_identity.test.id
5645+
5646+
identity {
5647+
type = "UserAssigned"
5648+
identity_ids = [azurerm_user_assigned_identity.test.id]
5649+
}
5650+
}
5651+
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
5652+
}

website/docs/r/app_service.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ The following arguments are supported:
8787

8888
* `https_only` - (Optional) Can the App Service only be accessed via HTTPS? Defaults to `false`.
8989

90+
* `key_vault_reference_identity_id` - (Optional) The User Assigned Identity Id used for looking up KeyVault secrets. The identity must be assigned to the application. [For more information see - Access vaults with a user-assigned identity](https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references#access-vaults-with-a-user-assigned-identity)
91+
9092
* `logs` - (Optional) A `logs` block as defined below.
9193

9294
* `storage_account` - (Optional) One or more `storage_account` blocks as defined below.

0 commit comments

Comments
 (0)