Skip to content

Commit 89bd331

Browse files
committed
test: add testing for gcp org account mapping
1 parent 95de145 commit 89bd331

File tree

7 files changed

+254
-35
lines changed

7 files changed

+254
-35
lines changed

examples/resource_lacework_integration_aws_org_agentless_scanning/main.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ provider "lacework" {
1313
resource "lacework_integration_aws_org_agentless_scanning" "example" {
1414
name = var.name
1515
query_text = var.query_text
16-
scan_frequency = 24
16+
scan_frequency = var.scan_frequency
1717
scan_containers = true
1818
scan_host_vulnerabilities = true
1919
scan_multi_volume = false
@@ -50,6 +50,11 @@ variable "account_id" {
5050
default = ""
5151
}
5252

53+
variable "scan_frequency" {
54+
type = number
55+
default = 24
56+
}
57+
5358
variable "bucket_arn" {
5459
type = string
5560
default = ""
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
terraform {
2+
required_providers {
3+
lacework = {
4+
source = "lacework/lacework"
5+
}
6+
}
7+
}
8+
9+
provider "lacework" {
10+
organization = true
11+
}
12+
13+
variable "name" {
14+
type = string
15+
default = "GCP Agentless Scanning org_example"
16+
}
17+
18+
variable "client_id" {
19+
type = string
20+
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21+
}
22+
23+
variable "client_email" {
24+
type = string
25+
default = "[email protected]"
26+
}
27+
28+
variable "private_key_id" {
29+
type = string
30+
default = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
31+
}
32+
33+
variable "private_key" {
34+
type = string
35+
default = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
36+
}
37+
38+
variable "token_uri" {
39+
type = string
40+
default = "https://oauth2.googleapis.com/token"
41+
}
42+
43+
variable "integration_type" {
44+
type = string
45+
default = "PROJECT"
46+
}
47+
48+
variable "project_id" {
49+
type = string
50+
default = "org-example-project-id"
51+
}
52+
53+
variable "bucket_name" {
54+
type = string
55+
default = "storage bucket id"
56+
}
57+
58+
variable "scanning_project_id" {
59+
type = string
60+
default = "scanning-project-id"
61+
}
62+
63+
variable "query_text" {
64+
type = string
65+
default = ""
66+
}
67+
68+
variable "filter_list" {
69+
type = list(string)
70+
default = ["proj1", "proj2"]
71+
}
72+
73+
variable "scan_frequency" {
74+
type = number
75+
default = 24
76+
}
77+
78+
variable "org_account_mappings" {
79+
type = list(object({
80+
default_lacework_account = string
81+
mapping = list(object({
82+
lacework_account = string
83+
gcp_projects = list(string)
84+
}))
85+
}))
86+
default = []
87+
description = "Mapping of GCP projects to Lacework accounts within a Lacework organization"
88+
}
89+
90+
resource "lacework_integration_gcp_agentless_scanning" "org_example" {
91+
name = var.name
92+
credentials {
93+
client_id = var.client_id
94+
client_email = var.client_email
95+
private_key_id = var.private_key_id
96+
private_key = var.private_key
97+
token_uri = var.token_uri
98+
}
99+
resource_level = "ORGANIZATION"
100+
resource_id = "techally-test"
101+
bucket_name = var.bucket_name
102+
scanning_project_id = "gcp-lw-scanner"
103+
scan_frequency = var.scan_frequency
104+
scan_containers = true
105+
scan_host_vulnerabilities = true
106+
scan_multi_volume = false
107+
scan_stopped_instances = true
108+
query_text = var.query_text
109+
filter_list = var.filter_list
110+
111+
dynamic "org_account_mappings" {
112+
for_each = var.org_account_mappings
113+
content {
114+
default_lacework_account = org_account_mappings.value["default_lacework_account"]
115+
116+
dynamic "mapping" {
117+
for_each = org_account_mappings.value["mapping"]
118+
content {
119+
lacework_account = mapping.value["lacework_account"]
120+
gcp_projects = mapping.value["gcp_projects"]
121+
}
122+
}
123+
}
124+
}
125+
}
126+
127+
output "name" {
128+
value = lacework_integration_gcp_agentless_scanning.org_example.name
129+
}
130+
131+
output "client_id" {
132+
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_id
133+
}
134+
135+
output "client_email" {
136+
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_email
137+
}
138+
139+
output "bucket_name" {
140+
value = lacework_integration_gcp_agentless_scanning.org_example.bucket_name
141+
}
142+
143+
output "scanning_project_id" {
144+
value = lacework_integration_gcp_agentless_scanning.org_example.scanning_project_id
145+
}
146+
147+
output "scan_frequency" {
148+
value = lacework_integration_gcp_agentless_scanning.org_example.scan_frequency
149+
}
150+
151+
output "server_token" {
152+
value = lacework_integration_gcp_agentless_scanning.org_example.server_token
153+
}
154+
155+
output "org_account_mappings" {
156+
value = lacework_integration_gcp_agentless_scanning.org_example.org_account_mappings
157+
}

integration/resource_lacework_integration_aws_org_agentless_scanning_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func TestIntegrationAwsOrgAgentlessScanningLog(t *testing.T) {
4040
// Create new AWS Agentless Scanning Integration
4141
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
4242
createData := GetAwsAgentlessOrgScanningResponse(create)
43-
println(create)
4443
actualName := terraform.Output(t, terraformOptions, "name")
4544
assert.Equal(
4645
t,

integration/resource_lacework_integration_gcp_agentless_scanning_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,66 @@ func TestIntegrationGcpAgentlessScanningCreate(t *testing.T) {
4747
assert.Equal(t, update_integration_name, updateData.Data.Name)
4848
}
4949
}
50+
51+
func TestIntegrationGcpAgentlessOrgScanningCreate(t *testing.T) {
52+
gcreds, err := googleLoadDefaultCredentials()
53+
integration_name := "GCP Agentless Scanning Example Integration Test"
54+
update_integration_name := fmt.Sprintf("%s Updated", integration_name)
55+
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
56+
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
57+
TerraformDir: "../examples/resource_lacework_integration_gcp_org_agentless_scanning",
58+
Vars: map[string]interface{}{
59+
"name": integration_name,
60+
"client_id": gcreds.ClientID,
61+
"client_email": gcreds.ClientEmail,
62+
"private_key_id": gcreds.PrivateKeyID,
63+
"bucket_name": "storage bucket id",
64+
"org_account_mappings": []map[string]interface{}{
65+
{
66+
"default_lacework_account": "customerdemo",
67+
"mapping": []map[string]interface{}{
68+
{
69+
"lacework_account": "abc",
70+
"gcp_projects": []string{"lw-scanner-5"},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
EnvVars: map[string]string{
77+
"TF_VAR_private_key": gcreds.PrivateKey,
78+
"LW_API_TOKEN": LwApiToken,
79+
},
80+
})
81+
defer terraform.Destroy(t, terraformOptions)
82+
83+
// Create new Google Agentless Scanning integration
84+
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
85+
createData := GetGcpAgentlessScanningResponse(create)
86+
assert.Equal(t, integration_name, createData.Data.Name)
87+
88+
// Update Gcp integration
89+
terraformOptions.Vars = map[string]interface{}{
90+
"name": update_integration_name,
91+
"client_id": gcreds.ClientID,
92+
"client_email": gcreds.ClientEmail,
93+
"private_key_id": gcreds.PrivateKeyID,
94+
"bucket_name": "storage bucket id",
95+
"org_account_mappings": []map[string]interface{}{
96+
{
97+
"default_lacework_account": "customerdemo",
98+
"mapping": []map[string]interface{}{
99+
{
100+
"lacework_account": "abc",
101+
"gcp_projects": []string{"lw-scanner-5"},
102+
},
103+
},
104+
},
105+
},
106+
}
107+
108+
update := terraform.ApplyAndIdempotent(t, terraformOptions)
109+
updateData := GetGcpAgentlessScanningResponse(update)
110+
assert.Equal(t, update_integration_name, updateData.Data.Name)
111+
}
112+
}

lacework/account_mapping_helper.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ func (f *accountMappingsFile) Empty() bool {
1313
return f.DefaultLaceworkAccount == ""
1414
}
1515

16-
type typeStruct struct {
17-
awsAccounts string
18-
gcpProjects string
19-
}
20-
2116
var awsMappingType string = "aws_accounts"
2217
var gcpMappingType string = "gcp_projects"
2318

lacework/resource_lacework_integration_aws_org_agentless_scanning.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ var awsOrgAgentlessScanningIntegrationSchema = map[string]*schema.Schema{
175175
"lacework_account": {
176176
Type: schema.TypeString,
177177
Required: true,
178-
Description: "The Lacework account name where the CloudTrail activity from the selected AWS accounts will appear.",
178+
Description: "The Lacework account name where the Agentless activity from the selected AWS accounts will appear.",
179179
},
180180
"aws_accounts": {
181181
Type: schema.TypeSet,

lacework/resource_lacework_integration_gcp_agentless_scanning.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,31 +212,31 @@ func resourceLaceworkIntegrationGcpAgentlessScanning() *schema.Resource {
212212
"org_account_mappings": {
213213
Type: schema.TypeList,
214214
Optional: true,
215-
Description: "Mapping of AWS accounts to Lacework accounts within a Lacework organization.",
215+
Description: "Mapping of GCP projects to Lacework accounts within a Lacework organization.",
216216
Elem: &schema.Resource{
217217
Schema: map[string]*schema.Schema{
218218
"default_lacework_account": {
219219
Type: schema.TypeString,
220220
Required: true,
221-
Description: "The default Lacework account name where any non-mapped AWS account will appear",
221+
Description: "The default Lacework account name where any non-mapped GCP project will appear",
222222
},
223223
"mapping": {
224224
Type: schema.TypeSet,
225225
Required: true,
226-
Description: "A map of AWS accounts to Lacework account. This can be specified multiple times to map multiple Lacework accounts.",
226+
Description: "A map of GCP projects to Lacework account. This can be specified multiple times to map multiple Lacework accounts.",
227227
Elem: &schema.Resource{
228228
Schema: map[string]*schema.Schema{
229229
"lacework_account": {
230230
Type: schema.TypeString,
231231
Required: true,
232-
Description: "The Lacework account name where the CloudTrail activity from the selected AWS accounts will appear.",
232+
Description: "The Lacework account name where the Agentless activity from the selected gcp projects will appear.",
233233
},
234-
"aws_accounts": {
234+
"gcp_projects": {
235235
Type: schema.TypeSet,
236236
Elem: &schema.Schema{Type: schema.TypeString},
237237
MinItems: 1,
238238
Required: true,
239-
Description: "The list of AWS account IDs to map.",
239+
Description: "The list of GCP project IDs to map.",
240240
},
241241
},
242242
},
@@ -341,27 +341,6 @@ func resourceLaceworkIntegrationGcpAgentlessScanningCreate(d *schema.ResourceDat
341341
d.Set("server_token", integration.ServerToken)
342342
d.Set("uri", integration.Uri)
343343

344-
accountMapFileBytes, err := integration.Data.DecodeAccountMappingFile()
345-
if err != nil {
346-
return resource.NonRetryableError(err)
347-
}
348-
349-
accountMapFile := new(accountMappingsFile)
350-
if len(accountMapFileBytes) != 0 {
351-
// The integration has an account mapping file
352-
// unmarshal its content into the account mapping struct
353-
err := json.Unmarshal(accountMapFileBytes, accountMapFile)
354-
if err != nil {
355-
return resource.NonRetryableError(fmt.Errorf("Error decoding organization account mapping: %s", err))
356-
}
357-
358-
}
359-
360-
err = d.Set("org_account_mappings", flattenOrgAccountMappings(accountMapFile, gcpMappingType))
361-
if err != nil {
362-
return resource.NonRetryableError(fmt.Errorf("Error flattening organization account mapping: %s", err))
363-
}
364-
365344
log.Printf("[INFO] Created %s integration with guid: %v\n",
366345
api.GcpSidekickCloudAccount.String(), integration.IntgGuid)
367346
return nil
@@ -412,6 +391,27 @@ func resourceLaceworkIntegrationGcpAgentlessScanningRead(d *schema.ResourceData,
412391
d.Set("filter_list", trimmed_filter_list)
413392
}
414393

394+
accountMapFileBytes, err := integration.Data.DecodeAccountMappingFile()
395+
if err != nil {
396+
return err
397+
}
398+
399+
accountMapFile := new(accountMappingsFile)
400+
if len(accountMapFileBytes) != 0 {
401+
// The integration has an account mapping file
402+
// unmarshal its content into the account mapping struct
403+
err := json.Unmarshal(accountMapFileBytes, accountMapFile)
404+
if err != nil {
405+
return fmt.Errorf("Error decoding organization account mapping: %s", err)
406+
}
407+
408+
}
409+
410+
err = d.Set("org_account_mappings", flattenOrgAccountMappings(accountMapFile, gcpMappingType))
411+
if err != nil {
412+
return fmt.Errorf("Error flattening organization account mapping: %s", err)
413+
}
414+
415415
log.Printf("[INFO] Read %s integration with guid: %v\n",
416416
api.GcpSidekickCloudAccount.String(), integration.IntgGuid)
417417
return nil

0 commit comments

Comments
 (0)