Skip to content

Commit

Permalink
Merge pull request #3893 from clearbank/function_app_auth_settings
Browse files Browse the repository at this point in the history
Add function_app auth_settings
  • Loading branch information
tombuildsstuff committed Jul 22, 2019
2 parents e1cfceb + f518d76 commit b8bd475
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
36 changes: 36 additions & 0 deletions azurerm/resource_arm_function_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ func resourceArmFunctionApp() *schema.Resource {
},
},

"auth_settings": azure.SchemaAppServiceAuthSettings(),

"site_credential": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -316,6 +318,17 @@ func resourceArmFunctionAppCreate(d *schema.ResourceData, meta interface{}) erro

d.SetId(*read.ID)

authSettingsRaw := d.Get("auth_settings").([]interface{})
authSettings := azure.ExpandAppServiceAuthSettings(authSettingsRaw)

auth := web.SiteAuthSettings{
ID: read.ID,
SiteAuthSettingsProperties: &authSettings}

if _, err := client.UpdateAuthSettings(ctx, resourceGroup, name, auth); err != nil {
return fmt.Errorf("Error updating auth settings for Function App %q (resource group %q): %+s", name, resourceGroup, err)
}

return resourceArmFunctionAppUpdate(d, meta)
}

Expand Down Expand Up @@ -396,6 +409,20 @@ func resourceArmFunctionAppUpdate(d *schema.ResourceData, meta interface{}) erro
}
}

if d.HasChange("auth_settings") {
authSettingsRaw := d.Get("auth_settings").([]interface{})
authSettingsProperties := azure.ExpandAppServiceAuthSettings(authSettingsRaw)
id := d.Id()
authSettings := web.SiteAuthSettings{
ID: &id,
SiteAuthSettingsProperties: &authSettingsProperties,
}

if _, err := client.UpdateAuthSettings(ctx, resGroup, name, authSettings); err != nil {
return fmt.Errorf("Error updating Authentication Settings for Function App %q: %+v", name, err)
}
}

if d.HasChange("connection_string") {
// update the ConnectionStrings
connectionStrings := expandFunctionAppConnectionStrings(d)
Expand Down Expand Up @@ -460,6 +487,10 @@ func resourceArmFunctionAppRead(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf("Error making Read request on AzureRM App Service Site Credential %q: %+v", name, err)
}
authResp, err := client.GetAuthSettings(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving the AuthSettings for Function App %q (Resource Group %q): %+v", name, resGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
Expand Down Expand Up @@ -514,6 +545,11 @@ func resourceArmFunctionAppRead(d *schema.ResourceData, meta interface{}) error
return err
}

authSettings := azure.FlattenAppServiceAuthSettings(authResp.SiteAuthSettingsProperties)
if err := d.Set("auth_settings", authSettings); err != nil {
return fmt.Errorf("Error setting `auth_settings`: %s", err)
}

siteCred := flattenFunctionAppSiteCredential(siteCredResp.UserProperties)
if err = d.Set("site_credential", siteCred); err != nil {
return err
Expand Down
102 changes: 102 additions & 0 deletions azurerm/resource_arm_function_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -596,6 +597,45 @@ func TestAccAzureRMFunctionApp_updateLogging(t *testing.T) {
})
}

func TestAccAzureRMFunctionApp_authSettings(t *testing.T) {
resourceName := "azurerm_function_app.test"
ri := tf.AccRandTimeInt()
rs := strings.ToLower(acctest.RandString(11))
tenantID := os.Getenv("ARM_TENANT_ID")
config := testAccAzureRMFunctionApp_authSettings(ri, rs, testLocation(), tenantID)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMFunctionAppDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.issuer", fmt.Sprintf("https://sts.windows.net/%s", tenantID)),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.runtime_version", "1.0"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.unauthenticated_client_action", "RedirectToLoginPage"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.token_refresh_extension_hours", "75"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.token_store_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.additional_login_params.test_key", "test_value"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.allowed_external_redirect_urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.allowed_external_redirect_urls.0", "https://terra.form"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.active_directory.0.client_id", "aadclientid"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.active_directory.0.client_secret", "aadsecret"),
resource.TestCheckResourceAttr(resourceName, "auth_settings.0.active_directory.0.allowed_audiences.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMFunctionAppDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).appServicesClient

Expand Down Expand Up @@ -1425,3 +1465,65 @@ resource "azurerm_function_app" "test" {
}
`, rInt, location, storage)
}

func testAccAzureRMFunctionApp_authSettings(rInt int, storage string, location string, tenantID string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%[1]d"
location = "%[2]s"
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%[3]s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_app_service_plan" "test" {
name = "acctestASP-%[1]d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_function_app" "test" {
name = "acctest-%[1]d-func"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"
storage_connection_string = "${azurerm_storage_account.test.primary_connection_string}"
auth_settings {
enabled = true
issuer = "https://sts.windows.net/%[4]s"
runtime_version = "1.0"
unauthenticated_client_action = "RedirectToLoginPage"
token_refresh_extension_hours = 75
token_store_enabled = true
additional_login_params = {
test_key = "test_value"
}
allowed_external_redirect_urls = [
"https://terra.form",
]
active_directory {
client_id = "aadclientid"
client_secret = "aadsecret"
allowed_audiences = [
"activedirectorytokenaudiences",
]
}
}
}
`, rInt, location, storage, tenantID)
}
75 changes: 75 additions & 0 deletions website/docs/r/function_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ The following arguments are supported:

* `app_settings` - (Optional) A key-value pair of App Settings.

* `auth_settings` - (Optional) A `auth_settings` block as defined below.

* `enable_builtin_logging` - (Optional) Should the built-in logging of this Function App be enabled? Defaults to `true`.

* `connection_string` - (Optional) An `connection_string` block as defined below.
Expand Down Expand Up @@ -144,6 +146,79 @@ The following arguments are supported:

* `type` - (Required) Specifies the identity type of the App Service. At this time the only allowed value is `SystemAssigned`.

---

A `auth_settings` block supports the following:

* `enabled` - (Required) Is Authentication enabled?

* `active_directory` - (Optional) A `active_directory` block as defined below.

* `additional_login_params` - (Optional) Login parameters to send to the OpenID Connect authorization endpoint when a user logs in. Each parameter must be in the form "key=value".

* `allowed_external_redirect_urls` - (Optional) External URLs that can be redirected to as part of logging in or logging out of the app.

* `default_provider` - (Optional) The default provider to use when multiple providers have been set up. Possible values are `AzureActiveDirectory`, `Facebook`, `Google`, `MicrosoftAccount` and `Twitter`.

~> **NOTE:** When using multiple providers, the default provider must be set for settings like `unauthenticated_client_action` to work.

* `facebook` - (Optional) A `facebook` block as defined below.

* `google` - (Optional) A `google` block as defined below.

* `issuer` - (Optional) Issuer URI. When using Azure Active Directory, this value is the URI of the directory tenant, e.g. https://sts.windows.net/{tenant-guid}/.

* `microsoft` - (Optional) A `microsoft` block as defined below.

* `runtime_version` - (Optional) The runtime version of the Authentication/Authorization module.

* `token_refresh_extension_hours` - (Optional) The number of hours after session token expiration that a session token can be used to call the token refresh API. Defaults to 72.

* `token_store_enabled` - (Optional) If enabled the module will durably store platform-specific security tokens that are obtained during login flows. Defaults to false.

* `twitter` - (Optional) A `twitter` block as defined below.

* `unauthenticated_client_action` - (Optional) The action to take when an unauthenticated client attempts to access the app. Possible values are `AllowAnonymous` and `RedirectToLoginPage`.

---

A `active_directory` block supports the following:

* `client_id` - (Required) The Client ID of this relying party application. Enables OpenIDConnection authentication with Azure Active Directory.

* `client_secret` - (Optional) The Client Secret of this relying party application. If no secret is provided, implicit flow will be used.

* `allowed_audiences` (Optional) Allowed audience values to consider when validating JWTs issued by Azure Active Directory.

---

A `facebook` block supports the following:

* `app_id` - (Required) The App ID of the Facebook app used for login

* `app_secret` - (Required) The App Secret of the Facebook app used for Facebook Login.

* `oauth_scopes` (Optional) The OAuth 2.0 scopes that will be requested as part of Facebook Login authentication. https://developers.facebook.com/docs/facebook-login

---

A `google` block supports the following:

* `client_id` - (Required) The OpenID Connect Client ID for the Google web application.

* `client_secret` - (Required) The client secret associated with the Google web application.

* `oauth_scopes` (Optional) The OAuth 2.0 scopes that will be requested as part of Google Sign-In authentication. https://developers.google.com/identity/sign-in/web/

---

A `microsoft` block supports the following:

* `client_id` - (Required) The OAuth 2.0 client ID that was created for the app used for authentication.

* `client_secret` - (Required) The OAuth 2.0 client secret that was created for the app used for authentication.

* `oauth_scopes` (Optional) The OAuth 2.0 scopes that will be requested as part of Microsoft Account authentication. https://msdn.microsoft.com/en-us/library/dn631845.aspx

## Attributes Reference

Expand Down

0 comments on commit b8bd475

Please sign in to comment.