forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
121 lines (106 loc) · 3.6 KB
/
filter.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
package requests
import (
"context"
"fmt"
"net/http"
"reflect"
"sort"
"github.com/rancher/rancher/pkg/audit"
"github.com/rancher/rancher/pkg/auth/providerrefresh"
"github.com/rancher/rancher/pkg/auth/requests/sar"
"github.com/rancher/rancher/pkg/auth/util"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
k8sUser "k8s.io/apiserver/pkg/authentication/user"
)
func NewAuthenticationFilter(ctx context.Context, managementContext *config.ScaledContext, next http.Handler, sar sar.SubjectAccessReview) (http.Handler, error) {
if managementContext == nil {
return nil, fmt.Errorf("Failed to build NewAuthenticationFilter, nil ManagementContext")
}
auth := NewAuthenticator(ctx, managementContext)
return &authHeaderHandler{
auth: auth,
next: next,
sar: sar,
userAuthRefresher: providerrefresh.NewUserAuthRefresher(ctx, managementContext),
}, nil
}
type authHeaderHandler struct {
auth Authenticator
next http.Handler
sar sar.SubjectAccessReview
userAuthRefresher providerrefresh.UserAuthRefresher
}
func (h authHeaderHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
authed, user, groups, err := h.auth.Authenticate(req)
if err != nil || !authed {
util.ReturnHTTPError(rw, req, 401, err.Error())
return
}
var impersonateUser bool
var impersonateGroup bool
reqUser := req.Header.Get("Impersonate-User")
var reqGroup []string
if g, ok := req.Header["Impersonate-Group"]; ok {
reqGroup = g
}
// If there is an impersonate header, the incoming request is attempting to
// impersonate a different user, verify the token user is authz to impersonate
if h.sar != nil {
if reqUser != "" && reqUser != user {
canDo, err := h.sar.UserCanImpersonateUser(req, user, reqUser)
if err != nil {
util.ReturnHTTPError(rw, req, 401, err.Error())
return
} else if !canDo {
util.ReturnHTTPError(rw, req, 401, "not allowed to impersonate")
return
}
impersonateUser = true
}
if len(reqGroup) > 0 && !groupsEqual(reqGroup, groups) {
canDo, err := h.sar.UserCanImpersonateGroups(req, user, reqGroup)
if err != nil {
util.ReturnHTTPError(rw, req, 401, err.Error())
return
} else if !canDo {
util.ReturnHTTPError(rw, req, 401, "not allowed to impersonate")
return
}
impersonateGroup = true
}
}
// Not impersonating either, set to the token users creds - this is the default
// case if sar is nil
if !impersonateUser && !impersonateGroup {
req.Header.Set("Impersonate-User", user)
req.Header.Del("Impersonate-Group")
for _, group := range groups {
req.Header.Add("Impersonate-Group", group)
}
} else if impersonateUser && !impersonateGroup { // Impersonating user, only user the authd header
req.Header.Set("Impersonate-Group", k8sUser.AllAuthenticated)
} else if !impersonateUser && impersonateGroup { // Impersonating groups, drop the user, add authd header
req.Header.Del("Impersonate-User")
req.Header.Add("Impersonate-Group", k8sUser.AllAuthenticated)
}
logrus.Debugf("Requesting user: %v, Requesting groups: %v Impersonate user: %v Impersonate group: %v",
user, groups, reqUser, reqGroup)
auditUser, ok := audit.FromContext(req.Context())
if ok {
auditUser.Name = user
auditUser.Group = groups
auditUser.RequestUser = reqUser
auditUser.RequestGroups = reqGroup
}
go h.userAuthRefresher.TriggerUserRefresh(user, false)
h.next.ServeHTTP(rw, req)
}
func groupsEqual(group1, group2 []string) bool {
if len(group1) != len(group2) {
return false
}
sort.Strings(group1)
sort.Strings(group2)
return reflect.DeepEqual(group1, group2)
}