Skip to content

Commit 7629466

Browse files
committed
chore: create org specific integration test
1 parent 89bd331 commit 7629466

File tree

6 files changed

+367
-315
lines changed

6 files changed

+367
-315
lines changed

examples/resource_lacework_integration_gcp_org_agentless_scanning/main.tf

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ terraform {
77
}
88

99
provider "lacework" {
10-
organization = true
10+
organization = true
1111
}
1212

13-
variable "name" {
13+
variable "integration_name" {
1414
type = string
15-
default = "GCP Agentless Scanning org_example"
15+
default = "GCP Agentless Scanning Example"
1616
}
1717

1818
variable "client_id" {
@@ -47,15 +47,15 @@ variable "integration_type" {
4747

4848
variable "project_id" {
4949
type = string
50-
default = "org-example-project-id"
50+
default = "example-project-id"
5151
}
5252

5353
variable "bucket_name" {
5454
type = string
5555
default = "storage bucket id"
5656
}
5757

58-
variable "scanning_project_id" {
58+
variable "scanning-project-id" {
5959
type = string
6060
default = "scanning-project-id"
6161
}
@@ -70,11 +70,6 @@ variable "filter_list" {
7070
default = ["proj1", "proj2"]
7171
}
7272

73-
variable "scan_frequency" {
74-
type = number
75-
default = 24
76-
}
77-
7873
variable "org_account_mappings" {
7974
type = list(object({
8075
default_lacework_account = string
@@ -88,7 +83,7 @@ variable "org_account_mappings" {
8883
}
8984

9085
resource "lacework_integration_gcp_agentless_scanning" "org_example" {
91-
name = var.name
86+
name = var.integration_name
9287
credentials {
9388
client_id = var.client_id
9489
client_email = var.client_email
@@ -97,14 +92,14 @@ resource "lacework_integration_gcp_agentless_scanning" "org_example" {
9792
token_uri = var.token_uri
9893
}
9994
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
95+
resource_id = "294451184225"
96+
scanning_project_id = "techally-test"
97+
scan_frequency = 24
10498
scan_containers = true
10599
scan_host_vulnerabilities = true
106100
scan_multi_volume = false
107101
scan_stopped_instances = true
102+
bucket_name = var.bucket_name
108103
query_text = var.query_text
109104
filter_list = var.filter_list
110105

@@ -154,4 +149,4 @@ output "server_token" {
154149

155150
output "org_account_mappings" {
156151
value = lacework_integration_gcp_agentless_scanning.org_example.org_account_mappings
157-
}
152+
}

integration/integration.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ func GetContainerRegisteryGar(result string) api.GcpGarIntegrationResponse {
189189

190190
func GetGcpAgentlessScanningResponse(result string) api.GcpSidekickIntegrationResponse {
191191
id := GetIDFromTerraResults(result)
192-
193192
res, err := LwClient.V2.CloudAccounts.GetGcpSidekick(id)
194193

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

201+
func GetGcpAgentlessOrgScanningResponse(result string) api.GcpSidekickIntegrationResponse {
202+
id := GetIDFromTerraResults(result)
203+
res, err := LwOrgClient.V2.CloudAccounts.GetGcpSidekick(id)
204+
205+
if err != nil {
206+
log.Fatalf("Unable to find integration id: %s\n Response: %v", id, res)
207+
}
208+
209+
return res
210+
}
211+
202212
func GetContainerRegisteryGcr(result string) api.GcpGcrIntegrationResponse {
203213
id := GetIDFromTerraResults(result)
204214

integration/resource_lacework_integration_gcp_agentless_scanning_test.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -47,66 +47,3 @@ 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-
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/terraform"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestIntegrationGcpAgentlessOrgScanningCreateAndUpdate(t *testing.T) {
12+
gcreds, err := googleLoadDefaultCredentials()
13+
integration_name := "GCP Org Agentless Scanning Example Integration Test"
14+
update_integration_name := fmt.Sprintf("%s Updated", integration_name)
15+
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
16+
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
17+
TerraformDir: "../examples/resource_lacework_integration_gcp_org_agentless_scanning",
18+
Vars: map[string]interface{}{
19+
"integration_name": integration_name,
20+
"client_id": gcreds.ClientID,
21+
"client_email": gcreds.ClientEmail,
22+
"private_key_id": gcreds.PrivateKeyID,
23+
"bucket_name": "storage bucket id",
24+
"org_account_mappings": []map[string]interface{}{
25+
{
26+
"default_lacework_account_aws": "customerdemo",
27+
"mapping": []map[string]interface{}{
28+
{
29+
"lacework_account": "tech-ally",
30+
"gcp_projects": []string{"techally-test"},
31+
},
32+
},
33+
},
34+
},
35+
},
36+
EnvVars: map[string]string{
37+
"TF_VAR_private_key": gcreds.PrivateKey,
38+
"LW_API_TOKEN": LwApiToken,
39+
},
40+
})
41+
defer terraform.Destroy(t, terraformOptions)
42+
43+
// Create new Google Agentless Scanning integration
44+
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
45+
createData := GetGcpAgentlessOrgScanningResponse(create)
46+
assert.Equal(t, integration_name, createData.Data.Name)
47+
48+
// Update Gcp integration
49+
terraformOptions.Vars = map[string]interface{}{
50+
"integration_name": update_integration_name,
51+
"client_id": gcreds.ClientID,
52+
"client_email": gcreds.ClientEmail,
53+
"private_key_id": gcreds.PrivateKeyID,
54+
"bucket_name": "storage bucket id",
55+
"org_account_mappings": []map[string]interface{}{
56+
{
57+
"default_lacework_account_aws": "customerdemo",
58+
"mapping": []map[string]interface{}{
59+
{
60+
"lacework_account": "abc",
61+
"gcp_projects": []string{"techally-test"},
62+
},
63+
},
64+
},
65+
},
66+
}
67+
68+
update := terraform.ApplyAndIdempotent(t, terraformOptions)
69+
updateData := GetGcpAgentlessOrgScanningResponse(update)
70+
assert.Equal(t, update_integration_name, updateData.Data.Name)
71+
}
72+
}

lacework/account_mapping_helper.go

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
type accountMappingsFile struct {
8-
DefaultLaceworkAccount string `json:"defaultLaceworkAccountAws"`
8+
DefaultLaceworkAccount string `json:"defaultLaceworkAccount"`
99
Mappings map[string]interface{} `json:"integration_mappings"`
1010
}
1111

@@ -30,13 +30,18 @@ func getResourceOrgAccountMappings(d *schema.ResourceData, mappingsType string)
3030
mappingSet := accountMappings["mapping"].(*schema.Set)
3131
for _, m := range mappingSet.List() {
3232
mapping := m.(map[string]interface{})
33-
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
34-
mappingsType: castStringSlice(mapping[mappingsType].(*schema.Set).List()),
33+
if mappingsType == "gcp_projects" {
34+
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
35+
"gcp_projects": castStringSlice(mapping[mappingsType].(*schema.Set).List()),
36+
}
37+
} else {
38+
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
39+
"aws_accounts": castStringSlice(mapping[mappingsType].(*schema.Set).List()),
40+
}
3541
}
3642
}
3743

3844
}
39-
4045
return accountMapFile
4146
}
4247

@@ -58,21 +63,57 @@ func flattenOrgAccountMappings(mappingFile *accountMappingsFile, mappingsType st
5863

5964
func flattenMappings(mappings map[string]interface{}, mappingsType string) *schema.Set {
6065
var (
61-
orgAccountMappingsSchema = awsCloudTrailIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
62-
mappingSchema = orgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
63-
accountsSchema = mappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
64-
res = schema.NewSet(schema.HashResource(mappingSchema), []interface{}{})
66+
awsOrgAccountMappingsSchema = awsCloudTrailIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
67+
awsMappingSchema = awsOrgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
68+
awsAccountsSchema = awsMappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
69+
awsRes = schema.NewSet(schema.HashResource(awsMappingSchema), []interface{}{})
6570
)
6671

6772
for laceworkAccount, m := range mappings {
6873
mappingValue := m.(map[string]interface{})
69-
res.Add(map[string]interface{}{
74+
awsRes.Add(map[string]interface{}{
7075
"lacework_account": laceworkAccount,
71-
mappingsType: schema.NewSet(schema.HashSchema(accountsSchema),
72-
mappingValue[mappingsType].([]interface{}),
76+
"aws_accounts": schema.NewSet(schema.HashSchema(awsAccountsSchema),
77+
mappingValue["aws_accounts"].([]interface{}),
7378
),
7479
})
7580
}
7681

77-
return res
82+
return awsRes
83+
}
84+
85+
func flattenOrgGcpAccountMappings(mappingFile *accountMappingsFile) []map[string]interface{} {
86+
orgAccMappings := make([]map[string]interface{}, 0, 1)
87+
88+
if mappingFile.Empty() {
89+
return orgAccMappings
90+
}
91+
92+
mappings := map[string]interface{}{
93+
"default_lacework_account": mappingFile.DefaultLaceworkAccount,
94+
"mapping": flattenGcpMappings(mappingFile.Mappings),
95+
}
96+
97+
orgAccMappings = append(orgAccMappings, mappings)
98+
return orgAccMappings
99+
}
100+
101+
func flattenGcpMappings(mappings map[string]interface{}) *schema.Set {
102+
var (
103+
gcpOrgAccountMappingsSchema = gcpAgentlessScanningIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
104+
gcpMappingSchema = gcpOrgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
105+
gcpAccountsSchema = gcpMappingSchema.Schema["mapping"].Elem.(*schema.Schema)
106+
gcpRes = schema.NewSet(schema.HashResource(gcpMappingSchema), []interface{}{})
107+
)
108+
109+
for laceworkAccount, m := range mappings {
110+
mappingValue := m.(map[string]interface{})
111+
gcpRes.Add(map[string]interface{}{
112+
"lacework_account": laceworkAccount,
113+
"gcp_projects": schema.NewSet(schema.HashSchema(gcpAccountsSchema),
114+
mappingValue["gcp_projects"].([]interface{}),
115+
),
116+
})
117+
}
118+
return gcpRes
78119
}

0 commit comments

Comments
 (0)