@@ -10,33 +10,28 @@ import (
10
10
"strings"
11
11
"time"
12
12
13
+ "github.com/armosec/armoapi-go/apis"
14
+ apitypes "github.com/armosec/armoapi-go/armotypes"
15
+ "github.com/armosec/armoapi-go/identifiers"
13
16
"github.com/armosec/registryx/interfaces"
14
17
"github.com/armosec/registryx/registryclients"
15
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
- "k8s.io/utils/strings/slices"
17
-
18
+ "github.com/armosec/utils-go/httputils"
19
+ "github.com/armosec/utils-k8s-go/armometadata"
18
20
"github.com/distribution/reference"
19
21
dockerregistry "github.com/docker/docker/api/types/registry"
22
+ "github.com/google/uuid"
20
23
"github.com/kubescape/go-logger"
21
24
"github.com/kubescape/go-logger/helpers"
25
+ "github.com/kubescape/k8s-interface/cloudsupport"
22
26
"github.com/kubescape/k8s-interface/k8sinterface"
23
27
"github.com/kubescape/operator/config"
24
28
"github.com/kubescape/operator/utils"
25
29
"go.opentelemetry.io/otel"
26
30
"go.opentelemetry.io/otel/attribute"
27
31
"go.opentelemetry.io/otel/trace"
28
-
29
32
corev1 "k8s.io/api/core/v1"
30
-
31
- "github.com/google/uuid"
32
-
33
- "github.com/armosec/armoapi-go/apis"
34
- apitypes "github.com/armosec/armoapi-go/armotypes"
35
- "github.com/armosec/armoapi-go/identifiers"
36
- "github.com/armosec/utils-k8s-go/armometadata"
37
-
38
- "github.com/armosec/utils-go/httputils"
39
- "github.com/kubescape/k8s-interface/cloudsupport"
33
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
+ "k8s.io/utils/strings/slices"
40
35
)
41
36
42
37
func getAPScanURL (config config.IConfig ) * url.URL {
@@ -100,7 +95,7 @@ func (actionHandler *ActionHandler) scanRegistriesV2AndUpdateStatus(ctx context.
100
95
scanTime := time .Now ()
101
96
imageRegistry , err := actionHandler .loadRegistryFromSessionObj ()
102
97
if err != nil {
103
- return fmt .Errorf ("failed to load registry from sessionObj with err %v " , err )
98
+ return fmt .Errorf ("failed to load registry from sessionObj: %w " , err )
104
99
}
105
100
106
101
err = actionHandler .scanRegistriesV2 (ctx , imageRegistry )
@@ -119,27 +114,27 @@ func (actionHandler *ActionHandler) scanRegistriesV2AndUpdateStatus(ctx context.
119
114
120
115
func (actionHandler * ActionHandler ) scanRegistriesV2 (ctx context.Context , imageRegistry apitypes.ContainerImageRegistry ) error {
121
116
if err := actionHandler .loadRegistrySecret (ctx , imageRegistry ); err != nil {
122
- return fmt .Errorf ("failed to load secret with err %v " , err )
117
+ return fmt .Errorf ("failed to load secret: %w " , err )
123
118
}
124
119
125
120
client , err := registryclients .GetRegistryClient (imageRegistry )
126
121
if err != nil {
127
- return fmt .Errorf ("failed to get registry client with err %v " , err )
122
+ return fmt .Errorf ("failed to get registry client: %w " , err )
128
123
}
129
124
130
125
images , err := client .GetImagesToScan (ctx )
131
126
if err != nil {
132
- return fmt .Errorf ("failed to get registry images to scan with err %v " , err )
127
+ return fmt .Errorf ("failed to get registry images to scan: %w " , err )
133
128
} else if len (images ) == 0 {
134
129
return errors .New (noImagesToScanError )
135
130
}
136
131
137
132
registryScanCMDList , err := actionHandler .getRegistryImageScanCommands (client , imageRegistry , images )
138
133
if err != nil {
139
- return fmt .Errorf ("failed to get registry images scan commands with err %v " , err )
134
+ return fmt .Errorf ("failed to get registry images scan commands: %w " , err )
140
135
}
141
136
if err = sendAllImagesToRegistryScan (ctx , actionHandler .config , registryScanCMDList ); err != nil {
142
- return fmt .Errorf ("failed to send scan commands with err %v " , err )
137
+ return fmt .Errorf ("failed to send scan commands: %w " , err )
143
138
}
144
139
145
140
return nil
@@ -149,17 +144,17 @@ func (actionHandler *ActionHandler) loadRegistrySecret(ctx context.Context, imag
149
144
secretName := actionHandler .sessionObj .Command .Args [apitypes .RegistrySecretNameArgKey ].(string )
150
145
secret , err := actionHandler .k8sAPI .KubernetesClient .CoreV1 ().Secrets (actionHandler .config .Namespace ()).Get (ctx , secretName , metav1.GetOptions {})
151
146
if err != nil {
152
- return fmt .Errorf ("loadRegistrySecret failed to get secret with err %v " , err )
147
+ return fmt .Errorf ("loadRegistrySecret failed to get secret: %w " , err )
153
148
}
154
149
155
150
var secretMap map [string ]interface {}
156
151
err = json .Unmarshal (secret .Data [apitypes .RegistryAuthFieldInSecret ], & secretMap )
157
152
if err != nil {
158
- return fmt .Errorf ("loadRegistrySecret failed to unmarshal registry secret with err %v " , err )
153
+ return fmt .Errorf ("loadRegistrySecret failed to unmarshal registry secret: %w " , err )
159
154
}
160
155
err = imageRegistry .FillSecret (secretMap )
161
156
if err != nil {
162
- return fmt .Errorf ("loadRegistrySecret failed to fill registry secret with err %v " , err )
157
+ return fmt .Errorf ("loadRegistrySecret failed to fill registry secret: %w " , err )
163
158
}
164
159
return nil
165
160
}
@@ -168,11 +163,11 @@ func (actionHandler *ActionHandler) loadRegistryFromSessionObj() (apitypes.Conta
168
163
regInfo := actionHandler .sessionObj .Command .Args [apitypes .RegistryInfoArgKey ].(map [string ]interface {})
169
164
regInfoBytes , err := json .Marshal (regInfo )
170
165
if err != nil {
171
- return nil , fmt .Errorf ("scanRegistriesV2 failed to marshal command arg with err %v " , err )
166
+ return nil , fmt .Errorf ("scanRegistriesV2 failed to marshal command arg: %w " , err )
172
167
}
173
168
imageRegistry , err := apitypes .UnmarshalRegistry (regInfoBytes )
174
169
if err != nil {
175
- return nil , fmt .Errorf ("scanRegistriesV2 failed to unmarshal command with err %v " , err )
170
+ return nil , fmt .Errorf ("scanRegistriesV2 failed to unmarshal command: %w " , err )
176
171
}
177
172
return imageRegistry , nil
178
173
}
@@ -206,7 +201,7 @@ func (actionHandler *ActionHandler) getRegistryImageScanCommands(client interfac
206
201
}
207
202
auth , err := client .GetDockerAuth ()
208
203
if err != nil {
209
- return nil , fmt .Errorf ("failed to get docker auth with err %v " , err )
204
+ return nil , fmt .Errorf ("failed to get docker auth: %w " , err )
210
205
}
211
206
registryScanCommand .Credentialslist = append (registryScanCommand .Credentialslist , * auth )
212
207
registryScanCMDList = append (registryScanCMDList , & apis.RegistryScanCommand {
@@ -240,7 +235,7 @@ func (actionHandler *ActionHandler) scanImage(ctx context.Context) error {
240
235
cmd := actionHandler .getImageScanCommand (containerData , imageScanConfig )
241
236
242
237
if err := sendCommandToScanner (ctx , actionHandler .config , cmd , actionHandler .sessionObj .Command .CommandName ); err != nil {
243
- return fmt .Errorf ("failed to send command to scanner with err %v " , err )
238
+ return fmt .Errorf ("failed to send command to scanner: %w " , err )
244
239
}
245
240
return nil
246
241
}
@@ -253,6 +248,21 @@ func (actionHandler *ActionHandler) scanApplicationProfile(ctx context.Context)
253
248
return errors .New ("kubevuln is not enabled" )
254
249
}
255
250
251
+ // get the pod from the session object
252
+ pod , _ := actionHandler .sessionObj .Command .Args [utils .ArgsPod ].(* corev1.Pod )
253
+
254
+ var authConfigs []dockerregistry.AuthConfig
255
+ if pod != nil {
256
+ // build a list of secrets from the registry secrets
257
+ secrets , err := cloudsupport .GetImageRegistryCredentials (actionHandler .k8sAPI , "" , pod )
258
+ if err != nil {
259
+ return fmt .Errorf ("failed to get registry credentials: %w" , err )
260
+ }
261
+ for i := range secrets {
262
+ authConfigs = append (authConfigs , secrets [i ]... )
263
+ }
264
+ }
265
+
256
266
span .AddEvent ("scanning" , trace .WithAttributes (attribute .String ("wlid" , actionHandler .wlid )))
257
267
cmd := & apis.WebsocketScanCommand {
258
268
Wlid : actionHandler .wlid ,
@@ -261,13 +271,14 @@ func (actionHandler *ActionHandler) scanApplicationProfile(ctx context.Context)
261
271
"name" : actionHandler .sessionObj .Command .Args [utils .ArgsName ],
262
272
"namespace" : actionHandler .sessionObj .Command .Args [utils .ArgsNamespace ],
263
273
},
274
+ Credentialslist : authConfigs ,
264
275
},
265
276
}
266
277
267
278
prepareSessionChain (actionHandler .sessionObj , cmd , actionHandler )
268
279
269
280
if err := sendCommandToScanner (ctx , actionHandler .config , cmd , apis .TypeScanApplicationProfile ); err != nil {
270
- return fmt .Errorf ("failed to send command to scanner with err %v " , err )
281
+ return fmt .Errorf ("failed to send command to scanner: %w " , err )
271
282
}
272
283
return nil
273
284
}
@@ -344,7 +355,7 @@ func getImageScanConfig(k8sAPI *k8sinterface.KubernetesApi, namespace string, po
344
355
345
356
if pod != nil {
346
357
// TODO: this should not happen every scan
347
- // build a list of secrets from the the registry secrets
358
+ // build a list of secrets from the registry secrets
348
359
secrets , err := cloudsupport .GetImageRegistryCredentials (k8sAPI , imageTag , pod )
349
360
if err != nil {
350
361
return nil , err
@@ -398,7 +409,7 @@ func sendWorkloadWithCredentials(ctx context.Context, scanUrl *url.URL, command
398
409
}
399
410
400
411
if err != nil {
401
- return fmt .Errorf ("failed to marshal websocketScanCommand with err %v " , err )
412
+ return fmt .Errorf ("failed to marshal websocketScanCommand: %w " , err )
402
413
}
403
414
if command .GetWlid () == "" {
404
415
logger .L ().Debug (fmt .Sprintf ("sending scan command to kubevuln: %s" , string (jsonScannerC )))
0 commit comments