forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usercontext.go
132 lines (112 loc) · 3.76 KB
/
usercontext.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
/*
Copyright 2015 Gravitational, Inc.
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 ui
import (
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
)
type access struct {
List bool `json:"list"`
Read bool `json:"read"`
Edit bool `json:"edit"`
Create bool `json:"create"`
Delete bool `json:"remove"`
}
type userACL struct {
// Sessions defines access to recorded sessions
Sessions access `json:"sessions"`
// AuthConnectors defines access to auth.connectors
AuthConnectors access `json:"authConnectors"`
// Roles defines access to roles
Roles access `json:"roles"`
// TrustedClusters defines access to trusted clusters
TrustedClusters access `json:"trustedClusters"`
// SSH defines access to servers
SSHLogins []string `json:"sshLogins"`
}
type authType string
const (
authLocal authType = "local"
authSSO authType = "sso"
)
type userContext struct {
// AuthType is auth method of this user
AuthType authType `json:"authType"`
// Name is this user name
Name string `json:"userName"`
// ACL contains user access control list
ACL userACL `json:"userAcl"`
}
func getLogins(roleSet services.RoleSet) []string {
allowed := []string{}
denied := []string{}
for _, role := range roleSet {
denied = append(denied, role.GetLogins(services.Deny)...)
allowed = append(allowed, role.GetLogins(services.Allow)...)
}
allowed = utils.Deduplicate(allowed)
denied = utils.Deduplicate(denied)
userLogins := []string{}
for _, login := range allowed {
if services.MatchLogin(denied, login) == false {
userLogins = append(userLogins, login)
}
}
return userLogins
}
func hasAccess(roleSet services.RoleSet, ctx *services.Context, kind string, verbs ...string) bool {
for _, verb := range verbs {
err := roleSet.CheckAccessToRule(ctx, defaults.Namespace, kind, verb)
if err != nil {
return false
}
}
return true
}
func newAccess(roleSet services.RoleSet, ctx *services.Context, kind string) access {
return access{
List: hasAccess(roleSet, ctx, kind, services.VerbList),
Read: hasAccess(roleSet, ctx, kind, services.VerbRead),
Edit: hasAccess(roleSet, ctx, kind, services.VerbUpdate),
Create: hasAccess(roleSet, ctx, kind, services.VerbCreate),
Delete: hasAccess(roleSet, ctx, kind, services.VerbDelete),
}
}
// NewUserContext constructs user context from roles assigned to user
func NewUserContext(user services.User, userRoles services.RoleSet) (*userContext, error) {
ctx := &services.Context{User: user}
sessionAccess := newAccess(userRoles, ctx, services.KindSession)
roleAccess := newAccess(userRoles, ctx, services.KindRole)
authConnectors := newAccess(userRoles, ctx, services.KindAuthConnector)
trustedClusterAccess := newAccess(userRoles, ctx, services.KindTrustedCluster)
logins := getLogins(userRoles)
acl := userACL{
AuthConnectors: authConnectors,
TrustedClusters: trustedClusterAccess,
Sessions: sessionAccess,
Roles: roleAccess,
SSHLogins: logins,
}
// detect auth method for this user
identities := append(user.GetOIDCIdentities(), user.GetSAMLIdentities()...)
authType := authLocal
if len(identities) > 0 {
authType = authSSO
}
return &userContext{
Name: user.GetName(),
ACL: acl,
AuthType: authType,
}, nil
}