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

fix(generate): load AWS_PROFILE & AWS_REGION environment variables #1438

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ run-api-example: ## Run an API example like 'make run-api-example example=api/_e
LW_SUBACCOUNT=$(shell lacework configure show subaccount) \
go run $(example)

.PHONY: build
build: ## Compiles binary for the running workstation
go build -o bin/lacework -ldflags=$(GO_LDFLAGS) github.com/lacework/go-sdk/cli
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


.PHONY: build-cli-cross-platform
build-cli-cross-platform: ## Compiles the Lacework CLI for all supported platforms
gox -output="bin/$(PACKAGENAME)-{{.OS}}-{{.Arch}}" \
Expand Down
28 changes: 20 additions & 8 deletions cli/cmd/generate_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/imdario/mergo"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/AlecAivazis/survey/v2"
"github.com/lacework/go-sdk/lwgenerate/aws"
Expand Down Expand Up @@ -102,12 +103,15 @@ This command can also be run in noninteractive mode.
See help output for more details on the parameter value(s) required for Terraform code generation.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cli.Log.Debugw("run cmd", "state", GenerateAwsCommandState)

// Generate TF Code
cli.StartProgress("Generating Terraform Code...")

// Explicitly set Lacework profile if it was passed in main args
if cli.Profile != "default" {
GenerateAwsCommandState.LaceworkProfile = cli.Profile
cli.Log.Debugw("command state changed", "lacework_profile", cli.Profile)
}

// Setup modifiers for NewTerraform constructor
Expand Down Expand Up @@ -206,22 +210,18 @@ See help output for more details on the parameter value(s) required for Terrafor
}

// Validate aws profile, if passed
profile, err := cmd.Flags().GetString("aws_profile")
if err != nil {
return errors.Wrap(err, "failed to load command flags")
}
profile := viper.GetString("aws_profile")
if err := validateAwsProfile(profile); profile != "" && err != nil {
return err
}
GenerateAwsCommandState.AwsProfile = profile

// Validate aws region, if passed
region, err := cmd.Flags().GetString("aws_region")
if err != nil {
return errors.Wrap(err, "failed to load command flags")
}
region := viper.GetString("aws_region")
if err := validateAwsRegion(region); region != "" && err != nil {
return err
}
GenerateAwsCommandState.AwsRegion = region

// Validate cloudtrail bucket arn, if passed
arn, err := cmd.Flags().GetString("existing_bucket_arn")
Expand Down Expand Up @@ -359,11 +359,23 @@ func initGenerateAwsTfCommandFlags() {
"aws_region",
"",
"specify aws region")

// Bind the environment variable AWS_REGION
errcheckWARN(viper.BindPFlag("aws_region",
generateAwsTfCommand.PersistentFlags().Lookup("aws_region")))
errcheckWARN(viper.BindEnv("aws_region", "AWS_REGION"))

generateAwsTfCommand.PersistentFlags().StringVar(
&GenerateAwsCommandState.AwsProfile,
"aws_profile",
"",
"specify aws profile")

// Bind the environment variable AWS_PROFILE
errcheckWARN(viper.BindPFlag("aws_profile",
generateAwsTfCommand.PersistentFlags().Lookup("aws_profile")))
errcheckWARN(viper.BindEnv("aws_profile", "AWS_PROFILE"))

generateAwsTfCommand.PersistentFlags().StringVar(
&GenerateAwsCommandState.AwsAssumeRole,
"aws_assume_role",
Expand Down
15 changes: 15 additions & 0 deletions integration/aws_generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,21 @@ func TestGenerationAwsS3BucketNotification(t *testing.T) {
assert.Equal(t, buildTf, tfResult)
}

func TestGenerationAwsWithEnvironmentVariable(t *testing.T) {
// setting this silly region is on purpose so that we run the CLI
// in non-interactive mode and we catch the error that the region
// is invalid, we are just testing the environment variables
os.Setenv("AWS_REGION", "something-silly") // this is on purpose
defer os.Setenv("AWS_REGION", "") // so if fails and we check

out, err, exitcode := LaceworkCLIWithTOMLConfig(
"generate", "cloud-account", "aws", "--config", "--noninteractive")
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
assert.Empty(t, out.String(), "STDOUT should be empty")
assert.Contains(t, err.String(), "invalid region name supplied",
"STDERR changed, please check")
}

func TestGenerationAwsS3BucketNotificationInteractive(t *testing.T) {
os.Setenv("LW_NOCACHE", "true")
defer os.Setenv("LW_NOCACHE", "")
Expand Down