Skip to content

Commit acbf0d5

Browse files
committed
chore: create org specific integration test
1 parent 5cc8142 commit acbf0d5

File tree

6 files changed

+373
-318
lines changed

6 files changed

+373
-318
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": "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: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package lacework
22

33
import (
4+
"fmt"
5+
46
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
57
)
68

79
type accountMappingsFile struct {
8-
DefaultLaceworkAccount string `json:"defaultLaceworkAccountAws"`
10+
DefaultLaceworkAccount string `json:"defaultLaceworkAccount"`
911
Mappings map[string]interface{} `json:"integration_mappings"`
1012
}
1113

@@ -17,26 +19,32 @@ var awsMappingType string = "aws_accounts"
1719
var gcpMappingType string = "gcp_projects"
1820

1921
func getResourceOrgAccountMappings(d *schema.ResourceData, mappingsType string) *accountMappingsFile {
22+
fmt.Println("MAPPINGS TYPE: ", mappingsType)
2023
accountMapFile := new(accountMappingsFile)
2124
accMapsInt := d.Get("org_account_mappings").([]interface{})
2225
if len(accMapsInt) != 0 && accMapsInt[0] != nil {
2326
accountMappings := accMapsInt[0].(map[string]interface{})
2427

2528
accountMapFile = &accountMappingsFile{
26-
DefaultLaceworkAccount: accountMappings["default_lacework_account"].(string),
29+
DefaultLaceworkAccount: accountMappings["default_lacework_account_aws"].(string),
2730
Mappings: map[string]interface{}{},
2831
}
2932

3033
mappingSet := accountMappings["mapping"].(*schema.Set)
3134
for _, m := range mappingSet.List() {
3235
mapping := m.(map[string]interface{})
33-
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
34-
mappingsType: castStringSlice(mapping[mappingsType].(*schema.Set).List()),
36+
if mappingsType == "gcp_projects" {
37+
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
38+
"gcp_projects": castStringSlice(mapping[mappingsType].(*schema.Set).List()),
39+
}
40+
} else {
41+
accountMapFile.Mappings[mapping["lacework_account"].(string)] = map[string]interface{}{
42+
"aws_accounts": castStringSlice(mapping[mappingsType].(*schema.Set).List()),
43+
}
3544
}
3645
}
3746

3847
}
39-
4048
return accountMapFile
4149
}
4250

@@ -48,8 +56,8 @@ func flattenOrgAccountMappings(mappingFile *accountMappingsFile, mappingsType st
4856
}
4957

5058
mappings := map[string]interface{}{
51-
"default_lacework_account": mappingFile.DefaultLaceworkAccount,
52-
"mapping": flattenMappings(mappingFile.Mappings, mappingsType),
59+
"default_lacework_account_aws": mappingFile.DefaultLaceworkAccount,
60+
"mapping": flattenMappings(mappingFile.Mappings, mappingsType),
5361
}
5462

5563
orgAccMappings = append(orgAccMappings, mappings)
@@ -58,21 +66,57 @@ func flattenOrgAccountMappings(mappingFile *accountMappingsFile, mappingsType st
5866

5967
func flattenMappings(mappings map[string]interface{}, mappingsType string) *schema.Set {
6068
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{}{})
69+
awsOrgAccountMappingsSchema = awsCloudTrailIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
70+
awsMappingSchema = awsOrgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
71+
awsAccountsSchema = awsMappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
72+
awsRes = schema.NewSet(schema.HashResource(awsMappingSchema), []interface{}{})
6573
)
6674

6775
for laceworkAccount, m := range mappings {
6876
mappingValue := m.(map[string]interface{})
69-
res.Add(map[string]interface{}{
77+
awsRes.Add(map[string]interface{}{
7078
"lacework_account": laceworkAccount,
71-
mappingsType: schema.NewSet(schema.HashSchema(accountsSchema),
72-
mappingValue[mappingsType].([]interface{}),
79+
"aws_accounts": schema.NewSet(schema.HashSchema(awsAccountsSchema),
80+
mappingValue["aws_accounts"].([]interface{}),
7381
),
7482
})
7583
}
7684

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

0 commit comments

Comments
 (0)