auth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 app
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/util/httputils"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  25. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  27. "yunion.io/x/onecloud/pkg/mcclient/auth"
  28. identity_modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  29. )
  30. func InitAuth(options *common_options.CommonOptions, authComplete auth.AuthCompletedCallback) {
  31. if len(options.AuthURL) == 0 {
  32. log.Fatalln("Missing AuthURL")
  33. }
  34. if len(options.AdminUser) == 0 {
  35. log.Fatalln("Mising AdminUser")
  36. }
  37. if len(options.AdminPassword) == 0 {
  38. log.Fatalln("Missing AdminPasswd")
  39. }
  40. if len(options.AdminProject) == 0 {
  41. log.Fatalln("Missing AdminProject")
  42. }
  43. a := auth.NewAuthInfo(
  44. options.AuthURL,
  45. options.AdminDomain,
  46. options.AdminUser,
  47. options.AdminPassword,
  48. options.AdminProject,
  49. options.AdminProjectDomain,
  50. )
  51. // debug := options.LogLevel == "debug"
  52. if options.SessionEndpointType != "" {
  53. if !utils.IsInStringArray(options.SessionEndpointType,
  54. []string{
  55. identity.EndpointInterfacePublic,
  56. identity.EndpointInterfaceInternal,
  57. identity.EndpointInterfaceApigateway,
  58. }) {
  59. log.Fatalf("Invalid session endpoint type %s", options.SessionEndpointType)
  60. }
  61. auth.SetEndpointType(options.SessionEndpointType)
  62. }
  63. auth.Init(a, options.DebugClient, true, options.SslCertfile, options.SslKeyfile) // , authComplete)
  64. users := options.NotifyAdminUsers
  65. groups := options.NotifyAdminGroups
  66. if len(users) == 0 && len(groups) == 0 {
  67. users = []string{"sysadmin"}
  68. }
  69. notifyclient.FetchNotifyAdminRecipients(context.Background(), options.Region, users, groups)
  70. if authComplete != nil {
  71. authComplete()
  72. }
  73. consts.SetTenantCacheExpireSeconds(options.TenantCacheExpireSeconds)
  74. InitBaseAuth(&options.BaseOptions)
  75. if options.SessionEndpointType == identity.EndpointInterfaceInternal {
  76. watcher := newEndpointChangeManager()
  77. watcher.StartWatching(&identity_modules.EndpointsV3)
  78. startEtcdEndpointPuller()
  79. }
  80. }
  81. func InitBaseAuth(options *common_options.BaseOptions) {
  82. policy.EnableGlobalRbac(
  83. time.Second*time.Duration(options.RbacPolicyRefreshIntervalSeconds),
  84. options.RbacDebug,
  85. options.PolicyWorkerCount,
  86. )
  87. consts.SetNonDefaultDomainProjects(options.NonDefaultDomainProjects)
  88. }
  89. func FetchEtcdServiceInfo() (*identity.EndpointDetails, error) {
  90. s := auth.GetAdminSession(context.Background(), "")
  91. return s.GetCommonEtcdEndpoint()
  92. }
  93. func startEtcdEndpointPuller() {
  94. retryInterval := 60
  95. etecdUrl, err := auth.GetServiceURL(apis.SERVICE_TYPE_ETCD, consts.GetRegion(), "", identity.EndpointInterfaceInternal, httputils.POST)
  96. if err != nil {
  97. log.Errorf("[etcd] GetServiceURL fail %s, retry after %d seconds", err, retryInterval)
  98. } else if len(etecdUrl) == 0 {
  99. log.Errorf("[etcd] no service url found, retry after %d seconds", retryInterval)
  100. } else {
  101. return
  102. }
  103. auth.ReAuth()
  104. time.AfterFunc(time.Second*time.Duration(retryInterval), startEtcdEndpointPuller)
  105. }