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(55293): Add acct mapping handling gcp agentless #520

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ provider "lacework" {
resource "lacework_integration_aws_org_agentless_scanning" "example" {
name = var.name
query_text = var.query_text
scan_frequency = 24
scan_frequency = var.scan_frequency
scan_containers = true
scan_host_vulnerabilities = true
scan_multi_volume = false
Expand Down Expand Up @@ -50,6 +50,11 @@ variable "account_id" {
default = ""
}

variable "scan_frequency" {
type = number
default = 24
}

variable "bucket_arn" {
type = string
default = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
terraform {
required_providers {
lacework = {
source = "lacework/lacework"
}
}
}

provider "lacework" {
organization = true
}

variable "integration_name" {
type = string
default = "GCP Agentless Scanning Example"
}

variable "client_id" {
type = string
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

variable "client_email" {
type = string
default = "email@some-project-name.iam.gserviceaccount.com"
}

variable "private_key_id" {
type = string
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

variable "private_key" {
type = string
default = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

variable "token_uri" {
type = string
default = "https://oauth2.googleapis.com/token"
}

variable "integration_type" {
type = string
default = "PROJECT"
}

variable "project_id" {
type = string
default = "example-project-id"
}

variable "bucket_name" {
type = string
default = "storage bucket id"
}

variable "scanning-project-id" {
type = string
default = "scanning-project-id"
}

variable "query_text" {
type = string
default = ""
}

variable "filter_list" {
type = list(string)
default = ["proj1", "proj2"]
}

variable "org_account_mappings" {
type = list(object({
default_lacework_account = string
mapping = list(object({
lacework_account = string
gcp_projects = list(string)
}))
}))
default = []
description = "Mapping of GCP projects to Lacework accounts within a Lacework organization"
}

resource "lacework_integration_gcp_agentless_scanning" "org_example" {
name = var.integration_name
credentials {
client_id = var.client_id
client_email = var.client_email
private_key_id = var.private_key_id
private_key = var.private_key
token_uri = var.token_uri
}
resource_level = "ORGANIZATION"
resource_id = "294451184225"
scanning_project_id = "techally-test"
scan_frequency = 24
scan_containers = true
scan_host_vulnerabilities = true
scan_multi_volume = false
scan_stopped_instances = true
bucket_name = var.bucket_name
query_text = var.query_text
filter_list = var.filter_list

dynamic "org_account_mappings" {
for_each = var.org_account_mappings
content {
default_lacework_account = org_account_mappings.value["default_lacework_account"]

dynamic "mapping" {
for_each = org_account_mappings.value["mapping"]
content {
lacework_account = mapping.value["lacework_account"]
gcp_projects = mapping.value["gcp_projects"]
}
}
}
}
}

output "name" {
value = lacework_integration_gcp_agentless_scanning.org_example.name
}

output "client_id" {
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_id
}

output "client_email" {
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_email
}

output "bucket_name" {
value = lacework_integration_gcp_agentless_scanning.org_example.bucket_name
}

output "scanning_project_id" {
value = lacework_integration_gcp_agentless_scanning.org_example.scanning_project_id
}

output "scan_frequency" {
value = lacework_integration_gcp_agentless_scanning.org_example.scan_frequency
}

output "server_token" {
value = lacework_integration_gcp_agentless_scanning.org_example.server_token
}

output "org_account_mappings" {
value = lacework_integration_gcp_agentless_scanning.org_example.org_account_mappings
}
12 changes: 11 additions & 1 deletion integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func GetContainerRegisteryGar(result string) api.GcpGarIntegrationResponse {

func GetGcpAgentlessScanningResponse(result string) api.GcpSidekickIntegrationResponse {
id := GetIDFromTerraResults(result)

res, err := LwClient.V2.CloudAccounts.GetGcpSidekick(id)

if err != nil {
Expand All @@ -199,6 +198,17 @@ func GetGcpAgentlessScanningResponse(result string) api.GcpSidekickIntegrationRe
return res
}

func GetGcpAgentlessOrgScanningResponse(result string) api.GcpSidekickIntegrationResponse {
id := GetIDFromTerraResults(result)
res, err := LwOrgClient.V2.CloudAccounts.GetGcpSidekick(id)

if err != nil {
log.Fatalf("Unable to find integration id: %s\n Response: %v", id, res)
}

return res
}

func GetContainerRegisteryGcr(result string) api.GcpGcrIntegrationResponse {
id := GetIDFromTerraResults(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestIntegrationAwsOrgAgentlessScanningLog(t *testing.T) {
// Create new AWS Agentless Scanning Integration
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
createData := GetAwsAgentlessOrgScanningResponse(create)
println(create)
actualName := terraform.Output(t, terraformOptions, "name")
assert.Equal(
t,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package integration

import (
"fmt"
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

func TestIntegrationGcpAgentlessOrgScanningCreateAndUpdate(t *testing.T) {
gcreds, err := googleLoadDefaultCredentials()
integration_name := "GCP Org Agentless Scanning Example Integration Test"
update_integration_name := fmt.Sprintf("%s Updated", integration_name)
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples/resource_lacework_integration_gcp_org_agentless_scanning",
Vars: map[string]interface{}{
"integration_name": integration_name,
"client_id": gcreds.ClientID,
"client_email": gcreds.ClientEmail,
"private_key_id": gcreds.PrivateKeyID,
"bucket_name": "storage bucket id",
"org_account_mappings": []map[string]interface{}{
{
"default_lacework_account": "customerdemo",
"mapping": []map[string]interface{}{
{
"lacework_account": "tech-ally",
"gcp_projects": []string{"techally-test"},
},
},
},
},
},
EnvVars: map[string]string{
"TF_VAR_private_key": gcreds.PrivateKey,
"LW_API_TOKEN": LwApiToken,
},
})
defer terraform.Destroy(t, terraformOptions)

// Create new Google Agentless Scanning integration
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
createData := GetGcpAgentlessOrgScanningResponse(create)
assert.Equal(t, integration_name, createData.Data.Name)

// Update Gcp integration
terraformOptions.Vars = map[string]interface{}{
"integration_name": update_integration_name,
"client_id": gcreds.ClientID,
"client_email": gcreds.ClientEmail,
"private_key_id": gcreds.PrivateKeyID,
"bucket_name": "storage bucket id",
"org_account_mappings": []map[string]interface{}{
{
"default_lacework_account": "customerdemo",
"mapping": []map[string]interface{}{
{
"lacework_account": "abc",
"gcp_projects": []string{"techally-test"},
},
},
},
},
}

update := terraform.ApplyAndIdempotent(t, terraformOptions)
updateData := GetGcpAgentlessOrgScanningResponse(update)
assert.Equal(t, update_integration_name, updateData.Data.Name)
}
}
Loading