From edee04fcc72f5f312076455ae4f614990a775504 Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Mon, 2 Jan 2023 12:00:39 +0800 Subject: [PATCH] fix(notify): add some template and notification (#15669) --- .../content@cn/MYSQL_OUT_OF_SYNC.tmpl | 2 + .../content@cn/NET_OUT_OF_SYNC.tmpl | 2 + .../content@cn/PASSWORD_EXPIRE_SOON.tmpl | 2 + .../content@cn/SYNC_ACCOUNT_STATUS.tmpl | 2 + .../content@cn/WORK_BLOCK.tmpl | 2 + .../content@en/MYSQL_OUT_OF_SYNC.tmpl | 2 + .../content@en/NET_OUT_OF_SYNC.tmpl | 2 + .../content@en/PASSWORD_EXPIRE_SOON.tmpl | 2 + .../content@en/SYNC_ACCOUNT_STATUS.tmpl | 2 + .../content@en/WORK_BLOCK.tmpl | 2 + .../title@cn/MYSQL_OUT_OF_SYNC.tmpl | 2 + .../title@cn/NET_OUT_OF_SYNC.tmpl | 2 + .../title@cn/PASSWORD_EXPIRE_SOON.tmpl | 2 + .../title@cn/SYNC_ACCOUNT_STATUS.tmpl | 2 + .../local-templates/title@cn/WORK_BLOCK.tmpl | 2 + .../title@en/MYSQL_OUT_OF_SYNC.tmpl | 2 + .../title@en/NET_OUT_OF_SYNC.tmpl | 2 + .../title@en/PASSWORD_EXPIRE_SOON.tmpl | 2 + .../title@en/USER_LOCK copy.tmpl | 2 + .../local-templates/title@en/WORK_BLOCK.tmpl | 2 + pkg/apihelper/apihelper.go | 20 ++++++ pkg/apis/notify/const.go | 3 + pkg/apis/notify/event.go | 7 +- pkg/cloudcommon/notifyclient/events.go | 2 + pkg/compute/models/cloudaccounts.go | 28 ++++++-- pkg/keystone/models/passwords.go | 72 +++++++++++++++++++ pkg/keystone/service/service.go | 1 + pkg/notify/models/topic.go | 60 ++++++++++++++++ 28 files changed, 227 insertions(+), 6 deletions(-) create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/MYSQL_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/NET_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/PASSWORD_EXPIRE_SOON.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/SYNC_ACCOUNT_STATUS.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@cn/WORK_BLOCK.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/MYSQL_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/NET_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/PASSWORD_EXPIRE_SOON.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/SYNC_ACCOUNT_STATUS.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/content@en/WORK_BLOCK.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/MYSQL_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/NET_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/PASSWORD_EXPIRE_SOON.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/SYNC_ACCOUNT_STATUS.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@cn/WORK_BLOCK.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/MYSQL_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/NET_OUT_OF_SYNC.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/PASSWORD_EXPIRE_SOON.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/USER_LOCK copy.tmpl create mode 100644 build/notify/root/opt/yunion/share/local-templates/title@en/WORK_BLOCK.tmpl diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/MYSQL_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/MYSQL_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..890d898dca --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/MYSQL_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +数据库 {{$d.ip}} 的主从同步不一致,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/NET_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/NET_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..a7d4cc5494 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/NET_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.service_name }}服务的网络拓扑信息同步不一致,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/PASSWORD_EXPIRE_SOON.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/PASSWORD_EXPIRE_SOON.tmpl new file mode 100644 index 0000000000..3e92b0d910 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/PASSWORD_EXPIRE_SOON.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.account }}:您的密码有效期将过,请及时登录平台更新密码。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/SYNC_ACCOUNT_STATUS.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/SYNC_ACCOUNT_STATUS.tmpl new file mode 100644 index 0000000000..e5b46b8550 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/SYNC_ACCOUNT_STATUS.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +云账号{{ $d.name }}状态异常,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/WORK_BLOCK.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/WORK_BLOCK.tmpl new file mode 100644 index 0000000000..47f3799ccc --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/WORK_BLOCK.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +服务{{ d.service_name}} worker阻塞半小时,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/MYSQL_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/MYSQL_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..89b5725302 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/MYSQL_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The primary and secondary synchronization of the database ({{ $d.ip }}) is inconsistent, please check in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/NET_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/NET_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..e0e51c4e3e --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/NET_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.service_name }}: The network topology information of the service is inconsistent, please check in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/PASSWORD_EXPIRE_SOON.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/PASSWORD_EXPIRE_SOON.tmpl new file mode 100644 index 0000000000..0afabae532 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/PASSWORD_EXPIRE_SOON.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.account }}:Your password is valid and will expire soon. Please log in to the platform in time to update your password. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/SYNC_ACCOUNT_STATUS.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/SYNC_ACCOUNT_STATUS.tmpl new file mode 100644 index 0000000000..6c0281c22b --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/SYNC_ACCOUNT_STATUS.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The account {{ $d.name }} status is abnormal. Please check in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/WORK_BLOCK.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/WORK_BLOCK.tmpl new file mode 100644 index 0000000000..ef727fb66a --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/WORK_BLOCK.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The service: {{ d.service_name}} worker has been block 30 minutes.Please verify the service in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/MYSQL_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/MYSQL_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..890d898dca --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/MYSQL_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +数据库 {{$d.ip}} 的主从同步不一致,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/NET_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/NET_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..a7d4cc5494 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/NET_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.service_name }}服务的网络拓扑信息同步不一致,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/PASSWORD_EXPIRE_SOON.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/PASSWORD_EXPIRE_SOON.tmpl new file mode 100644 index 0000000000..3e92b0d910 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/PASSWORD_EXPIRE_SOON.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.account }}:您的密码有效期将过,请及时登录平台更新密码。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/SYNC_ACCOUNT_STATUS.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/SYNC_ACCOUNT_STATUS.tmpl new file mode 100644 index 0000000000..c6b612f393 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/SYNC_ACCOUNT_STATUS.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +云账号{{ $d.name}}状态异常,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@cn/WORK_BLOCK.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@cn/WORK_BLOCK.tmpl new file mode 100644 index 0000000000..47f3799ccc --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@cn/WORK_BLOCK.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +服务{{ d.service_name}} worker阻塞半小时,请及时检查。 \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/MYSQL_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/MYSQL_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..89b5725302 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/MYSQL_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The primary and secondary synchronization of the database ({{ $d.ip }}) is inconsistent, please check in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/NET_OUT_OF_SYNC.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/NET_OUT_OF_SYNC.tmpl new file mode 100644 index 0000000000..e0e51c4e3e --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/NET_OUT_OF_SYNC.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.service_name }}: The network topology information of the service is inconsistent, please check in time. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/PASSWORD_EXPIRE_SOON.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/PASSWORD_EXPIRE_SOON.tmpl new file mode 100644 index 0000000000..0afabae532 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/PASSWORD_EXPIRE_SOON.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +{{ $d.account }}:Your password is valid and will expire soon. Please log in to the platform in time to update your password. \ No newline at end of file diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/USER_LOCK copy.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/USER_LOCK copy.tmpl new file mode 100644 index 0000000000..73c692f632 --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/USER_LOCK copy.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The account {{ $d.name }} has been locked due to abnormal login. Please verify the situation. If you need to unlock the user, please go to the user list to enable the user. diff --git a/build/notify/root/opt/yunion/share/local-templates/title@en/WORK_BLOCK.tmpl b/build/notify/root/opt/yunion/share/local-templates/title@en/WORK_BLOCK.tmpl new file mode 100644 index 0000000000..ef727fb66a --- /dev/null +++ b/build/notify/root/opt/yunion/share/local-templates/title@en/WORK_BLOCK.tmpl @@ -0,0 +1,2 @@ +{{- $d := .resource_details -}} +The service: {{ d.service_name}} worker has been block 30 minutes.Please verify the service in time. \ No newline at end of file diff --git a/pkg/apihelper/apihelper.go b/pkg/apihelper/apihelper.go index 0d94dcc92a..5a7cb71135 100644 --- a/pkg/apihelper/apihelper.go +++ b/pkg/apihelper/apihelper.go @@ -20,13 +20,17 @@ import ( "sync" "time" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/httputils" + api "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" + npk "yunion.io/x/onecloud/pkg/mcclient/modules/notify" ) const ( @@ -161,6 +165,11 @@ func (h *APIHelper) doSync(ctx context.Context) (changed bool, err error) { } h.modelSets = mss if !r.Correct { + // 发送消息通知 + err := sendSyncErrNotify(s) + if err != nil { + log.Errorf("unable to EventNotify: %s", err) + } return false, errors.Wrap(ErrSync, "incorrect") } changed = r.Changed @@ -181,3 +190,14 @@ func (h *APIHelper) adminClientSession(ctx context.Context) *mcclient.ClientSess h.mcclientSession = auth.GetAdminSession(ctx, region) return h.mcclientSession } + +func sendSyncErrNotify(s *mcclient.ClientSession) error { + params := api.NotificationManagerEventNotifyInput{} + params.Event = api.Event.WithAction(api.ActionNetOutOfSync).WithResourceType(api.TOPIC_RESOURCE_NET).String() + params.AdvanceDays = 0 + message := &jsonutils.JSONDict{} + message.Add(jsonutils.NewString(consts.GetServiceType()), "service_name") + params.ResourceDetails = message + _, err := npk.Notification.PerformClassAction(s, "event-notify", jsonutils.Marshal(params)) + return err +} diff --git a/pkg/apis/notify/const.go b/pkg/apis/notify/const.go index f3f91c418b..d7cb3c52bc 100644 --- a/pkg/apis/notify/const.go +++ b/pkg/apis/notify/const.go @@ -126,6 +126,9 @@ const ( TOPIC_RESOURCE_CLOUDPODS_COMPONENT = "cloudpods_component" TOPIC_RESOURCE_USER = "user" TOPIC_RESOURCE_ACTION_LOG = "action_log" + TOPIC_RESOURCE_ACCOUNT_STATUS = "account" + TOPIC_RESOURCE_WORKER = "worker" + TOPIC_RESOURCE_NET = "net" SUBSCRIBER_TYPE_ROLE = "role" SUBSCRIBER_TYPE_ROBOT = "robot" diff --git a/pkg/apis/notify/event.go b/pkg/apis/notify/event.go index 9fce068aef..42105f61ba 100644 --- a/pkg/apis/notify/event.go +++ b/pkg/apis/notify/event.go @@ -52,7 +52,12 @@ var ( ActionLock SAction = "lock" - ActionExceedCount SAction = "exceed_count" + ActionExceedCount SAction = "exceed_count" + ActionSyncAccountStatus SAction = "sync_account_status" + ActionPasswordExpireSoon SAction = "password_expire_soon" + ActionWorkerBlock SAction = "woker_block" + ActionNetOutOfSync SAction = "net_out_of_sync" + ActionMysqlOutOfSync SAction = "mysql_out_of_sync" ResultFailed SResult = "failed" ResultSucceed SResult = "succeed" diff --git a/pkg/cloudcommon/notifyclient/events.go b/pkg/cloudcommon/notifyclient/events.go index d843d36ee5..956ad0abc4 100644 --- a/pkg/cloudcommon/notifyclient/events.go +++ b/pkg/cloudcommon/notifyclient/events.go @@ -56,6 +56,8 @@ var ( ActionCreateBackupServer = api.ActionCreateBackupServer ActionDelBackupServer = api.ActionDelBackupServer ActionSyncStatus = api.ActionSyncStatus + ActionNetOutOfSync = api.ActionNetOutOfSync + ActionMysqlOutOfSync = api.ActionMysqlOutOfSync ActionPendingDelete = api.ActionPendingDelete diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index c7f5ad0d50..20ea736e4f 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -40,11 +40,13 @@ import ( "yunion.io/x/onecloud/pkg/apis" proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy" api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudcommon/db/proxy" "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/cloudcommon/validators" "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/httperrors" @@ -1348,7 +1350,7 @@ func migrateCloudprovider(cloudprovider *SCloudprovider) error { secret, err := cloudprovider.getPassword() if err != nil { - account.markAccountDiscconected(context.Background(), auth.AdminCredential()) + account.markAccountDisconected(context.Background(), auth.AdminCredential()) log.Errorf("Get password from provider %s error %v", cloudprovider.Name, err) } else { err = account.savePassword(secret) @@ -1755,7 +1757,7 @@ func (manager *SCloudaccountManager) OrderByExtraFields( return q, nil } -func (account *SCloudaccount) markAccountDiscconected(ctx context.Context, userCred mcclient.TokenCredential) error { +func (account *SCloudaccount) markAccountDisconected(ctx context.Context, userCred mcclient.TokenCredential) error { _, err := db.UpdateWithLock(ctx, account, func() error { account.ErrorCount = account.ErrorCount + 1 account.HealthStatus = api.CLOUD_PROVIDER_HEALTH_UNKNOWN @@ -1764,10 +1766,13 @@ func (account *SCloudaccount) markAccountDiscconected(ctx context.Context, userC if err != nil { return err } + if account.Status == api.CLOUD_PROVIDER_CONNECTED { + account.EventNotify(ctx, userCred, notify.ActionSyncAccountStatus) + } return account.SetStatus(userCred, api.CLOUD_PROVIDER_DISCONNECTED, "") } -func (account *SCloudaccount) markAllProvidersDicconnected(ctx context.Context, userCred mcclient.TokenCredential) error { +func (account *SCloudaccount) markAllProvidersDisconnected(ctx context.Context, userCred mcclient.TokenCredential) error { providers := account.GetCloudproviders() for i := 0; i < len(providers); i += 1 { err := providers[i].markProviderDisconnected(ctx, userCred, "cloud account disconnected") @@ -2037,8 +2042,8 @@ func (account *SCloudaccount) syncAccountStatus(ctx context.Context, userCred mc account.MarkSyncing(userCred, true) subaccounts, err := account.probeAccountStatus(ctx, userCred) if err != nil { - account.markAllProvidersDicconnected(ctx, userCred) - account.markAccountDiscconected(ctx, userCred) + account.markAllProvidersDisconnected(ctx, userCred) + account.markAccountDisconected(ctx, userCred) return errors.Wrap(err, "account.probeAccountStatus") } account.markAccountConnected(ctx, userCred) @@ -2861,3 +2866,16 @@ func (self *SCloudaccount) PerformProjectMapping(ctx context.Context, userCred m } return nil, refreshPmCaches() } + +// 同步云账号消息通知 +func (account *SCloudaccount) EventNotify(ctx context.Context, userCred mcclient.TokenCredential, action notify.SAction) { + var resourceType string + resourceType = notify.TOPIC_RESOURCE_ACCOUNT_STATUS + + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: account, + ResourceType: resourceType, + Action: action, + AdvanceDays: 0, + }) +} diff --git a/pkg/keystone/models/passwords.go b/pkg/keystone/models/passwords.go index bc4b3879b0..9e23f3c215 100644 --- a/pkg/keystone/models/passwords.go +++ b/pkg/keystone/models/passwords.go @@ -21,11 +21,16 @@ import ( "encoding/hex" "time" + "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/httperrors" o "yunion.io/x/onecloud/pkg/keystone/options" + "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/seclib2" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -194,3 +199,70 @@ func (passwd *SPassword) IsExpired() bool { } return false } + +// 定时任务判断用户是否需要密码过期通知 +func CheckAllUserPasswordIsExpired(ctx context.Context, userCred mcclient.TokenCredential, startRun bool) { + pwds := []SPassword{} + pwdQ := PasswordManager.Query() + pwdQ = pwdQ.Desc("created_at") + err := db.FetchModelObjects(PasswordManager, pwdQ, &pwds) + if err != nil { + log.Errorln("fetch Password error:", err) + return + } + hasCheckedPwd := make(map[int]struct{}) + for _, pwd := range pwds { + if _, isExist := hasCheckedPwd[pwd.LocalUserId]; isExist { + continue + } + if pwd.ExpiresAt.IsZero() { + continue + } + hasCheckedPwd[pwd.LocalUserId] = struct{}{} + err = pwd.NeedSendNotify(ctx, userCred) + if err != nil { + log.Errorln(errors.Wrap(err, "pwd.NeedSendNotify")) + } + } +} + +func (pwd *SPassword) NeedSendNotify(ctx context.Context, userCred mcclient.TokenCredential) error { + expireTime := time.Date(pwd.ExpiresAt.Year(), pwd.ExpiresAt.Month(), pwd.ExpiresAt.Day(), 0, 0, 0, 0, time.Local) + nowTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local) + + sub := expireTime.Sub(nowTime) + subDay := sub.Hours() / 24 + switch { + case subDay == 7: + localUser, err := LocalUserManager.fetchLocalUser("", "", pwd.LocalUserId) + if err != nil { + return errors.Wrap(err, "fetchLocalUser error:") + } + pwd.EventNotify(ctx, userCred, notify.ActionPasswordExpireSoon, localUser.Name, 7) + case subDay == 1: + localUser, err := LocalUserManager.fetchLocalUser("", "", pwd.LocalUserId) + if err != nil { + return errors.Wrap(err, "fetchLocalUser error:") + } + pwd.EventNotify(ctx, userCred, notify.ActionPasswordExpireSoon, localUser.Name, 1) + } + return nil +} + +// 密码即将失效消息通知 +func (pwd *SPassword) EventNotify(ctx context.Context, userCred mcclient.TokenCredential, action notify.SAction, userName string, advanceDays int) { + resourceType := notify.TOPIC_RESOURCE_USER + + detailsDecro := func(ctx context.Context, details *jsonutils.JSONDict) { + details.Set("account", jsonutils.NewString(userName)) + } + pwd.Password = "" + + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: pwd, + ObjDetailsDecorator: detailsDecro, + ResourceType: resourceType, + Action: action, + AdvanceDays: advanceDays, + }) +} diff --git a/pkg/keystone/service/service.go b/pkg/keystone/service/service.go index 3d9cdc174d..96ee47f427 100644 --- a/pkg/keystone/service/service.go +++ b/pkg/keystone/service/service.go @@ -90,6 +90,7 @@ func StartService() { cron.AddJobAtIntervalsWithStartRun("CalculateIdentityQuotaUsages", time.Duration(opts.CalculateQuotaUsageIntervalSeconds)*time.Second, models.IdentityQuotaManager.CalculateQuotaUsages, true) cron.AddJobEveryFewHour("AutoPurgeSplitable", 4, 30, 0, db.AutoPurgeSplitable, false) + cron.AddJobEveryFewDays("CheckAllUserPasswordIsExpired", 4, 30, 0, 0, models.CheckAllUserPasswordIsExpired, true) cron.Start() defer cron.Stop() diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index b70941a360..e8242b3d8d 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -95,6 +95,11 @@ const ( DefaultChecksumTestFailed = "checksum test failed" DefaultUserLock = "user lock" DefaultActionLogExceedCount = "action log exceed count" + DefaultSyncAccountStatus = "cloud account sync status" + DefaultPasswordExpireDue1Day = "password expire due 1 day" + DefaultPasswordExpireDue7Day = "password expire due 7 day" + DefaultNetOutOfSync = "net out of sync" + DefaultMysqlOutOfSync = "mysql out of sync" ) func (sm *STopicManager) InitializeData() error { @@ -114,6 +119,11 @@ func (sm *STopicManager) InitializeData() error { DefaultChecksumTestFailed, DefaultUserLock, DefaultActionLogExceedCount, + DefaultSyncAccountStatus, + DefaultPasswordExpireDue1Day, + DefaultPasswordExpireDue7Day, + DefaultNetOutOfSync, + DefaultMysqlOutOfSync, ) q := sm.Query() topics := make([]STopic, 0, initSNames.Len()) @@ -328,6 +338,50 @@ func (sm *STopicManager) InitializeData() error { notify.ActionExceedCount, ) t.Type = notify.TOPIC_TYPE_RESOURCE + case DefaultSyncAccountStatus: + t.addResources( + notify.TOPIC_RESOURCE_ACCOUNT_STATUS, + ) + t.addAction( + notify.ActionSyncAccountStatus, + ) + t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS + case DefaultPasswordExpireDue1Day: + t.addResources( + notify.TOPIC_RESOURCE_USER, + ) + t.addAction( + notify.ActionPasswordExpireSoon, + ) + t.Type = notify.TOPIC_TYPE_SECURITY + t.AdvanceDays = 1 + case DefaultPasswordExpireDue7Day: + t.addResources( + notify.TOPIC_RESOURCE_USER, + ) + t.addAction( + notify.ActionPasswordExpireSoon, + ) + t.Type = notify.TOPIC_TYPE_SECURITY + t.AdvanceDays = 7 + case DefaultNetOutOfSync: + t.addResources( + notify.TOPIC_RESOURCE_NET, + ) + t.addAction( + notify.ActionNetOutOfSync, + ) + t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS + t.AdvanceDays = 0 + case DefaultMysqlOutOfSync: + t.addResources( + notify.TOPIC_RESOURCE_DBINSTANCE, + ) + t.addAction( + notify.ActionMysqlOutOfSync, + ) + t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS + t.AdvanceDays = 0 } if topic == nil { err := sm.TableSpec().Insert(ctx, t) @@ -557,6 +611,8 @@ func init() { notify.TOPIC_RESOURCE_DB_TABLE_RECORD: 35, notify.TOPIC_RESOURCE_USER: 36, notify.TOPIC_RESOURCE_ACTION_LOG: 37, + notify.TOPIC_RESOURCE_ACCOUNT_STATUS: 38, + notify.TOPIC_RESOURCE_NET: 39, }, ) converter.registerAction( @@ -585,6 +641,10 @@ func init() { notify.ActionChecksumTest: 21, notify.ActionLock: 22, notify.ActionExceedCount: 23, + notify.ActionSyncAccountStatus: 24, + notify.ActionPasswordExpireSoon: 25, + notify.ActionNetOutOfSync: 26, + notify.ActionMysqlOutOfSync: 27, }, ) }