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

feat: add option to clear rules from default VPC security group and ACL #516

Merged
merged 25 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bd9d99b
feat: add clean default sec group option
toddgiguere Apr 18, 2023
d59a84a
fix: use refresh token for sg cleanup script
toddgiguere Apr 19, 2023
cca7177
feat: add option and resource to remove all default VPC ACL rules
toddgiguere Apr 19, 2023
eff5b11
fix: add sg and acl clean booleans to submodule and example
toddgiguere Apr 19, 2023
8e971ae
Merge branch 'main' into fix-default-acl-sg
toddgiguere Apr 20, 2023
b2a76c8
fix: pre-commit issues
toddgiguere Apr 20, 2023
67834f0
Merge branch 'main' into fix-default-acl-sg
ocofaigh Apr 21, 2023
e3edfc1
fix: python scripts now use all native imports
toddgiguere Apr 21, 2023
a4c8ab7
refactor: pre-commit changes and formatting
toddgiguere Apr 21, 2023
ecaafb3
Merge branch 'main' into fix-default-acl-sg
ocofaigh Apr 24, 2023
d99eb3d
fix: private endpoint support and python common module
toddgiguere Apr 24, 2023
043322f
refactor: removed unintended python files
toddgiguere Apr 24, 2023
3576457
refactor: fixed pre-commit python issues
toddgiguere Apr 24, 2023
0eca7cb
feat: added bash/cli versions of rule removal
toddgiguere Apr 24, 2023
1db71d3
refactor: pre-commit fixes
toddgiguere Apr 24, 2023
96dafea
feat: convert clean rule scripts to bash and cli
toddgiguere Apr 26, 2023
118d156
fix: shellcheck fixes and other pre-commit issues
toddgiguere Apr 26, 2023
315ac89
fix: add resource group to CLI login
toddgiguere Apr 26, 2023
68314d3
fix: null_resource CLI scripts now use unique config dir
toddgiguere Apr 27, 2023
0deadb8
refactor: pre-commit fixes
toddgiguere Apr 27, 2023
ada4fbc
docs: added more verbose variable descriptions
toddgiguere Apr 28, 2023
a76c688
fix: cli remove temp config on exit/error
toddgiguere Apr 28, 2023
5cb28b1
refactor: pre-commit fixes
toddgiguere Apr 28, 2023
9470747
Merge branch 'main' into fix-default-acl-sg
vburckhardt Apr 29, 2023
664753c
Merge branch 'main' into fix-default-acl-sg
toddgiguere May 1, 2023
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ You need the following permissions to run this module.
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.52.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1 |

## Modules

Expand All @@ -101,6 +102,9 @@ You need the following permissions to run this module.
| [ibm_is_vpc_address_prefix.subnet_prefix](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_address_prefix) | resource |
| [ibm_is_vpc_routing_table.route_table](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_routing_table) | resource |
| [ibm_is_vpc_routing_table_route.routing_table_routes](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/is_vpc_routing_table_route) | resource |
| [null_resource.clean_default_acl](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.clean_default_security_group](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [ibm_iam_auth_token.tokendata](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/iam_auth_token) | data source |
| [ibm_is_vpc_address_prefixes.get_address_prefixes](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_vpc_address_prefixes) | data source |

## Inputs
Expand All @@ -109,6 +113,8 @@ You need the following permissions to run this module.
|------|-------------|------|---------|:--------:|
| <a name="input_address_prefixes"></a> [address\_prefixes](#input\_address\_prefixes) | OPTIONAL - IP range that will be defined for the VPC for a certain location. Use only with manual address prefixes | <pre>object({<br> zone-1 = optional(list(string))<br> zone-2 = optional(list(string))<br> zone-3 = optional(list(string))<br> })</pre> | <pre>{<br> "zone-1": null,<br> "zone-2": null,<br> "zone-3": null<br>}</pre> | no |
| <a name="input_classic_access"></a> [classic\_access](#input\_classic\_access) | OPTIONAL - Classic Access to the VPC | `bool` | `false` | no |
| <a name="input_clean_default_acl"></a> [clean\_default\_acl](#input\_clean\_default\_acl) | Remove all rules from the default VPC ACL (less permissive) | `bool` | `false` | no |
| <a name="input_clean_default_security_group"></a> [clean\_default\_security\_group](#input\_clean\_default\_security\_group) | Remove all rules from the default VPC security group (less permissive) | `bool` | `false` | no |
| <a name="input_create_authorization_policy_vpc_to_cos"></a> [create\_authorization\_policy\_vpc\_to\_cos](#input\_create\_authorization\_policy\_vpc\_to\_cos) | Create authorisation policy for VPC to access COS. Set as false if authorization policy exists already | `bool` | `false` | no |
| <a name="input_default_network_acl_name"></a> [default\_network\_acl\_name](#input\_default\_network\_acl\_name) | OPTIONAL - Name of the Default ACL. If null, a name will be automatically generated | `string` | `null` | no |
| <a name="input_default_routing_table_name"></a> [default\_routing\_table\_name](#input\_default\_routing\_table\_name) | OPTIONAL - Name of the Default Routing Table. If null, a name will be automatically generated | `string` | `null` | no |
Expand Down
14 changes: 9 additions & 5 deletions examples/landing_zone/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ module "workload_vpc" {
create_authorization_policy_vpc_to_cos = var.create_authorization_policy_vpc_to_cos
existing_cos_instance_guid = module.cos_bucket[0].cos_instance_guid
existing_cos_bucket_name = module.cos_bucket[0].bucket_name[0]
clean_default_security_group = true
clean_default_acl = true
}


module "management_vpc" {
source = "../../landing-zone-submodule/management-vpc/"
resource_group_id = module.resource_group.resource_group_id
region = var.region
prefix = var.prefix
tags = var.resource_tags
source = "../../landing-zone-submodule/management-vpc/"
resource_group_id = module.resource_group.resource_group_id
region = var.region
prefix = var.prefix
tags = var.resource_tags
clean_default_security_group = true
clean_default_acl = true
}


Expand Down
2 changes: 2 additions & 0 deletions landing-zone-submodule/management-vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ No resources.
|------|-------------|------|---------|:--------:|
| <a name="input_address_prefixes"></a> [address\_prefixes](#input\_address\_prefixes) | Use `address_prefixes` only if `use_manual_address_prefixes` is true otherwise prefixes will not be created. Use only if you need to manage prefixes manually. | <pre>object({<br> zone-1 = optional(list(string))<br> zone-2 = optional(list(string))<br> zone-3 = optional(list(string))<br> })</pre> | `null` | no |
| <a name="input_classic_access"></a> [classic\_access](#input\_classic\_access) | Optionally allow VPC to access classic infrastructure network | `bool` | `null` | no |
| <a name="input_clean_default_acl"></a> [clean\_default\_acl](#input\_clean\_default\_acl) | Remove all rules from the default VPC ACL (less permissive) | `bool` | `false` | no |
| <a name="input_clean_default_security_group"></a> [clean\_default\_security\_group](#input\_clean\_default\_security\_group) | Remove all rules from the default VPC security group (less permissive) | `bool` | `false` | no |
| <a name="input_create_authorization_policy_vpc_to_cos"></a> [create\_authorization\_policy\_vpc\_to\_cos](#input\_create\_authorization\_policy\_vpc\_to\_cos) | Set it to true if authorization policy is required for VPC to access COS | `bool` | `false` | no |
| <a name="input_default_network_acl_name"></a> [default\_network\_acl\_name](#input\_default\_network\_acl\_name) | Override default ACL name | `string` | `null` | no |
| <a name="input_default_routing_table_name"></a> [default\_routing\_table\_name](#input\_default\_routing\_table\_name) | Override default VPC routing table name | `string` | `null` | no |
Expand Down
2 changes: 2 additions & 0 deletions landing-zone-submodule/management-vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ module "management_vpc" {
create_authorization_policy_vpc_to_cos = var.create_authorization_policy_vpc_to_cos
existing_cos_instance_guid = var.existing_cos_instance_guid
existing_storage_bucket_name = var.existing_cos_bucket_name
clean_default_security_group = var.clean_default_security_group
clean_default_acl = var.clean_default_acl
}
12 changes: 12 additions & 0 deletions landing-zone-submodule/management-vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ variable "default_security_group_rules" {
default = []
}

variable "clean_default_security_group" {
description = "Remove all rules from the default VPC security group (less permissive)"
type = bool
default = false
}

variable "clean_default_acl" {
description = "Remove all rules from the default VPC ACL (less permissive)"
type = bool
default = false
}

variable "address_prefixes" {
description = "Use `address_prefixes` only if `use_manual_address_prefixes` is true otherwise prefixes will not be created. Use only if you need to manage prefixes manually."
type = object({
Expand Down
2 changes: 2 additions & 0 deletions landing-zone-submodule/workload-vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ No resources.
|------|-------------|------|---------|:--------:|
| <a name="input_address_prefixes"></a> [address\_prefixes](#input\_address\_prefixes) | Use `address_prefixes` only if `use_manual_address_prefixes` is true otherwise prefixes will not be created. Use only if you need to manage prefixes manually. | <pre>object({<br> zone-1 = optional(list(string))<br> zone-2 = optional(list(string))<br> zone-3 = optional(list(string))<br> })</pre> | `null` | no |
| <a name="input_classic_access"></a> [classic\_access](#input\_classic\_access) | Optionally allow VPC to access classic infrastructure network | `bool` | `null` | no |
| <a name="input_clean_default_acl"></a> [clean\_default\_acl](#input\_clean\_default\_acl) | Remove all rules from the default VPC ACL (less permissive) | `bool` | `false` | no |
| <a name="input_clean_default_security_group"></a> [clean\_default\_security\_group](#input\_clean\_default\_security\_group) | Remove all rules from the default VPC security group (less permissive) | `bool` | `false` | no |
| <a name="input_create_authorization_policy_vpc_to_cos"></a> [create\_authorization\_policy\_vpc\_to\_cos](#input\_create\_authorization\_policy\_vpc\_to\_cos) | Set it to true if authorization policy is required for VPC to access COS | `bool` | `false` | no |
| <a name="input_default_network_acl_name"></a> [default\_network\_acl\_name](#input\_default\_network\_acl\_name) | Override default ACL name | `string` | `null` | no |
| <a name="input_default_routing_table_name"></a> [default\_routing\_table\_name](#input\_default\_routing\_table\_name) | Override default VPC routing table name | `string` | `null` | no |
Expand Down
2 changes: 2 additions & 0 deletions landing-zone-submodule/workload-vpc/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ module "workload_vpc" {
create_authorization_policy_vpc_to_cos = var.create_authorization_policy_vpc_to_cos
existing_cos_instance_guid = var.existing_cos_instance_guid
existing_storage_bucket_name = var.existing_cos_bucket_name
clean_default_security_group = var.clean_default_security_group
clean_default_acl = var.clean_default_acl
}
12 changes: 12 additions & 0 deletions landing-zone-submodule/workload-vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ variable "default_security_group_rules" {
default = []
}

variable "clean_default_security_group" {
description = "Remove all rules from the default VPC security group (less permissive)"
type = bool
default = false
}

variable "clean_default_acl" {
description = "Remove all rules from the default VPC ACL (less permissive)"
type = bool
default = false
}

variable "address_prefixes" {
description = "Use `address_prefixes` only if `use_manual_address_prefixes` is true otherwise prefixes will not be created. Use only if you need to manage prefixes manually."
type = object({
Expand Down
46 changes: 46 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,49 @@ resource "ibm_is_flow_log" "flow_logs" {
}

##############################################################################

##############################################################################
# Clean default network objects if required
##############################################################################

locals {
# only get auth tokens if needed
auth_token_required = (var.clean_default_security_group || var.clean_default_acl) ? true : false
}

# valid refresh token from provider is needed for scripts
data "ibm_iam_auth_token" "tokendata" {
count = local.auth_token_required ? 1 : 0
}

resource "null_resource" "clean_default_security_group" {
count = (var.clean_default_security_group) ? 1 : 0
# only clean if default security group changes
triggers = {
security_group_id = ibm_is_vpc.vpc.default_security_group
}

provisioner "local-exec" {
command = "/usr/bin/env python3 ${path.module}/scripts/fix_security_group.py --ibmApiRefreshTokenEnvName \"IBMCLOUD_REFRESH_TOKEN\" --security_group_id \"${ibm_is_vpc.vpc.default_security_group}\" --region \"${var.region}\""
interpreter = ["/bin/bash", "-c"]
environment = {
IBMCLOUD_REFRESH_TOKEN = data.ibm_iam_auth_token.tokendata[0].iam_refresh_token
}
}
}

resource "null_resource" "clean_default_acl" {
count = (var.clean_default_acl) ? 1 : 0
# only clean if default acl changes
triggers = {
acl_id = ibm_is_vpc.vpc.default_network_acl
}

provisioner "local-exec" {
command = "/usr/bin/env python3 ${path.module}/scripts/fix_access_control_list.py --ibmApiRefreshTokenEnvName \"IBMCLOUD_REFRESH_TOKEN\" --acl_id \"${ibm_is_vpc.vpc.default_network_acl}\" --region \"${var.region}\""
interpreter = ["/bin/bash", "-c"]
environment = {
IBMCLOUD_REFRESH_TOKEN = data.ibm_iam_auth_token.tokendata[0].iam_refresh_token
}
}
}
86 changes: 80 additions & 6 deletions module-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,40 @@
},
"immutable": true
},
"clean_default_acl": {
"name": "clean_default_acl",
"type": "bool",
"description": "Remove all rules from the default VPC ACL (less permissive)",
"default": false,
"source": [
"null_resource.clean_default_acl.count"
],
"pos": {
"filename": "variables.tf",
"line": 372
}
},
"clean_default_security_group": {
"name": "clean_default_security_group",
"type": "bool",
"description": "Remove all rules from the default VPC security group (less permissive)",
"default": false,
"source": [
"null_resource.clean_default_security_group.count"
],
"pos": {
"filename": "variables.tf",
"line": 366
}
},
"create_authorization_policy_vpc_to_cos": {
"name": "create_authorization_policy_vpc_to_cos",
"type": "bool",
"description": "Create authorisation policy for VPC to access COS. Set as false if authorization policy exists already",
"default": false,
"pos": {
"filename": "variables.tf",
"line": 408
"line": 419
}
},
"default_network_acl_name": {
Expand Down Expand Up @@ -102,7 +128,7 @@
],
"pos": {
"filename": "variables.tf",
"line": 402
"line": 413
}
},
"existing_cos_instance_guid": {
Expand All @@ -114,7 +140,7 @@
],
"pos": {
"filename": "variables.tf",
"line": 414
"line": 425
},
"immutable": true,
"computed": true
Expand All @@ -129,7 +155,7 @@
],
"pos": {
"filename": "variables.tf",
"line": 420
"line": 431
},
"immutable": true
},
Expand All @@ -143,7 +169,7 @@
],
"pos": {
"filename": "variables.tf",
"line": 426
"line": 437
}
},
"name": {
Expand Down Expand Up @@ -257,7 +283,7 @@
],
"pos": {
"filename": "variables.tf",
"line": 374
"line": 385
}
},
"security_group_rules": {
Expand Down Expand Up @@ -468,6 +494,12 @@
"version_constraints": [
"\u003e= 1.52.0"
]
},
"null": {
"source": "hashicorp/null",
"version_constraints": [
"\u003e= 3.2.1"
]
}
},
"managed_resources": {
Expand Down Expand Up @@ -643,9 +675,51 @@
"filename": "main.tf",
"line": 59
}
},
"null_resource.clean_default_acl": {
"mode": "managed",
"type": "null_resource",
"name": "clean_default_acl",
"attributes": {
"count": "clean_default_acl"
},
"provider": {
"name": "null"
},
"pos": {
"filename": "main.tf",
"line": 160
}
},
"null_resource.clean_default_security_group": {
"mode": "managed",
"type": "null_resource",
"name": "clean_default_security_group",
"attributes": {
"count": "clean_default_security_group"
},
"provider": {
"name": "null"
},
"pos": {
"filename": "main.tf",
"line": 144
}
}
},
"data_resources": {
"data.ibm_iam_auth_token.tokendata": {
"mode": "data",
"type": "ibm_iam_auth_token",
"name": "tokendata",
"provider": {
"name": "ibm"
},
"pos": {
"filename": "main.tf",
"line": 140
}
},
"data.ibm_is_vpc_address_prefixes.get_address_prefixes": {
"mode": "data",
"type": "ibm_is_vpc_address_prefixes",
Expand Down
Loading