Skip to content

Commit 788f2b8

Browse files
make one of the source_ parameters required for ingress firewall (#5253) (#10369)
* make one of the source_ parameters required for ingress firewall * go back to checking not egress in customize diff * add to upgrade guide * update upgrade guide toc Signed-off-by: Modular Magician <[email protected]>
1 parent fee32cd commit 788f2b8

6 files changed

+50
-10
lines changed

.changelog/5253.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
compute: required one of `source_tags`, `source_ranges` or `source_service_accounts` on INGRESS `google_compute_firewall` resources
3+
```

google/resource_compute_firewall.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
2829
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2930
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
3031
)
@@ -76,6 +77,28 @@ func resourceComputeFirewallEnableLoggingCustomizeDiff(_ context.Context, diff *
7677
return nil
7778
}
7879

80+
// Per https://github.com/hashicorp/terraform-provider-google/issues/2924
81+
// Make one of the source_ parameters Required in ingress google_compute_firewall
82+
func resourceComputeFirewallSourceFieldsCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
83+
direction := diff.Get("direction").(string)
84+
85+
if direction != "EGRESS" {
86+
_, tagsOk := diff.GetOk("source_tags")
87+
_, rangesOk := diff.GetOk("source_ranges")
88+
_, sasOk := diff.GetOk("source_service_accounts")
89+
90+
_, tagsExist := diff.GetOkExists("source_tags")
91+
// ranges is computed, but this is what we're trying to avoid, so we're not going to check this
92+
_, sasExist := diff.GetOkExists("source_service_accounts")
93+
94+
if !tagsOk && !rangesOk && !sasOk && !tagsExist && !sasExist {
95+
return fmt.Errorf("one of source_tags, source_ranges, or source_service_accounts must be defined")
96+
}
97+
}
98+
99+
return nil
100+
}
101+
79102
func resourceComputeFirewall() *schema.Resource {
80103
return &schema.Resource{
81104
Create: resourceComputeFirewallCreate,
@@ -95,7 +118,10 @@ func resourceComputeFirewall() *schema.Resource {
95118

96119
SchemaVersion: 1,
97120
MigrateState: resourceComputeFirewallMigrateState,
98-
CustomizeDiff: resourceComputeFirewallEnableLoggingCustomizeDiff,
121+
CustomizeDiff: customdiff.All(
122+
resourceComputeFirewallEnableLoggingCustomizeDiff,
123+
resourceComputeFirewallSourceFieldsCustomizeDiff,
124+
),
99125

100126
Schema: map[string]*schema.Schema{
101127
"name": {
@@ -164,7 +190,8 @@ must be expressed in CIDR format. Only IPv4 is supported.`,
164190
Description: `Direction of traffic to which this firewall applies; default is
165191
INGRESS. Note: For INGRESS traffic, it is NOT supported to specify
166192
destinationRanges; For EGRESS traffic, it is NOT supported to specify
167-
sourceRanges OR sourceTags. Possible values: ["INGRESS", "EGRESS"]`,
193+
'source_ranges' OR 'source_tags'. For INGRESS traffic, one of 'source_ranges',
194+
'source_tags' or 'source_service_accounts' is required. Possible values: ["INGRESS", "EGRESS"]`,
168195
},
169196
"disabled": {
170197
Type: schema.TypeBool,

google/resource_compute_firewall_generated_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ resource "google_compute_firewall" "rules" {
110110
protocol = "tcp"
111111
ports = ["80", "8080", "1000-2000"]
112112
}
113+
114+
source_tags = ["foo"]
113115
target_tags = ["web"]
114116
}
115117
`, context)

google/resource_compute_firewall_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -81,12 +82,8 @@ func TestAccComputeFirewall_noSource(t *testing.T) {
8182
CheckDestroy: testAccCheckComputeFirewallDestroyProducer(t),
8283
Steps: []resource.TestStep{
8384
{
84-
Config: testAccComputeFirewall_noSource(networkName, firewallName),
85-
},
86-
{
87-
ResourceName: "google_compute_firewall.foobar",
88-
ImportState: true,
89-
ImportStateVerify: true,
85+
Config: testAccComputeFirewall_noSource(networkName, firewallName),
86+
ExpectError: regexp.MustCompile("one of source_tags, source_ranges, or source_service_accounts must be defined"),
9087
},
9188
},
9289
})

website/docs/guides/version_4_upgrade.html.markdown

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ description: |-
1515
- [Runtime Configurator (`runtimeconfig`) resources have been removed from the GA provider](#runtime-configurator-runtimeconfig-resources-have-been-removed-from-the-ga-provider)
1616
- [Datasource: `google_product_resource`](#datasource-google_product_resource)
1717
- [Datasource-level change example](#datasource-level-change-example)
18+
- [Resource: `google_compute_firewall`](#resource-google_compute_firewall)
19+
- [One of `source_tags`, `source_ranges` or `source_service_accounts` are required on INGRESS firewalls](#one-of-source_tags-source_ranges-or-source_service_accounts-are-required-on-ingress-firewalls)
1820
- [Resource: `google_compute_instance_group_manager`](#resource-google_compute_instance_group_manager)
1921
- [`update_policy.min_ready_sec` is removed from the GA provider](#update_policymin_ready_sec-is-removed-from-the-ga-provider)
2022
- [Resource: `google_compute_region_instance_group_manager`](#resource-google_compute_region_instance_group_manager)
@@ -162,10 +164,15 @@ resource "google_runtimeconfig_config" "my-runtime-config" {
162164

163165
Description of the change and how users should adjust their configuration (if needed).
164166

167+
## Resource: `google_compute_firewall`
168+
169+
### One of `source_tags`, `source_ranges` or `source_service_accounts` are required on INGRESS firewalls
170+
171+
Previously, if all of these fields were left empty, the firewall defaulted to allowing traffic from 0.0.0.0/0, which is a suboptimal default.
172+
165173
## Resource: `google_compute_instance_group_manager`
166174

167175
### `update_policy.min_ready_sec` is removed from the GA provider
168-
169176
This field was incorrectly included in the GA `google` provider in past releases.
170177
In order to continue to use the feature, add `provider = google-beta` to your
171178
resource definition.
@@ -225,3 +232,4 @@ resource "google_container_cluster" "cluster" {
225232
This field was incorrectly included in the GA `google` provider in past releases.
226233
In order to continue to use the feature, add `provider = google-beta` to your
227234
resource definition.
235+

website/docs/r/compute_firewall.html.markdown

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ resource "google_compute_firewall" "rules" {
8686
protocol = "tcp"
8787
ports = ["80", "8080", "1000-2000"]
8888
}
89+
90+
source_tags = ["foo"]
8991
target_tags = ["web"]
9092
}
9193
```
@@ -142,7 +144,8 @@ The following arguments are supported:
142144
Direction of traffic to which this firewall applies; default is
143145
INGRESS. Note: For INGRESS traffic, it is NOT supported to specify
144146
destinationRanges; For EGRESS traffic, it is NOT supported to specify
145-
sourceRanges OR sourceTags.
147+
`source_ranges` OR `source_tags`. For INGRESS traffic, one of `source_ranges`,
148+
`source_tags` or `source_service_accounts` is required.
146149
Possible values are `INGRESS` and `EGRESS`.
147150

148151
* `disabled` -

0 commit comments

Comments
 (0)