Skip to content

Commit

Permalink
feat(resource): New lacework_report_rule (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurray-lacework authored Dec 2, 2021
1 parent c92b8da commit c2928b6
Show file tree
Hide file tree
Showing 43 changed files with 2,147 additions and 143 deletions.
122 changes: 122 additions & 0 deletions examples/resource_lacework_report_rule/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
terraform {
required_providers {
lacework = {
source = "lacework/lacework"
}
}
}

resource "lacework_alert_channel_email" "email_alerts" {
name = "Used for Report Rules Testing"
recipients = ["foo@example.com"]

// test_integration input is used in this example only for testing
// purposes, it help us avoid sending a "test" request to the
// system we are integrating to. In production, this should remain
// turned on ("true") which is the default setting
test_integration = false
}

resource "lacework_resource_group_aws" "aws_group" {
name = var.resource_group_name
accounts = ["*"]
}

resource "lacework_report_rule" "example" {
name = var.name
description = var.description
enabled = true
severities = var.severities
resource_groups = [lacework_resource_group_aws.aws_group.id]
email_alert_channels = [lacework_alert_channel_email.email_alerts.id]

aws_compliance_reports {
pci = var.aws_pci
cis_s3 = true
}

gcp_compliance_reports {
pci = var.gcp_pci
cis = true
}

daily_compliance_reports {
aws_cloudtrail = var.daily_cloudtrail
}

weekly_snapshot = var.snapshot
}

variable "name" {
type = string
default = "Terraform Report Rule"
}

variable "description" {
type = string
default = "Report Rule created by Terraform"
}

variable "severities" {
type = list(string)
default = ["High", "Medium"]
}

variable "resource_groups" {
type = list(string)
default = ["TECHALLY_8416B4ADCED28565254842AA5906B729174653E1725F107"]
}

variable "channels" {
type = list(string)
default = ["TECHALLY_2F0C086E17AB64BEC84F4A5FF8A3F068CF2CE15847BCBCA"]
}

variable "aws_pci" {
type = bool
default = true
}

variable "gcp_pci" {
type = bool
default = true
}

variable "daily_cloudtrail" {
type = bool
default = true
}

variable "snapshot" {
type = bool
default = true
}

variable "resource_group_name" {
type = string
default = "Used for Report Rules Testing"
}

output "name" {
value = lacework_report_rule.example.name
}

output "description" {
value = lacework_report_rule.example.description
}

output "severities" {
value = lacework_report_rule.example.severities
}

output "resource_groups" {
value = lacework_report_rule.example.resource_groups
}

output "channels" {
value = lacework_report_rule.example.email_alert_channels
}

output "aws_pci" {
value = lacework_report_rule.example.aws_compliance_reports.0.pci
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.8.0
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/lacework/go-sdk v0.19.0
github.com/lacework/go-sdk v0.22.1-0.20211129140431-f37b91349c03
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/hcl/v2 v2.9.1 h1:eOy4gREY0/ZQHNItlfuEZqtcQbXIxzojlP301hDpnac=
github.com/hashicorp/hcl/v2 v2.9.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
Expand All @@ -577,6 +579,7 @@ github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/honeycombio/libhoney-go v1.15.5/go.mod h1:8NyBoM746bz+nw3yQzQF8gtJO/z4mkr/MD5C4r4uC2Y=
github.com/honeycombio/libhoney-go v1.15.6/go.mod h1:8NyBoM746bz+nw3yQzQF8gtJO/z4mkr/MD5C4r4uC2Y=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -648,6 +651,12 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/kyokomi/emoji/v2 v2.2.8/go.mod h1:JUcn42DTdsXJo1SWanHh4HKDEyPaR5CqkmoirZZP9qE=
github.com/lacework/go-sdk v0.19.0 h1:KJiFgKDpgxD7pMRuvLo5p3wALuywGMujDN6N60Rl6H0=
github.com/lacework/go-sdk v0.19.0/go.mod h1:izb0r7jZ9EA1y4O4P8Cq7b3coQzmry2TizQ0DKXLEOg=
github.com/lacework/go-sdk v0.21.1-0.20211119005311-30637bcce5b7 h1:EuCiTPgrJjiwH33L4DnDlzh6M7gNN1I3jZnmFqhPi7o=
github.com/lacework/go-sdk v0.21.1-0.20211119005311-30637bcce5b7/go.mod h1:h2lPPxD2001YfAuaJjK0nbCi/IDOqACEYPdaXrwJtAE=
github.com/lacework/go-sdk v0.22.1-0.20211124024508-e12f02235699 h1:QrsCRQzyvYOEkzDX0T8hatMvK+YT/eKKpvgjp24o8R8=
github.com/lacework/go-sdk v0.22.1-0.20211124024508-e12f02235699/go.mod h1:nHJGElQ4mz+7e1wkUT/bNQ0qhzIfqj6QrI9afm1Yvak=
github.com/lacework/go-sdk v0.22.1-0.20211129140431-f37b91349c03 h1:S/xheRUDbLChxScHZ+uX9fT00vXI3Vpk8q41D3Ci6qA=
github.com/lacework/go-sdk v0.22.1-0.20211129140431-f37b91349c03/go.mod h1:nHJGElQ4mz+7e1wkUT/bNQ0qhzIfqj6QrI9afm1Yvak=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -938,6 +947,8 @@ github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUA
github.com/zclconf/go-cty v1.8.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.8.4 h1:pwhhz5P+Fjxse7S7UriBrMu6AUJSZM5pKqGem1PjGAs=
github.com/zclconf/go-cty v1.8.4/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty v1.10.0 h1:mp9ZXQeIcN8kAwuqorjH+Q+njbJKjLrvB2yIh4q7U+0=
github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
11 changes: 11 additions & 0 deletions integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ func GetAlertRuleProps(result string) api.AlertRuleResponse {
return data
}

func GetReportRuleProps(result string) api.ReportRuleResponse {
id := GetSpecificIDFromTerraResults(3, result)

var data api.ReportRuleResponse
err := LwClient.V2.ReportRules.Get(id, &data)
if err != nil {
log.Fatalf("Unable to retrieve report rule with id: %s", id)
}
return data
}

// GetSpecificIDFromTerraResults returns the specific index id found in the Terraform output
func GetSpecificIDFromTerraResults(i int, result string) string {
re := regexp.MustCompile(`\[id=(.*?)\]`)
Expand Down
77 changes: 77 additions & 0 deletions integration/resource_lacework_report_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package integration

import (
"fmt"
"testing"
"time"

"github.com/lacework/go-sdk/api"

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

// TestReportRuleCreate applies integration terraform:
// => '../examples/resource_lacework_report_rule'
//
// It uses the go-sdk to verify the created report rule,
// applies an update and destroys it
func TestReportRuleCreate(t *testing.T) {
name := fmt.Sprintf("Report Rule - %s", time.Now())
resourceGroupName := fmt.Sprintf("Used for Report Rule Test - %s", time.Now())
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples/resource_lacework_report_rule",
Vars: map[string]interface{}{
"name": name,
"description": "Report Rule created by Terraform",
"severities": []string{"Critical"},
"aws_pci": true,
"resource_group_name": resourceGroupName,
},
})
defer terraform.Destroy(t, terraformOptions)

// Create new Report Rule
terraformOptions.TimeBetweenRetries = 2 * time.Second
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
createProps := GetReportRuleProps(create)

actualDescription := terraform.Output(t, terraformOptions, "description")
actualChannels := terraform.Output(t, terraformOptions, "channels")
actualSeverities := terraform.Output(t, terraformOptions, "severities")
actualAwsNotifications := terraform.Output(t, terraformOptions, "aws_pci")

assert.Equal(t, "Report Rule created by Terraform", createProps.Data.Filter.Description)
assert.Equal(t, []string{"Critical"}, api.NewReportRuleSeveritiesFromIntSlice(createProps.Data.Filter.Severity).ToStringSlice())
assert.Equal(t, actualAwsNotifications, "true")
assert.True(t, createProps.Data.ReportNotificationTypes.AwsPci)
assert.Equal(t, "Report Rule created by Terraform", actualDescription)
assert.Equal(t, string("[Critical]"), actualSeverities)
assert.NotEmpty(t, actualChannels)
assert.NotEmpty(t, createProps.Data.EmailAlertChannels)

// Update Report Rule
terraformOptions.Vars = map[string]interface{}{
"name": name,
"description": "Updated Report Rule created by Terraform",
"severities": []string{"High", "Medium"},
"aws_pci": false,
"resource_group_name": resourceGroupName,
}

update := terraform.ApplyAndIdempotent(t, terraformOptions)
updateProps := GetReportRuleProps(update)
actualDescription = terraform.Output(t, terraformOptions, "description")
actualChannels = terraform.Output(t, terraformOptions, "channels")
actualSeverities = terraform.Output(t, terraformOptions, "severities")
actualAwsNotifications = terraform.Output(t, terraformOptions, "aws_pci")

assert.Equal(t, "Updated Report Rule created by Terraform", updateProps.Data.Filter.Description)
assert.Equal(t, []string{"High", "Medium"}, api.NewReportRuleSeveritiesFromIntSlice(updateProps.Data.Filter.Severity).ToStringSlice())
assert.Equal(t, "Updated Report Rule created by Terraform", actualDescription)
assert.Equal(t, "[High Medium]", actualSeverities)
assert.Equal(t, actualAwsNotifications, "false")
assert.False(t, updateProps.Data.ReportNotificationTypes.AwsPci)
assert.NotEmpty(t, actualChannels)
assert.NotEmpty(t, updateProps.Data.EmailAlertChannels)
}
1 change: 1 addition & 0 deletions lacework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func Provider() *schema.Provider {
"lacework_integration_gar": resourceLaceworkIntegrationGar(),
"lacework_integration_gcr": resourceLaceworkIntegrationGcr(),
"lacework_integration_ghcr": resourceLaceworkIntegrationGhcr(),
"lacework_report_rule": resourceLaceworkReportRule(),
"lacework_resource_group_account": resourceLaceworkResourceGroupLwAccount(),
"lacework_resource_group_aws": resourceLaceworkResourceGroupAws(),
"lacework_resource_group_azure": resourceLaceworkResourceGroupAzure(),
Expand Down
Loading

0 comments on commit c2928b6

Please sign in to comment.