-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
gatekeeper.go
274 lines (251 loc) · 9.61 KB
/
gatekeeper.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
package auth
import (
"context"
"fmt"
"net/http"
"sort"
"strconv"
"github.com/antonmedv/expr"
eventsource "github.com/argoproj/argo-events/pkg/client/eventsource/clientset/versioned"
sensor "github.com/argoproj/argo-events/pkg/client/sensor/clientset/versioned"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
workflow "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
"github.com/argoproj/argo-workflows/v3/server/auth/serviceaccount"
"github.com/argoproj/argo-workflows/v3/server/auth/sso"
"github.com/argoproj/argo-workflows/v3/server/auth/types"
servertypes "github.com/argoproj/argo-workflows/v3/server/types"
jsonutil "github.com/argoproj/argo-workflows/v3/util/json"
"github.com/argoproj/argo-workflows/v3/util/kubeconfig"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
type ContextKey string
const (
WfKey ContextKey = "workflow.Interface"
SensorKey ContextKey = "sensor.Interface"
EventSourceKey ContextKey = "eventsource.Interface"
KubeKey ContextKey = "kubernetes.Interface"
ClaimsKey ContextKey = "types.Claims"
)
//go:generate mockery -name Gatekeeper
type Gatekeeper interface {
Context(ctx context.Context) (context.Context, error)
UnaryServerInterceptor() grpc.UnaryServerInterceptor
StreamServerInterceptor() grpc.StreamServerInterceptor
}
type ClientForAuthorization func(authorization string) (*rest.Config, *servertypes.Clients, error)
type gatekeeper struct {
Modes Modes
// global clients, not to be used if there are better ones
clients *servertypes.Clients
restConfig *rest.Config
ssoIf sso.Interface
clientForAuthorization ClientForAuthorization
// The namespace the server is installed in.
namespace string
}
func NewGatekeeper(modes Modes, clients *servertypes.Clients, restConfig *rest.Config, ssoIf sso.Interface, clientForAuthorization ClientForAuthorization, namespace string) (Gatekeeper, error) {
if len(modes) == 0 {
return nil, fmt.Errorf("must specify at least one auth mode")
}
return &gatekeeper{modes, clients, restConfig, ssoIf, clientForAuthorization, namespace}, nil
}
func (s *gatekeeper) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
ctx, err = s.Context(ctx)
if err != nil {
return nil, err
}
return handler(ctx, req)
}
}
func (s *gatekeeper) StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx, err := s.Context(ss.Context())
if err != nil {
return err
}
wrapped := grpc_middleware.WrapServerStream(ss)
wrapped.WrappedContext = ctx
return handler(srv, wrapped)
}
}
func (s *gatekeeper) Context(ctx context.Context) (context.Context, error) {
clients, claims, err := s.getClients(ctx)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, WfKey, clients.Workflow)
ctx = context.WithValue(ctx, EventSourceKey, clients.EventSource)
ctx = context.WithValue(ctx, SensorKey, clients.Sensor)
ctx = context.WithValue(ctx, KubeKey, clients.Kubernetes)
ctx = context.WithValue(ctx, ClaimsKey, claims)
return ctx, nil
}
func GetWfClient(ctx context.Context) workflow.Interface {
return ctx.Value(WfKey).(workflow.Interface)
}
func GetEventSourceClient(ctx context.Context) eventsource.Interface {
return ctx.Value(EventSourceKey).(eventsource.Interface)
}
func GetSensorClient(ctx context.Context) sensor.Interface {
return ctx.Value(SensorKey).(sensor.Interface)
}
func GetKubeClient(ctx context.Context) kubernetes.Interface {
return ctx.Value(KubeKey).(kubernetes.Interface)
}
func GetClaims(ctx context.Context) *types.Claims {
config, _ := ctx.Value(ClaimsKey).(*types.Claims)
return config
}
func getAuthHeader(md metadata.MD) string {
// looks for the HTTP header `Authorization: Bearer ...`
for _, t := range md.Get("authorization") {
return t
}
// check the HTTP cookie
for _, t := range md.Get("cookie") {
header := http.Header{}
header.Add("Cookie", t)
request := http.Request{Header: header}
token, err := request.Cookie("authorization")
if err == nil {
return token.Value
}
}
return ""
}
func (s gatekeeper) getClients(ctx context.Context) (*servertypes.Clients, *types.Claims, error) {
md, _ := metadata.FromIncomingContext(ctx)
authorization := getAuthHeader(md)
mode, valid := s.Modes.GetMode(authorization)
if !valid {
return nil, nil, status.Error(codes.Unauthenticated, "token not valid for running mode")
}
switch mode {
case Client:
restConfig, clients, err := s.clientForAuthorization(authorization)
if err != nil {
return nil, nil, status.Error(codes.Unauthenticated, err.Error())
}
claims, _ := serviceaccount.ClaimSetFor(restConfig)
return clients, claims, nil
case Server:
claims, _ := serviceaccount.ClaimSetFor(s.restConfig)
return s.clients, claims, nil
case SSO:
claims, err := s.ssoIf.Authorize(authorization)
if err != nil {
return nil, nil, status.Error(codes.Unauthenticated, err.Error())
}
if s.ssoIf.IsRBACEnabled() {
clients, err := s.rbacAuthorization(ctx, claims)
if err != nil {
log.WithError(err).Error("failed to perform RBAC authorization")
return nil, nil, status.Error(codes.PermissionDenied, "not allowed")
}
return clients, claims, nil
} else {
// important! write an audit entry (i.e. log entry) so we know which user performed an operation
log.WithFields(log.Fields{"subject": claims.Subject}).Info("using the default service account for user")
return s.clients, claims, nil
}
default:
panic("this should never happen")
}
}
func (s *gatekeeper) rbacAuthorization(ctx context.Context, claims *types.Claims) (*servertypes.Clients, error) {
list, err := s.clients.Kubernetes.CoreV1().ServiceAccounts(s.namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list SSO RBAC service accounts: %w", err)
}
var serviceAccounts []corev1.ServiceAccount
for _, serviceAccount := range list.Items {
_, ok := serviceAccount.Annotations[common.AnnotationKeyRBACRule]
if !ok {
continue
}
serviceAccounts = append(serviceAccounts, serviceAccount)
}
precedence := func(serviceAccount corev1.ServiceAccount) int {
i, _ := strconv.Atoi(serviceAccount.Annotations[common.AnnotationKeyRBACRulePrecedence])
return i
}
sort.Slice(serviceAccounts, func(i, j int) bool { return precedence(serviceAccounts[i]) > precedence(serviceAccounts[j]) })
for _, serviceAccount := range serviceAccounts {
rule := serviceAccount.Annotations[common.AnnotationKeyRBACRule]
v, err := jsonutil.Jsonify(claims)
if err != nil {
return nil, fmt.Errorf("failed to marshall claims: %w", err)
}
result, err := expr.Eval(rule, v)
if err != nil {
return nil, fmt.Errorf("failed to evaluate rule: %w", err)
}
allow, ok := result.(bool)
if !ok {
return nil, fmt.Errorf("failed to evaluate rule: not a boolean")
}
if !allow {
continue
}
authorization, err := s.authorizationForServiceAccount(ctx, serviceAccount.Name)
if err != nil {
return nil, err
}
_, clients, err := s.clientForAuthorization(authorization)
if err != nil {
return nil, err
}
claims.ServiceAccountName = serviceAccount.Name
// important! write an audit entry (i.e. log entry) so we know which user performed an operation
log.WithFields(log.Fields{"serviceAccount": serviceAccount.Name, "subject": claims.Subject}).Info("selected SSO RBAC service account for user")
return clients, nil
}
return nil, fmt.Errorf("no service account rule matches")
}
func (s *gatekeeper) authorizationForServiceAccount(ctx context.Context, serviceAccountName string) (string, error) {
serviceAccount, err := s.clients.Kubernetes.CoreV1().ServiceAccounts(s.namespace).Get(ctx, serviceAccountName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get service account: %w", err)
}
if len(serviceAccount.Secrets) == 0 {
return "", fmt.Errorf("expected at least one secret for SSO RBAC service account: %w", err)
}
secret, err := s.clients.Kubernetes.CoreV1().Secrets(s.namespace).Get(ctx, serviceAccount.Secrets[0].Name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get service account secret: %w", err)
}
return "Bearer " + string(secret.Data["token"]), nil
}
func DefaultClientForAuthorization(authorization string) (*rest.Config, *servertypes.Clients, error) {
restConfig, err := kubeconfig.GetRestConfig(authorization)
if err != nil {
return nil, nil, fmt.Errorf("failed to create REST config: %w", err)
}
wfClient, err := workflow.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("failure to create workflow client: %w", err)
}
eventSourceClient, err := eventsource.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("failure to create event source client: %w", err)
}
sensorClient, err := sensor.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("failure to create sensor client: %w", err)
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, nil, fmt.Errorf("failure to create kubernetes client: %w", err)
}
return restConfig, &servertypes.Clients{Workflow: wfClient, EventSource: eventSourceClient, Sensor: sensorClient, Kubernetes: kubeClient}, nil
}