-
Notifications
You must be signed in to change notification settings - Fork 18
/
manager.go
458 lines (380 loc) · 14.1 KB
/
manager.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2023 The ClusterLink Authors.
// 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 authz
import (
"context"
"crypto/rand"
"crypto/rsa"
"fmt"
"sync"
"time"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1"
cpapi "github.com/clusterlink-net/clusterlink/pkg/controlplane/api"
"github.com/clusterlink-net/clusterlink/pkg/controlplane/authz/connectivitypdp"
"github.com/clusterlink-net/clusterlink/pkg/controlplane/peer"
"github.com/clusterlink-net/clusterlink/pkg/util/tls"
)
const (
// the number of seconds a JWT access token is valid before it expires.
jwtExpirySeconds = 5
ServiceNameLabel = "clusterlink/metadata.serviceName"
ServiceNamespaceLabel = "clusterlink/metadata.serviceNamespace"
GatewayNameLabel = "clusterlink/metadata.gatewayName"
)
// egressAuthorizationRequest (from local dataplane)
// represents a request for accessing an imported service.
type egressAuthorizationRequest struct {
// ImportName is the name of the requested imported service.
ImportName types.NamespacedName
// IP address of the client connecting to the service.
IP string
}
// egressAuthorizationResponse (to local dataplane) represents a response for an egressAuthorizationRequest.
type egressAuthorizationResponse struct {
// ServiceExists is true if the requested service exists.
ServiceExists bool
// Allowed is true if the request is allowed.
Allowed bool
// RemotePeerCluster is the cluster name of the remote peer where the connection should be routed to.
RemotePeerCluster string
// AccessToken is a token that allows accessing the requested service.
AccessToken string
}
// ingressAuthorizationRequest (to remote peer controlplane) represents a request for accessing an exported service.
type ingressAuthorizationRequest struct {
// Service is the name of the requested exported service.
ServiceName types.NamespacedName
}
// ingressAuthorizationResponse (from remote peer controlplane) represents a response for an ingressAuthorizationRequest.
type ingressAuthorizationResponse struct {
// ServiceExists is true if the requested service exists.
ServiceExists bool
// Allowed is true if the request is allowed.
Allowed bool
// AccessToken is a token that allows accessing the requested service.
AccessToken string
}
type podInfo struct {
name string
namespace string
labels map[string]string
}
// Manager manages the authorization dataplane connections.
type Manager struct {
client client.Client
namespace string
loadBalancer *LoadBalancer
connectivityPDP *connectivitypdp.PDP
peerTLS *tls.ParsedCertData
peerLock sync.RWMutex
peerClient map[string]*peer.Client
podLock sync.RWMutex
ipToPod map[string]types.NamespacedName
podList map[types.NamespacedName]podInfo
jwkSignKey jwk.Key
jwkVerifyKey jwk.Key
// callback for getting an import (for non-CRD mode)
getImportCallback func(name string, imp *v1alpha1.Import) error
// callback for getting an export (for non-CRD mode)
getExportCallback func(name string, imp *v1alpha1.Export) error
// callback for getting a peer (for non-CRD mode)
getPeerCallback func(name string, pr *v1alpha1.Peer) error
logger *logrus.Entry
}
func (m *Manager) SetGetImportCallback(callback func(name string, imp *v1alpha1.Import) error) {
m.getImportCallback = callback
}
func (m *Manager) SetGetExportCallback(callback func(name string, imp *v1alpha1.Export) error) {
m.getExportCallback = callback
}
func (m *Manager) SetGetPeerCallback(callback func(name string, pr *v1alpha1.Peer) error) {
m.getPeerCallback = callback
}
// AddPeer defines a new route target for egress dataplane connections.
func (m *Manager) AddPeer(pr *v1alpha1.Peer) {
m.logger.Infof("Adding peer '%s'.", pr.Name)
// initialize peer client
cl := peer.NewClient(pr, m.peerTLS.ClientConfig(pr.Name))
m.peerLock.Lock()
m.peerClient[pr.Name] = cl
m.peerLock.Unlock()
}
// DeletePeer removes the possibility for egress dataplane connections to be routed to a given peer.
func (m *Manager) DeletePeer(name string) {
m.logger.Infof("Deleting peer '%s'.", name)
m.peerLock.Lock()
delete(m.peerClient, name)
m.peerLock.Unlock()
}
// AddAccessPolicy adds an access policy to allow/deny specific connections.
func (m *Manager) AddAccessPolicy(policy *connectivitypdp.AccessPolicy) error {
return m.connectivityPDP.AddOrUpdatePolicy(policy)
}
// DeleteAccessPolicy removes an access policy to allow/deny specific connections.
func (m *Manager) DeleteAccessPolicy(name types.NamespacedName, privileged bool) error {
return m.connectivityPDP.DeletePolicy(name, privileged)
}
// deletePod deletes pod to ipToPod list.
func (m *Manager) deletePod(podID types.NamespacedName) {
m.podLock.Lock()
defer m.podLock.Unlock()
delete(m.podList, podID)
for key, pod := range m.ipToPod {
if pod.Name == podID.Name && pod.Namespace == podID.Namespace {
delete(m.ipToPod, key)
}
}
}
// addPod adds or updates pod to ipToPod and podList.
func (m *Manager) addPod(pod *v1.Pod) {
m.podLock.Lock()
defer m.podLock.Unlock()
podID := types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}
m.podList[podID] = podInfo{name: pod.Name, namespace: pod.Namespace, labels: pod.Labels}
for _, ip := range pod.Status.PodIPs {
// ignoring host-networked Pod IPs
if ip.IP != pod.Status.HostIP {
m.ipToPod[ip.IP] = podID
}
}
}
// getPodInfoByIP returns the information about the Pod with the specified IP address.
func (m *Manager) getPodInfoByIP(ip string) *podInfo {
m.podLock.RLock()
defer m.podLock.RUnlock()
if p, ipExsit := m.ipToPod[ip]; ipExsit {
if pInfo, podExist := m.podList[p]; podExist {
return &pInfo
}
}
return nil
}
// authorizeEgress authorizes a request for accessing an imported service.
func (m *Manager) authorizeEgress(ctx context.Context, req *egressAuthorizationRequest) (*egressAuthorizationResponse, error) {
m.logger.Infof("Received egress authorization request: %v.", req)
srcAttributes := connectivitypdp.WorkloadAttrs{}
podInfo := m.getPodInfoByIP(req.IP)
if podInfo != nil {
srcAttributes[ServiceNamespaceLabel] = podInfo.namespace
if src, ok := podInfo.labels["app"]; ok { // TODO: Add support for labels other than just the "app" key.
m.logger.Infof("Received egress authorization srcLabels[app]: %v.", podInfo.labels["app"])
srcAttributes[ServiceNameLabel] = src
}
}
var imp v1alpha1.Import
if err := m.getImport(ctx, req.ImportName, &imp); err != nil {
return nil, fmt.Errorf("cannot get import %v: %w", req.ImportName, err)
}
lbResult := NewLoadBalancingResult(&imp)
for {
if err := m.loadBalancer.Select(lbResult); err != nil {
return nil, fmt.Errorf("cannot select import source: %w", err)
}
importSource := lbResult.Get()
var pr v1alpha1.Peer
if err := m.getPeer(ctx, importSource.Peer, &pr); err != nil {
return nil, fmt.Errorf("cannot get peer '%s': %w", importSource.Peer, err)
}
if !meta.IsStatusConditionTrue(pr.Status.Conditions, v1alpha1.PeerReachable) {
if !lbResult.IsDelayed() {
lbResult.Delay()
continue
}
}
dstAttributes := connectivitypdp.WorkloadAttrs{
ServiceNameLabel: imp.Name,
ServiceNamespaceLabel: imp.Namespace,
GatewayNameLabel: importSource.Peer,
}
decision, err := m.connectivityPDP.Decide(srcAttributes, dstAttributes, req.ImportName.Namespace)
if err != nil {
return nil, fmt.Errorf("error deciding on an egress connection: %w", err)
}
if decision.Decision != connectivitypdp.DecisionAllow {
continue
}
m.peerLock.RLock()
cl, ok := m.peerClient[importSource.Peer]
m.peerLock.RUnlock()
if !ok {
return nil, fmt.Errorf("missing client for peer: %s", importSource.Peer)
}
DstName := importSource.ExportName
DstNamespace := importSource.ExportNamespace
if DstName == "" { // TODO- remove when controlplane will support only CRD mode.
DstName = req.ImportName.Name
}
if DstNamespace == "" { // TODO- remove when controlplane will support only CRD mode.
DstNamespace = req.ImportName.Namespace
}
peerResp, err := cl.Authorize(&cpapi.AuthorizationRequest{
ServiceName: DstName,
ServiceNamespace: DstNamespace,
})
if err != nil {
m.logger.Infof("Unable to get access token from peer: %v", err)
continue
}
if !peerResp.ServiceExists {
m.logger.Infof(
"Peer %s does not have an import source for %v",
importSource.Peer, req.ImportName)
continue
}
if !peerResp.Allowed {
m.logger.Infof(
"Peer %s did not allow connection to import %v: %v",
importSource.Peer, req.ImportName, err)
continue
}
return &egressAuthorizationResponse{
ServiceExists: true,
Allowed: true,
RemotePeerCluster: cpapi.RemotePeerClusterName(importSource.Peer),
AccessToken: peerResp.AccessToken,
}, nil
}
}
// parseAuthorizationHeader verifies an access token for an ingress dataplane connection.
// On success, returns the parsed target cluster name.
func (m *Manager) parseAuthorizationHeader(token string) (string, error) {
m.logger.Debug("Parsing access token.")
parsedToken, err := jwt.ParseString(
token, jwt.WithVerify(cpapi.JWTSignatureAlgorithm, m.jwkVerifyKey), jwt.WithValidate(true))
if err != nil {
return "", err
}
// TODO: verify client name
exportName, ok := parsedToken.PrivateClaims()[cpapi.ExportNameJWTClaim]
if !ok {
return "", fmt.Errorf("token missing '%s' claim", cpapi.ExportNameJWTClaim)
}
exportNamespace, ok := parsedToken.PrivateClaims()[cpapi.ExportNamespaceJWTClaim]
if !ok {
return "", fmt.Errorf("token missing '%s' claim", cpapi.ExportNamespaceJWTClaim)
}
return cpapi.ExportClusterName(exportName.(string), exportNamespace.(string)), nil
}
// authorizeIngress authorizes a request for accessing an exported service.
func (m *Manager) authorizeIngress(
ctx context.Context,
req *ingressAuthorizationRequest,
pr string,
) (*ingressAuthorizationResponse, error) {
m.logger.Infof("Received ingress authorization request: %v.", req)
resp := &ingressAuthorizationResponse{}
// check that a corresponding export exists
exportName := types.NamespacedName{
Namespace: req.ServiceName.Namespace,
Name: req.ServiceName.Name,
}
var export v1alpha1.Export
if err := m.getExport(ctx, exportName, &export); err != nil {
if errors.IsNotFound(err) || !meta.IsStatusConditionTrue(export.Status.Conditions, v1alpha1.ExportValid) {
return resp, nil
}
return nil, fmt.Errorf("cannot get export %v: %w", exportName, err)
}
resp.ServiceExists = true
srcAttributes := connectivitypdp.WorkloadAttrs{GatewayNameLabel: pr}
dstAttributes := connectivitypdp.WorkloadAttrs{
ServiceNameLabel: req.ServiceName.Name,
ServiceNamespaceLabel: req.ServiceName.Namespace,
}
decision, err := m.connectivityPDP.Decide(srcAttributes, dstAttributes, req.ServiceName.Namespace)
if err != nil {
return nil, fmt.Errorf("error deciding on an ingress connection: %w", err)
}
if decision.Decision != connectivitypdp.DecisionAllow {
resp.Allowed = false
return resp, nil
}
resp.Allowed = true
// create access token
// TODO: include client name as a claim
token, err := jwt.NewBuilder().
Expiration(time.Now().Add(time.Second*jwtExpirySeconds)).
Claim(cpapi.ExportNameJWTClaim, req.ServiceName.Name).
Claim(cpapi.ExportNamespaceJWTClaim, req.ServiceName.Namespace).
Build()
if err != nil {
return nil, fmt.Errorf("unable to generate access token: %w", err)
}
// sign access token
signed, err := jwt.Sign(token, cpapi.JWTSignatureAlgorithm, m.jwkSignKey)
if err != nil {
return nil, fmt.Errorf("unable to sign access token: %w", err)
}
resp.AccessToken = string(signed)
return resp, nil
}
func (m *Manager) getImport(ctx context.Context, name types.NamespacedName, imp *v1alpha1.Import) error {
if m.getImportCallback != nil {
return m.getImportCallback(name.Name, imp)
}
return m.client.Get(ctx, name, imp)
}
func (m *Manager) getExport(ctx context.Context, name types.NamespacedName, export *v1alpha1.Export) error {
if m.getExportCallback != nil {
return m.getExportCallback(name.Name, export)
}
return m.client.Get(ctx, name, export)
}
func (m *Manager) getPeer(ctx context.Context, name string, pr *v1alpha1.Peer) error {
if m.getPeerCallback != nil {
return m.getPeerCallback(name, pr)
}
peerName := types.NamespacedName{
Name: name,
Namespace: m.namespace,
}
return m.client.Get(ctx, peerName, pr)
}
// NewManager returns a new authorization manager.
func NewManager(peerTLS *tls.ParsedCertData, cl client.Client, namespace string) (*Manager, error) {
// generate RSA key-pair for JWT signing
// TODO: instead of generating, read from k8s secret
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("unable to generate RSA keys: %w", err)
}
jwkSignKey, err := jwk.New(rsaKey)
if err != nil {
return nil, fmt.Errorf("unable to create JWK signing key: %w", err)
}
jwkVerifyKey, err := jwk.New(rsaKey.PublicKey)
if err != nil {
return nil, fmt.Errorf("unable to create JWK verifing key: %w", err)
}
return &Manager{
client: cl,
namespace: namespace,
connectivityPDP: connectivitypdp.NewPDP(),
loadBalancer: NewLoadBalancer(),
peerTLS: peerTLS,
peerClient: make(map[string]*peer.Client),
jwkSignKey: jwkSignKey,
jwkVerifyKey: jwkVerifyKey,
ipToPod: make(map[string]types.NamespacedName),
podList: make(map[types.NamespacedName]podInfo),
logger: logrus.WithField("component", "controlplane.authz.manager"),
}, nil
}