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

fix(google): do not check flow logs on proxy-only subnets #51

Merged
merged 3 commits into from
Nov 21, 2023
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
7 changes: 6 additions & 1 deletion internal/adapters/terraform/google/compute/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/aquasecurity/defsec/pkg/terraform"
)

const (
defaultSubnetPurpose = "PRIVATE_RFC_1918"
)

func adaptNetworks(modules terraform.Modules) (networks []compute.Network) {

networkMap := make(map[string]compute.Network)
Expand All @@ -28,12 +32,13 @@ func adaptNetworks(modules terraform.Modules) (networks []compute.Network) {
subnetwork := compute.SubNetwork{
Metadata: subnetworkBlock.GetMetadata(),
Name: subnetworkBlock.GetAttribute("name").AsStringValueOrDefault("", subnetworkBlock),
Purpose: subnetworkBlock.GetAttribute("purpose").AsStringValueOrDefault(defaultSubnetPurpose, subnetworkBlock),
EnableFlowLogs: defsecTypes.BoolDefault(false, subnetworkBlock.GetMetadata()),
}

// logging
if logConfigBlock := subnetworkBlock.GetBlock("log_config"); logConfigBlock.IsNotNil() {
subnetwork.EnableFlowLogs = defsecTypes.BoolExplicit(true, subnetworkBlock.GetBlock("log_config").GetMetadata())
subnetwork.EnableFlowLogs = defsecTypes.BoolExplicit(true, logConfigBlock.GetMetadata())
}

nwAttr := subnetworkBlock.GetAttribute("network")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func Test_adaptNetworks(t *testing.T) {
Metadata: defsecTypes.NewTestMetadata(),
Name: defsecTypes.String("test-subnetwork", defsecTypes.NewTestMetadata()),
EnableFlowLogs: defsecTypes.Bool(true, defsecTypes.NewTestMetadata()),
Purpose: defsecTypes.StringDefault("PRIVATE_RFC_1918", defsecTypes.NewTestMetadata()),
Copy link
Member

Choose a reason for hiding this comment

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

can we use the const?

Suggested change
Purpose: defsecTypes.StringDefault("PRIVATE_RFC_1918", defsecTypes.NewTestMetadata()),
Purpose: defsecTypes.StringDefault(defaultSubnetPurpose, defsecTypes.NewTestMetadata()),

Copy link
Member

Choose a reason for hiding this comment

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

Actually never mind, I see this is a test so we should have it fail in case we ever decide to change the const. Your implementation is correct.

},
},
},
Expand All @@ -84,7 +85,8 @@ func Test_adaptNetworks(t *testing.T) {
name: "defaults",
terraform: `
resource "google_compute_subnetwork" "example" {
network = google_compute_network.example.id
network = google_compute_network.example.id
purpose = "REGIONAL_MANAGED_PROXY"
}

resource "google_compute_network" "example" {
Expand All @@ -106,6 +108,7 @@ func Test_adaptNetworks(t *testing.T) {
Metadata: defsecTypes.NewTestMetadata(),
Name: defsecTypes.String("", defsecTypes.NewTestMetadata()),
EnableFlowLogs: defsecTypes.Bool(false, defsecTypes.NewTestMetadata()),
Purpose: defsecTypes.String("REGIONAL_MANAGED_PROXY", defsecTypes.NewTestMetadata()),
},
},
},
Expand Down