Skip to content

Commit 9e12df9

Browse files
authored
feat(RAIN-92599): Add AzureAdAl integration support (#1590)
- Add support for get and update methods for AzureAdAl integration - Add unit tests
1 parent e076880 commit 9e12df9

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
8+
"github.com/lacework/go-sdk/api"
9+
)
10+
11+
func main() {
12+
lacework, err := api.NewClient(os.Getenv("LW_ACCOUNT"),
13+
api.WithSubaccount(os.Getenv("LW_SUBACCOUNT")),
14+
api.WithApiKeys(os.Getenv("LW_API_KEY"), os.Getenv("LW_API_SECRET")),
15+
)
16+
if err != nil {
17+
log.Fatal(err)
18+
}
19+
20+
res, err := lacework.V2.CloudAccounts.List()
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
for _, account := range res.Data {
26+
support := "Unsupported"
27+
switch account.Type {
28+
case api.AzureAdAlCloudAccount.String():
29+
support = "Supported"
30+
}
31+
32+
// Output: INTEGRATION-GUID:INTEGRATION-TYPE:[Supported|Unsupported]
33+
fmt.Printf("%s:%s:%s\n", account.IntgGuid, account.Type, support)
34+
}
35+
36+
azureAdAlData := api.AzureAdAlData{
37+
Credentials: api.AzureAdAlCredentials{
38+
ClientID: "client-id",
39+
ClientSecret: "some-secret",
40+
},
41+
TenantID: "tenant-id",
42+
EventHubNamespace: "EventHubNamespace",
43+
EventHubName: "EventHubName",
44+
}
45+
46+
azureAdAlCloudAccount := api.NewCloudAccount(
47+
"cloud-from-golang",
48+
api.AzureAdAlCloudAccount,
49+
azureAdAlData,
50+
)
51+
52+
azureAdAlIntegrationResponse, err := lacework.V2.CloudAccounts.Create(azureAdAlCloudAccount)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
57+
// Output: AzureAdAl Cloud Account created: THE-INTEGRATION-GUID
58+
fmt.Printf("Cloud Account created: %s", azureAdAlIntegrationResponse.Data.IntgGuid)
59+
}

api/cloud_accounts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const (
9191
AwsSidekickOrgCloudAccount
9292
AwsUsGovCfgCloudAccount
9393
AwsUsGovCtSqsCloudAccount
94+
AzureAdAlCloudAccount
9495
AzureAlSeqCloudAccount
9596
AzureCfgCloudAccount
9697
GcpAtSesCloudAccount
@@ -112,6 +113,7 @@ var CloudAccountTypes = map[cloudAccountType]string{
112113
AwsSidekickOrgCloudAccount: "AwsSidekickOrg",
113114
AwsUsGovCfgCloudAccount: "AwsUsGovCfg",
114115
AwsUsGovCtSqsCloudAccount: "AwsUsGovCtSqs",
116+
AzureAdAlCloudAccount: "AzureAdAl",
115117
AzureAlSeqCloudAccount: "AzureAlSeq",
116118
AzureCfgCloudAccount: "AzureCfg",
117119
GcpAtSesCloudAccount: "GcpAtSes",

api/cloud_accounts_azure_ad_al.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Author:: Rubinder Singh (<[email protected]>)
3+
// Copyright:: Copyright 2024, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api
20+
21+
// GetAzureAdAl gets a single AzureAdAl integration matching the
22+
// provided integration guid
23+
func (svc *CloudAccountsService) GetAzureAdAl(guid string) (
24+
response AzureAdAlIntegrationResponse,
25+
err error,
26+
) {
27+
err = svc.get(guid, &response)
28+
return
29+
}
30+
31+
// UpdateAzureAdAl updates a single AzureAdAl integration on the Lacework Server
32+
func (svc *CloudAccountsService) UpdateAzureAdAl(data CloudAccount) (
33+
response AzureAdAlIntegrationResponse,
34+
err error,
35+
) {
36+
err = svc.update(data.ID(), data, &response)
37+
return
38+
}
39+
40+
type AzureAdAlIntegrationResponse struct {
41+
Data AzureAdAl `json:"data"`
42+
}
43+
44+
type AzureAdAl struct {
45+
v2CommonIntegrationData
46+
Data AzureAdAlData `json:"data"`
47+
}
48+
49+
type AzureAdAlData struct {
50+
Credentials AzureAdAlCredentials `json:"credentials"`
51+
TenantID string `json:"tenantId"`
52+
EventHubNamespace string `json:"eventHubNamespace"`
53+
EventHubName string `json:"eventHubName"`
54+
}
55+
56+
type AzureAdAlCredentials struct {
57+
ClientID string `json:"clientId"`
58+
ClientSecret string `json:"clientSecret"`
59+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//
2+
// Author:: Rubinder Singh (<[email protected]>)
3+
// Copyright:: Copyright 2024, Lacework Inc.
4+
// License:: Apache License, Version 2.0
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
package api_test
20+
21+
import (
22+
"fmt"
23+
"net/http"
24+
"testing"
25+
26+
"github.com/lacework/go-sdk/api"
27+
"github.com/lacework/go-sdk/internal/intgguid"
28+
"github.com/lacework/go-sdk/internal/lacework"
29+
"github.com/stretchr/testify/assert"
30+
)
31+
32+
func TestCloudAccountsAzureAdAlGet(t *testing.T) {
33+
var (
34+
intgGUID = intgguid.New()
35+
apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID)
36+
fakeServer = lacework.MockServer()
37+
)
38+
fakeServer.MockToken("TOKEN")
39+
defer fakeServer.Close()
40+
41+
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) {
42+
assert.Equal(t, "GET", r.Method, "GetAzureAdAl() should be a GET method")
43+
fmt.Fprintf(w, generateCloudAccountResponse(azureAdAlCloudAccount(intgGUID)))
44+
})
45+
46+
c, err := api.NewClient("test",
47+
api.WithToken("TOKEN"),
48+
api.WithURL(fakeServer.URL()),
49+
)
50+
assert.Nil(t, err)
51+
52+
response, err := c.V2.CloudAccounts.GetAzureAdAl(intgGUID)
53+
assert.Nil(t, err)
54+
assert.NotNil(t, response)
55+
assert.Equal(t, intgGUID, response.Data.IntgGuid)
56+
assert.Equal(t, "azure_ad_al_integration_test", response.Data.Name)
57+
assert.True(t, response.Data.State.Ok)
58+
assert.Equal(t, "123456777", response.Data.Data.Credentials.ClientID)
59+
assert.Equal(t, "test-secret-1234", response.Data.Data.Credentials.ClientSecret)
60+
assert.Equal(t, "AzureAdAl", response.Data.Type)
61+
assert.Equal(t, "tenant-1", response.Data.Data.TenantID)
62+
assert.Equal(t, "eventHubNamespace-1", response.Data.Data.EventHubNamespace)
63+
assert.Equal(t, "eventHubName-1", response.Data.Data.EventHubName)
64+
}
65+
66+
func TestCloudAccountsAzureAdAlUpdate(t *testing.T) {
67+
var (
68+
intgGUID = intgguid.New()
69+
apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID)
70+
fakeServer = lacework.MockServer()
71+
intgData = api.AzureAdAlData{
72+
TenantID: "tenant-1",
73+
Credentials: api.AzureAdAlCredentials{
74+
ClientID: "123456777",
75+
ClientSecret: "test-secret-1234",
76+
},
77+
EventHubNamespace: "eventHubNamespace-1",
78+
EventHubName: "eventHubName-1",
79+
}
80+
)
81+
fakeServer.MockToken("TOKEN")
82+
defer fakeServer.Close()
83+
84+
// Step 1 - Start Fake Server to return updated data
85+
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) {
86+
assert.Equal(t, "PATCH", r.Method, "UpdateAzureAdAl() should be a PATCH method")
87+
88+
if assert.NotNil(t, r.Body) {
89+
body := httpBodySniffer(r)
90+
assert.Contains(t, body, intgGUID, "INTG_GUID missing")
91+
assert.Contains(t, body, "azure_ad_al_integration_test", "cloud account name is missing")
92+
assert.Contains(t, body, "AzureAdAl", "wrong cloud account type")
93+
assert.Contains(t, body, intgData.Credentials.ClientID, "wrong ClientId")
94+
assert.Contains(t, body, intgData.Credentials.ClientSecret, "wrong ClientSecret")
95+
assert.Contains(t, body, intgData.TenantID, "wrong TenantId")
96+
assert.Contains(t, body, intgData.EventHubNamespace, "wrong EventHubNamespace")
97+
assert.Contains(t, body, intgData.EventHubName, "wrong EventHubName")
98+
assert.Contains(t, body, "enabled\":1", "cloud account is not enabled")
99+
}
100+
101+
fmt.Fprintf(w, generateCloudAccountResponse(azureAdAlCloudAccount(intgGUID)))
102+
})
103+
104+
c, err := api.NewClient("test",
105+
api.WithToken("TOKEN"),
106+
api.WithURL(fakeServer.URL()),
107+
)
108+
assert.Nil(t, err)
109+
110+
// Step 2 - Get Updated data from Fake server
111+
cloudAccount := api.NewCloudAccount("azure_ad_al_integration_test",
112+
api.AzureAdAlCloudAccount,
113+
intgData,
114+
)
115+
116+
cloudAccount.IntgGuid = intgGUID
117+
response, err := c.V2.CloudAccounts.UpdateAzureAdAl(cloudAccount)
118+
assert.Nil(t, err, "Cannot update integration")
119+
assert.NotNil(t, response)
120+
integration := response.Data
121+
assert.Equal(t, intgGUID, integration.IntgGuid)
122+
123+
integrationData := integration.Data
124+
assert.Equal(t, "azure_ad_al_integration_test", cloudAccount.Name)
125+
assert.Equal(t, "AzureAdAl", cloudAccount.Type)
126+
assert.Equal(t, 1, cloudAccount.Enabled)
127+
assert.Equal(t, "tenant-1", integrationData.TenantID)
128+
assert.Equal(t, "eventHubNamespace-1", integrationData.EventHubNamespace)
129+
assert.Equal(t, "eventHubName-1", integrationData.EventHubName)
130+
assert.Equal(t, "123456777", integrationData.Credentials.ClientID)
131+
assert.Equal(t, "test-secret-1234", integrationData.Credentials.ClientSecret)
132+
}
133+
134+
func azureAdAlCloudAccount(id string) string {
135+
return fmt.Sprintf(`{
136+
"createdOrUpdatedBy": "[email protected]",
137+
"createdOrUpdatedTime": "2024-03-11T00:00:00.000Z",
138+
"enabled": 1,
139+
"intgGuid": %q,
140+
"isOrg": 0,
141+
"name": "azure_ad_al_integration_test",
142+
"state": {
143+
"ok": true,
144+
"lastUpdatedTime": 1710104691000,
145+
"lastSuccessfulTime": 1710104691000,
146+
"details": {
147+
"queueRx": "OK",
148+
"decodeNtfn": "OK",
149+
"logFileGet": "OK",
150+
"queueDel": "OK",
151+
"lastMsgRxTime": 1710104691000,
152+
"noData": true
153+
}
154+
},
155+
"type": "AzureAdAl",
156+
"data": {
157+
"credentials": {
158+
"clientId": "123456777",
159+
"clientSecret": "test-secret-1234"
160+
},
161+
"tenantId": "tenant-1",
162+
"eventHubNamespace": "eventHubNamespace-1",
163+
"eventHubName": "eventHubName-1"
164+
}
165+
}`, id)
166+
}

0 commit comments

Comments
 (0)