Skip to content

Commit 9ba45aa

Browse files
authored
chore: GKE alias and make required providers optional (#1516)
1 parent dd520fb commit 9ba45aa

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

lwgenerate/gcp/gcp.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ func (args *GenerateGcpTfConfigurationArgs) Generate() (string, error) {
417417
}
418418

419419
// Create blocks
420-
requiredProviders, err := createRequiredProviders()
420+
requiredProviders, err := createRequiredProviders(false)
421421
if err != nil {
422422
return "", errors.Wrap(err, "failed to generate required providers")
423423
}
424424

425-
gcpProvider, err := createGcpProvider(args.ServiceAccountCredentials, args.GcpProjectId, args.Regions)
425+
gcpProvider, err := createGcpProvider(args.ServiceAccountCredentials, args.GcpProjectId, args.Regions, "")
426426
if err != nil {
427427
return "", errors.Wrap(err, "failed to generate gcp provider")
428428
}
@@ -461,7 +461,10 @@ func (args *GenerateGcpTfConfigurationArgs) Generate() (string, error) {
461461
return hclBlocks, nil
462462
}
463463

464-
func createRequiredProviders() (*hclwrite.Block, error) {
464+
func createRequiredProviders(useExistingRequiredProviders bool) (*hclwrite.Block, error) {
465+
if useExistingRequiredProviders {
466+
return nil, nil
467+
}
465468
return lwgenerate.CreateRequiredProviders(
466469
lwgenerate.NewRequiredProvider(
467470
"lacework",
@@ -485,6 +488,7 @@ func createGcpProvider(
485488
serviceAccountCredentials string,
486489
projectId string,
487490
regionsArg []string,
491+
alias string,
488492
) ([]*hclwrite.Block, error) {
489493
blocks := []*hclwrite.Block{}
490494

@@ -504,6 +508,10 @@ func createGcpProvider(
504508
attrs["project"] = projectId
505509
}
506510

511+
if alias != "" {
512+
attrs["alias"] = alias
513+
}
514+
507515
if region != "" {
508516
attrs["alias"] = region
509517
attrs["region"] = region

lwgenerate/gcp/gke.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import (
99
)
1010

1111
type GenerateGkeTfConfigurationArgs struct {
12-
ExistingServiceAccount *ServiceAccount
13-
ExistingSinkName string
14-
IntegrationName string
15-
Labels map[string]string
16-
LaceworkProfile string
17-
OrganizationId string
18-
OrganizationIntegration bool
19-
Prefix string
20-
ProjectId string
21-
PubSubSubscriptionLabels map[string]string
22-
PubSubTopicLabels map[string]string
23-
ServiceAccountCredentials string
24-
WaitTime string
12+
UseExistingRequiredProviders bool
13+
GcpProviderAlias string
14+
ExistingServiceAccount *ServiceAccount
15+
ExistingSinkName string
16+
IntegrationName string
17+
Labels map[string]string
18+
LaceworkProfile string
19+
OrganizationId string
20+
OrganizationIntegration bool
21+
Prefix string
22+
ProjectId string
23+
PubSubSubscriptionLabels map[string]string
24+
PubSubTopicLabels map[string]string
25+
ServiceAccountCredentials string
26+
WaitTime string
2527
}
2628

2729
type Modifier func(c *GenerateGkeTfConfigurationArgs)
@@ -31,12 +33,17 @@ func (args *GenerateGkeTfConfigurationArgs) Generate() (string, error) {
3133
return "", errors.Wrap(err, "invalid inputs")
3234
}
3335

34-
requiredProviders, err := createRequiredProviders()
36+
requiredProviders, err := createRequiredProviders(args.UseExistingRequiredProviders)
3537
if err != nil {
3638
return "", errors.Wrap(err, "failed to generate required providers")
3739
}
3840

39-
gcpProvider, err := createGcpProvider(args.ServiceAccountCredentials, args.ProjectId, []string{})
41+
gcpProvider, err := createGcpProvider(
42+
args.ServiceAccountCredentials,
43+
args.ProjectId,
44+
[]string{},
45+
args.GcpProviderAlias,
46+
)
4047
if err != nil {
4148
return "", errors.Wrap(err, "failed to generate gcp provider")
4249
}
@@ -93,6 +100,18 @@ func NewGkeTerraform(mods ...Modifier) *GenerateGkeTfConfigurationArgs {
93100
return config
94101
}
95102

103+
func WithGkeExistingRequiredProviders() Modifier {
104+
return func(c *GenerateGkeTfConfigurationArgs) {
105+
c.UseExistingRequiredProviders = true
106+
}
107+
}
108+
109+
func WithGkeGcpProviderAlias(alias string) Modifier {
110+
return func(c *GenerateGkeTfConfigurationArgs) {
111+
c.GcpProviderAlias = alias
112+
}
113+
}
114+
96115
func WithGkeExistingServiceAccount(serviceAccount *ServiceAccount) Modifier {
97116
return func(c *GenerateGkeTfConfigurationArgs) {
98117
c.ExistingServiceAccount = serviceAccount
@@ -207,10 +226,23 @@ func createGKEAuditLog(args *GenerateGkeTfConfigurationArgs) (*hclwrite.Block, e
207226
attributes["wait_time"] = args.WaitTime
208227
}
209228

229+
moduleDetails := []lwgenerate.HclModuleModifier{
230+
lwgenerate.HclModuleWithAttributes(attributes),
231+
lwgenerate.HclModuleWithVersion(lwgenerate.GcpGKEAuditLogVersion),
232+
}
233+
234+
if args.GcpProviderAlias != "" {
235+
moduleDetails = append(
236+
moduleDetails,
237+
lwgenerate.HclModuleWithProviderDetails(
238+
map[string]string{"google": fmt.Sprintf("google.%s", args.GcpProviderAlias)},
239+
),
240+
)
241+
}
242+
210243
return lwgenerate.NewModule(
211244
fmt.Sprintf("gcp_%s_level_gke_audit_log", level),
212245
lwgenerate.GcpGKEAuditLogSource,
213-
lwgenerate.HclModuleWithAttributes(attributes),
214-
lwgenerate.HclModuleWithVersion(lwgenerate.GcpGKEAuditLogVersion),
246+
moduleDetails...,
215247
).ToBlock()
216248
}

lwgenerate/gcp/gke_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ func TestGenerateGKETfConfigurationArgs_Generate(t *testing.T) {
109109
}
110110
`),
111111
},
112+
{
113+
"TestGeneration GKE Audit Log using alias and existing required providers ",
114+
gcp.NewGkeTerraform(
115+
gcp.WithGkeGcpProviderAlias("gke"),
116+
gcp.WithGkeExistingRequiredProviders(),
117+
gcp.WithGkeProjectId("project1"),
118+
),
119+
`provider "google" {
120+
alias = "gke"
121+
project = "project1"
122+
}
123+
124+
module "gcp_project_level_gke_audit_log" {
125+
source = "lacework/gke-audit-log/gcp"
126+
version = "~> 0.3"
127+
integration_type = "PROJECT"
128+
129+
providers = {
130+
google = google.gke
131+
}
132+
}
133+
`,
134+
},
112135
}
113136

114137
for _, tc := range tests {

0 commit comments

Comments
 (0)