-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathpresets.go
230 lines (213 loc) · 8.35 KB
/
presets.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
/*
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 services
import (
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/constants"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
)
// NewPresetEditorRole returns a new pre-defined role for cluster
// editors who can edit cluster configuration resources.
func NewPresetEditorRole() types.Role {
role := &types.RoleV6{
Kind: types.KindRole,
Version: types.V6,
Metadata: types.Metadata{
Name: teleport.PresetEditorRoleName,
Namespace: apidefaults.Namespace,
Description: "Edit cluster configuration",
},
Spec: types.RoleSpecV6{
Options: types.RoleOptions{
CertificateFormat: constants.CertificateFormatStandard,
MaxSessionTTL: types.NewDuration(apidefaults.MaxCertDuration),
PortForwarding: types.NewBoolOption(true),
ForwardAgent: types.NewBool(true),
BPF: apidefaults.EnhancedEvents(),
RecordSession: &types.RecordSession{
Desktop: types.NewBoolOption(false),
},
},
Allow: types.RoleConditions{
Namespaces: []string{apidefaults.Namespace},
Rules: []types.Rule{
types.NewRule(types.KindUser, RW()),
types.NewRule(types.KindRole, RW()),
types.NewRule(types.KindOIDC, RW()),
types.NewRule(types.KindSAML, RW()),
types.NewRule(types.KindGithub, RW()),
types.NewRule(types.KindOIDCRequest, RW()),
types.NewRule(types.KindSAMLRequest, RW()),
types.NewRule(types.KindGithubRequest, RW()),
types.NewRule(types.KindClusterAuditConfig, RW()),
types.NewRule(types.KindClusterAuthPreference, RW()),
types.NewRule(types.KindAuthConnector, RW()),
types.NewRule(types.KindClusterName, RW()),
types.NewRule(types.KindClusterNetworkingConfig, RW()),
types.NewRule(types.KindSessionRecordingConfig, RW()),
types.NewRule(types.KindTrustedCluster, RW()),
types.NewRule(types.KindRemoteCluster, RW()),
types.NewRule(types.KindToken, RW()),
types.NewRule(types.KindConnectionDiagnostic, RW()),
types.NewRule(types.KindDatabase, RW()),
types.NewRule(types.KindDatabaseCertificate, RW()),
types.NewRule(types.KindInstaller, RW()),
types.NewRule(types.KindDevice, append(RW(), types.VerbCreateEnrollToken, types.VerbEnroll)),
types.NewRule(types.KindDatabaseService, RO()),
types.NewRule(types.KindInstance, RO()),
// Please see defaultAllowRules when adding a new rule.
},
},
},
}
return role
}
// NewPresetAccessRole creates a role for users who are allowed to initiate
// interactive sessions.
func NewPresetAccessRole() types.Role {
role := &types.RoleV6{
Kind: types.KindRole,
Version: types.V6,
Metadata: types.Metadata{
Name: teleport.PresetAccessRoleName,
Namespace: apidefaults.Namespace,
Description: "Access cluster resources",
},
Spec: types.RoleSpecV6{
Options: types.RoleOptions{
CertificateFormat: constants.CertificateFormatStandard,
MaxSessionTTL: types.NewDuration(apidefaults.MaxCertDuration),
PortForwarding: types.NewBoolOption(true),
ForwardAgent: types.NewBool(true),
BPF: apidefaults.EnhancedEvents(),
RecordSession: &types.RecordSession{Desktop: types.NewBoolOption(true)},
},
Allow: types.RoleConditions{
Namespaces: []string{apidefaults.Namespace},
NodeLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
AppLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
KubernetesLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
WindowsDesktopLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseNames: []string{teleport.TraitInternalDBNamesVariable},
DatabaseUsers: []string{teleport.TraitInternalDBUsersVariable},
KubernetesResources: []types.KubernetesResource{
{
Kind: types.KindKubePod,
Namespace: types.Wildcard,
Name: types.Wildcard,
},
},
Rules: []types.Rule{
types.NewRule(types.KindEvent, RO()),
{
Resources: []string{types.KindSession},
Verbs: []string{types.VerbRead, types.VerbList},
Where: "contains(session.participants, user.metadata.name)",
},
types.NewRule(types.KindInstance, RO()),
// Please see defaultAllowRules when adding a new rule.
},
},
},
}
role.SetLogins(types.Allow, []string{teleport.TraitInternalLoginsVariable})
role.SetWindowsLogins(types.Allow, []string{teleport.TraitInternalWindowsLoginsVariable})
role.SetKubeUsers(types.Allow, []string{teleport.TraitInternalKubeUsersVariable})
role.SetKubeGroups(types.Allow, []string{teleport.TraitInternalKubeGroupsVariable})
role.SetAWSRoleARNs(types.Allow, []string{teleport.TraitInternalAWSRoleARNs})
role.SetAzureIdentities(types.Allow, []string{teleport.TraitInternalAzureIdentities})
role.SetGCPServiceAccounts(types.Allow, []string{teleport.TraitInternalGCPServiceAccounts})
return role
}
// NewPresetAuditorRole returns a new pre-defined role for cluster
// auditor - someone who can review cluster events and replay sessions,
// but can't initiate interactive sessions or modify configuration.
func NewPresetAuditorRole() types.Role {
role := &types.RoleV6{
Kind: types.KindRole,
Version: types.V6,
Metadata: types.Metadata{
Name: teleport.PresetAuditorRoleName,
Namespace: apidefaults.Namespace,
Description: "Review cluster events and replay sessions",
},
Spec: types.RoleSpecV6{
Options: types.RoleOptions{
CertificateFormat: constants.CertificateFormatStandard,
MaxSessionTTL: types.NewDuration(apidefaults.MaxCertDuration),
RecordSession: &types.RecordSession{
Desktop: types.NewBoolOption(false),
},
},
Allow: types.RoleConditions{
Namespaces: []string{apidefaults.Namespace},
Rules: []types.Rule{
types.NewRule(types.KindSession, RO()),
types.NewRule(types.KindEvent, RO()),
types.NewRule(types.KindSessionTracker, RO()),
// Please see defaultAllowRules when adding a new rule.
},
},
},
}
role.SetLogins(types.Allow, []string{"no-login-" + uuid.New().String()})
return role
}
// defaultAllowRules has the Allow rules that should be set as default when they were not explicitly defined.
// This is used to update the current cluster roles when deploying a new resource.
func defaultAllowRules() map[string][]types.Rule {
return map[string][]types.Rule{
teleport.PresetAuditorRoleName: {
types.NewRule(types.KindSessionTracker, RO()),
},
teleport.PresetEditorRoleName: {
types.NewRule(types.KindConnectionDiagnostic, RW()),
types.NewRule(types.KindDatabase, RW()),
types.NewRule(types.KindDatabaseService, RO()),
},
}
}
// AddDefaultAllowRules adds default rules to a preset role.
// Only rules whose resources are not already defined (either allowing or denying) are added.
func AddDefaultAllowRules(role types.Role) types.Role {
defaultRules, ok := defaultAllowRules()[role.GetName()]
if !ok || len(defaultRules) == 0 {
return role
}
combined := append(role.GetRules(types.Allow), role.GetRules(types.Deny)...)
for _, defaultRule := range defaultRules {
if resourceBelongsToRules(combined, defaultRule.Resources) {
continue
}
log.Debugf("Adding default allow rule %v for role %q", defaultRule, role.GetName())
rules := role.GetRules(types.Allow)
rules = append(rules, defaultRule)
role.SetRules(types.Allow, rules)
}
return role
}
func resourceBelongsToRules(rules []types.Rule, resources []string) bool {
for _, rule := range rules {
for _, ruleResource := range rule.Resources {
if slices.Contains(resources, ruleResource) {
return true
}
}
}
return false
}