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

refactor: account mapping file for consolidated CT #252

Merged
merged 1 commit into from
Dec 2, 2020
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
12 changes: 6 additions & 6 deletions api/integrations_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,25 +176,25 @@ type AwsIntegrationData struct {
AccountMappingFile string `json:"ACCOUNT_MAPPING_FILE,omitempty" mapstructure:"ACCOUNT_MAPPING_FILE"`
}

func (aws *AwsIntegrationData) EncodeAccountMappingFile(mapping string) {
encodedMappings := base64.StdEncoding.EncodeToString([]byte(mapping))
func (aws *AwsIntegrationData) EncodeAccountMappingFile(mapping []byte) {
encodedMappings := base64.StdEncoding.EncodeToString(mapping)
aws.AccountMappingFile = fmt.Sprintf("data:application/json;name=i.json;base64,%s", encodedMappings)
}

func (aws *AwsIntegrationData) DecodeAccountMappingFile() (string, error) {
func (aws *AwsIntegrationData) DecodeAccountMappingFile() ([]byte, error) {
if len(aws.AccountMappingFile) == 0 {
return "", nil
return []byte{}, nil
}

var (
b64 = strings.Split(aws.AccountMappingFile, ",")
raw, err = base64.StdEncoding.DecodeString(b64[1])
)
if err != nil {
return "", err
return []byte{}, err
}

return string(raw), nil
return raw, nil
}

type AwsIntegrationCreds struct {
Expand Down
12 changes: 6 additions & 6 deletions api/integrations_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestIntegrationsNewAwsCfgIntegration(t *testing.T) {
}

func TestIntegrationsNewAwsCfgIntegrationWithCustomTemplateFile(t *testing.T) {
accountMappingJSON := `{
accountMappingJSON := []byte(`{
"defaultLaceworkAccountAws": "lw_account_1",
"integration_mappings": {
"lw_account_2": {
Expand All @@ -60,7 +60,7 @@ func TestIntegrationsNewAwsCfgIntegrationWithCustomTemplateFile(t *testing.T) {
]
}
}
}`
}`)
awsData := api.AwsIntegrationData{
Credentials: api.AwsIntegrationCreds{
RoleArn: "arn:foo:bar",
Expand All @@ -76,16 +76,16 @@ func TestIntegrationsNewAwsCfgIntegrationWithCustomTemplateFile(t *testing.T) {
"data:application/json;name=i.json;base64,",
"check the custom_template_file encoder",
)
accountMappingString, err := subject.Data.DecodeAccountMappingFile()
accountMapping, err := subject.Data.DecodeAccountMappingFile()
assert.Nil(t, err)
assert.Equal(t, accountMappingJSON, accountMappingString)
assert.Equal(t, accountMappingJSON, accountMapping)

// When there is no custom account mapping file, this function should
// return an empty string to match the pattern
subject.Data.AccountMappingFile = ""
accountMappingString, err = subject.Data.DecodeAccountMappingFile()
accountMapping, err = subject.Data.DecodeAccountMappingFile()
assert.Nil(t, err)
assert.Equal(t, "", accountMappingString)
assert.Empty(t, accountMapping)

}

Expand Down
28 changes: 26 additions & 2 deletions cli/cmd/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,25 @@ func reflectIntegrationData(raw api.RawIntegration) [][]string {
[]string{"EXTERNAL ID", iData.Credentials.ExternalID},
}
if iData.QueueUrl != "" {
return append(out, []string{"QUEUE URL", iData.QueueUrl})
out = append(out, []string{"QUEUE URL", iData.QueueUrl})
}

accountMapping, err := iData.DecodeAccountMappingFile()
if err != nil {
cli.Log.Debugw("unable to decode account mapping file",
"integration_type", raw.Type,
"raw_data", iData.AccountMappingFile,
"error", err,
)
}

if len(accountMapping) != 0 {
// @afiune should we disable the colors here?
accountMappingJSON, err := cli.FormatJSONString(string(accountMapping))
if err != nil {
accountMappingJSON = string(accountMapping)
}
out = append(out, []string{"ACCOUNT MAPPING FILE", accountMappingJSON})
}
return out

Expand Down Expand Up @@ -530,14 +548,20 @@ func reflectIntegrationData(raw api.RawIntegration) [][]string {
"error", err,
)
}

// @afiune should we disable the colors here?
tmplStrPretty, err := cli.FormatJSONString(templateString)
if err != nil {
tmplStrPretty = templateString
}
out := [][]string{
[]string{"JIRA INTEGRATION TYPE", iData.JiraType},
[]string{"JIRA URL", iData.JiraUrl},
[]string{"PROJECT KEY", iData.ProjectID},
[]string{"USERNAME", iData.Username},
[]string{"ISSUE TYPE", iData.IssueType},
[]string{"ISSUE GROUPING", iData.IssueGrouping},
[]string{"CUSTOM TEMPLATE FILE", templateString},
[]string{"CUSTOM TEMPLATE FILE", tmplStrPretty},
}

return out
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/integration_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func createAwsCloudTrailIntegration() error {
return err
}

aws.EncodeAccountMappingFile(content)
aws.EncodeAccountMappingFile([]byte(content))
}

awsCT := api.NewAwsCloudTrailIntegration(answers.Name, aws)
Expand Down