/
container_registries.go
275 lines (242 loc) · 8.27 KB
/
container_registries.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
//
// Author:: Salim Afiune Maya (<afiune@lacework.net>)
// 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"
"github.com/fatih/structs"
"github.com/pkg/errors"
)
// ContainerRegistriesService is the service that interacts with
// the ContainerRegistries schema from the Lacework APIv2 Server
type ContainerRegistriesService struct {
client *Client
}
// NewContainerRegistry returns an instance of the ContainerRegistryRaw struct with the
// provided Container Registry integration type, name and raw data as an interface{}.
//
// NOTE: This function must be used by any Container Registry type.
//
// Basic usage: Initialize a new GhcrContainerRegistry integration struct, then
//
// use the new instance to do CRUD operations
//
// client, err := api.NewClient("account")
// if err != nil {
// return err
// }
//
// ghcr := api.NewContainerRegistry("foo",
// api.GhcrContainerRegistry,
// api.GhcrData{
// Credentials: api.GhcrCredentials {
// Username: "bubu",
// Password: "supers3cret",
// Ssl: true,
// },
// },
// )
//
// client.V2.ContainerRegistries.Create(ghcr)
func NewContainerRegistry(name string, regType containerRegistryType, data interface{}) ContainerRegistryRaw {
reg := ContainerRegistryRaw{
v2CommonIntegrationData: v2CommonIntegrationData{
Name: name,
Type: "ContVulnCfg",
Enabled: 1,
},
}
switch regType {
case GcpGarContainerRegistry:
reg.Data = verifyGcpGarContainerRegistry(data)
case GhcrContainerRegistry:
reg.Data = verifyGhcrContainerRegistry(data)
case InlineScannerContainerRegistry:
reg.Data = verifyInlineScannerContainerRegistry(data)
case ProxyScannerContainerRegistry:
reg.Data = verifyProxyScannerContainerRegistry(data)
case AwsEcrContainerRegistry:
reg.Data = verifyAwsEcrContainerRegistry(data)
case DockerhubContainerRegistry:
reg.Data = verifyDockerhubContainerRegistry(data)
case DockerhubV2ContainerRegistry:
reg.Data = verifyDockerhubV2ContainerRegistry(data)
case GcpGcrContainerRegistry:
reg.Data = verifyGcpGcrContainerRegistry(data)
default:
reg.Data = data
}
return reg
}
// ContainerRegistry is an interface that helps us implement a few functions
// that any Container Registry might use, there are some cases, like during
// Update, where we need to get the ID of the Container Registry and its type,
// this will allow users to pass any Container Registry that implements these
// methods
type ContainerRegistry interface {
ID() string
ContainerRegistryType() containerRegistryType
}
type containerRegistryType int
const (
// type that defines a non-existing Container Registry integration
NoneContainerRegistry containerRegistryType = iota
GcpGarContainerRegistry
GhcrContainerRegistry
InlineScannerContainerRegistry
ProxyScannerContainerRegistry
AwsEcrContainerRegistry
DockerhubContainerRegistry
DockerhubV2ContainerRegistry
GcpGcrContainerRegistry
)
// ContainerRegistryTypes is the list of available Container Registry integration types
var ContainerRegistryTypes = map[containerRegistryType]string{
NoneContainerRegistry: "None",
GcpGarContainerRegistry: "GCP_GAR",
GhcrContainerRegistry: "GHCR",
InlineScannerContainerRegistry: "INLINE_SCANNER",
ProxyScannerContainerRegistry: "PROXY_SCANNER",
AwsEcrContainerRegistry: "AWS_ECR",
DockerhubContainerRegistry: "DOCKERHUB",
DockerhubV2ContainerRegistry: "V2_REGISTRY",
GcpGcrContainerRegistry: "GCP_GCR",
}
// String returns the string representation of a Container Registry integration type
func (i containerRegistryType) String() string {
return ContainerRegistryTypes[i]
}
// FindContainerRegistryType looks up inside the list of available container registry types
// the matching type from the provided string, if none, returns NoneContainerRegistry
func FindContainerRegistryType(containerRegistry string) (containerRegistryType, bool) {
for cType, cStr := range ContainerRegistryTypes {
if cStr == containerRegistry {
return cType, true
}
}
return NoneContainerRegistry, false
}
// List returns a list of Container Registry integrations
func (svc *ContainerRegistriesService) List() (response ContainerRegistriesResponse, err error) {
err = svc.client.RequestDecoder("GET", apiV2ContainerRegistries, nil, &response)
return
}
// Create creates a single Container Registry integration
func (svc *ContainerRegistriesService) Create(integration ContainerRegistryRaw) (
response ContainerRegistryResponse,
err error,
) {
err = svc.create(integration, &response)
return
}
// Delete deletes a Container Registry integration that matches the provided guid
func (svc *ContainerRegistriesService) Delete(guid string) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
return svc.client.RequestDecoder(
"DELETE",
fmt.Sprintf(apiV2ContainerRegistryFromGUID, guid),
nil,
nil,
)
}
// Get returns a raw response of the Container Registry with the matching integration guid.
//
// To return a more specific Go struct of a Container Registry integration, use the proper
// method such as GetGhcr() where the function name is composed by:
//
// Get<Type>(guid)
//
// Where <Type> is the Container Registry integration type.
func (svc *ContainerRegistriesService) Get(guid string, response interface{}) error {
return svc.get(guid, &response)
}
type ContainerRegistryRaw struct {
v2CommonIntegrationData
Data interface{} `json:"data,omitempty"`
ServerToken *V2ServerToken `json:"serverToken,omitempty"`
}
func (reg ContainerRegistryRaw) StateString() string {
switch reg.ContainerRegistryType() {
case InlineScannerContainerRegistry, ProxyScannerContainerRegistry:
return "Ok"
default:
return reg.v2CommonIntegrationData.StateString()
}
}
type V2ServerToken struct {
ServerToken string `json:"serverToken"`
Uri string `json:"uri"`
}
func (reg ContainerRegistryRaw) GetData() any {
return reg.Data
}
func (reg ContainerRegistryRaw) GetCommon() v2CommonIntegrationData {
return reg.v2CommonIntegrationData
}
func (reg ContainerRegistryRaw) ContainerRegistryType() containerRegistryType {
if casting, ok := reg.Data.(map[string]interface{}); ok {
if regType, exist := casting["registryType"]; exist {
t, _ := FindContainerRegistryType(regType.(string))
return t
}
}
m := structs.Map(reg.Data)
if regType, exist := m["RegistryType"]; exist {
t, _ := FindContainerRegistryType(regType.(string))
return t
}
return NoneContainerRegistry
}
func (reg ContainerRegistryRaw) ContainerRegistryDomain() string {
if casting, ok := reg.Data.(map[string]interface{}); ok {
if domain, exist := casting["registryDomain"]; exist {
return domain.(string)
}
}
if structs.IsStruct(reg.Data) {
m := structs.Map(reg.Data)
if domain, exist := m["RegistryDomain"]; exist {
return domain.(string)
}
}
return ""
}
type ContainerRegistryResponse struct {
Data ContainerRegistryRaw `json:"data"`
}
type ContainerRegistriesResponse struct {
Data []ContainerRegistryRaw `json:"data"`
}
func (svc *ContainerRegistriesService) create(data interface{}, response interface{}) error {
return svc.client.RequestEncoderDecoder("POST", apiV2ContainerRegistries, data, response)
}
func (svc *ContainerRegistriesService) get(guid string, response interface{}) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
apiPath := fmt.Sprintf(apiV2ContainerRegistryFromGUID, guid)
return svc.client.RequestDecoder("GET", apiPath, nil, response)
}
func (svc *ContainerRegistriesService) update(guid string, data interface{}, response interface{}) error {
if guid == "" {
return errors.New("specify an intgGuid")
}
apiPath := fmt.Sprintf(apiV2ContainerRegistryFromGUID, guid)
return svc.client.RequestEncoderDecoder("PATCH", apiPath, data, response)
}