This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
forked from lacework/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_accounts.go
296 lines (261 loc) · 8.62 KB
/
cloud_accounts.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
//
// 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
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/circleci/fork-lacework-go-sdk/lwtime"
)
// CloudAccountsService is the service that interacts with
// the CloudAccounts schema from the Lacework APIv2 Server
type CloudAccountsService struct {
client *Client
}
// NewCloudAccount returns an instance of the CloudAccountRaw struct with the
// provided Cloud Account integration type, name and raw data as an interface{}.
//
// NOTE: This function must be used by any Cloud Account type.
//
// Basic usage: Initialize a new AwsIntegration struct, then use the new
// instance to do CRUD operations
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// awsCtSqs := api.NewCloudAccount("foo",
// api.AwsCtSqsCloudAccount,
// api.AwsCtSqsData{
// QueueUrl: "https://sqs.us-west-2.amazonaws.com/123456789000/lw",
// Credentials: &api.AwsCtSqsCredentials {
// RoleArn: "arn:aws:XYZ",
// ExternalID: "1",
// },
// },
// )
//
// client.V2.CloudAccount.Create(awsCtSqs)
func NewCloudAccount(name string, iType cloudAccountType, data interface{}) CloudAccountRaw {
return CloudAccountRaw{
v2CommonIntegrationData: v2CommonIntegrationData{
Name: name,
Type: iType.String(),
Enabled: 1,
},
Data: data,
}
}
// CloudAccount is an interface that helps us implement a few functions
// that any Cloud Account might use, there are some cases, like during
// Update, where we need to get the ID of the Cloud Account and its type,
// this will allow users to pass any Cloud Account that implements these
// methods
type CloudAccount interface {
ID() string
CloudAccountType() cloudAccountType
}
type cloudAccountType int
const (
// type that defines a non-existing Cloud Account integration
NoneCloudAccount cloudAccountType = iota
AwsCfgCloudAccount
AwsCtSqsCloudAccount
AwsEksAuditCloudAccount
AwsSidekickCloudAccount
AwsSidekickOrgCloudAccount
AwsUsGovCfgCloudAccount
AwsUsGovCtSqsCloudAccount
AzureAdAlCloudAccount
AzureAlSeqCloudAccount
AzureCfgCloudAccount
GcpAtSesCloudAccount
GcpCfgCloudAccount
GcpGkeAuditCloudAccount
GcpSidekickCloudAccount
AzureSidekickCloudAccount
GcpAlPubSubCloudAccount
OciCfgCloudAccount
)
// CloudAccountTypes is the list of available Cloud Account integration types
var CloudAccountTypes = map[cloudAccountType]string{
NoneCloudAccount: "None",
AwsCfgCloudAccount: "AwsCfg",
AwsCtSqsCloudAccount: "AwsCtSqs",
AwsEksAuditCloudAccount: "AwsEksAudit",
AwsSidekickCloudAccount: "AwsSidekick",
AwsSidekickOrgCloudAccount: "AwsSidekickOrg",
AwsUsGovCfgCloudAccount: "AwsUsGovCfg",
AwsUsGovCtSqsCloudAccount: "AwsUsGovCtSqs",
AzureAdAlCloudAccount: "AzureAdAl",
AzureAlSeqCloudAccount: "AzureAlSeq",
AzureCfgCloudAccount: "AzureCfg",
GcpAtSesCloudAccount: "GcpAtSes",
GcpCfgCloudAccount: "GcpCfg",
GcpGkeAuditCloudAccount: "GcpGkeAudit",
GcpSidekickCloudAccount: "GcpSidekick",
AzureSidekickCloudAccount: "AzureSidekick",
GcpAlPubSubCloudAccount: "GcpAlPubSub",
OciCfgCloudAccount: "OciCfg",
}
// String returns the string representation of a Cloud Account integration type
func (i cloudAccountType) String() string {
return CloudAccountTypes[i]
}
// FindCloudAccountType looks up inside the list of available cloud account types
// the matching type from the provided string, if none, returns NoneCloudAccount
func FindCloudAccountType(cloudAccount string) (cloudAccountType, bool) {
for cType, cStr := range CloudAccountTypes {
if cStr == cloudAccount {
return cType, true
}
}
return NoneCloudAccount, false
}
// List returns a list of Cloud Account integrations
func (svc *CloudAccountsService) List() (response CloudAccountsResponse, err error) {
err = svc.client.RequestDecoder("GET", apiV2CloudAccounts, nil, &response)
return
}
// ListByType lists the cloud accounts from the provided type that are available
// on the Lacework Server
func (svc *CloudAccountsService) ListByType(caType cloudAccountType) (response CloudAccountsResponse, err error) {
err = svc.get(caType.String(), &response)
return
}
// Create creates a single Cloud Account integration
func (svc *CloudAccountsService) Create(integration CloudAccountRaw) (
response CloudAccountResponse,
err error,
) {
err = svc.create(integration, &response)
return
}
// Delete deletes a Cloud Account integration that matches the provided guid
func (svc *CloudAccountsService) Delete(guid string) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
return svc.client.RequestDecoder(
"DELETE",
fmt.Sprintf(apiV2CloudAccountsWithParam, guid),
nil,
nil,
)
}
// Migrate marks a Cloud Account integration that matches the provided guid for migration
func (svc *CloudAccountsService) Migrate(guid string) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
data := MigrateRequestData{
MigrateData{
IntgGuid: guid,
Props: Props{
Migrate: true,
MigrationTimestamp: time.Now(),
},
},
}
return svc.client.RequestEncoderDecoder(
"PATCH",
apiV2MigrateGcpAtSes,
data,
nil,
)
}
// Get returns a raw response of the Cloud Account with the matching integration guid.
//
// To return a more specific Go struct of a Cloud Account integration, use the proper
// method such as GetAwsCtSqs() where the function name is composed by:
//
// Get<Type>(guid)
//
// Where <Type> is the Cloud Account integration type.
func (svc *CloudAccountsService) Get(guid string, response interface{}) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
return svc.get(guid, &response)
}
type CloudAccountRaw struct {
v2CommonIntegrationData
Data interface{} `json:"data,omitempty"`
}
func (cloud CloudAccountRaw) GetData() any {
return cloud.Data
}
func (cloud CloudAccountRaw) GetCommon() v2CommonIntegrationData {
return cloud.v2CommonIntegrationData
}
func (cloud CloudAccountRaw) CloudAccountType() cloudAccountType {
t, _ := FindCloudAccountType(cloud.Type)
return t
}
type CloudAccountResponse struct {
Data CloudAccountRaw `json:"data"`
}
type CloudAccountsResponse struct {
Data []CloudAccountRaw `json:"data"`
}
type v2CommonIntegrationData struct {
IntgGuid string `json:"intgGuid,omitempty"`
Name string `json:"name"`
CreatedOrUpdatedTime string `json:"createdOrUpdatedTime,omitempty"`
CreatedOrUpdatedBy string `json:"createdOrUpdatedBy,omitempty"`
Type string `json:"type"`
Enabled int `json:"enabled"`
IsOrg int `json:"isOrg,omitempty"`
State *V2IntegrationState `json:"state,omitempty"`
}
func (c v2CommonIntegrationData) ID() string {
return c.IntgGuid
}
func (c v2CommonIntegrationData) Status() string {
if c.Enabled == 1 {
return "Enabled"
}
return "Disabled"
}
func (c v2CommonIntegrationData) StateString() string {
if c.State != nil && c.State.Ok {
return "Ok"
}
return "Pending"
}
type V2IntegrationState struct {
Ok bool `json:"ok"`
Details map[string]interface{} `json:"details"`
LastUpdatedTime lwtime.Epoch `json:"lastUpdatedTime"`
LastSuccessfulTime lwtime.Epoch `json:"lastSuccessfulTime"`
}
func (svc *CloudAccountsService) create(data interface{}, response interface{}) error {
return svc.client.RequestEncoderDecoder("POST", apiV2CloudAccounts, data, response)
}
func (svc *CloudAccountsService) get(param string, response interface{}) error {
apiPath := fmt.Sprintf(apiV2CloudAccountsWithParam, param)
return svc.client.RequestDecoder("GET", apiPath, nil, response)
}
func (svc *CloudAccountsService) update(guid string, data interface{}, response interface{}) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
apiPath := fmt.Sprintf(apiV2CloudAccountsWithParam, guid)
return svc.client.RequestEncoderDecoder("PATCH", apiPath, data, response)
}