mod_cloudusers.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cloudid
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/utils"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules"
  27. )
  28. type SClouduserManager struct {
  29. modulebase.ResourceManager
  30. }
  31. var (
  32. Cloudusers SClouduserManager
  33. )
  34. func init() {
  35. Cloudusers = SClouduserManager{modules.NewCloudIdManager("clouduser", "cloudusers",
  36. []string{},
  37. []string{})}
  38. modules.Register(&Cloudusers)
  39. }
  40. func (this *SClouduserManager) GetLoginInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  41. data, err := this.Get(s, id, nil)
  42. if err != nil {
  43. return nil, err
  44. }
  45. user := struct {
  46. Id string
  47. Name string
  48. Secret string
  49. Provider string
  50. IamLoginUrl string
  51. }{}
  52. err = data.Unmarshal(&user)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "data.Unmarshal")
  55. }
  56. if len(user.Secret) == 0 {
  57. return nil, httperrors.NewNotFoundError("No login secret found")
  58. }
  59. password, err := utils.DescryptAESBase64(user.Id, user.Secret)
  60. if err != nil {
  61. return nil, errors.Wrap(err, "Descrypt")
  62. }
  63. account := ""
  64. switch user.Provider {
  65. case api.CLOUD_PROVIDER_ALIYUN:
  66. suffix := strings.TrimPrefix(user.IamLoginUrl, "https://signin.aliyun.com/")
  67. suffix = strings.TrimSuffix(suffix, "/login.htm")
  68. if len(suffix) > 0 {
  69. user.Name = fmt.Sprintf("%s@%s", user.Name, suffix)
  70. account = suffix
  71. }
  72. case api.CLOUD_PROVIDER_QCLOUD, api.CLOUD_PROVIDER_HUAWEI:
  73. u, _ := url.Parse(user.IamLoginUrl)
  74. if u != nil {
  75. account = u.Query().Get("account")
  76. }
  77. case api.CLOUD_PROVIDER_AWS:
  78. account = strings.TrimPrefix(user.IamLoginUrl, "https://")
  79. if info := strings.Split(account, "."); len(info) > 0 {
  80. account = info[0]
  81. }
  82. }
  83. return jsonutils.Marshal(map[string]string{
  84. "account": account,
  85. "username": user.Name,
  86. "password": password,
  87. "url": user.IamLoginUrl,
  88. }), nil
  89. }