Skip to content

Commit b742eb7

Browse files
feat: add new flag to generate cmds set lacework aws root account (#1121)
* feat: add new flag to generate cmds set lacework aws root account
1 parent 929499d commit b742eb7

File tree

9 files changed

+91
-1
lines changed

9 files changed

+91
-1
lines changed

cli/cmd/generate_aws.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ See help output for more details on the parameter value(s) required for Terrafor
109109
mods := []aws.AwsTerraformModifier{
110110
aws.WithAwsProfile(GenerateAwsCommandState.AwsProfile),
111111
aws.WithLaceworkProfile(GenerateAwsCommandState.LaceworkProfile),
112+
aws.WithLaceworkAccountID(GenerateAwsCommandState.LaceworkAccountID),
112113
aws.ExistingCloudtrailBucketArn(GenerateAwsCommandState.ExistingCloudtrailBucketArn),
113114
aws.ExistingSnsTopicArn(GenerateAwsCommandState.ExistingSnsTopicArn),
114115
aws.WithSubaccounts(GenerateAwsCommandState.SubAccounts...),
@@ -442,6 +443,11 @@ func initGenerateAwsTfCommandFlags() {
442443
"sqs_queue_name",
443444
"",
444445
"specify SQS queue name if creating new one")
446+
generateAwsTfCommand.PersistentFlags().StringVar(
447+
&GenerateAwsCommandState.LaceworkAccountID,
448+
"lacework_aws_account_id",
449+
"",
450+
"the Lacework AWS root account id")
445451
}
446452

447453
// survey.Validator for aws ARNs

cli/cmd/generate_aws_eks_audit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ See help output for more details on the parameter values required for Terraform
115115
// Setup modifiers for NewTerraform constructor
116116
mods := []aws_eks_audit.AwsEksAuditTerraformModifier{
117117
aws_eks_audit.WithAwsProfile(GenerateAwsEksAuditCommandState.AwsProfile),
118+
aws_eks_audit.WithLaceworkAccountID(GenerateAwsEksAuditCommandState.LaceworkAccountID),
118119
aws_eks_audit.WithBucketLifecycleExpirationDays(GenerateAwsEksAuditCommandState.BucketLifecycleExpirationDays),
119120
aws_eks_audit.WithBucketSseAlgorithm(GenerateAwsEksAuditCommandState.BucketSseAlgorithm),
120121
aws_eks_audit.WithBucketSseKeyArn(GenerateAwsEksAuditCommandState.BucketSseKeyArn),
@@ -333,6 +334,8 @@ func initGenerateAwsEksAuditTfCommandFlags() {
333334
// TODO Share the help with the interactive generation
334335
generateAwsEksAuditTfCommand.PersistentFlags().StringVar(
335336
&GenerateAwsEksAuditCommandState.AwsProfile, "aws_profile", "", "specify aws profile")
337+
generateAwsEksAuditTfCommand.PersistentFlags().StringVar(
338+
&GenerateAwsEksAuditCommandState.LaceworkAccountID, "lacework_aws_account_id", "", "the Lacework AWS root account id")
336339
generateAwsEksAuditTfCommand.PersistentFlags().BoolVar(
337340
&GenerateAwsEksAuditCommandState.BucketEnableMfaDelete,
338341
"enable_mfa_delete_s3",

cli/cmd/generate_gcp.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ func initGenerateGcpTfCommandFlags() {
434434
"",
435435
"location to write generated content (default is ~/lacework/gcp)",
436436
)
437-
438437
}
439438

440439
// survey.Validator for gcp region

integration/test_resources/help/generate_cloud-account_aws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Flags:
3838
--existing_sns_topic_arn string specify existing SNS topic arn
3939
--force_destroy_s3 enable force destroy S3 bucket
4040
-h, --help help for aws
41+
--lacework_aws_account_id string the Lacework AWS root account id
4142
--output string location to write generated content (default is ~/lacework/aws)
4243
--sns_topic_encryption_enabled enable encryption on SNS topic when creating one (default true)
4344
--sns_topic_encryption_key_arn string specify existing KMS encryption key arn for SNS topic

integration/test_resources/help/generate_k8s_eks

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Flags:
4141
-h, --help help for eks
4242
--integration_name string specify the name of the eks audit integration
4343
--kms_key_deletion_days int specify the kms waiting period before deletion, in number of days
44+
--lacework_aws_account_id string the Lacework AWS root account id
4445
--output string location to write generated content
4546
--prefix string specify the prefix that will be used at the beginning of every generated resource
4647
--region_clusters stringToString configure eks clusters per aws region. To configure multiple regions pass the flag multiple times. Example format: --region_clusters <region>="cluster,list" (default [])

lwgenerate/aws/aws.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ type GenerateAwsTfConfigurationArgs struct {
147147

148148
// Lacework Profile to use
149149
LaceworkProfile string
150+
151+
// The Lacework AWS Root Account ID
152+
LaceworkAccountID string
150153
}
151154

152155
// Ensure all combinations of inputs our valid for supported spec
@@ -208,6 +211,13 @@ func WithLaceworkProfile(name string) AwsTerraformModifier {
208211
}
209212
}
210213

214+
// WithLaceworkAccountID Set the Lacework AWS root account ID to use
215+
func WithLaceworkAccountID(accountID string) AwsTerraformModifier {
216+
return func(c *GenerateAwsTfConfigurationArgs) {
217+
c.LaceworkAccountID = accountID
218+
}
219+
}
220+
211221
// ExistingCloudtrailBucketArn Set the bucket ARN of an existing Cloudtrail setup
212222
func ExistingCloudtrailBucketArn(arn string) AwsTerraformModifier {
213223
return func(c *GenerateAwsTfConfigurationArgs) {
@@ -451,6 +461,9 @@ func createConfig(args *GenerateAwsTfConfigurationArgs) ([]*hclwrite.Block, erro
451461
moduleDetails = append(moduleDetails,
452462
lwgenerate.HclModuleWithProviderDetails(map[string]string{"aws": "aws.main"}))
453463
}
464+
if args.LaceworkAccountID != "" {
465+
moduleDetails = append(moduleDetails, lwgenerate.HclModuleWithAttributes(map[string]interface{}{"lacework_aws_account_id": args.LaceworkAccountID}))
466+
}
454467

455468
moduleBlock, err := lwgenerate.NewModule(
456469
"aws_config",
@@ -489,6 +502,10 @@ func createCloudtrail(args *GenerateAwsTfConfigurationArgs) (*hclwrite.Block, er
489502
if args.Cloudtrail {
490503
attributes := map[string]interface{}{}
491504
modDetails := []lwgenerate.HclModuleModifier{lwgenerate.HclModuleWithVersion(lwgenerate.AwsCloudTrailVersion)}
505+
506+
if args.LaceworkAccountID != "" {
507+
attributes["lacework_aws_account_id"] = args.LaceworkAccountID
508+
}
492509
if args.ConsolidatedCloudtrail {
493510
attributes["consolidated_trail"] = true
494511
}

lwgenerate/aws/aws_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func TestGenerationWithLaceworkProvider(t *testing.T) {
6363
assert.Equal(t, reqProviderAndRegion(laceworkProvider, moduleImportCtWithoutConfig), hcl)
6464
}
6565

66+
func TestGenerationWithLaceworkAccountID(t *testing.T) {
67+
hcl, err := NewTerraform("us-east-2", true, true, WithLaceworkAccountID("123456789")).Generate()
68+
assert.Nil(t, err)
69+
assert.NotNil(t, hcl)
70+
assert.Equal(t, reqProviderAndRegion(moduleImportConfigWithLaceworkAccountID, moduleImportCtWithLaceworkAccountID), hcl)
71+
}
72+
6673
func TestGenerationCloudtrailForceDestroyS3(t *testing.T) {
6774
data, err := createCloudtrail(&GenerateAwsTfConfigurationArgs{
6875
Cloudtrail: true,
@@ -412,3 +419,21 @@ var moduleImportConfig = `module "aws_config" {
412419
version = "~> 0.5"
413420
}
414421
`
422+
423+
var moduleImportConfigWithLaceworkAccountID = `module "aws_config" {
424+
source = "lacework/config/aws"
425+
version = "~> 0.5"
426+
lacework_aws_account_id = "123456789"
427+
}
428+
`
429+
430+
var moduleImportCtWithLaceworkAccountID = `module "main_cloudtrail" {
431+
source = "lacework/cloudtrail/aws"
432+
version = "~> 2.0"
433+
iam_role_arn = module.aws_config.iam_role_arn
434+
iam_role_external_id = module.aws_config.external_id
435+
iam_role_name = module.aws_config.iam_role_name
436+
lacework_aws_account_id = "123456789"
437+
use_existing_iam_role = true
438+
}
439+
`

lwgenerate/aws_eks_audit/aws_eks_audit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ type GenerateAwsEksAuditTfConfigurationArgs struct {
129129

130130
// Lacework Profile to use
131131
LaceworkProfile string
132+
133+
// The Lacework AWS Root Account ID
134+
LaceworkAccountID string
132135
}
133136

134137
// Ensure all combinations of inputs our valid for supported spec
@@ -184,6 +187,13 @@ func NewTerraform(mods ...AwsEksAuditTerraformModifier) *GenerateAwsEksAuditTfCo
184187
return config
185188
}
186189

190+
// WithLaceworkAccountID Set the Lacework AWS root account ID to use
191+
func WithLaceworkAccountID(accountID string) AwsEksAuditTerraformModifier {
192+
return func(c *GenerateAwsEksAuditTfConfigurationArgs) {
193+
c.LaceworkAccountID = accountID
194+
}
195+
}
196+
187197
// WithAwsProfile Set the AWS Profile to utilize when integrating
188198
func WithAwsProfile(name string) AwsEksAuditTerraformModifier {
189199
return func(c *GenerateAwsEksAuditTfConfigurationArgs) {
@@ -477,6 +487,10 @@ func createEksAudit(args *GenerateAwsEksAuditTfConfigurationArgs) ([]*hclwrite.B
477487
resourceAttrs := map[string]interface{}{}
478488
moduleDetails := []lwgenerate.HclModuleModifier{lwgenerate.HclModuleWithVersion(lwgenerate.AwsEksAuditVersion)}
479489

490+
if args.LaceworkAccountID != "" {
491+
moduleAttrs["lacework_aws_account_id"] = args.LaceworkAccountID
492+
}
493+
480494
if args.BucketEnableMfaDelete && args.BucketVersioning {
481495
moduleAttrs["bucket_enable_mfa_delete"] = true
482496
}

lwgenerate/aws_eks_audit/aws_eks_audit_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ func TestGenerationEksSingleRegion(t *testing.T) {
3232
assert.Equal(t, reqProviderAndRegion(moduleSingleRegionBasic), hcl)
3333
}
3434

35+
func TestGenerationEksSingleWithLaceworkAccountID(t *testing.T) {
36+
clusterMap := make(map[string][]string)
37+
clusterMap["us-east-1"] = []string{"cluster1", "cluster2"}
38+
hcl, err := NewTerraform(WithParsedRegionClusterMap(clusterMap), WithLaceworkAccountID("123456789")).Generate()
39+
assert.Nil(t, err)
40+
assert.NotNil(t, hcl)
41+
assert.Equal(t, reqProviderAndRegion(moduleSingleRegionWithLaceworkAccountID), hcl)
42+
}
43+
3544
func TestGenerationEksMultiRegion(t *testing.T) {
3645
clusterMap := make(map[string][]string)
3746
clusterMap["us-east-1"] = []string{"cluster1", "cluster2"}
@@ -409,6 +418,21 @@ module "aws_eks_audit_log" {
409418
}
410419
`
411420

421+
var moduleSingleRegionWithLaceworkAccountID = `provider "aws" {
422+
region = "us-east-1"
423+
}
424+
425+
module "aws_eks_audit_log" {
426+
source = "lacework/eks-audit-log/aws"
427+
version = "~> 0.4"
428+
cloudwatch_regions = ["us-east-1"]
429+
cluster_names = ["cluster1", "cluster2"]
430+
kms_key_multi_region = false
431+
lacework_aws_account_id = "123456789"
432+
no_cw_subscription_filter = false
433+
}
434+
`
435+
412436
var multiRegionBasic = `provider "aws" {
413437
alias = "us-east-1"
414438
region = "us-east-1"

0 commit comments

Comments
 (0)