-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcloud_accounts_test.go
374 lines (337 loc) · 11 KB
/
cloud_accounts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2021, Lacework Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package api_test
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/lacework/go-sdk/v2/api"
"github.com/lacework/go-sdk/v2/internal/intgguid"
"github.com/lacework/go-sdk/v2/internal/lacework"
)
func TestCloudAccountTypeAwsCtsSqs(t *testing.T) {
assert.Equal(t,
"AwsCtSqs", api.AwsCtSqsCloudAccount.String(),
"wrong cloud account type",
)
}
func TestFindCloudAccountType(t *testing.T) {
cloudFound, found := api.FindCloudAccountType("SOME_NON_EXISTING_INTEGRATION")
assert.False(t, found, "cloud account type should not be found")
assert.Equal(t, 0, int(cloudFound), "wrong cloud account type")
assert.Equal(t, "None", cloudFound.String(), "wrong cloud account type")
cloudFound, found = api.FindCloudAccountType("AwsCtSqs")
assert.True(t, found, "cloud account type should exist")
assert.Equal(t, "AwsCtSqs", cloudFound.String(), "wrong cloud account type")
//cloudFound, found = api.FindCloudAccountType("GcpCfg")
//assert.True(t, found, "cloud account type should exist")
//assert.Equal(t, "GcpCfg", cloudFound.String(), "wrong cloud account type")
}
func TestCloudAccountsGet(t *testing.T) {
var (
intgGUID = intgguid.New()
vanillaType = "VANILLA"
apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID)
vanillaInt = singleVanillaCloudAccount(intgGUID, vanillaType, "")
fakeServer = lacework.MockServer()
)
fakeServer.MockToken("TOKEN")
defer fakeServer.Close()
fakeServer.MockAPI(apiPath,
func(w http.ResponseWriter, r *http.Request) {
if assert.Equal(t, "GET", r.Method, "Get() should be a GET method") {
fmt.Fprintf(w, generateCloudAccountResponse(vanillaInt))
}
},
)
fakeServer.MockAPI("CloudAccounts/UNKNOWN_INTG_GUID",
func(w http.ResponseWriter, r *http.Request) {
if assert.Equal(t, "GET", r.Method, "Get() should be a GET method") {
http.Error(w, "{ \"message\": \"Not Found\"}", 404)
}
},
)
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
t.Run("when cloud account exists", func(t *testing.T) {
var response api.CloudAccountResponse
err := c.V2.CloudAccounts.Get(intgGUID, &response)
assert.Nil(t, err)
if assert.NotNil(t, response) {
assert.Equal(t, intgGUID, response.Data.IntgGuid)
assert.Equal(t, "integration_name", response.Data.Name)
assert.Equal(t, "VANILLA", response.Data.Type)
}
})
t.Run("when cloud account does NOT exist", func(t *testing.T) {
var response api.CloudAccountResponse
err := c.V2.CloudAccounts.Get("UNKNOWN_INTG_GUID", response)
assert.Empty(t, response)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "api/v2/CloudAccounts/UNKNOWN_INTG_GUID")
assert.Contains(t, err.Error(), "[404] Not Found")
}
})
}
func TestCloudAccountsDelete(t *testing.T) {
var (
intgGUID = intgguid.New()
vanillaType = "VANILLA"
apiPath = fmt.Sprintf("CloudAccounts/%s", intgGUID)
vanillaInt = singleVanillaCloudAccount(intgGUID, vanillaType, "")
getResponse = generateCloudAccountResponse(vanillaInt)
fakeServer = lacework.MockServer()
)
fakeServer.MockToken("TOKEN")
defer fakeServer.Close()
fakeServer.MockAPI(apiPath,
func(w http.ResponseWriter, r *http.Request) {
if getResponse != "" {
switch r.Method {
case "GET":
fmt.Fprintf(w, getResponse)
case "DELETE":
// once deleted, empty the getResponse so that
// further GET requests return 404s
getResponse = ""
}
} else {
http.Error(w, "{ \"message\": \"Not Found\"}", 404)
}
},
)
fakeServer.MockAPI("CloudAccounts/UNKNOWN_INTG_GUID",
func(w http.ResponseWriter, r *http.Request) {
if assert.Equal(t, "GET", r.Method, "Get() should be a GET method") {
http.Error(w, "{ \"message\": \"Not Found\"}", 404)
}
},
)
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
t.Run("verify cloud account exists", func(t *testing.T) {
var response api.CloudAccountResponse
err := c.V2.CloudAccounts.Get(intgGUID, &response)
assert.Nil(t, err)
if assert.NotNil(t, response) {
assert.Equal(t, intgGUID, response.Data.IntgGuid)
assert.Equal(t, "integration_name", response.Data.Name)
assert.Equal(t, "VANILLA", response.Data.Type)
}
})
t.Run("when cloud account has been deleted", func(t *testing.T) {
err := c.V2.CloudAccounts.Delete(intgGUID)
assert.Nil(t, err)
var response api.CloudAccountResponse
err = c.V2.CloudAccounts.Get(intgGUID, response)
assert.Empty(t, response)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "api/v2/CloudAccounts/MOCK_")
assert.Contains(t, err.Error(), "[404] Not Found")
}
})
}
func TestCloudAccountsList(t *testing.T) {
var (
awsIntgGUIDs = []string{intgguid.New(), intgguid.New(), intgguid.New()}
awsCfgGUIDs = []string{intgguid.New(), intgguid.New(), intgguid.New()}
awsEksAuditLogGUIDs = []string{intgguid.New()}
azureIntgGUIDs = []string{intgguid.New(), intgguid.New()}
gcpIntgGUIDs = []string{
intgguid.New(), intgguid.New(), intgguid.New(), intgguid.New(),
}
allGUIDs = append(awsEksAuditLogGUIDs, append(azureIntgGUIDs, append(awsCfgGUIDs, append(gcpIntgGUIDs, awsIntgGUIDs...)...)...)...)
expectedLen = len(allGUIDs)
fakeServer = lacework.MockServer()
)
fakeServer.MockToken("TOKEN")
fakeServer.MockAPI("CloudAccounts",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method, "List() should be a GET method")
cloudAccounts := []string{
generateCloudAccounts(awsIntgGUIDs, "AwsCtSqs"),
generateCloudAccounts(awsEksAuditLogGUIDs, "AwsEksAudit"),
// TODO @afiune come back here and update these Cloud Accounts types when they exist
generateCloudAccounts(awsCfgGUIDs, "AwsCfg"),
generateCloudAccounts(gcpIntgGUIDs, "AwsCtSqs"), // "GcpCfg"),
generateCloudAccounts(azureIntgGUIDs, "AwsCtSqs"), // "AzureAlSeq"),
}
fmt.Fprintf(w,
generateCloudAccountsResponse(
strings.Join(cloudAccounts, ", "),
),
)
},
)
defer fakeServer.Close()
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
response, err := c.V2.CloudAccounts.List()
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, expectedLen, len(response.Data))
for _, d := range response.Data {
assert.Contains(t, allGUIDs, d.IntgGuid)
}
}
func TestCloudAccountsListByType(t *testing.T) {
var (
awsIntgGUIDs = []string{intgguid.New(), intgguid.New(), intgguid.New()}
expectedLen = len(awsIntgGUIDs)
fakeServer = lacework.MockServer()
)
fakeServer.MockToken("TOKEN")
fakeServer.MockAPI("CloudAccounts/AwsCtSqs",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method, "List() should be a GET method")
cloudAccounts := []string{
generateCloudAccounts(awsIntgGUIDs, "AwsCtSqs"),
}
fmt.Fprintf(w,
generateCloudAccountsResponse(
strings.Join(cloudAccounts, ", "),
),
)
},
)
defer fakeServer.Close()
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
caType, _ := api.FindCloudAccountType("AwsCtSqs")
response, err := c.V2.CloudAccounts.ListByType(caType)
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, expectedLen, len(response.Data))
for _, d := range response.Data {
assert.Contains(t, awsIntgGUIDs, d.IntgGuid)
}
}
func TestCloudAccountMigrate(t *testing.T) {
var (
intgGUID = intgguid.New()
apiPath = "migrateGcpAtSes"
fakeServer = lacework.MockServer()
)
fakeServer.MockToken("TOKEN")
defer fakeServer.Close()
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PATCH", r.Method, "cloudAccountMigrate() should be a PATCH method")
if assert.NotNil(t, r.Body) {
body := httpBodySniffer(r)
assert.Contains(t, body, intgGUID, "INTG_GUID missing")
assert.Contains(t, body, "props", "migration props are missing")
assert.Contains(t, body, "migrate\":true",
"migrate field is missing or it is set to false")
assert.Contains(t, body, "migrationTimestamp", "migration timestamp is missing")
}
fmt.Fprintf(w, generateCloudAccountResponse(singleGcpAtCloudAccount(intgGUID)))
})
c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)
cloudAccount := api.NewCloudAccount("integration_name",
api.GcpAtSesCloudAccount,
api.GcpAtSesData{
Credentials: api.GcpAtSesCredentials{
ClientID: "123456789",
ClientEmail: "[email protected]",
PrivateKeyID: "",
PrivateKey: "",
},
},
)
assert.Equal(t, "integration_name", cloudAccount.Name, "GcpAtSes cloud account name mismatch")
assert.Equal(t, "GcpAtSes", cloudAccount.Type,
"a new GcpAtSes cloud account should match its type")
assert.Equal(t, 1, cloudAccount.Enabled, "a new GcpAtSes cloud account should be enabled")
cloudAccount.IntgGuid = intgGUID
err = c.V2.CloudAccounts.Migrate(intgGUID)
assert.Nil(t, err)
}
func generateCloudAccounts(guids []string, iType string) string {
cloudAccounts := make([]string, len(guids))
for i, guid := range guids {
switch iType {
case api.AwsCtSqsCloudAccount.String():
cloudAccounts[i] = singleAwsCtSqsCloudAccount(guid)
case api.AwsEksAuditCloudAccount.String():
cloudAccounts[i] = singleAwsEksAuditCloudAccount(guid)
case api.AwsCfgCloudAccount.String():
cloudAccounts[i] = singleAwsCfgCloudAccount(guid)
}
}
return strings.Join(cloudAccounts, ", ")
}
func generateCloudAccountsResponse(data string) string {
return `
{
"data": [` + data + `]
}
`
}
func generateCloudAccountResponse(data string) string {
return `
{
"data": ` + data + `
}
`
}
func singleVanillaCloudAccount(id string, iType string, data string) string {
if data == "" {
data = "{}"
}
return `
{
"createdOrUpdatedBy": "[email protected]",
"createdOrUpdatedTime": "2021-06-01T19:28:00.092Z",
"data": ` + data + `,
"enabled": 1,
"intgGuid": "` + id + `",
"isOrg": 0,
"name": "integration_name",
"state": {
"details": {
"complianceOpsDeniedAccess": [
"GetBucketAcl",
"GetBucketLogging"
]
},
"lastSuccessfulTime": 1624456896915,
"lastUpdatedTime": 1624456896915,
"ok": true
},
"type": "` + iType + `"
}
`
}