FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
stackrox/config-controller/pkg/client/client.go at master · stackrox/stackrox · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
stackrox
/
stackrox
Public
Notifications
You must be signed in to change notification settings
Fork
191
Star
1.3k
Code
Issues
54
Pull requests
603
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
stackrox
/
config-controller
/
pkg
/
client
/
client.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
425 lines (342 loc) · 12.4 KB
Breadcrumbs
stackrox
/
config-controller
/
pkg
/
client
/
client.go
Copy path
File metadata and controls
425 lines (342 loc) · 12.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package
client
//go:generate mockgen-wrapper CachedCentralClient,CentralClient
import
(
"context"
"fmt"
"os"
"time"
"github.com/pkg/errors"
v1
"github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/pkg/mtls"
"github.com/stackrox/rox/pkg/mtls/verifier"
"github.com/stackrox/rox/pkg/netutil"
"github.com/stackrox/rox/pkg/size"
"google.golang.org/grpc"
)
var
(
log
=
logging
.
LoggerForModule
()
)
func
centralEndpoint
()
string
{
// Check if ROX_CENTRAL_ENDPOINT is explicitly set (not just the default value).
// This provides backwards compatibility for deployments that don't set ROX_CENTRAL_ENDPOINT.
endpoint
,
exists
:=
os
.
LookupEnv
(
"ROX_CENTRAL_ENDPOINT"
)
if
exists
&&
endpoint
!=
""
{
return
endpoint
}
// Fall back to namespace-based endpoint for backwards compatibility
return
fmt
.
Sprintf
(
"central.%s.svc:443"
,
env
.
Namespace
.
Setting
())
}
type
perRPCCreds
struct
{
svc
v1.
AuthServiceClient
metadata
map
[
string
]
string
lastUpdated
time.
Time
}
func
(
c
*
perRPCCreds
)
GetRequestMetadata
(
_
context.
Context
,
_
...
string
) (
map
[
string
]
string
,
error
) {
return
c
.
metadata
,
nil
}
func
(
c
*
perRPCCreds
)
RequireTransportSecurity
()
bool
{
return
true
}
func
(
c
*
perRPCCreds
)
refreshToken
(
ctx
context.
Context
)
error
{
log
.
Debug
(
"Refreshing Central API token"
)
token
,
err
:=
os
.
ReadFile
(
"/var/run/secrets/stackrox.io/m2m/token"
)
if
err
!=
nil
{
return
errors
.
WithMessage
(
err
,
"error reading service account token file"
)
}
req
:=
v1.
ExchangeAuthMachineToMachineTokenRequest
{
IdToken
:
string
(
token
),
}
resp
,
err
:=
c
.
svc
.
ExchangeAuthMachineToMachineToken
(
ctx
,
&
req
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to exchange token"
)
}
authHeaderValue
:=
fmt
.
Sprintf
(
"Bearer %s"
,
resp
.
GetAccessToken
())
c
.
metadata
=
map
[
string
]
string
{
"authorization"
:
authHeaderValue
,
}
c
.
lastUpdated
=
time
.
Now
()
return
nil
}
type
CachedCentralClient
interface
{
ListPolicies
(
ctx
context.
Context
) ([]
*
storage.
Policy
,
error
)
GetPolicy
(
ctx
context.
Context
,
name
string
) (
*
storage.
Policy
,
bool
,
error
)
CreatePolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
) (
*
storage.
Policy
,
error
)
UpdatePolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
)
error
DeletePolicy
(
ctx
context.
Context
,
name
string
)
error
GetNotifiers
()
map
[
string
]
string
GetClusters
()
map
[
string
]
string
FlushCache
(
ctx
context.
Context
)
error
EnsureFresh
(
ctx
context.
Context
)
error
}
type
CentralClient
interface
{
ListPolicies
(context.
Context
) ([]
*
storage.
ListPolicy
,
error
)
GetPolicy
(
ctx
context.
Context
,
id
string
) (
*
storage.
Policy
,
error
)
PostPolicy
(context.
Context
,
*
storage.
Policy
) (
*
storage.
Policy
,
error
)
PutPolicy
(context.
Context
,
*
storage.
Policy
)
error
DeletePolicy
(
ctx
context.
Context
,
id
string
)
error
ListNotifiers
(
ctx
context.
Context
) ([]
*
storage.
Notifier
,
error
)
ListClusters
(
ctx
context.
Context
) ([]
*
storage.
Cluster
,
error
)
TokenExchange
(
ctx
context.
Context
)
error
}
type
grpcClient
struct
{
policySvc
v1.
PolicyServiceClient
notifierSvc
v1.
NotifierServiceClient
clusterSvc
v1.
ClustersServiceClient
perRPCCreds
*
perRPCCreds
}
func
newGrpcClient
(
ctx
context.
Context
) (
CentralClient
,
error
) {
clientconn
.
SetUserAgent
(
clientconn
.
ConfigController
)
// Use system cert pool to include additional CAs from import-additional-cas
rootCAs
,
err
:=
verifier
.
SystemCertPool
()
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to get system cert pool"
)
}
// Parse the endpoint to get the hostname for TLS ServerName verification
endpoint
:=
centralEndpoint
()
host
,
_
,
_
,
err
:=
netutil
.
ParseEndpoint
(
endpoint
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"failed to parse central endpoint %q"
,
endpoint
)
}
dialOpts
:=
[]grpc.
DialOption
{
grpc
.
WithNoProxy
(),
}
perRPCCreds
:=
&
perRPCCreds
{}
opts
:=
clientconn.
Options
{
InsecureNoTLS
:
false
,
InsecureAllowCredsViaPlaintext
:
false
,
DialOptions
:
dialOpts
,
PerRPCCreds
:
perRPCCreds
,
TLS
: clientconn.
TLSConfigOptions
{
RootCAs
:
rootCAs
,
ServerName
:
host
,
},
}
callOpts
:=
[]grpc.
CallOption
{
grpc
.
MaxCallRecvMsgSize
(
12
*
size
.
MB
)}
conn
,
err
:=
clientconn
.
GRPCConnection
(
ctx
,
mtls
.
CentralSubject
,
endpoint
,
opts
,
grpc
.
WithDefaultCallOptions
(
callOpts
...
))
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"Failed to create gRPC connection"
)
}
perRPCCreds
.
svc
=
v1
.
NewAuthServiceClient
(
conn
)
return
&
grpcClient
{
perRPCCreds
:
perRPCCreds
,
policySvc
:
v1
.
NewPolicyServiceClient
(
conn
),
notifierSvc
:
v1
.
NewNotifierServiceClient
(
conn
),
clusterSvc
:
v1
.
NewClustersServiceClient
(
conn
),
},
nil
}
func
(
gc
*
grpcClient
)
ListNotifiers
(
ctx
context.
Context
) ([]
*
storage.
Notifier
,
error
) {
allNotifiers
,
err
:=
gc
.
notifierSvc
.
GetNotifiers
(
ctx
,
&
v1.
GetNotifiersRequest
{})
if
err
!=
nil
{
return
[]
*
storage.
Notifier
{},
errors
.
Wrap
(
err
,
"Failed to list notifiers from grpc client"
)
}
return
allNotifiers
.
GetNotifiers
(),
nil
}
func
(
gc
*
grpcClient
)
ListClusters
(
ctx
context.
Context
) ([]
*
storage.
Cluster
,
error
) {
allClusters
,
err
:=
gc
.
clusterSvc
.
GetClusters
(
ctx
,
&
v1.
GetClustersRequest
{})
if
err
!=
nil
{
return
[]
*
storage.
Cluster
{},
errors
.
Wrap
(
err
,
"Failed to list clusters from grpc client"
)
}
return
allClusters
.
GetClusters
(),
nil
}
func
(
gc
*
grpcClient
)
ListPolicies
(
ctx
context.
Context
) ([]
*
storage.
ListPolicy
,
error
) {
allPolicies
,
err
:=
gc
.
policySvc
.
ListPolicies
(
ctx
,
&
v1.
RawQuery
{})
if
err
!=
nil
{
return
[]
*
storage.
ListPolicy
{},
errors
.
Wrap
(
err
,
"Failed to list policies from grpc client"
)
}
return
allPolicies
.
GetPolicies
(),
nil
}
func
(
gc
*
grpcClient
)
GetPolicy
(
ctx
context.
Context
,
id
string
) (
*
storage.
Policy
,
error
) {
policy
,
err
:=
gc
.
policySvc
.
GetPolicy
(
ctx
,
&
v1.
ResourceByID
{
Id
:
id
})
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"Failed to fetch policy %s"
,
id
)
}
return
policy
,
nil
}
func
(
gc
*
grpcClient
)
PostPolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
) (
*
storage.
Policy
,
error
) {
req
:=
&
v1.
PostPolicyRequest
{
Policy
:
policy
,
EnableStrictValidation
:
true
,
}
policy
,
err
:=
gc
.
policySvc
.
PostPolicy
(
ctx
,
req
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"Failed to create policy %q"
,
policy
.
GetName
())
}
return
policy
,
nil
}
func
(
gc
*
grpcClient
)
PutPolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
)
error
{
_
,
err
:=
gc
.
policySvc
.
PutPolicy
(
ctx
,
policy
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"Failed to update policy %q"
,
policy
.
GetName
())
}
return
nil
}
func
(
gc
*
grpcClient
)
DeletePolicy
(
ctx
context.
Context
,
id
string
)
error
{
_
,
err
:=
gc
.
policySvc
.
DeletePolicy
(
ctx
,
&
v1.
ResourceByID
{
Id
:
id
})
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"Failed to delete policy: %s"
,
id
)
}
return
nil
}
func
(
gc
*
grpcClient
)
TokenExchange
(
ctx
context.
Context
)
error
{
if
time
.
Since
(
gc
.
perRPCCreds
.
lastUpdated
).
Seconds
()
>
60.0
{
return
gc
.
perRPCCreds
.
refreshToken
(
ctx
)
}
return
nil
}
type
client
struct
{
centralSvc
CentralClient
policyObjectCache
map
[
string
]
*
storage.
Policy
// policy ID to policy
policyNameToIDCache
map
[
string
]
string
// policy name to policy ID
notifierNameToIDCache
map
[
string
]
string
// notifier name to notifier ID
clusterNameToIDCache
map
[
string
]
string
// cluster name to cluster ID
lastUpdated
time.
Time
}
type
clientOptions
interface
{
Apply
(
centralClient
CachedCentralClient
)
}
func
New
(
ctx
context.
Context
,
opts
...
clientOptions
) (
CachedCentralClient
,
error
) {
c
:=
client
{}
for
_
,
o
:=
range
opts
{
o
.
Apply
(
&
c
)
}
if
c
.
centralSvc
==
nil
{
gc
,
err
:=
newGrpcClient
(
ctx
)
if
err
!=
nil
{
log
.
Error
(
err
,
"Failed to connect to Central"
)
}
c
.
centralSvc
=
gc
}
err
:=
c
.
EnsureFresh
(
ctx
)
if
err
==
nil
{
return
&
c
,
nil
}
// Log the error once and then keep trying silently
log
.
Error
(
err
,
"Failed to initialize client. Will continue to retry..."
)
for
{
if
err
:=
c
.
EnsureFresh
(
ctx
);
err
!=
nil
{
log
.
Warnf
(
"Initialization Error: %s"
,
err
)
time
.
Sleep
(
time
.
Second
*
5
)
continue
}
break
}
return
&
c
,
nil
}
func
(
c
*
client
)
GetNotifiers
()
map
[
string
]
string
{
return
c
.
notifierNameToIDCache
}
func
(
c
*
client
)
GetClusters
()
map
[
string
]
string
{
return
c
.
clusterNameToIDCache
}
func
(
c
*
client
)
ListPolicies
(
_
context.
Context
) ([]
*
storage.
Policy
,
error
) {
policies
:=
make
([]
*
storage.
Policy
,
0
,
len
(
c
.
policyObjectCache
))
for
_
,
value
:=
range
c
.
policyObjectCache
{
policies
=
append
(
policies
,
value
)
}
return
policies
,
nil
}
func
(
c
*
client
)
GetPolicy
(
_
context.
Context
,
name
string
) (
*
storage.
Policy
,
bool
,
error
) {
id
,
exists
:=
c
.
policyNameToIDCache
[
name
]
return
c
.
policyObjectCache
[
id
],
exists
,
nil
}
func
(
c
*
client
)
CreatePolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
) (
*
storage.
Policy
,
error
) {
log
.
Infof
(
"Creating policy %q"
,
policy
.
GetName
())
createdPolicy
,
err
:=
c
.
centralSvc
.
PostPolicy
(
ctx
,
policy
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
fmt
.
Sprintf
(
"Failed to POST policy '%s'"
,
policy
.
GetName
()))
}
c
.
policyObjectCache
[
createdPolicy
.
GetId
()]
=
createdPolicy
c
.
policyNameToIDCache
[
createdPolicy
.
GetName
()]
=
createdPolicy
.
GetId
()
return
createdPolicy
,
nil
}
func
(
c
*
client
)
UpdatePolicy
(
ctx
context.
Context
,
policy
*
storage.
Policy
)
error
{
log
.
Infof
(
"Updating policy %q"
,
policy
.
GetName
())
var
existingPolicyName
string
if
id
,
ok
:=
c
.
policyNameToIDCache
[
policy
.
GetName
()];
ok
{
existingPolicyName
=
c
.
policyObjectCache
[
id
].
GetName
()
}
// update policy on central
err
:=
c
.
centralSvc
.
PutPolicy
(
ctx
,
policy
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to PUT policy"
)
}
// update caches, taking care of the legit rename a declarative policy case
c
.
policyObjectCache
[
policy
.
GetId
()]
=
policy
if
existingPolicyName
!=
policy
.
GetName
() {
delete
(
c
.
policyNameToIDCache
,
existingPolicyName
)
}
c
.
policyNameToIDCache
[
policy
.
GetName
()]
=
policy
.
GetId
()
return
nil
}
func
(
c
*
client
)
DeletePolicy
(
ctx
context.
Context
,
policyID
string
)
error
{
log
.
Infof
(
"Deleting policy %q"
,
policyID
)
policy
:=
c
.
policyObjectCache
[
policyID
]
if
policy
.
GetSource
()
!=
storage
.
PolicySource_DECLARATIVE
{
return
errors
.
New
(
fmt
.
Sprintf
(
"policy %q is not externally managed and can be deleted only from central"
,
policy
.
GetName
()))
}
if
err
:=
c
.
centralSvc
.
DeletePolicy
(
ctx
,
policyID
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"Failed to DELETE policy %q in central"
,
policy
.
GetName
())
}
delete
(
c
.
policyObjectCache
,
policyID
)
delete
(
c
.
policyNameToIDCache
,
policy
.
GetName
())
return
nil
}
func
(
c
*
client
)
FlushCache
(
ctx
context.
Context
)
error
{
if
time
.
Since
(
c
.
lastUpdated
).
Seconds
()
<
1
{
// Don't flush the cache more often than every 1s
return
nil
}
log
.
Info
(
"Flushing caches"
)
log
.
Debug
(
"Listing policies"
)
allPolicies
,
err
:=
c
.
centralSvc
.
ListPolicies
(
ctx
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to list policies"
)
}
allNotifiers
,
err
:=
c
.
centralSvc
.
ListNotifiers
(
ctx
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Failed to list notifiers"
)
}
allClusters
,
err
:=
c
.
centralSvc
.
ListClusters
(
ctx
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"Faield to list clusters"
)
}
newPolicyObjectCache
:=
make
(
map
[
string
]
*
storage.
Policy
,
len
(
allPolicies
))
newPolicyNameToIDCache
:=
make
(
map
[
string
]
string
,
len
(
allPolicies
))
newClusterNameToIDCache
:=
make
(
map
[
string
]
string
,
len
(
allClusters
))
newNotifierNameToIDCache
:=
make
(
map
[
string
]
string
,
len
(
allNotifiers
))
for
_
,
listPolicy
:=
range
allPolicies
{
log
.
Debugf
(
"Get policy: %s"
,
listPolicy
.
GetName
())
policy
,
err
:=
c
.
centralSvc
.
GetPolicy
(
ctx
,
listPolicy
.
GetId
())
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"Failed to fetch policy %s"
,
listPolicy
.
GetId
())
}
newPolicyObjectCache
[
policy
.
GetId
()]
=
policy
newPolicyNameToIDCache
[
policy
.
GetName
()]
=
policy
.
GetId
()
}
for
_
,
notifier
:=
range
allNotifiers
{
newNotifierNameToIDCache
[
notifier
.
GetName
()]
=
notifier
.
GetId
()
}
for
_
,
cluster
:=
range
allClusters
{
newClusterNameToIDCache
[
cluster
.
GetName
()]
=
cluster
.
GetId
()
}
c
.
policyObjectCache
=
newPolicyObjectCache
c
.
policyNameToIDCache
=
newPolicyNameToIDCache
c
.
clusterNameToIDCache
=
newClusterNameToIDCache
c
.
notifierNameToIDCache
=
newNotifierNameToIDCache
c
.
lastUpdated
=
time
.
Now
()
return
nil
}
func
(
c
*
client
)
EnsureFresh
(
ctx
context.
Context
)
error
{
// Make sure token isn't expired before flushing cache
if
err
:=
c
.
centralSvc
.
TokenExchange
(
ctx
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"could not exchange token"
)
}
if
time
.
Since
(
c
.
lastUpdated
).
Minutes
()
>
5.0
{
return
c
.
FlushCache
(
ctx
)
}
return
nil
}
Back
|
FazBrowse Home
|
New Git URL