forked from gravitational/teleport
/
system_role.go
269 lines (242 loc) · 8.2 KB
/
system_role.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
/*
Copyright 2021 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 types
import (
"strings"
"github.com/gravitational/trace"
)
// SystemRole identifies the role of an SSH connection. Unlike "user roles"
// introduced as part of RBAC in Teleport 1.4+ these are built-in roles used
// for different Teleport components when connecting to each other.
type SystemRole string
// SystemRoles is a TeleportRole list
type SystemRoles []SystemRole
const (
// RoleAuth is for teleport auth server (authority, authentication and authorization)
RoleAuth SystemRole = "Auth"
// RoleNode is a role for SSH node in the cluster
RoleNode SystemRole = "Node"
// RoleProxy is a role for SSH proxy in the cluster
RoleProxy SystemRole = "Proxy"
// RoleAdmin is admin role
RoleAdmin SystemRole = "Admin"
// RoleProvisionToken is a role for nodes authenticated using provisioning tokens
RoleProvisionToken SystemRole = "ProvisionToken"
// RoleTrustedCluster is a role needed for tokens used to add trusted clusters.
RoleTrustedCluster SystemRole = "Trusted_cluster"
// RoleSignup is for first time signing up users
RoleSignup SystemRole = "Signup"
// RoleNop is used for actions that are already using external authz mechanisms
// e.g. tokens or passwords
RoleNop SystemRole = "Nop"
// RoleRemoteProxy is a role for remote SSH proxy in the cluster
RoleRemoteProxy SystemRole = "RemoteProxy"
// RoleKube is a role for a kubernetes service.
RoleKube SystemRole = "Kube"
// RoleApp is a role for a app proxy in the cluster.
RoleApp SystemRole = "App"
// RoleDatabase is a role for a database proxy in the cluster.
RoleDatabase SystemRole = "Db"
// RoleWindowsDesktop is a role for a Windows desktop service.
RoleWindowsDesktop SystemRole = "WindowsDesktop"
// RoleBot is a role for a bot.
RoleBot SystemRole = "Bot"
// RoleInstance is a role implicitly held by teleport servers (i.e. any teleport
// auth token which grants a server role such as proxy/node/etc also implicitly
// grants the instance role, and any valid cert that proves that the caller holds
// a server role also implies that the caller holds the instance role). This role
// doesn't grant meaningful privileges on its own, but is a useful placeholder in
// contexts such as multi-role certs where there is no particular system role that
// is "primary".
RoleInstance SystemRole = "Instance"
// RoleDiscovery is a role for discovery nodes in the cluster
RoleDiscovery SystemRole = "Discovery"
)
// roleMappings maps a set of allowed lowercase system role names
// to the proper system role
var roleMappings = map[string]SystemRole{
"auth": RoleAuth,
"node": RoleNode,
"proxy": RoleProxy,
"admin": RoleAdmin,
"provisiontoken": RoleProvisionToken,
"trusted_cluster": RoleTrustedCluster,
"trustedcluster": RoleTrustedCluster,
"signup": RoleSignup,
"nop": RoleNop,
"remoteproxy": RoleRemoteProxy,
"remote_proxy": RoleRemoteProxy,
"kube": RoleKube,
"app": RoleApp,
"db": RoleDatabase,
"windowsdesktop": RoleWindowsDesktop,
"windows_desktop": RoleWindowsDesktop,
"bot": RoleBot,
"instance": RoleInstance,
"discovery": RoleDiscovery,
}
// localServiceMappings is the subset of role mappings which happen to be true
// teleport services (e.g. db, kube, etc), excluding those which represent remote
// services (i.e. remoteproxy).
var localServiceMappings = map[SystemRole]struct{}{
RoleAuth: {},
RoleNode: {},
RoleProxy: {},
RoleKube: {},
RoleApp: {},
RoleDatabase: {},
RoleWindowsDesktop: {},
RoleDiscovery: {},
}
// LocalServiceMappings returns the subset of role mappings which happen
// to be true Teleport services (e.g. db, kube, proxy, etc), excluding
// those which represent remote service (i.e. remoteproxy).
func LocalServiceMappings() SystemRoles {
var sr SystemRoles
for k := range localServiceMappings {
sr = append(sr, k)
}
return sr
}
// NewTeleportRoles return a list of teleport roles from slice of strings
func NewTeleportRoles(in []string) (SystemRoles, error) {
var roles SystemRoles
for _, val := range in {
roles = append(roles, SystemRole(val))
}
return roles, roles.Check()
}
// ParseTeleportRoles takes a comma-separated list of roles and returns a slice
// of teleport roles, or an error if parsing failed
func ParseTeleportRoles(str string) (SystemRoles, error) {
var roles SystemRoles
for _, s := range strings.Split(str, ",") {
cleaned := strings.ToLower(strings.TrimSpace(s))
if r, ok := roleMappings[cleaned]; ok && r.Check() == nil {
roles = append(roles, r)
continue
}
return nil, trace.BadParameter("invalid role %q", s)
}
if len(roles) == 0 {
return nil, trace.BadParameter("no valid roles in $%q", str)
}
return roles, roles.Check()
}
// Include returns 'true' if a given list of teleport roles includes a given role
func (roles SystemRoles) Include(role SystemRole) bool {
for _, r := range roles {
if r == role {
return true
}
}
return false
}
// IncludeAny returns 'true' if a given list of teleport roles includes any of
// the given candidate roles.
func (roles SystemRoles) IncludeAny(candidates ...SystemRole) bool {
for _, r := range candidates {
if roles.Include(r) {
return true
}
}
return false
}
// StringSlice returns teleport roles as string slice
func (roles SystemRoles) StringSlice() []string {
s := make([]string, 0)
for _, r := range roles {
s = append(s, r.String())
}
return s
}
// asSet returns teleport roles as set (map).
func (roles SystemRoles) asSet() map[SystemRole]struct{} {
s := make(map[SystemRole]struct{}, len(roles))
for _, r := range roles {
s[r] = struct{}{}
}
return s
}
// Equals compares two sets of teleport roles
func (roles SystemRoles) Equals(other SystemRoles) bool {
rs, os := roles.asSet(), other.asSet()
if len(rs) != len(os) {
return false
}
for r := range rs {
if _, ok := os[r]; !ok {
return false
}
}
return true
}
// Check returns an error if the teleport role set is incorrect (contains unknown roles)
func (roles SystemRoles) Check() error {
seen := make(map[SystemRole]struct{})
for _, role := range roles {
if err := role.Check(); err != nil {
return trace.Wrap(err)
}
if _, ok := seen[role]; ok {
return trace.BadParameter("duplicate role %q", role)
}
seen[role] = struct{}{}
}
return nil
}
// String returns comma separated string with teleport roles
func (roles SystemRoles) String() string {
return strings.Join(roles.StringSlice(), ",")
}
// Set sets the value of the teleport role from string, used to integrate with CLI tools
func (r *SystemRole) Set(v string) error {
if len(v) > 0 {
v = strings.ToUpper(v[:1]) + v[1:]
}
val := SystemRole(v)
if err := val.Check(); err != nil {
return trace.Wrap(err)
}
*r = val
return nil
}
// String returns debug-friendly representation of this teleport role.
func (r *SystemRole) String() string {
switch *r {
case RoleSignup:
return "Password"
case RoleTrustedCluster:
return "trusted_cluster"
default:
return string(*r)
}
}
// Check checks if this a a valid teleport role value, returns nil
// if it's ok, false otherwise
// Check checks if this a a valid teleport role value, returns nil
// if it's ok, false otherwise
func (r *SystemRole) Check() error {
sr, ok := roleMappings[strings.ToLower(string(*r))]
if ok && string(*r) == string(sr) {
return nil
}
return trace.BadParameter("role %v is not registered", *r)
}
// IsLocalService checks if the given system role is a teleport service (e.g. auth),
// as opposed to some non-service role (e.g. admin). Excludes remote services such
// as remoteproxy.
func (r *SystemRole) IsLocalService() bool {
_, ok := localServiceMappings[*r]
return ok
}