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

Adding IP restrictions to Web App #1231

Merged
merged 6 commits into from
May 14, 2018
Merged
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
22 changes: 22 additions & 0 deletions azurerm/data_source_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ func dataSourceArmAppService() *schema.Resource {
Computed: true,
},

"ip_restriction": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some additional documentation for these fields?

Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"subnet_mask": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"https_only": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -284,6 +301,11 @@ func dataSourceArmAppServiceRead(d *schema.ResourceData, meta interface{}) error
return err
}

restrictions := flattenAppServiceIpRestrictions(configResp.SiteConfig.IPSecurityRestrictions)
if err := d.Set("ip_restriction", restrictions); err != nil {
return fmt.Errorf("Error setting `ip_restriction`: %s", err)
}

scm := flattenAppServiceSourceControl(scmResp.SiteSourceControlProperties)
if err := d.Set("source_control", scm); err != nil {
return err
Expand Down
32 changes: 32 additions & 0 deletions azurerm/data_source_app_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ func TestAccDataSourceAzureRMAppService_connectionString(t *testing.T) {
})
}

func TestAccDataSourceAzureRMAppService_ipRestriction(t *testing.T) {
dataSourceName := "data.azurerm_app_service.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAppService_ipRestriction(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ip_restriction.0.ip_address", "10.10.10.10"),
resource.TestCheckResourceAttr(dataSourceName, "ip_restriction.0.subnet_mask", "255.255.255.255"),
),
},
},
})
}

func TestAccDataSourceAzureRMAppService_http2Enabled(t *testing.T) {
dataSourceName := "data.azurerm_app_service.test"
rInt := acctest.RandInt()
Expand Down Expand Up @@ -217,6 +237,18 @@ data "azurerm_app_service" "test" {
`, config)
}

func testAccDataSourceAppService_ipRestriction(rInt int, location string) string {
config := testAccAzureRMAppService_oneIpRestriction(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_app_service" "test" {
name = "${azurerm_app_service.test.name}"
resource_group_name = "${azurerm_app_service.test.resource_group_name}"
}
`, config)
}

func testAccDataSourceAppService_http2Enabled(rInt int, location string) string {
config := testAccAzureRMAppService_http2Enabled(rInt, location)
return fmt.Sprintf(`
Expand Down
23 changes: 23 additions & 0 deletions azurerm/import_arm_app_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ func TestAccAzureRMAppService_importConnectionStrings(t *testing.T) {
})
}

func TestAccAzureRMAppService_importIpRestriction(t *testing.T) {
resourceName := "azurerm_app_service.test"

ri := acctest.RandInt()
config := testAccAzureRMAppService_oneIpRestriction(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMAppService_importDefaultDocuments(t *testing.T) {
resourceName := "azurerm_app_service.test"

Expand Down
63 changes: 62 additions & 1 deletion azurerm/resource_arm_app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ func resourceArmAppService() *schema.Resource {
Computed: true,
},

"ip_restriction": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_address": {
Type: schema.TypeString,
Required: true,
},
"subnet_mask": {
Type: schema.TypeString,
Optional: true,
Default: "255.255.255.255",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context from the Azure Portal: Specify an IP address range by providing a subnet mask (i.e. 255.255.255.0). If no mask is provided 255.255.255.255 is assumed.

},
},
},
},

"https_only": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -432,7 +451,7 @@ func resourceArmAppServiceUpdate(d *schema.ResourceData, meta interface{}) error
return err
}

if d.HasChange("site_config") {
if d.HasChange("site_config") || d.HasChange("ip_restriction") {
// update the main configuration
siteConfig := expandAppServiceSiteConfig(d)
siteConfigResource := web.SiteConfigResource{
Expand Down Expand Up @@ -594,6 +613,11 @@ func resourceArmAppServiceRead(d *schema.ResourceData, meta interface{}) error {
return err
}

restrictions := flattenAppServiceIpRestrictions(configResp.SiteConfig.IPSecurityRestrictions)
if err := d.Set("ip_restriction", restrictions); err != nil {
return fmt.Errorf("Error flattening `ip_restrictions`: %s", err)
}

scm := flattenAppServiceSourceControl(scmResp.SiteSourceControlProperties)
if err := d.Set("source_control", scm); err != nil {
return err
Expand Down Expand Up @@ -643,6 +667,9 @@ func expandAppServiceSiteConfig(d *schema.ResourceData) web.SiteConfig {
configs := d.Get("site_config").([]interface{})
siteConfig := web.SiteConfig{}

ipRestrictions := expandIpRestrictions(d)
siteConfig.IPSecurityRestrictions = ipRestrictions

if len(configs) == 0 {
return siteConfig
}
Expand Down Expand Up @@ -852,6 +879,25 @@ func expandAppServiceConnectionStrings(d *schema.ResourceData) map[string]*web.C
return output
}

func expandIpRestrictions(d *schema.ResourceData) *[]web.IPSecurityRestriction {
ipSecurityRestrictions := d.Get("ip_restriction").([]interface{})
restrictions := make([]web.IPSecurityRestriction, 0)

for _, ipSecurityRestriction := range ipSecurityRestrictions {
restriction := ipSecurityRestriction.(map[string]interface{})

ip_address := restriction["ip_address"].(string)
subnet_mask := restriction["subnet_mask"].(string)

restrictions = append(restrictions, web.IPSecurityRestriction{
IPAddress: &ip_address,
SubnetMask: &subnet_mask,
})
}

return &restrictions
}

func flattenAppServiceConnectionStrings(input map[string]*web.ConnStringValueTypePair) interface{} {
results := make([]interface{}, 0)

Expand All @@ -866,6 +912,21 @@ func flattenAppServiceConnectionStrings(input map[string]*web.ConnStringValueTyp
return results
}

func flattenAppServiceIpRestrictions(input *[]web.IPSecurityRestriction) interface{} {
results := make([]interface{}, 0)

if input != nil {
for _, v := range *input {
result := make(map[string]interface{}, 0)
result["ip_address"] = *v.IPAddress
result["subnet_mask"] = *v.SubnetMask
results = append(results, result)
}
}

return results
}

func flattenAppServiceAppSettings(input map[string]*string) map[string]string {
output := make(map[string]string, 0)
for k, v := range input {
Expand Down
120 changes: 120 additions & 0 deletions azurerm/resource_arm_app_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,54 @@ func TestAccAzureRMAppService_connectionStrings(t *testing.T) {
})
}

func TestAccAzureRMAppService_oneIpRestriction(t *testing.T) {
resourceName := "azurerm_app_service.test"
ri := acctest.RandInt()
config := testAccAzureRMAppService_oneIpRestriction(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.0.ip_address", "10.10.10.10"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.0.subnet_mask", "255.255.255.255"),
),
},
},
})
}

func TestAccAzureRMAppService_manyIpRestrictions(t *testing.T) {
resourceName := "azurerm_app_service.test"
ri := acctest.RandInt()
config := testAccAzureRMAppService_manyIpRestrictions(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAppServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAppServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.0.ip_address", "10.10.10.10"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.0.subnet_mask", "255.255.255.255"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.1.ip_address", "20.20.20.0"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.1.subnet_mask", "255.255.255.0"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.2.ip_address", "30.30.0.0"),
resource.TestCheckResourceAttr(resourceName, "ip_restriction.2.subnet_mask", "255.255.0.0"),
),
},
},
})
}

func TestAccAzureRMAppService_defaultDocuments(t *testing.T) {
resourceName := "azurerm_app_service.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -1153,6 +1201,78 @@ resource "azurerm_app_service" "test" {
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAppService_oneIpRestriction(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_app_service_plan" "test" {
name = "acctestASP-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

sku {
tier = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"

ip_restriction {
ip_address = "10.10.10.10"
}
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAppService_manyIpRestrictions(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_app_service_plan" "test" {
name = "acctestASP-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

sku {
tier = "Standard"
size = "S1"
}
}

resource "azurerm_app_service" "test" {
name = "acctestAS-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
app_service_plan_id = "${azurerm_app_service_plan.test.id}"

ip_restriction {
ip_address = "10.10.10.10"
}

ip_restriction {
ip_address = "20.20.20.0"
subnet_mask = "255.255.255.0"
}

ip_restriction {
ip_address = "30.30.0.0"
subnet_mask = "255.255.0.0"
}
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAppService_defaultDocuments(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
12 changes: 11 additions & 1 deletion website/docs/d/app_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ output "app_service_id" {

* `https_only` - Can the App Service only be accessed via HTTPS?

* `site_config` - A `site_config` object as defined below.
* `ip_restriction` - A `ip_restriction` block as defined below.

* `site_config` - A `site_config` block as defined below.

* `tags` - A mapping of tags to assign to the resource.

Expand All @@ -63,6 +65,14 @@ output "app_service_id" {

---

`ip_restriction` exports the following:

* `ip_address` - The IP Address used for this IP Restriction.

* `subnet_mask` - The Subnet mask used for this IP Restriction.

---

`site_config` supports the following:

* `always_on` - Is the app be loaded at all times?
Expand Down
Loading