From 29d53eb04a5fd2f3e9d2b4f5f15aa7f66e707e69 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 11:01:18 +0800 Subject: [PATCH 01/12] fix(notify): change the delivery format of config and eliminate some redundant steps of sending messages --- pkg/notify/interface.go | 3 -- pkg/notify/models/config.go | 51 ++++++++++----------------- pkg/notify/rpc/send.go | 70 ++----------------------------------- 3 files changed, 20 insertions(+), 104 deletions(-) diff --git a/pkg/notify/interface.go b/pkg/notify/interface.go index 02514599f1..8cc7e2968c 100644 --- a/pkg/notify/interface.go +++ b/pkg/notify/interface.go @@ -58,10 +58,7 @@ type SBatchSendParams struct { type IServiceConfigStore interface { GetConfigs(service string) ([]SConfig, error) - GetConfig(service, domainId string) (SConfig, error) SetConfig(service string, config SConfig) error - HasSystemConfig(service string) (bool, error) - BatchCheckConfig(service string, domainIds []string) ([]bool, error) } type SNotification struct { diff --git a/pkg/notify/models/config.go b/pkg/notify/models/config.go index a79978126e..6a9bcd89d4 100644 --- a/pkg/notify/models/config.go +++ b/pkg/notify/models/config.go @@ -95,7 +95,7 @@ func (cm *SConfigManager) ValidateCreateData(ctx context.Context, userCred mccli if input.Content == nil { return input, httperrors.NewMissingParameterError("content") } - config, err := cm.Config(input.Type, input.ProjectDomainId) + config, err := cm.Config(input.Type, input.ProjectDomainId, input.Attribution) if err == nil && config != nil { return input, httperrors.NewDuplicateResourceError("duplicate type %q", input.Type) } @@ -171,10 +171,7 @@ func (c *SConfig) PostCreate(ctx context.Context, userCred mcclient.TokenCredent log.Errorf("unable to unmarshal: %v", err) return } - NotifyService.AddConfig(ctx, c.Type, notifyv2.SConfig{ - Config: configMap, - DomainId: c.DomainId, - }) + NotifyService.AddConfig(ctx, c.Type, c.Config()) err = c.StartRepullSubcontactTask(ctx, userCred) if err != nil { log.Errorf("unable to StartRepullSubcontactTask: %v", err) @@ -201,7 +198,7 @@ func (c *SConfig) PostUpdate(ctx context.Context, userCred mcclient.TokenCredent func (c *SConfig) PreDelete(ctx context.Context, userCred mcclient.TokenCredential) { c.SStandaloneResourceBase.PreDelete(ctx, userCred) - NotifyService.DeleteConfig(ctx, c.Type, c.DomainId) + NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId) } func (c *SConfig) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) { err := c.StartRepullSubcontactTask(ctx, userCred) @@ -534,10 +531,10 @@ func (self *SConfigManager) Configs(contactType string) ([]SConfig, error) { return configs, nil } -func (self *SConfigManager) Config(contactType, domainId string) (*SConfig, error) { +func (self *SConfigManager) Config(contactType, domainId string, attribution string) (*SConfig, error) { q := self.Query() q = q.Equals("type", contactType) - if len(domainId) == 0 { + if attribution == api.CONFIG_ATTRIBUTION_SYSTEM { q = q.Equals("attribution", api.CONFIG_ATTRIBUTION_SYSTEM) } else { q = q.Equals("domain_id", domainId).Equals("attribution", api.CONFIG_ATTRIBUTION_DOMAIN) @@ -590,35 +587,11 @@ func (self *SConfigManager) GetConfigs(contactType string) ([]notifyv2.SConfig, } ret := make([]notifyv2.SConfig, 0, len(configs)) for i := range configs { - c := make(map[string]string) - err := configs[i].Content.Unmarshal(&c) - if err != nil { - return nil, errors.Wrap(err, "fail unmarshal config content") - } - ret = append(ret, notifyv2.SConfig{ - Config: c, - DomainId: configs[i].DomainId, - }) + ret = append(ret, configs[i].Config()) } return ret, nil } -func (self *SConfigManager) GetConfig(contactType, domainId string) (notifyv2.SConfig, error) { - config, err := self.Config(contactType, domainId) - if err != nil { - return notifyv2.SConfig{}, err - } - ret := make(map[string]string) - err = config.Content.Unmarshal(&ret) - if err != nil { - return notifyv2.SConfig{}, errors.Wrap(err, "fail unmarshal config content") - } - return notifyv2.SConfig{ - Config: ret, - DomainId: config.DomainId, - }, nil -} - func (self *SConfigManager) SetConfig(contactType string, config notifyv2.SConfig) error { content := jsonutils.Marshal(config.Config) sConfig := &SConfig{ @@ -634,6 +607,18 @@ func (self *SConfigManager) SetConfig(contactType string, config notifyv2.SConfi return self.TableSpec().InsertOrUpdate(context.Background(), sConfig) } +func (self *SConfig) Config() notifyv2.SConfig { + c := make(map[string]string) + _ = self.Content.Unmarshal(&c) + sc := notifyv2.SConfig{ + Config: c, + } + if self.Attribution == api.CONFIG_ATTRIBUTION_DOMAIN { + sc.DomainId = self.DomainId + } + return sc +} + func intersection(sa1, sa2 []string) []string { set1 := sets.NewString(sa1...) set2 := sets.NewString(sa2...) diff --git a/pkg/notify/rpc/send.go b/pkg/notify/rpc/send.go index 86bf0c4184..032bab06d3 100644 --- a/pkg/notify/rpc/send.go +++ b/pkg/notify/rpc/send.go @@ -41,7 +41,7 @@ import ( const ( // ErrSendServiceNotFound means SRpcService's SendSerivces hasn't this Send Service. ErrSendServiceNotFound = errors.Error("No such send service") - ErrSendServiceNotInit = errors.Error("Send service hasn't been init") + // ErrSendServiceNotInit = errors.Error("Send service hasn't been init") ) // SRpcService provide rpc service about sending message for notify module and manage these services. @@ -134,53 +134,8 @@ func (self *SRpcService) BatchSend(ctx context.Context, contactType string, args if len(args.RemoteTemplate) == 0 && contactType == api.MOBILE { return nil, fmt.Errorf("empty remote template for mobile type notification") } - domainIds := make([]string, len(args.Receivers)) - for i := range domainIds { - domainIds[i] = args.Receivers[i].DomainId - } - checks, err := self.configStore.BatchCheckConfig(contactType, domainIds) - if err != nil { - return nil, errors.Wrap(err, "BatchCheckConfig") - } - hasSystem, err := self.configStore.HasSystemConfig(contactType) - if err != nil { - return nil, errors.Wrap(err, "HasSystemConfig") - } ret := make([]*apis.FailedRecord, 0) - receiverIndex := 0 - for i := range checks { - if checks[i] { - receiverIndex++ - continue - } - if hasSystem { - args.Receivers[receiverIndex].DomainId = "" - receiverIndex++ - continue - } - ret = append(ret, &apis.FailedRecord{ - Receiver: args.Receivers[i], - Reason: fmt.Sprintf("no %q config for in domain %q and system", contactType, domainIds[i]), - }) - args.Receivers = append(args.Receivers[:receiverIndex], args.Receivers[receiverIndex+1:]...) - } f := func(service *apis.SendNotificationClient) (interface{}, error) { - // check ready - domainIds := make([]string, len(args.Receivers)) - for i := range domainIds { - domainIds[i] = args.Receivers[i].DomainId - } - output, err := service.Ready(ctx, &apis.ReadyInput{DomainIds: domainIds}) - if err != nil { - return nil, err - } - if !output.Ok { - // if NOINIT, try to restart server and send again - service, err = self.restartService(ctx, contactType) - if err != nil { - return nil, errors.Wrapf(err, "restart service %s failed", contactType) - } - } return service.BatchSend(ctx, &args) } @@ -290,28 +245,7 @@ func (self *SRpcService) execute(ctx context.Context, f func(client *apis.SendNo self.closeService(ctx, serviceName) return nil, ErrSendServiceNotFound } - - if st.Message() != ErrSendServiceNotInit.Error() { - return nil, err - } - - // if NOINIT, try to restart server and send again - sendService, err = self.restartService(ctx, serviceName) - if err != nil { - return nil, errors.Wrapf(err, "restart service %s failed", serviceName) - } - - ret, err = f(sendService) - if err != nil { - st := status.Convert(err) - if st.Code() == codes.Unavailable { - // sock is bad - self.closeService(ctx, serviceName) - - return nil, errors.Wrap(ErrSendServiceNotFound, serviceName) - } - return nil, err - } + return nil, err } return ret, nil } From 964a64949e366a23901cdb2555b251cf3a594f41 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 10:15:57 +0800 Subject: [PATCH 02/12] fix(notify): modify NotifyRobotWithCtx --- pkg/cloudcommon/notifyclient/notify.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index 1a3fce5ceb..20575f8759 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -134,7 +134,7 @@ func NotifyRobot(robotIds []string, priority npk.TNotifyPriority, event string, // NotifyRobot will send messages via all robot contact type such as dingtalk-robot. func NotifyRobotWithCtx(ctx context.Context, robotIds []string, priority npk.TNotifyPriority, event string, data jsonutils.JSONObject) error { - rawNotify(context.Background(), sNotifyParams{ + rawNotify(ctx, sNotifyParams{ robots: robotIds, channel: npk.NotifyByRobot, priority: priority, From 8d70077da4f8c571b313a50bcdcf2041c49be426 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 10:16:22 +0800 Subject: [PATCH 03/12] fix(notify): repull subcontact correctly when creating or deleting config --- pkg/notify/models/config.go | 33 +++++++++++++++++----- pkg/notify/tasks/repull_subcontact_task.go | 33 +++++++++++++--------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/pkg/notify/models/config.go b/pkg/notify/models/config.go index 6a9bcd89d4..ea5fe673dc 100644 --- a/pkg/notify/models/config.go +++ b/pkg/notify/models/config.go @@ -172,7 +172,7 @@ func (c *SConfig) PostCreate(ctx context.Context, userCred mcclient.TokenCredent return } NotifyService.AddConfig(ctx, c.Type, c.Config()) - err = c.StartRepullSubcontactTask(ctx, userCred) + err = c.StartRepullSubcontactTask(ctx, userCred, false) if err != nil { log.Errorf("unable to StartRepullSubcontactTask: %v", err) } @@ -190,7 +190,7 @@ func (c *SConfig) PostUpdate(ctx context.Context, userCred mcclient.TokenCredent Config: configMap, DomainId: c.DomainId, }) - err = c.StartRepullSubcontactTask(ctx, userCred) + err = c.StartRepullSubcontactTask(ctx, userCred, false) if err != nil { log.Errorf("unable to StartRepullSubcontactTask: %v", err) } @@ -200,15 +200,34 @@ func (c *SConfig) PreDelete(ctx context.Context, userCred mcclient.TokenCredenti c.SStandaloneResourceBase.PreDelete(ctx, userCred) NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId) } -func (c *SConfig) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) { - err := c.StartRepullSubcontactTask(ctx, userCred) + +func (c *SConfig) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { + err := c.SStandaloneResourceBase.CustomizeDelete(ctx, userCred, query, data) if err != nil { - log.Errorf("unable to StartRepullSubcontactTask: %v", err) + return err } + NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId) + err = c.StartRepullSubcontactTask(ctx, userCred, true) + if err != nil { + return errors.Wrap(err, "unable to start repull subcontact") + } + return err } -func (c *SConfig) StartRepullSubcontactTask(ctx context.Context, userCred mcclient.TokenCredential) error { - task, err := taskman.TaskManager.NewTask(ctx, "RepullSuncontactTask", c, userCred, nil, "", "") +func (c *SConfig) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { + return nil +} + +func (c *SConfig) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error { + return c.SStandaloneResourceBase.Delete(ctx, userCred) +} + +func (c *SConfig) StartRepullSubcontactTask(ctx context.Context, userCred mcclient.TokenCredential, del bool) error { + taskData := jsonutils.NewDict() + if del { + taskData.Set("deleted", jsonutils.JSONTrue) + } + task, err := taskman.TaskManager.NewTask(ctx, "RepullSuncontactTask", c, userCred, taskData, "", "") if err != nil { return err } diff --git a/pkg/notify/tasks/repull_subcontact_task.go b/pkg/notify/tasks/repull_subcontact_task.go index 802bf40c91..6e4edfea02 100644 --- a/pkg/notify/tasks/repull_subcontact_task.go +++ b/pkg/notify/tasks/repull_subcontact_task.go @@ -41,7 +41,9 @@ func init() { } func (self *RepullSuncontactTask) taskFailed(ctx context.Context, config *models.SConfig, reason string) { - logclient.AddActionLogWithContext(ctx, config, logclient.ACT_PULL_SUBCONTACT, reason, self.UserCred, false) + if !config.Deleted { + logclient.AddActionLogWithContext(ctx, config, logclient.ACT_PULL_SUBCONTACT, reason, self.UserCred, false) + } self.SetStageFailed(ctx, jsonutils.NewString(reason)) } @@ -66,8 +68,8 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone q = q.Equals("domain_id", config.DomainId) } else { // The system-level config update should not affect the receiver under the domain with config - configq := models.ConfigManager.Query("domain_id").Equals("attribution", notify.CONFIG_ATTRIBUTION_DOMAIN).SubQuery() - q = q.Join(configq, sqlchemy.NotEquals(q.Field("domain_id"), configq.Field("domain_id"))) + configq := models.ConfigManager.Query("domain_id").Equals("type", config.Type).Equals("attribution", notify.CONFIG_ATTRIBUTION_DOMAIN).SubQuery() + q = q.NotIn("domain_id", configq) } q.Join(subq, sqlchemy.Equals(q.Field("id"), subq.Field("receiver_id"))) rs := make([]models.SReceiver, 0) @@ -77,6 +79,10 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone return } + if del, _ := self.GetParams().Bool("deleted"); del { + config.RealDelete(ctx, self.GetUserCred()) + } + var reasons []string for i := range rs { r := &rs[i] @@ -93,17 +99,16 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone return } ctSets := sets.NewString(cts...) - if !ctSets.Has(config.Type) { - return - } - ctSets.Delete(config.Type) - err = r.SetVerifiedContactTypes(ctSets.UnsortedList()) - if err != nil { - reasons = append(reasons, repullFailedReason{ - ReceiverId: r.Id, - Reason: fmt.Sprintf("unable to SetVerifiedContactTypes: %v", err), - }.String()) - return + if ctSets.Has(config.Type) { + ctSets.Delete(config.Type) + err = r.SetVerifiedContactTypes(ctSets.UnsortedList()) + if err != nil { + reasons = append(reasons, repullFailedReason{ + ReceiverId: r.Id, + Reason: fmt.Sprintf("unable to SetVerifiedContactTypes: %v", err), + }.String()) + return + } } // pull params := jsonutils.NewDict() From c5ea363f43c07468009e2a1c01ee37eca47d5b33 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 11:38:01 +0800 Subject: [PATCH 04/12] fix(cloudcommon): set Priority as normal when calling EventNotify --- pkg/cloudcommon/notifyclient/notify.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index 20575f8759..47b5d35df6 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -284,6 +284,7 @@ func EventNotify(ctx context.Context, userCred mcclient.TokenCredential, ep SEve ResourceDetails: objDetails, Event: event.String(), AdvanceDays: ep.AdvanceDays, + Priority: string(npk.NotifyPriorityNormal), ProjectId: projectId, ProjectDomainId: projectDomainId, } From 66dc7388b55069d460cdc94ab6fde22ef3a600d9 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 15:53:35 +0800 Subject: [PATCH 05/12] fix(notify): update database when change subscriber's identification --- pkg/notify/models/subscriber.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/notify/models/subscriber.go b/pkg/notify/models/subscriber.go index ea91026e72..0b4059e25f 100644 --- a/pkg/notify/models/subscriber.go +++ b/pkg/notify/models/subscriber.go @@ -260,9 +260,21 @@ func (s *SSubscriber) PerformChange(ctx context.Context, userCred mcclient.Token log.Errorf("unable to set receivers %s", input.Receivers) } case api.SUBSCRIBER_TYPE_ROBOT: - s.Identification = input.Robot + _, err := db.Update(s, func() error { + s.Identification = input.Robot + return nil + }) + if err != nil { + return nil, errors.Wrap(err, "unable to update subscriber") + } case api.SUBSCRIBER_TYPE_ROLE: - s.Identification = input.Role + _, err := db.Update(s, func() error { + s.Identification = input.Role + return nil + }) + if err != nil { + return nil, errors.Wrap(err, "unable to update subscriber") + } } return nil, nil } From 16fc73f3559fea7c82cd006477a176ef9cc10460 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 16:15:02 +0800 Subject: [PATCH 06/12] fix(notify): check for duplicate subscribers when creating --- pkg/notify/models/subscriber.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/notify/models/subscriber.go b/pkg/notify/models/subscriber.go index 0b4059e25f..100e56600a 100644 --- a/pkg/notify/models/subscriber.go +++ b/pkg/notify/models/subscriber.go @@ -165,6 +165,8 @@ func (sm *SSubscriberManager) ValidateCreateData(ctx context.Context, userCred m if input.Scope == sDomain && domainId != userCred.GetDomainId() { return input, httperrors.NewForbiddenError("domain %s admin can't create subscriber for domain %s", userCred.GetDomainId(), domainId) } + + var checkQuery *sqlchemy.SQuery input.TopicID = t.GetId() switch input.Type { case api.SUBSCRIBER_TYPE_RECEIVER: @@ -182,6 +184,7 @@ func (sm *SSubscriberManager) ValidateCreateData(ctx context.Context, userCred m return input, errors.Wrapf(err, "unable find role %q", input.Role) } input.Role = roleCache.GetId() + checkQuery = sm.Query().Equals("topic_id", input.TopicID).Equals("type", api.SUBSCRIBER_TYPE_ROLE).Equals("resource_scope", input.ResourceScope).Equals("identification", input.Role).Equals("role_scope", input.RoleScope) case api.SUBSCRIBER_TYPE_ROBOT: robot, err := RobotManager.FetchByIdOrName(userCred, input.Robot) if errors.Cause(err) == sql.ErrNoRows { @@ -191,9 +194,20 @@ func (sm *SSubscriberManager) ValidateCreateData(ctx context.Context, userCred m return input, errors.Wrapf(err, "unable to fetch robot %q", input.Robot) } input.Robot = robot.GetId() + checkQuery = sm.Query().Equals("type", api.SUBSCRIBER_TYPE_ROLE).Equals("topic_id", input.TopicID).Equals("resource_scope", input.ResourceScope).Equals("identification", input.Robot) default: return input, httperrors.NewInputParameterError("unkown type %q", input.Type) } + // check type+resourceScope+identification + if checkQuery != nil { + count, err := checkQuery.CountWithError() + if err != nil { + return input, errors.Wrap(err, "unable to count") + } + if count > 0 { + return input, httperrors.NewForbiddenError("repeated with existing subscribers") + } + } return input, nil } From 78115949a04f34fca33f4c04ed61616dbab3c04e Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 16:41:58 +0800 Subject: [PATCH 07/12] fix(notify): correctly use tenant cache to query domain --- pkg/notify/models/subscriber.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/notify/models/subscriber.go b/pkg/notify/models/subscriber.go index 100e56600a..4e0598f2ab 100644 --- a/pkg/notify/models/subscriber.go +++ b/pkg/notify/models/subscriber.go @@ -157,10 +157,10 @@ func (sm *SSubscriberManager) ValidateCreateData(ctx context.Context, userCred m if err != nil { return input, errors.Wrapf(err, "unable to fetch domain %s", input.ResourceAttributionId) } - domainId = tenant.DomainId + domainId = tenant.Id input.DomainId = domainId - input.ResourceAttributionId = tenant.DomainId - input.ResourceAttributionName = tenant.Domain + input.ResourceAttributionId = tenant.Id + input.ResourceAttributionName = tenant.Name } if input.Scope == sDomain && domainId != userCred.GetDomainId() { return input, httperrors.NewForbiddenError("domain %s admin can't create subscriber for domain %s", userCred.GetDomainId(), domainId) From e300795f42ddc67f719871c2b8008c2b7caba979 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 18:24:20 +0800 Subject: [PATCH 08/12] fix(notify): allow list and get topics for all --- pkg/notify/policy/defaults.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/notify/policy/defaults.go b/pkg/notify/policy/defaults.go index e7b990dcca..e2d4bb0fa8 100644 --- a/pkg/notify/policy/defaults.go +++ b/pkg/notify/policy/defaults.go @@ -73,6 +73,24 @@ var ( }, }, }, + { + Auth: true, + Scope: rbacutils.ScopeSystem, + Rules: []rbacutils.SRbacRule{ + { + Service: api.SERVICE_TYPE, + Resource: "topics", + Action: PolicyActionGet, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "topics", + Action: PolicyActionList, + Result: rbacutils.Allow, + }, + }, + }, } ) From e1f8850f2507e028efcfebc346ec2cace4a253f6 Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 19:06:24 +0800 Subject: [PATCH 09/12] fix(notify): solve the permission problem of subscriber --- pkg/notify/models/subscriber.go | 12 +++++----- pkg/notify/policy/defaults.go | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pkg/notify/models/subscriber.go b/pkg/notify/models/subscriber.go index 4e0598f2ab..16fef33a16 100644 --- a/pkg/notify/models/subscriber.go +++ b/pkg/notify/models/subscriber.go @@ -162,8 +162,8 @@ func (sm *SSubscriberManager) ValidateCreateData(ctx context.Context, userCred m input.ResourceAttributionId = tenant.Id input.ResourceAttributionName = tenant.Name } - if input.Scope == sDomain && domainId != userCred.GetDomainId() { - return input, httperrors.NewForbiddenError("domain %s admin can't create subscriber for domain %s", userCred.GetDomainId(), domainId) + if input.Scope == sDomain && domainId != userCred.GetProjectDomainId() { + return input, httperrors.NewForbiddenError("domain %s admin can't create subscriber for domain %s", userCred.GetProjectDomainId(), domainId) } var checkQuery *sqlchemy.SQuery @@ -263,7 +263,7 @@ func (s *SSubscriber) PerformChange(ctx context.Context, userCred mcclient.Token if !db.IsDomainAllowUpdate(userCred, s) { return nil, httperrors.NewForbiddenError("") } - if s.DomainId != userCred.GetDomainId() { + if s.DomainId != userCred.GetProjectDomainId() { return nil, httperrors.NewForbiddenError("") } } @@ -314,11 +314,11 @@ func (sm *SSubscriberManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ return nil, httperrors.NewForbiddenError("") } case sDomain: - allow := db.IsAdminAllowList(userCred, sm) + allow := db.IsDomainAllowList(userCred, sm) if !allow { return nil, httperrors.NewForbiddenError("") } - q = q.Equals("domain_id", userCred.GetDomainId()) + q = q.Equals("domain_id", userCred.GetProjectDomainId()) default: return nil, httperrors.NewInputParameterError("unkown scope %s", input.Scope) } @@ -375,7 +375,7 @@ func (s *SSubscriber) CustomizeDelete(ctx context.Context, userCred mcclient.Tok if !db.IsDomainAllowDelete(userCred, s) { return httperrors.NewForbiddenError("") } - if s.DomainId != userCred.GetDomainId() { + if s.DomainId != userCred.GetProjectDomainId() { return httperrors.NewForbiddenError("") } } diff --git a/pkg/notify/policy/defaults.go b/pkg/notify/policy/defaults.go index e2d4bb0fa8..f3e5bb3dfd 100644 --- a/pkg/notify/policy/defaults.go +++ b/pkg/notify/policy/defaults.go @@ -91,6 +91,48 @@ var ( }, }, }, + { + Auth: true, + Scope: rbacutils.ScopeSystem, + Rules: []rbacutils.SRbacRule{ + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionGet, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionList, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionCreate, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionUpdate, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionDelete, + Result: rbacutils.Allow, + }, + { + Service: api.SERVICE_TYPE, + Resource: "subscribers", + Action: PolicyActionPerform, + Result: rbacutils.Allow, + }, + }, + }, } ) From 8cb7ef24f2827ead05b1dcaa6f3f38a1af47b0fa Mon Sep 17 00:00:00 2001 From: rainzm Date: Mon, 27 Sep 2021 19:54:35 +0800 Subject: [PATCH 10/12] fix(notify): remove disk in 'update' topic --- pkg/notify/models/topic.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/notify/models/topic.go b/pkg/notify/models/topic.go index d19840fbde..861b605e73 100644 --- a/pkg/notify/models/topic.go +++ b/pkg/notify/models/topic.go @@ -155,7 +155,6 @@ func (sm *STopicManager) InitializeData() error { case DefaultResourceUpdate: t.addResources( notify.TOPIC_RESOURCE_SERVER, - notify.TOPIC_RESOURCE_DISK, notify.TOPIC_RESOURCE_DBINSTANCE, notify.TOPIC_RESOURCE_ELASTICCACHE, ) From de0db5ac3d0c04bfaee7a2a265cc036d730751a5 Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 28 Sep 2021 11:13:09 +0800 Subject: [PATCH 11/12] fix(notify): add connect info in EXPIRED_RELEASE template --- .../share/local-templates/content@cn/EXPIRED_RELEASE.tmpl | 8 ++++++++ .../share/local-templates/content@en/DELETE_WITH_IP.tmpl | 2 +- .../share/local-templates/content@en/EXPIRED_RELEASE.tmpl | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/EXPIRED_RELEASE.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/EXPIRED_RELEASE.tmpl index 9eae642ba6..b7ac9d6bd0 100644 --- a/build/notify/root/opt/yunion/share/local-templates/content@cn/EXPIRED_RELEASE.tmpl +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/EXPIRED_RELEASE.tmpl @@ -3,4 +3,12 @@ {{- if $d.brand -}} {{ $d.brand }}平台 {{- end -}} + +{{- if $d.private_dns -}} +,内网地址为{{ $d.private_dns }}:{{ $d.private_connect_port }} +{{- end -}} + +{{- if $d.public_dns -}} +,外网地址为{{ $d.public_dns }}:{{ $d.public_connect_port }}的 +{{- end -}} {{ .resource_type_display }}{{ $d.name }}还有{{ .advance_days }}天就要到期释放,如需续期使用,请尽快前往控制台处理 diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/DELETE_WITH_IP.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/DELETE_WITH_IP.tmpl index e1a909f968..5845d6903d 100644 --- a/build/notify/root/opt/yunion/share/local-templates/content@en/DELETE_WITH_IP.tmpl +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/DELETE_WITH_IP.tmpl @@ -6,7 +6,7 @@ Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type_display }} { {{- end -}} {{- if $d.public_dns -}} -, the internet address is {{ $d.public_dns }}:{{ $d.public_connect_port }} +, the external address is {{ $d.public_dns }}:{{ $d.public_connect_port }} {{- end -}} {{- if and $d.address_type $d.address -}} diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/EXPIRED_RELEASE.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/EXPIRED_RELEASE.tmpl index 04c84d7298..3bcf9d94e4 100644 --- a/build/notify/root/opt/yunion/share/local-templates/content@en/EXPIRED_RELEASE.tmpl +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/EXPIRED_RELEASE.tmpl @@ -1,2 +1,2 @@ {{- $d := .resource_details -}} -Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type }} {{ $d.name }} {{ if $d.project -}} in project {{ $d.project }} {{ end -}} will expire and be released in {{ .advance_days }} days. If you need to renew, please go to the console as soon as possible +Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type }} {{ $d.name }} {{ if $d.public_dns -}} with external address {{ $d.public_dns }}:{{ $d.public_connect_port }} {{ end -}} {{ if $d.project -}} in project {{ $d.project }} {{ end -}} will expire and be released in {{ .advance_days }} days. If you need to renew, please go to the console as soon as possible From 8d32750850929d3f67414273dceb0cc071f71d31 Mon Sep 17 00:00:00 2001 From: rainzm Date: Tue, 28 Sep 2021 14:48:26 +0800 Subject: [PATCH 12/12] fix(notify): optimization of message template --- .../share/local-templates/content@cn/CREATE.tmpl | 10 +++++----- .../share/local-templates/content@en/CREATE.tmpl | 6 +++--- pkg/compute/models/guest_actions.go | 2 +- pkg/notify/models/event_template.go | 5 +++++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/build/notify/root/opt/yunion/share/local-templates/content@cn/CREATE.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@cn/CREATE.tmpl index 3e387a6430..8aa7130b4a 100644 --- a/build/notify/root/opt/yunion/share/local-templates/content@cn/CREATE.tmpl +++ b/build/notify/root/opt/yunion/share/local-templates/content@cn/CREATE.tmpl @@ -13,8 +13,8 @@ ,外网地址为{{ $d.public_dns }}:{{ $d.public_connect_port }} {{- end -}} -{{- if and $d.address_type $d.address -}} -,服务地址为{{ $d.address_type }}{{ $d.address }} +{{- if $d.address -}} +,服务地址为{{ $d.address }} {{- end -}} {{- if $d.ips -}} @@ -29,8 +29,8 @@ ,IP地址为{{ $d.ip_addr }} {{- end -}} -{{- if $d.account -}} -,初始帐号为{{ $d.account }} +{{- if $d.login_account -}} +,初始帐号为{{ $d.login_account }} {{- end -}} {{- if $d.password -}} @@ -56,7 +56,7 @@ {{- else -}} SSH {{- end -}} -或控制面板控制台访问虚拟机。 +或前往控制台访问虚拟机。 {{- end -}} {{- if has .resource_type $consoleServiceType -}} diff --git a/build/notify/root/opt/yunion/share/local-templates/content@en/CREATE.tmpl b/build/notify/root/opt/yunion/share/local-templates/content@en/CREATE.tmpl index 77fec21237..181b6483bd 100644 --- a/build/notify/root/opt/yunion/share/local-templates/content@en/CREATE.tmpl +++ b/build/notify/root/opt/yunion/share/local-templates/content@en/CREATE.tmpl @@ -9,8 +9,8 @@ Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type_display }} { , the internet address is {{ $d.public_dns }}:{{ $d.public_connect_port }} {{- end -}} -{{- if and $d.address_type $d.address -}} -, the service address is {{ $d.address_type }}{{ $d.address }} +{{- if $d.address -}} +, the service address is {{ $d.address }} {{- end -}} {{- if $d.ips -}} @@ -46,7 +46,7 @@ Your {{ if $d.brand -}} {{ $d.brand }} {{ end -}} {{ .resource_type_display }} { {{- end -}} {{- if eq .resource_type "server" -}} -, and please use {{ if $d.windows -}} Remote Desktop Connector (RDC) {{ else -}} SSH {{ end -}} or the control panel console to access the VM. +, and please use {{ if $d.windows -}} Remote Desktop Connector (RDC) {{ else -}} SSH {{ end -}} or go to the console to access the VM. {{- end -}} {{- if has .resource_type $consoleServiceType -}} diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 9c7d21d98d..a5e3d9f84e 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -925,7 +925,7 @@ func (self *SGuest) EventNotify(ctx context.Context, userCred mcclient.TokenCred } loginAccount := meta[api.VM_METADATA_LOGIN_ACCOUNT] if len(loginAccount) > 0 { - details.Add(jsonutils.NewString(loginAccount), "account") + details.Add(jsonutils.NewString(loginAccount), "login_account") } keypair := self.getKeypairName() if len(keypair) > 0 { diff --git a/pkg/notify/models/event_template.go b/pkg/notify/models/event_template.go index f86c3d146f..2f1d475374 100644 --- a/pkg/notify/models/event_template.go +++ b/pkg/notify/models/event_template.go @@ -429,6 +429,11 @@ func init() { "baremetal", "裸金属", }, + sI18nElme{ + api.TOPIC_RESOURCE_SNAPSHOTPOLICY, + "snapshot policy", + "快照策略", + }, sI18nElme{ string(api.ActionCreate), "created",