service.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 service
  15. import (
  16. "context"
  17. "os"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. _ "yunion.io/x/sqlchemy/backends"
  22. api "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/cloudcommon"
  24. common_app "yunion.io/x/onecloud/pkg/cloudcommon/app"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/cronman"
  27. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  29. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  30. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  31. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  32. "yunion.io/x/onecloud/pkg/keystone/cache"
  33. "yunion.io/x/onecloud/pkg/keystone/cronjobs"
  34. "yunion.io/x/onecloud/pkg/keystone/models"
  35. "yunion.io/x/onecloud/pkg/keystone/options"
  36. kpolicy "yunion.io/x/onecloud/pkg/keystone/policy"
  37. "yunion.io/x/onecloud/pkg/keystone/saml"
  38. _ "yunion.io/x/onecloud/pkg/keystone/tasks"
  39. "yunion.io/x/onecloud/pkg/keystone/tokens"
  40. "yunion.io/x/onecloud/pkg/keystone/util"
  41. "yunion.io/x/onecloud/pkg/mcclient/auth"
  42. "yunion.io/x/onecloud/pkg/util/logclient"
  43. )
  44. func StartService() {
  45. auth.DefaultTokenVerifier = tokens.FernetTokenVerifier
  46. db.DefaultUUIDGenerator = keystoneUUIDGenerator
  47. db.DefaultProjectFetcher = keystoneProjectFetcher
  48. db.DefaultDomainFetcher = keystoneDomainFetcher
  49. db.DefaultUserFetcher = keystoneUserFetcher
  50. db.DefaultDomainQuery = keystoneDomainQuery
  51. db.DefaultProjectQuery = keystoneProjectQuery
  52. db.DefaultProjectsFetcher = keystoneProjectsFetcher
  53. policy.DefaultPolicyFetcher = localPolicyFetcher
  54. logclient.DefaultSessionGenerator = models.GetDefaultClientSession
  55. cronman.DefaultAdminSessionGenerator = tokens.GetDefaultAdminCredToken
  56. notifyclient.AdminSessionGenerator = util.GetDefaulAdminSession
  57. notifyclient.UserLangFetcher = models.GetUserLangForKeyStone
  58. models.InitSyncWorkers()
  59. opts := &options.Options
  60. common_options.ParseOptions(opts, os.Args, "keystone.conf", api.SERVICE_TYPE)
  61. kpolicy.Init()
  62. if opts.Port == 0 {
  63. opts.Port = 5000 // keystone well-known port
  64. }
  65. /* err := keys.Init(opts.FernetKeyRepository, opts.SetupCredentialKey)
  66. if err != nil {
  67. log.Fatalf("init fernet keys fail %s", err)
  68. }
  69. */
  70. app := common_app.InitApp(&opts.BaseOptions, true).
  71. OnException(func(method, path string, body jsonutils.JSONObject, err error) {
  72. ctx := context.Background()
  73. token := tokens.GetDefaultAdminCredToken()
  74. notifyclient.EventNotifyServiceAbnormal(ctx, token, consts.GetServiceType(), method, path, body, err)
  75. })
  76. cloudcommon.InitDB(&opts.DBOptions)
  77. InitHandlers(app, opts.IsSlaveNode)
  78. db.EnsureAppSyncDB(app, &opts.DBOptions, models.InitDB)
  79. common_app.InitBaseAuth(&opts.BaseOptions)
  80. common_options.StartOptionManagerWithSessionDriver(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, "", options.OnOptionsChange, models.NewServiceConfigSession())
  81. if !opts.IsSlaveNode {
  82. err := models.UserManager.EnforceUserMfa(context.Background())
  83. if err != nil {
  84. log.Errorf("EnforceUserMfa fail %s", err)
  85. }
  86. }
  87. cache.Init(opts.TokenExpirationSeconds)
  88. if !opts.IsSlaveNode {
  89. err := taskman.TaskManager.InitializeData()
  90. if err != nil {
  91. log.Fatalf("TaskManager.InitializeData fail %s", err)
  92. }
  93. cron := cronman.InitCronJobManager(true, opts.CronJobWorkerCount, options.Options.TimeZone)
  94. cron.AddJobAtIntervalsWithStartRun("AutoSyncIdentityProviderTask", time.Duration(opts.AutoSyncIntervalSeconds)*time.Second, models.AutoSyncIdentityProviderTask, true)
  95. cron.AddJobAtIntervalsWithStartRun("FetchScopeResourceCount", time.Duration(opts.FetchScopeResourceCountIntervalSeconds)*time.Second, cronjobs.FetchScopeResourceCount, false)
  96. cron.AddJobAtIntervalsWithStartRun("CalculateIdentityQuotaUsages", time.Duration(opts.CalculateQuotaUsageIntervalSeconds)*time.Second, models.IdentityQuotaManager.CalculateQuotaUsages, true)
  97. cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false)
  98. cron.AddJobEveryFewDays("CheckAllUserPasswordIsExpired", 1, 8, 0, 0, models.CheckAllUserPasswordIsExpired, true)
  99. cron.AddJobEveryFewHour("RemoveObsoleteInvalidTokens", 6, 0, 0, models.RemoveObsoleteInvalidTokens, true)
  100. cron.AddJobAtIntervalsWithStartRun("TaskCleanupJob", time.Duration(options.Options.TaskArchiveIntervalMinutes)*time.Minute, taskman.TaskManager.TaskCleanupJob, true)
  101. cron.AddJobEveryFewHour("CleanPasswordsJob", 4, 8, 0, models.PasswordManager.CleanPasswordsJob, true)
  102. cron.Start()
  103. defer cron.Stop()
  104. }
  105. if options.Options.EnableSsl {
  106. // enable SAML support only if ssl is enabled
  107. err := saml.InitSAMLInstance()
  108. if err != nil {
  109. panic(err)
  110. }
  111. }
  112. go func() {
  113. common_app.ServeForeverExtended(app, &opts.BaseOptions, options.Options.AdminPort, nil, false)
  114. }()
  115. common_app.ServeForeverWithCleanup(app, &opts.BaseOptions, func() {
  116. cloudcommon.CloseDB()
  117. // cron.Stop()
  118. })
  119. }