/
saml_provider.go
350 lines (316 loc) · 9.61 KB
/
saml_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
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
// Copyright 2019 Yunion
//
// 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 hcso
import (
"fmt"
"time"
"unicode"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/stringutils"
api "yunion.io/x/cloudmux/pkg/apis/cloudid"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud"
"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/modules"
"yunion.io/x/cloudmux/pkg/multicloud/huawei"
"yunion.io/x/onecloud/pkg/util/samlutils"
)
type SAMLProviderLinks struct {
Self string
Protocols string
}
type SAMLProvider struct {
multicloud.SResourceBase
huawei.HuaweiTags
client *SHuaweiClient
Id string
Links SAMLProviderLinks
Description string
}
func (self *SAMLProvider) GetId() string {
return self.Id
}
func (self *SAMLProvider) GetGlobalId() string {
return self.Id
}
func (self *SAMLProvider) GetName() string {
return self.Id
}
func (self *SAMLProvider) GetStatus() string {
mapping, _ := self.client.findMapping()
if mapping != nil {
return api.SAML_PROVIDER_STATUS_AVAILABLE
}
return api.SAML_PROVIDER_STATUS_UNVALIABLE
}
func (self *SAMLProvider) GetAuthUrl() string {
return fmt.Sprintf("https://auth.%s/authui/federation/websso?domain_id=%s&idp=%s&protocol=saml", self.client.endpoints.EndpointDomain, self.client.ownerId, self.Id)
}
func (self *SAMLProvider) Delete() error {
return self.client.DeleteSAMLProvider(self.Id)
}
func (self *SAMLProvider) GetMetadataDocument() (*samlutils.EntityDescriptor, error) {
info, err := self.client.GetSAMLProviderMetadata(self.Id)
if err != nil {
return nil, errors.Wrapf(err, "GetSAMLProviderMetadata(%s)", self.Id)
}
metadata, err := samlutils.ParseMetadata([]byte(info.Data))
if err != nil {
return nil, errors.Wrapf(err, "ParseMetadata")
}
return &metadata, nil
}
func (self *SAMLProvider) UpdateMetadata(metadata samlutils.EntityDescriptor) error {
return self.client.UpdateSAMLProviderMetadata(self.Id, metadata.String())
}
func (self *SHuaweiClient) ListSAMLProviders() ([]SAMLProvider, error) {
client, err := self.newGeneralAPIClient()
if err != nil {
return nil, errors.Wrapf(err, "newGeneralAPIClient")
}
samls := []SAMLProvider{}
err = doListAllWithNextLink(client.SAMLProviders.List, nil, &samls)
if err != nil {
return nil, errors.Wrapf(err, "doListAll")
}
return samls, nil
}
type SAMLProviderProtocol struct {
MappingId string
Id string
}
func (self *SHuaweiClient) GetSAMLProviderProtocols(id string) ([]SAMLProviderProtocol, error) {
client, err := self.newGeneralAPIClient()
if err != nil {
return nil, errors.Wrap(err, "newGeneralAPIClient")
}
resp, err := client.SAMLProviders.ListInContextWithSpec(nil, fmt.Sprintf("%s/protocols", id), nil, "protocols")
if err != nil {
return nil, errors.Wrapf(err, "ListInContextWithSpec")
}
protocols := []SAMLProviderProtocol{}
return protocols, jsonutils.Update(&protocols, resp.Data)
}
func (self *SHuaweiClient) DeleteSAMLProviderProtocol(spId, id string) error {
client, err := self.newGeneralAPIClient()
if err != nil {
return errors.Wrap(err, "newGeneralAPIClient")
}
_, err = client.SAMLProviders.DeleteInContextWithSpec(nil, spId, fmt.Sprintf("protocols/%s", id), nil, nil, "")
return err
}
type SAMLProviderMetadata struct {
DomainId string
UpdateTime time.Time
Data string
IdpId string
ProtocolId string
Id string
EntityId string
XaccountType string
}
func (self *SHuaweiClient) GetSAMLProviderMetadata(id string) (*SAMLProviderMetadata, error) {
client, err := self.newGeneralAPIClient()
if err != nil {
return nil, errors.Wrap(err, "newGeneralAPIClient")
}
client.SAMLProviders.SetVersion("v3-ext/OS-FEDERATION")
resp, err := client.SAMLProviders.GetInContextWithSpec(nil, id, fmt.Sprintf("protocols/saml/metadata"), nil, "")
if err != nil {
return nil, err
}
metadata := &SAMLProviderMetadata{}
err = resp.Unmarshal(metadata)
if err != nil {
return nil, errors.Wrap(err, "resp.Unmarshal")
}
return metadata, nil
}
func (self *SHuaweiClient) UpdateSAMLProviderMetadata(id, metadata string) error {
params := map[string]string{
"domain_id": self.ownerId,
"xaccount_type": "",
"metadata": metadata,
}
client, err := self.newGeneralAPIClient()
if err != nil {
return errors.Wrap(err, "newGeneralAPIClient")
}
client.SAMLProviders.SetVersion("v3-ext/OS-FEDERATION")
_, err = client.SAMLProviders.PerformAction2("protocols/saml/metadata", id, jsonutils.Marshal(params), "")
if err != nil {
return errors.Wrapf(err, "SAMLProvider.PerformAction")
}
return nil
}
func (self *SHuaweiClient) GetICloudSAMLProviders() ([]cloudprovider.ICloudSAMLProvider, error) {
samls, err := self.ListSAMLProviders()
if err != nil {
return nil, errors.Wrapf(err, "ListSAMLProviders")
}
ret := []cloudprovider.ICloudSAMLProvider{}
for i := range samls {
samls[i].client = self
ret = append(ret, &samls[i])
}
return ret, nil
}
func (self *SHuaweiClient) DeleteSAMLProvider(id string) error {
client, err := self.newGeneralAPIClient()
if err != nil {
return errors.Wrap(err, "newGeneralAPIClient")
}
_, err = client.SAMLProviders.Delete(id, nil)
return err
}
func (self *SHuaweiClient) CreateSAMLProvider(opts *cloudprovider.SAMLProviderCreateOptions) (*SAMLProvider, error) {
client, err := self.newGeneralAPIClient()
if err != nil {
return nil, errors.Wrap(err, "newGeneralAPIClient")
}
params := jsonutils.Marshal(map[string]interface{}{
"identity_provider": map[string]interface{}{
"description": opts.Name,
"enabled": true,
},
})
opts.Name = fmt.Sprintf("%s-%s", self.ownerName, opts.Name)
name := []byte{}
for _, c := range opts.Name {
if unicode.IsLetter(c) || unicode.IsNumber(c) || c == '-' || c == '_' {
name = append(name, byte(c))
} else {
name = append(name, '-')
}
}
opts.Name = string(name)
_, err = client.SAMLProviders.Update(opts.Name, params)
if err != nil {
if he, ok := err.(*modules.HuaweiClientError); ok && he.Code != 409 {
return nil, errors.Wrapf(err, "SAMLProviders.Update")
}
}
ret := SAMLProvider{client: self, Id: opts.Name}
err = self.UpdateSAMLProviderMetadata(opts.Name, opts.Metadata.String())
if err != nil {
return nil, errors.Wrapf(err, "resp.Unmarshal")
}
err = self.InitSAMLProviderMapping(opts.Name)
if err != nil {
return nil, errors.Wrapf(err, "InitSAMLProviderMapping")
}
return &ret, nil
}
type SAMLProviderMapping struct {
Id string
Rules jsonutils.JSONObject
}
var (
onecloudMappingRules = jsonutils.Marshal(map[string]interface{}{
"rules": []map[string]interface{}{
{
"remote": []map[string]interface{}{
{
"type": "User",
},
{
"type": "Groups",
},
},
"local": []map[string]interface{}{
{
"groups": "{1}",
"user": map[string]string{"name": "{0}"},
},
},
},
},
})
)
func (self *SHuaweiClient) ListSAMLProviderMappings() ([]SAMLProviderMapping, error) {
client, err := self.newGeneralAPIClient()
if err != nil {
return nil, errors.Wrap(err, "newGeneralAPIClient")
}
mappings := []SAMLProviderMapping{}
err = doListAllWithNextLink(client.SAMLProviderMappings.List, nil, &mappings)
if err != nil {
return nil, err
}
return mappings, nil
}
func (self *SHuaweiClient) findMapping() (*SAMLProviderMapping, error) {
mappings, err := self.ListSAMLProviderMappings()
if err != nil {
return nil, errors.Wrapf(err, "ListSAMLProviderMappings")
}
for i := range mappings {
if jsonutils.Marshal(map[string]interface{}{"rules": mappings[i].Rules}).Equals(jsonutils.Marshal(onecloudMappingRules)) {
return &mappings[i], nil
}
}
return nil, cloudprovider.ErrNotFound
}
func (self *SHuaweiClient) InitSAMLProviderMapping(spId string) error {
client, err := self.newGeneralAPIClient()
if err != nil {
return errors.Wrap(err, "newGeneralAPIClient")
}
mapping, err := self.findMapping()
if err != nil {
if errors.Cause(err) != cloudprovider.ErrNotFound {
return errors.Wrapf(err, "findMapping")
}
mappingId := stringutils.UUID4()
params := map[string]interface{}{
"mapping": onecloudMappingRules,
}
_, err = client.SAMLProviderMappings.Update(mappingId, jsonutils.Marshal(params))
if err != nil {
return errors.Wrapf(err, "create mapping")
}
mapping = &SAMLProviderMapping{
Id: mappingId,
Rules: onecloudMappingRules,
}
}
protocols, err := self.GetSAMLProviderProtocols(spId)
if err != nil {
return errors.Wrapf(err, "GetSAMLProviderProtocols")
}
params := map[string]interface{}{
"protocol": map[string]string{
"mapping_id": mapping.Id,
},
}
for i := range protocols {
if protocols[i].Id == "saml" {
if protocols[i].MappingId == mapping.Id {
return nil
}
_, err = client.SAMLProviders.PatchInContextWithSpec(nil, spId, "protocols/saml", jsonutils.Marshal(params), "")
return err
}
}
_, err = client.SAMLProviders.UpdateInContextWithSpec(nil, spId, "protocols/saml", jsonutils.Marshal(params), "")
return err
}
func (self *SHuaweiClient) DeleteSAMLProviderMapping(id string) error {
client, err := self.newGeneralAPIClient()
if err != nil {
return errors.Wrap(err, "newGeneralAPIClient")
}
_, err = client.SAMLProviderMappings.Delete(id, nil)
return err
}