-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
google cloud sql instance network restrictions
- Loading branch information
Danielle
committed
Dec 29, 2023
1 parent
e61a21f
commit 2f38528
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package spacelift | ||
|
||
# Deny changes that expose Cloud SQL instance to 0.0.0.0/0 | ||
deny[msg] { | ||
change := input.terraform.resource_changes[_] | ||
change.type == "google_sql_database_instance" | ||
valid_action(change.change.actions) | ||
setting := change.change.after.settings[_] | ||
ip_config := setting.ip_configuration[_] | ||
auth_network := ip_config.authorized_networks[_] | ||
auth_network.value == "0.0.0.0/0" | ||
|
||
msg := sprintf("Cloud SQL instance '%s' is exposed to 0.0.0.0/0", [change.address]) | ||
} | ||
|
||
# Helper rule to check for valid actions | ||
valid_action(actions) { | ||
action := actions[_] | ||
action == "update" | ||
} | ||
|
||
# Helper rule to check for create action | ||
valid_action(actions) { | ||
action := actions[_] | ||
action == "create" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package spacelift | ||
|
||
# Test allowing a valid Cloud SQL instance configuration | ||
test_allow_valid_cloud_sql_instance { | ||
count(deny) == 0 with input as {"terraform": {"resource_changes": [{ | ||
"type": "google_sql_database_instance", | ||
"change": { | ||
"actions": ["create"], | ||
"after": {"settings": [{"ip_configuration": [{"authorized_networks": [{"value": "192.168.1.0/24"}]}]}]}, | ||
}, | ||
}]}} | ||
} | ||
|
||
# Test denying a Cloud SQL instance open to 0.0.0.0/0 | ||
test_deny_cloud_sql_instance_open_to_world { | ||
deny with input as {"terraform": {"resource_changes": [{ | ||
"type": "google_sql_database_instance", | ||
"change": { | ||
"actions": ["create"], | ||
"after": {"settings": [{"ip_configuration": [{"authorized_networks": [{"value": "0.0.0.0/0"}]}]}]}, | ||
}, | ||
}]}} | ||
} | ||
|
||
# Test non-applicability for a non-Cloud SQL resource | ||
test_allow_non_cloud_sql_resource { | ||
count(deny) == 0 with input as {"terraform": {"resource_changes": [{ | ||
"type": "google_compute_instance", | ||
"change": { | ||
"actions": ["create"], | ||
"after": {"settings": [{"ip_configuration": [{"authorized_networks": [{"value": "0.0.0.0/0"}]}]}]}, | ||
}, | ||
}]}} | ||
} |