Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GROW-2876): support terraform output blocks in lwgenerate #1609

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lwgenerate/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ type GenerateAwsTfConfigurationArgs struct {
// Config resource prefix
ConfigOrgCfResourcePrefix string

// Custom outputs
CustomOutputs []lwgenerate.HclOutput

// Supply an AWS region for where to find the cloudtrail resources
// TODO @ipcrm future: support split regions for resources (s3 one place, sns another, etc)
AwsRegion string
Expand Down Expand Up @@ -538,6 +541,13 @@ func WithConfigOrgUnits(orgUnits []string) AwsTerraformModifier {
}
}

// WithConfigOutputs Set Custom Terraform Outputs
func WithCustomOutputs(outputs []lwgenerate.HclOutput) AwsTerraformModifier {
return func(c *GenerateAwsTfConfigurationArgs) {
c.CustomOutputs = outputs
}
}

// WithConfigOrgCfResourcePrefix Set Config org resource prefix
func WithConfigOrgCfResourcePrefix(resourcePrefix string) AwsTerraformModifier {
return func(c *GenerateAwsTfConfigurationArgs) {
Expand Down Expand Up @@ -750,6 +760,15 @@ func (args *GenerateAwsTfConfigurationArgs) Generate() (string, error) {
return "", errors.Wrap(err, "failed to generate aws agentless global module")
}

outputBlocks := []*hclwrite.Block{}
for _, output := range args.CustomOutputs {
outputBlock, err := output.ToBlock()
if err != nil {
return "", errors.Wrap(err, "failed to add custom output")
}
outputBlocks = append(outputBlocks, outputBlock)
}

// Render
hclBlocks := lwgenerate.CreateHclStringOutput(
lwgenerate.CombineHclBlocks(
Expand All @@ -758,7 +777,8 @@ func (args *GenerateAwsTfConfigurationArgs) Generate() (string, error) {
laceworkProvider,
configModule,
cloudTrailModule,
agentlessModule),
agentlessModule,
outputBlocks),
)
return hclBlocks, nil
}
Expand Down
21 changes: 19 additions & 2 deletions lwgenerate/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/lacework/go-sdk/lwgenerate"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -77,6 +78,17 @@ func TestGenerationConfig(t *testing.T) {
assert.Equal(t, reqProviderAndRegion(moduleImportConfig), hcl)
}

func TestGenerationConfigWithOutputs(t *testing.T) {
hcl, err := NewTerraform(
false, false, true, false, WithAwsRegion("us-east-2"),
WithCustomOutputs([]lwgenerate.HclOutput{
*lwgenerate.NewOutput("test", []string{"module", "aws_config", "lacework_integration_guid"}, "test description"),
})).Generate()
assert.Nil(t, err)
assert.NotNil(t, hcl)
assert.Equal(t, reqProviderAndRegion(moduleImportConfig)+"\n"+customOutput, hcl)
}

func TestGenerationConfigWithMultipleAccounts(t *testing.T) {
hcl, err := NewTerraform(false, false, true, false,
WithAwsProfile("main"),
Expand Down Expand Up @@ -135,8 +147,7 @@ func TestGenerationCloudtrailConsolidated(t *testing.T) {
ConsolidatedCloudtrail: true,
})
assert.Nil(t, err)
assert.Equal(t,
"consolidated_trail=true\n",
assert.Equal(t, "consolidated_trail=true\n",
string(data.Body().GetAttribute("consolidated_trail").BuildTokens(nil).Bytes()))
}

Expand Down Expand Up @@ -674,6 +685,12 @@ var moduleImportConfig = `module "aws_config" {
}
`

var customOutput = `output "test" {
description = "test description"
value = module.aws_config.lacework_integration_guid
}
`

var moduleImportConfigWithMultipleAccounts = `terraform {
required_providers {
lacework = {
Expand Down
42 changes: 42 additions & 0 deletions lwgenerate/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,48 @@ type ForEach struct {
value map[string]string
}

type HclOutput struct {
// required, name of the resultant output
name string

// required, converted into a traversal
// e.g. []string{"a", "b", "c"} as input results in traversal having value a.b.c
value []string

// optional
description string
}

func (m *HclOutput) ToBlock() (*hclwrite.Block, error) {
if m.value == nil {
return nil, errors.New("value must be supplied")
}

attributes := map[string]interface{}{
"value": CreateSimpleTraversal(m.value),
}

if m.description != "" {
attributes["description"] = m.description
}

block, err := HclCreateGenericBlock(
"output",
[]string{m.name},
attributes,
)
if err != nil {
return nil, err
}

return block, nil
}

// NewOutput Create a provider statement in the HCL output
func NewOutput(name string, value []string, description string) *HclOutput {
return &HclOutput{name: name, description: description, value: value}
}

type HclModule struct {
// Required, module name
name string
Expand Down
17 changes: 17 additions & 0 deletions lwgenerate/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ func TestModuleBlockWithComplexAttributes(t *testing.T) {
assert.NoError(t, err)
}

func TestOutputBlockCreation(t *testing.T) {
t.Run("should generate correct block for simple output with no description", func(t *testing.T) {
o := lwgenerate.NewOutput("test", []string{"test", "one", "two"}, "")
b, err := o.ToBlock()
assert.NoError(t, err)
str := lwgenerate.CreateHclStringOutput([]*hclwrite.Block{b})
assert.Equal(t, "output \"test\" {\n value = test.one.two\n}\n", str)
})
t.Run("should generate correct block for simple output with description", func(t *testing.T) {
o := lwgenerate.NewOutput("test", []string{"test", "one", "two"}, "test description")
b, err := o.ToBlock()
assert.NoError(t, err)
str := lwgenerate.CreateHclStringOutput([]*hclwrite.Block{b})
assert.Equal(t, "output \"test\" {\n description = \"test description\"\n value = test.one.two\n}\n", str)
})
}

var testRequiredProvider = `terraform {
required_providers {
bar = {
Expand Down
Loading