forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.go
277 lines (254 loc) · 8.85 KB
/
permissions.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
/*
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 auth
import (
"context"
"fmt"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/trace"
)
// NewRoleAuthorizer authorizes everyone as predefined role
func NewRoleAuthorizer(r teleport.Role) (Authorizer, error) {
authContext, err := contextForBuiltinRole(r)
if err != nil {
return nil, trace.Wrap(err)
}
return &contextAuthorizer{authContext: *authContext}, nil
}
// contextAuthorizer is a helper struct that always authorizes
// based on predefined context, helpful for tests
type contextAuthorizer struct {
authContext AuthContext
}
// Authorize authorizes user based on identity supplied via context
func (r *contextAuthorizer) Authorize(ctx context.Context) (*AuthContext, error) {
return &r.authContext, nil
}
// NewUserAuthorizer authorizes everyone as predefined local user
func NewUserAuthorizer(username string, identity services.Identity, access services.Access) (Authorizer, error) {
authContext, err := contextForLocalUser(username, identity, access)
if err != nil {
return nil, trace.Wrap(err)
}
return &contextAuthorizer{authContext: *authContext}, nil
}
// NewAuthorizer returns new authorizer using backends
func NewAuthorizer(access services.Access, identity services.Identity, trust services.Trust) (Authorizer, error) {
if access == nil {
return nil, trace.BadParameter("missing parameter access")
}
if identity == nil {
return nil, trace.BadParameter("missing parameter identity")
}
if trust == nil {
return nil, trace.BadParameter("missing parameter trust")
}
return &authorizer{access: access, identity: identity, trust: trust}, nil
}
// Authorizer authorizes identity and returns auth context
type Authorizer interface {
// Authorize authorizes user based on identity supplied via context
Authorize(ctx context.Context) (*AuthContext, error)
}
// authorizer creates new local authorizer
type authorizer struct {
access services.Access
identity services.Identity
trust services.Trust
}
// AuthzContext is authorization context
type AuthContext struct {
// User is the user name
User services.User
// Checker is access checker
Checker services.AccessChecker
}
// Authorize authorizes user based on identity supplied via context
func (a *authorizer) Authorize(ctx context.Context) (*AuthContext, error) {
if ctx == nil {
return nil, trace.AccessDenied("missing authentication context")
}
userI := ctx.Value(teleport.ContextUser)
switch user := userI.(type) {
case teleport.LocalUser:
return a.authorizeLocalUser(user)
case teleport.RemoteUser:
return a.authorizeRemoteUser(user)
case teleport.BuiltinRole:
return a.authorizeBuiltinRole(user)
default:
return nil, trace.AccessDenied("unsupported context type %T", userI)
}
}
// authorizeLocalUser returns authz context based on the username
func (a *authorizer) authorizeLocalUser(u teleport.LocalUser) (*AuthContext, error) {
return contextForLocalUser(u.Username, a.identity, a.access)
}
// authorizeRemoteUser returns checker based on cert authority roles
func (a *authorizer) authorizeRemoteUser(u teleport.RemoteUser) (*AuthContext, error) {
ca, err := a.trust.GetCertAuthority(services.CertAuthID{Type: services.UserCA, DomainName: u.ClusterName}, false)
if err != nil {
return nil, trace.Wrap(err)
}
roleNames, err := ca.CombinedMapping().Map(u.RemoteRoles)
if err != nil {
return nil, trace.AccessDenied("failed to map roles for remote user %v from cluster %v", u.Username, u.ClusterName)
}
checker, err := services.FetchRoles(roleNames, a.access)
if err != nil {
return nil, trace.Wrap(err)
}
user, err := services.NewUser(fmt.Sprintf("remote-%v-%v", u.Username, u.ClusterName))
if err != nil {
return nil, trace.Wrap(err)
}
return &AuthContext{
// this is done on purpose to make sure user does not match some real local user
User: user,
Checker: checker,
}, nil
}
// authorizeBuiltinRole authorizes builtin role
func (a *authorizer) authorizeBuiltinRole(r teleport.BuiltinRole) (*AuthContext, error) {
return contextForBuiltinRole(r.Role)
}
// GetCheckerForBuiltinRole returns checkers for embedded builtin role
func GetCheckerForBuiltinRole(role teleport.Role) (services.AccessChecker, error) {
switch role {
case teleport.RoleAuth:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{services.Wildcard},
Resources: map[string][]string{
services.KindAuthServer: services.RW()},
})
case teleport.RoleProvisionToken:
return services.FromSpec(role.String(), services.RoleSpecV2{})
case teleport.RoleNode:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{services.Wildcard},
Resources: map[string][]string{
services.KindNode: services.RW(),
services.KindSession: services.RW(),
services.KindEvent: services.RW(),
services.KindProxy: services.RO(),
services.KindCertAuthority: services.RO(),
services.KindUser: services.RO(),
services.KindNamespace: services.RO(),
services.KindRole: services.RO(),
services.KindAuthServer: services.RO(),
},
})
case teleport.RoleProxy:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{services.Wildcard},
Resources: map[string][]string{
services.KindProxy: services.RW(),
services.KindOIDCRequest: services.RW(),
services.KindOIDC: services.RO(),
services.KindSAMLRequest: services.RW(),
services.KindSAML: services.RO(),
services.KindNamespace: services.RO(),
services.KindEvent: services.RW(),
services.KindSession: services.RW(),
services.KindNode: services.RO(),
services.KindAuthServer: services.RO(),
services.KindReverseTunnel: services.RO(),
services.KindCertAuthority: services.RO(),
services.KindUser: services.RO(),
services.KindRole: services.RO(),
services.KindClusterAuthPreference: services.RO(),
services.KindUniversalSecondFactor: services.RO(),
},
})
case teleport.RoleWeb:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{services.Wildcard},
Resources: map[string][]string{
services.KindWebSession: services.RW(),
services.KindSession: services.RW(),
services.KindAuthServer: services.RO(),
services.KindUser: services.RO(),
services.KindRole: services.RO(),
services.KindNamespace: services.RO(),
services.KindTrustedCluster: services.RO(),
},
})
case teleport.RoleSignup:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{services.Wildcard},
Resources: map[string][]string{
services.KindAuthServer: services.RO(),
services.KindClusterAuthPreference: services.RO(),
},
})
case teleport.RoleAdmin:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
MaxSessionTTL: services.MaxDuration(),
Logins: []string{},
Namespaces: []string{services.Wildcard},
NodeLabels: map[string]string{services.Wildcard: services.Wildcard},
Resources: map[string][]string{
services.Wildcard: services.RW(),
},
})
case teleport.RoleNop:
return services.FromSpec(
role.String(),
services.RoleSpecV2{
Namespaces: []string{},
Resources: map[string][]string{},
})
}
return nil, trace.NotFound("%v is not reconginzed", role.String())
}
func contextForBuiltinRole(r teleport.Role) (*AuthContext, error) {
checker, err := GetCheckerForBuiltinRole(r)
if err != nil {
return nil, trace.Wrap(err)
}
user, err := services.NewUser(fmt.Sprintf("builtin-%v", r))
if err != nil {
return nil, trace.Wrap(err)
}
return &AuthContext{
User: user,
Checker: checker,
}, nil
}
func contextForLocalUser(username string, identity services.Identity, access services.Access) (*AuthContext, error) {
user, err := identity.GetUser(username)
if err != nil {
return nil, trace.Wrap(err)
}
checker, err := services.FetchRoles(user.GetRoles(), access)
if err != nil {
return nil, trace.Wrap(err)
}
return &AuthContext{
User: user,
Checker: checker,
}, nil
}