-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
saml.go
446 lines (382 loc) · 13.5 KB
/
saml.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Copyright 2020-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 (
"slices"
"strings"
"time"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/utils"
)
// SAMLConnector specifies configuration for SAML 2.0 identity providers
type SAMLConnector interface {
// ResourceWithSecrets provides common methods for objects
ResourceWithSecrets
ResourceWithOrigin
// SetMetadata sets the connector metadata
SetMetadata(Metadata)
// GetDisplay returns display - friendly name for this provider.
GetDisplay() string
// SetDisplay sets friendly name for this provider.
SetDisplay(string)
// GetAttributesToRoles returns attributes to roles mapping
GetAttributesToRoles() []AttributeMapping
// SetAttributesToRoles sets attributes to roles mapping
SetAttributesToRoles(mapping []AttributeMapping)
// GetAttributes returns list of attributes expected by mappings
GetAttributes() []string
// GetTraitMappings converts gets all attribute mappings in the
// generic trait mapping format.
GetTraitMappings() TraitMappingSet
// SetIssuer sets issuer
SetIssuer(issuer string)
// GetIssuer returns issuer
GetIssuer() string
// GetSigningKeyPair returns signing key pair
GetSigningKeyPair() *AsymmetricKeyPair
// GetSigningKeyPair sets signing key pair
SetSigningKeyPair(k *AsymmetricKeyPair)
// GetSSO returns SSO service
GetSSO() string
// SetSSO sets SSO service
SetSSO(string)
// GetEntityDescriptor returns XML entity descriptor of the service
GetEntityDescriptor() string
// SetEntityDescriptor sets entity descriptor of the service
SetEntityDescriptor(v string)
// GetEntityDescriptorURL returns the URL to obtain the entity descriptor.
GetEntityDescriptorURL() string
// SetEntityDescriptorURL sets the entity descriptor url.
SetEntityDescriptorURL(string)
// GetCert returns identity provider checking x509 certificate
GetCert() string
// SetCert sets identity provider checking certificate
SetCert(string)
// GetServiceProviderIssuer returns service provider issuer
GetServiceProviderIssuer() string
// SetServiceProviderIssuer sets service provider issuer
SetServiceProviderIssuer(v string)
// GetAudience returns audience
GetAudience() string
// SetAudience sets audience
SetAudience(v string)
// GetAssertionConsumerService returns assertion consumer service URL
GetAssertionConsumerService() string
// SetAssertionConsumerService sets assertion consumer service URL
SetAssertionConsumerService(v string)
// GetProvider returns the identity provider.
GetProvider() string
// SetProvider sets the identity provider.
SetProvider(string)
// GetEncryptionKeyPair returns the key pair for SAML assertions.
GetEncryptionKeyPair() *AsymmetricKeyPair
// SetEncryptionKeyPair sets the key pair for SAML assertions.
SetEncryptionKeyPair(k *AsymmetricKeyPair)
// GetAllowIDPInitiated returns whether the identity provider can initiate a login or not.
GetAllowIDPInitiated() bool
// SetAllowIDPInitiated sets whether the identity provider can initiate a login or not.
SetAllowIDPInitiated(bool)
}
// NewSAMLConnector returns a new SAMLConnector based off a name and SAMLConnectorSpecV2.
func NewSAMLConnector(name string, spec SAMLConnectorSpecV2) (SAMLConnector, error) {
o := &SAMLConnectorV2{
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := o.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return o, nil
}
// GetVersion returns resource version
func (o *SAMLConnectorV2) GetVersion() string {
return o.Version
}
// GetKind returns resource kind
func (o *SAMLConnectorV2) GetKind() string {
return o.Kind
}
// GetSubKind returns resource sub kind
func (o *SAMLConnectorV2) GetSubKind() string {
return o.SubKind
}
// SetSubKind sets resource subkind
func (o *SAMLConnectorV2) SetSubKind(sk string) {
o.SubKind = sk
}
// GetResourceID returns resource ID
func (o *SAMLConnectorV2) GetResourceID() int64 {
return o.Metadata.ID
}
// SetResourceID sets resource ID
func (o *SAMLConnectorV2) SetResourceID(id int64) {
o.Metadata.ID = id
}
// GetRevision returns the revision
func (o *SAMLConnectorV2) GetRevision() string {
return o.Metadata.GetRevision()
}
// SetRevision sets the revision
func (o *SAMLConnectorV2) SetRevision(rev string) {
o.Metadata.SetRevision(rev)
}
// WithoutSecrets returns an instance of resource without secrets.
func (o *SAMLConnectorV2) WithoutSecrets() Resource {
k1 := o.GetSigningKeyPair()
k2 := o.GetEncryptionKeyPair()
o2 := *o
if k1 != nil {
q1 := *k1
q1.PrivateKey = ""
o2.SetSigningKeyPair(&q1)
}
if k2 != nil {
q2 := *k2
q2.PrivateKey = ""
o2.SetEncryptionKeyPair(&q2)
}
return &o2
}
// GetServiceProviderIssuer returns service provider issuer
func (o *SAMLConnectorV2) GetServiceProviderIssuer() string {
return o.Spec.ServiceProviderIssuer
}
// SetServiceProviderIssuer sets service provider issuer
func (o *SAMLConnectorV2) SetServiceProviderIssuer(v string) {
o.Spec.ServiceProviderIssuer = v
}
// GetAudience returns audience
func (o *SAMLConnectorV2) GetAudience() string {
return o.Spec.Audience
}
// SetAudience sets audience
func (o *SAMLConnectorV2) SetAudience(v string) {
o.Spec.Audience = v
}
// GetCert returns identity provider checking x509 certificate
func (o *SAMLConnectorV2) GetCert() string {
return o.Spec.Cert
}
// SetCert sets identity provider checking certificate
func (o *SAMLConnectorV2) SetCert(cert string) {
o.Spec.Cert = cert
}
// GetSSO returns SSO service
func (o *SAMLConnectorV2) GetSSO() string {
return o.Spec.SSO
}
// SetSSO sets SSO service
func (o *SAMLConnectorV2) SetSSO(sso string) {
o.Spec.SSO = sso
}
// GetEntityDescriptor returns XML entity descriptor of the service
func (o *SAMLConnectorV2) GetEntityDescriptor() string {
return o.Spec.EntityDescriptor
}
// SetEntityDescriptor sets entity descriptor of the service
func (o *SAMLConnectorV2) SetEntityDescriptor(v string) {
o.Spec.EntityDescriptor = v
}
// GetEntityDescriptorURL returns the URL to obtain the entity descriptor.
func (o *SAMLConnectorV2) GetEntityDescriptorURL() string {
return o.Spec.EntityDescriptorURL
}
// SetEntityDescriptorURL sets the entity descriptor url.
func (o *SAMLConnectorV2) SetEntityDescriptorURL(v string) {
o.Spec.EntityDescriptorURL = v
}
// GetAssertionConsumerService returns assertion consumer service URL
func (o *SAMLConnectorV2) GetAssertionConsumerService() string {
return o.Spec.AssertionConsumerService
}
// SetAssertionConsumerService sets assertion consumer service URL
func (o *SAMLConnectorV2) SetAssertionConsumerService(v string) {
o.Spec.AssertionConsumerService = v
}
// SetDisplay sets friendly name for this provider.
func (o *SAMLConnectorV2) SetDisplay(display string) {
o.Spec.Display = display
}
// GetMetadata returns object metadata
func (o *SAMLConnectorV2) GetMetadata() Metadata {
return o.Metadata
}
// SetMetadata sets object metadata
func (o *SAMLConnectorV2) SetMetadata(m Metadata) {
o.Metadata = m
}
// Origin returns the origin value of the resource.
func (o *SAMLConnectorV2) Origin() string {
return o.Metadata.Origin()
}
// SetOrigin sets the origin value of the resource.
func (o *SAMLConnectorV2) SetOrigin(origin string) {
o.Metadata.SetOrigin(origin)
}
// SetExpiry sets expiry time for the object
func (o *SAMLConnectorV2) SetExpiry(expires time.Time) {
o.Metadata.SetExpiry(expires)
}
// Expiry returns object expiry setting
func (o *SAMLConnectorV2) Expiry() time.Time {
return o.Metadata.Expiry()
}
// GetName returns the name of the connector
func (o *SAMLConnectorV2) GetName() string {
return o.Metadata.GetName()
}
// SetName sets client secret to some value
func (o *SAMLConnectorV2) SetName(name string) {
o.Metadata.SetName(name)
}
// SetIssuer sets issuer
func (o *SAMLConnectorV2) SetIssuer(issuer string) {
o.Spec.Issuer = issuer
}
// GetIssuer returns issuer
func (o *SAMLConnectorV2) GetIssuer() string {
return o.Spec.Issuer
}
// GetDisplay returns the friendly name for this provider.
func (o *SAMLConnectorV2) GetDisplay() string {
if o.Spec.Display != "" {
return o.Spec.Display
}
return o.GetName()
}
// GetAttributesToRoles returns attributes to roles mapping
func (o *SAMLConnectorV2) GetAttributesToRoles() []AttributeMapping {
return o.Spec.AttributesToRoles
}
// SetAttributesToRoles sets attributes to roles mapping
func (o *SAMLConnectorV2) SetAttributesToRoles(mapping []AttributeMapping) {
o.Spec.AttributesToRoles = mapping
}
// SetProvider sets the identity provider.
func (o *SAMLConnectorV2) SetProvider(identityProvider string) {
o.Spec.Provider = identityProvider
}
// GetProvider returns the identity provider.
func (o *SAMLConnectorV2) GetProvider() string {
return o.Spec.Provider
}
// GetAttributes returns list of attributes expected by mappings
func (o *SAMLConnectorV2) GetAttributes() []string {
var out []string
for _, mapping := range o.Spec.AttributesToRoles {
out = append(out, mapping.Name)
}
return utils.Deduplicate(out)
}
// GetTraitMappings returns the SAMLConnector's TraitMappingSet
func (o *SAMLConnectorV2) GetTraitMappings() TraitMappingSet {
tms := make([]TraitMapping, 0, len(o.Spec.AttributesToRoles))
for _, mapping := range o.Spec.AttributesToRoles {
tms = append(tms, TraitMapping{
Trait: mapping.Name,
Value: mapping.Value,
Roles: mapping.Roles,
})
}
return TraitMappingSet(tms)
}
// GetSigningKeyPair returns signing key pair
func (o *SAMLConnectorV2) GetSigningKeyPair() *AsymmetricKeyPair {
return o.Spec.SigningKeyPair
}
// SetSigningKeyPair sets signing key pair
func (o *SAMLConnectorV2) SetSigningKeyPair(k *AsymmetricKeyPair) {
o.Spec.SigningKeyPair = k
}
// GetEncryptionKeyPair returns the key pair for SAML assertions.
func (o *SAMLConnectorV2) GetEncryptionKeyPair() *AsymmetricKeyPair {
return o.Spec.EncryptionKeyPair
}
// SetEncryptionKeyPair sets the key pair for SAML assertions.
func (o *SAMLConnectorV2) SetEncryptionKeyPair(k *AsymmetricKeyPair) {
o.Spec.EncryptionKeyPair = k
}
// GetAllowIDPInitiated returns whether the identity provider can initiate a login or not.
func (o *SAMLConnectorV2) GetAllowIDPInitiated() bool {
return o.Spec.AllowIDPInitiated
}
// SetAllowIDPInitiated sets whether the identity provider can initiate a login or not.
func (o *SAMLConnectorV2) SetAllowIDPInitiated(allow bool) {
o.Spec.AllowIDPInitiated = allow
}
// setStaticFields sets static resource header and metadata fields.
func (o *SAMLConnectorV2) setStaticFields() {
o.Kind = KindSAMLConnector
o.Version = V2
}
// CheckAndSetDefaults checks and sets default values
func (o *SAMLConnectorV2) CheckAndSetDefaults() error {
o.setStaticFields()
if err := o.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if name := o.Metadata.Name; slices.Contains(constants.SystemConnectors, name) {
return trace.BadParameter("ID: invalid connector name, %v is a reserved name", name)
}
if o.Spec.AssertionConsumerService == "" {
return trace.BadParameter("missing acs - assertion consumer service parameter, set service URL that will receive POST requests from SAML")
}
if o.Spec.AllowIDPInitiated && !strings.HasSuffix(o.Spec.AssertionConsumerService, "/"+o.Metadata.Name) {
return trace.BadParameter("acs - assertion consumer service parameter must end with /%v when allow_idp_initiated is set to true, eg https://cluster.domain/webapi/v1/saml/acs/%v. Ensure this URI matches the one configured at the identity provider.", o.Metadata.Name, o.Metadata.Name)
}
if o.Spec.ServiceProviderIssuer == "" {
o.Spec.ServiceProviderIssuer = o.Spec.AssertionConsumerService
}
if o.Spec.Audience == "" {
o.Spec.Audience = o.Spec.AssertionConsumerService
}
// Issuer and SSO can be automatically set later if EntityDescriptor is provided
if o.Spec.EntityDescriptorURL == "" && o.Spec.EntityDescriptor == "" && (o.Spec.Issuer == "" || o.Spec.SSO == "") {
return trace.BadParameter("no entity_descriptor set, either provide entity_descriptor or entity_descriptor_url in spec")
}
// make sure claim mappings have either roles or a role template
for _, v := range o.Spec.AttributesToRoles {
if len(v.Roles) == 0 {
return trace.BadParameter("need roles field in attributes_to_roles")
}
}
return nil
}
// Check returns nil if all parameters are great, err otherwise
func (i *SAMLAuthRequest) Check() error {
if i.ConnectorID == "" {
return trace.BadParameter("ConnectorID: missing value")
}
if len(i.PublicKey) != 0 {
_, _, _, _, err := ssh.ParseAuthorizedKey(i.PublicKey)
if err != nil {
return trace.BadParameter("PublicKey: bad key: %v", err)
}
if (i.CertTTL > defaults.MaxCertDuration) || (i.CertTTL < defaults.MinCertDuration) {
return trace.BadParameter("CertTTL: wrong certificate TTL")
}
}
// we could collapse these two checks into one, but the error message would become ambiguous.
if i.SSOTestFlow && i.ConnectorSpec == nil {
return trace.BadParameter("ConnectorSpec cannot be nil when SSOTestFlow is true")
}
if !i.SSOTestFlow && i.ConnectorSpec != nil {
return trace.BadParameter("ConnectorSpec must be nil when SSOTestFlow is false")
}
return nil
}