-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
gatekeeper.go
150 lines (129 loc) · 4 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
package auth
import (
"context"
"net/http"
"strings"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/util/kubeconfig"
)
type ContextKey string
const (
WfKey ContextKey = "versioned.Interface"
KubeKey ContextKey = "kubernetes.Interface"
)
const (
Client = "client"
Server = "server"
Hybrid = "hybrid"
)
type Gatekeeper struct {
authType string
// global clients, not to be used if there are better ones
wfClient versioned.Interface
kubeClient kubernetes.Interface
restConfig *rest.Config
}
func NewGatekeeper(authType string, wfClient versioned.Interface, kubeClient kubernetes.Interface, restConfig *rest.Config) Gatekeeper {
return Gatekeeper{authType, wfClient, kubeClient, restConfig}
}
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) {
wfClient, kubeClient, err := s.getClients(ctx)
if err != nil {
return nil, err
}
return context.WithValue(context.WithValue(ctx, WfKey, wfClient), KubeKey, kubeClient), nil
}
func GetWfClient(ctx context.Context) versioned.Interface {
return ctx.Value(WfKey).(versioned.Interface)
}
func GetKubeClient(ctx context.Context) kubernetes.Interface {
return ctx.Value(KubeKey).(kubernetes.Interface)
}
func (s Gatekeeper) useServerAuth() bool {
return s.authType == Server
}
func (s Gatekeeper) useHybridAuth() bool {
return s.authType == Hybrid
}
func (s Gatekeeper) useClientAuth(token string) bool {
if s.authType == Client {
return true
}
if s.useHybridAuth() && token != "" {
return true
}
return false
}
func getToken(md metadata.MD) string {
// looks for the HTTP header `Authorization: Bearer ...`
for _, t := range md.Get("authorization") {
return strings.TrimPrefix(t, "Bearer ")
}
// check the HTTP cookie
for _, t := range md.Get("grpcgateway-cookie") {
header := http.Header{}
header.Add("Cookie", t)
request := http.Request{Header: header}
token, err := request.Cookie("authorization")
if err == nil {
return strings.TrimPrefix(token.Value, "Bearer ")
}
}
return ""
}
func (s Gatekeeper) getClients(ctx context.Context) (versioned.Interface, kubernetes.Interface, error) {
if s.useServerAuth() {
return s.wfClient, s.kubeClient, nil
}
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
if s.useHybridAuth() {
return s.wfClient, s.kubeClient, nil
}
return nil, nil, status.Error(codes.Unauthenticated, "unable to get metadata from incoming context")
}
token := getToken(md)
if !s.useClientAuth(token) {
return s.wfClient, s.kubeClient, nil
}
restConfig, err := kubeconfig.GetRestConfig(token)
if err != nil {
return nil, nil, status.Errorf(codes.Unauthenticated, "failed to create REST config: %v", err)
}
wfClient, err := versioned.NewForConfig(restConfig)
if err != nil {
return nil, nil, status.Errorf(codes.Unauthenticated, "failure to create wfClientset with ClientConfig: %v", err)
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, nil, status.Errorf(codes.Unauthenticated, "failure to create kubeClientset with ClientConfig: %v", err)
}
return wfClient, kubeClient, nil
}