-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
saml_idp_service_provider.go
185 lines (152 loc) · 5.9 KB
/
saml_idp_service_provider.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
/*
Copyright 2023 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 (
"encoding/xml"
"fmt"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/utils"
)
var (
// ErrMissingEntityDescriptorAndEntityID is returned when both entity descriptor and entity ID is empty.
ErrEmptyEntityDescriptorAndEntityID = trace.BadParameter("either entity_descriptor or entity_id must be provided")
// ErrMissingEntityDescriptorAndACSURL is returned when both entity descriptor and ACS URL is empty.
ErrEmptyEntityDescriptorAndACSURL = trace.BadParameter("either entity_descriptor or acs_url must be provided")
)
// SAMLIdPServiceProvider specifies configuration for service providers for Teleport's built in SAML IdP.
//
// Note: The EntityID is the entity ID for the entity descriptor. This ID is checked that it
// matches the entity ID in the entity descriptor at upsert time to avoid having to parse the
// XML blob in the entity descriptor every time we need to use this resource.
type SAMLIdPServiceProvider interface {
ResourceWithLabels
// GetEntityDescriptor returns the entity descriptor of the service provider.
GetEntityDescriptor() string
// SetEntityDescriptor sets the entity descriptor of the service provider.
SetEntityDescriptor(string)
// GetEntityID returns the entity ID.
GetEntityID() string
// SetEntityID sets the entity ID.
SetEntityID(string)
// GetACSURL returns the ACS URL.
GetACSURL() string
// SetACSURL sets the ACS URL.
SetACSURL(string)
// Copy returns a copy of this saml idp service provider object.
Copy() SAMLIdPServiceProvider
// CloneResource returns a copy of the SAMLIdPServiceProvider as a ResourceWithLabels
// This is helpful when interfacing with multiple types at the same time in unified resources
CloneResource() ResourceWithLabels
}
// NewSAMLIdPServiceProvider returns a new SAMLIdPServiceProvider based off a metadata object and SAMLIdPServiceProviderSpecV1.
func NewSAMLIdPServiceProvider(metadata Metadata, spec SAMLIdPServiceProviderSpecV1) (SAMLIdPServiceProvider, error) {
s := &SAMLIdPServiceProviderV1{
ResourceHeader: ResourceHeader{
Metadata: metadata,
},
Spec: spec,
}
if err := s.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return s, nil
}
// GetEntityDescriptor returns the entity descriptor.
func (s *SAMLIdPServiceProviderV1) GetEntityDescriptor() string {
return s.Spec.EntityDescriptor
}
// SetEntityDescriptor sets the entity descriptor.
func (s *SAMLIdPServiceProviderV1) SetEntityDescriptor(entityDescriptor string) {
s.Spec.EntityDescriptor = entityDescriptor
}
// GetEntityID returns the entity ID.
func (s *SAMLIdPServiceProviderV1) GetEntityID() string {
return s.Spec.EntityID
}
// SetEntityID sets the entity ID.
func (s *SAMLIdPServiceProviderV1) SetEntityID(entityID string) {
s.Spec.EntityID = entityID
}
// GetACSURL returns the ACS URL.
func (s *SAMLIdPServiceProviderV1) GetACSURL() string {
return s.Spec.ACSURL
}
// SetACSURL sets the ACS URL.
func (s *SAMLIdPServiceProviderV1) SetACSURL(acsURL string) {
s.Spec.ACSURL = acsURL
}
// String returns the SAML IdP service provider string representation.
func (s *SAMLIdPServiceProviderV1) String() string {
return fmt.Sprintf("SAMLIdPServiceProviderV1(Name=%v)",
s.GetName())
}
func (s *SAMLIdPServiceProviderV1) Copy() SAMLIdPServiceProvider {
return utils.CloneProtoMsg(s)
}
func (s *SAMLIdPServiceProviderV1) CloneResource() ResourceWithLabels {
return s.Copy()
}
// MatchSearch goes through select field values and tries to
// match against the list of search values.
func (s *SAMLIdPServiceProviderV1) MatchSearch(values []string) bool {
fieldVals := append(utils.MapToStrings(s.GetAllLabels()), s.GetEntityID(), s.GetName(), staticSAMLIdPServiceProviderDescription)
return MatchSearch(fieldVals, values, nil)
}
// setStaticFields sets static resource header and metadata fields.
func (s *SAMLIdPServiceProviderV1) setStaticFields() {
s.Kind = KindSAMLIdPServiceProvider
s.Version = V1
}
// CheckAndSetDefaults checks and sets default values
func (s *SAMLIdPServiceProviderV1) CheckAndSetDefaults() error {
s.setStaticFields()
if err := s.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if s.Spec.EntityDescriptor == "" {
if s.Spec.EntityID == "" {
return ErrEmptyEntityDescriptorAndEntityID
}
if s.Spec.ACSURL == "" {
return ErrEmptyEntityDescriptorAndACSURL
}
}
if s.Spec.EntityID == "" {
// Extract just the entityID attribute from the descriptor
ed := &struct {
EntityID string `xml:"entityID,attr"`
}{}
err := xml.Unmarshal([]byte(s.Spec.EntityDescriptor), ed)
if err != nil {
return trace.Wrap(err)
}
s.Spec.EntityID = ed.EntityID
}
return nil
}
// SAMLIdPServiceProviders is a list of SAML IdP service provider resources.
type SAMLIdPServiceProviders []SAMLIdPServiceProvider
// AsResources returns these service providers as resources with labels.
func (s SAMLIdPServiceProviders) AsResources() ResourcesWithLabels {
resources := make([]ResourceWithLabels, 0, len(s))
for _, sp := range s {
resources = append(resources, sp)
}
return resources
}
// Len returns the slice length.
func (s SAMLIdPServiceProviders) Len() int { return len(s) }
// Less compares service providers by name.
func (s SAMLIdPServiceProviders) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() }
// Swap swaps two service providers.
func (s SAMLIdPServiceProviders) Swap(i, j int) { s[i], s[j] = s[j], s[i] }