Skip to content

Commit feb5cbe

Browse files
authored
add account mapping for aws and gcp sidekick in api (#1251)
1 parent e45215b commit feb5cbe

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

api/cloud_accounts_aws_sidekick_org.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
package api
2020

21+
import (
22+
"encoding/base64"
23+
"fmt"
24+
"strings"
25+
)
26+
2127
// GetAwsSidekickOrg gets a single AwsSidekickOrg integration matching the provided integration guid
2228
func (svc *CloudAccountsService) GetAwsSidekickOrg(guid string) (
2329
response AwsSidekickOrgResponse,
@@ -70,7 +76,29 @@ type AwsSidekickOrgData struct {
7076
ManagementAccount string `json:"managementAccount,omitempty"`
7177
MonitoredAccounts string `json:"monitoredAccounts"`
7278

73-
AccountID string `json:"awsAccountId,omitempty"`
74-
BucketArn string `json:"bucketArn,omitempty"`
75-
CrossAccountCreds AwsSidekickCrossAccountCredentials `json:"crossAccountCredentials"`
79+
AccountID string `json:"awsAccountId,omitempty"`
80+
BucketArn string `json:"bucketArn,omitempty"`
81+
CrossAccountCreds AwsSidekickCrossAccountCredentials `json:"crossAccountCredentials"`
82+
AccountMappingFile string `json:"accountMappingFile,omitempty"`
83+
}
84+
85+
func (aws *AwsSidekickOrgData) EncodeAccountMappingFile(mapping []byte) {
86+
encodedMappings := base64.StdEncoding.EncodeToString(mapping)
87+
aws.AccountMappingFile = fmt.Sprintf("data:application/json;name=i.json;base64,%s", encodedMappings)
88+
}
89+
90+
func (aws *AwsSidekickOrgData) DecodeAccountMappingFile() ([]byte, error) {
91+
if len(aws.AccountMappingFile) == 0 {
92+
return []byte{}, nil
93+
}
94+
95+
var (
96+
b64 = strings.Split(aws.AccountMappingFile, ",")
97+
raw, err = base64.StdEncoding.DecodeString(b64[1])
98+
)
99+
if err != nil {
100+
return []byte{}, err
101+
}
102+
103+
return raw, nil
76104
}

api/cloud_accounts_aws_sidekick_org_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,30 @@ import (
3030
)
3131

3232
func TestCloudAccountsNewAwsSidekickOrgWithCustomTemplateFile(t *testing.T) {
33+
accountMappingJSON := []byte(`{
34+
"defaultLaceworkAccountAws": "lw_account_1",
35+
"integration_mappings": {
36+
"lw_account_2": {
37+
"aws_accounts": [
38+
"234556677",
39+
"774564564"
40+
]
41+
},
42+
"lw_account_3": {
43+
"aws_accounts": [
44+
"553453453",
45+
"934534535"
46+
]
47+
}
48+
}
49+
}`)
3350
awsSidekickOrgData := api.AwsSidekickOrgData{
3451
CrossAccountCreds: api.AwsSidekickCrossAccountCredentials{
3552
RoleArn: "arn:foo:bar",
3653
ExternalID: "0123456789",
3754
},
3855
}
39-
56+
awsSidekickOrgData.EncodeAccountMappingFile(accountMappingJSON)
4057
subject := api.NewCloudAccount("integration_name", api.AwsSidekickOrgCloudAccount, awsSidekickOrgData)
4158
assert.Equal(t, api.AwsSidekickOrgCloudAccount.String(), subject.Type)
4259

@@ -45,6 +62,21 @@ func TestCloudAccountsNewAwsSidekickOrgWithCustomTemplateFile(t *testing.T) {
4562

4663
assert.Equal(t, subjectData.CrossAccountCreds.RoleArn, "arn:foo:bar")
4764
assert.Equal(t, subjectData.CrossAccountCreds.ExternalID, "0123456789")
65+
assert.Contains(t,
66+
subjectData.AccountMappingFile,
67+
"data:application/json;name=i.json;base64,",
68+
"check the custom_template_file encoder",
69+
)
70+
accountMapping, err := subjectData.DecodeAccountMappingFile()
71+
assert.Nil(t, err)
72+
assert.Equal(t, accountMappingJSON, accountMapping)
73+
74+
// When there is no custom account mapping file, this function should
75+
// return an empty string to match the pattern
76+
subjectData.AccountMappingFile = ""
77+
accountMapping, err = subjectData.DecodeAccountMappingFile()
78+
assert.Nil(t, err)
79+
assert.Empty(t, accountMapping)
4880
}
4981

5082
func TestCloudAccountsAwsSidekickOrgGet(t *testing.T) {

api/cloud_accounts_gcp_sidekick.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
package api
2020

21+
import (
22+
"encoding/base64"
23+
"fmt"
24+
"strings"
25+
)
26+
2127
// GetGcpSidekick gets a single GcpSidekick integration matching the provided integration guid
2228
func (svc *CloudAccountsService) GetGcpSidekick(guid string) (
2329
response GcpSidekickIntegrationResponse,
@@ -70,9 +76,10 @@ type GcpSidekickData struct {
7076
FilterList string `json:"filterList,omitempty"`
7177
QueryText string `json:"queryText,omitempty"`
7278
//ScanFrequency in hours, 24 == 24 hours
73-
ScanFrequency int `json:"scanFrequency"`
74-
ScanContainers bool `json:"scanContainers"`
75-
ScanHostVulnerabilities bool `json:"scanHostVulnerabilities"`
79+
ScanFrequency int `json:"scanFrequency"`
80+
ScanContainers bool `json:"scanContainers"`
81+
ScanHostVulnerabilities bool `json:"scanHostVulnerabilities"`
82+
AccountMappingFile string `json:"accountMappingFile,omitempty"`
7683
}
7784

7885
type GcpSidekickCredentials struct {
@@ -82,3 +89,24 @@ type GcpSidekickCredentials struct {
8289
PrivateKey string `json:"privateKey,omitempty"`
8390
TokenUri string `json:"tokenUri,omitempty"`
8491
}
92+
93+
func (gcp *GcpSidekickData) EncodeAccountMappingFile(mapping []byte) {
94+
encodedMappings := base64.StdEncoding.EncodeToString(mapping)
95+
gcp.AccountMappingFile = fmt.Sprintf("data:application/json;name=i.json;base64,%s", encodedMappings)
96+
}
97+
98+
func (gcp *GcpSidekickData) DecodeAccountMappingFile() ([]byte, error) {
99+
if len(gcp.AccountMappingFile) == 0 {
100+
return []byte{}, nil
101+
}
102+
103+
var (
104+
b64 = strings.Split(gcp.AccountMappingFile, ",")
105+
raw, err = base64.StdEncoding.DecodeString(b64[1])
106+
)
107+
if err != nil {
108+
return []byte{}, err
109+
}
110+
111+
return raw, nil
112+
}

api/cloud_accounts_gcp_sidekick_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,29 @@ var (
7171
)
7272

7373
func TestCloudAccountsGcpSidekickCreate(t *testing.T) {
74+
accountMappingJSON := []byte(`{
75+
"defaultLaceworkAccountAws": "lw_account_1",
76+
"integration_mappings": {
77+
"lw_account_2": {
78+
"aws_accounts": [
79+
"234556677",
80+
"774564564"
81+
]
82+
},
83+
"lw_account_3": {
84+
"aws_accounts": [
85+
"553453453",
86+
"934534535"
87+
]
88+
}
89+
}
90+
}`)
7491
integration := api.NewCloudAccount("integration_name", api.GcpSidekickCloudAccount, gcpSidekickData)
7592
assert.Equal(t, api.GcpSidekickCloudAccount.String(), integration.Type)
7693

7794
// casting the data interface{} to type GcpSidekickData
7895
integrationData := integration.Data.(api.GcpSidekickData)
96+
integrationData.EncodeAccountMappingFile(accountMappingJSON)
7997

8098
assert.Equal(t, integrationData.IDType, "PROJECT")
8199
assert.Equal(t, integrationData.ID, "12345")
@@ -90,6 +108,21 @@ func TestCloudAccountsGcpSidekickCreate(t *testing.T) {
90108
assert.Equal(t, integrationData.Credentials.PrivateKeyID, "privateKeyID")
91109
assert.Equal(t, integrationData.Credentials.PrivateKey, "privateKey")
92110
assert.Equal(t, integrationData.Credentials.TokenUri, "tokenTest")
111+
assert.Contains(t,
112+
integrationData.AccountMappingFile,
113+
"data:application/json;name=i.json;base64,",
114+
"check the custom_template_file encoder",
115+
)
116+
accountMapping, err := integrationData.DecodeAccountMappingFile()
117+
assert.Nil(t, err)
118+
assert.Equal(t, accountMappingJSON, accountMapping)
119+
120+
// When there is no custom account mapping file, this function should
121+
// return an empty string to match the pattern
122+
integrationData.AccountMappingFile = ""
123+
accountMapping, err = integrationData.DecodeAccountMappingFile()
124+
assert.Nil(t, err)
125+
assert.Empty(t, accountMapping)
93126
}
94127

95128
func TestCloudAccountsGcpSidekickGet(t *testing.T) {

0 commit comments

Comments
 (0)