Skip to content

Commit

Permalink
add message
Browse files Browse the repository at this point in the history
  • Loading branch information
senk8 committed Feb 27, 2024
1 parent ceaae81 commit cc06917
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ test-notification:
-d '{"project_id":1001, "notification_id":1001}' \
$(CORE_API_ADDR) core.alert.AlertService.TestNotification

.PHONY: request-authz-notification
request-authz-notification:
$(GRPCURL) \
-plaintext \
-d '{"project_id":1001, "project_name": "projectA", "user_name":"userA"}' \
$(CORE_API_ADDR) core.alert.AlertService.RequestAuthzNotification

.PHONY: analyze-alert
analyze-alert:
$(GRPCURL) \
Expand Down
8 changes: 6 additions & 2 deletions pkg/server/alert/alert_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"time"

"github.com/ca-risken/core/pkg/model"
Expand Down Expand Up @@ -224,6 +225,9 @@ func (a *AlertService) RequestAuthzNotification(ctx context.Context, req *alert.
return nil, err
}
notifications, err := a.repository.ListNotification(ctx, req.ProjectId, "slack", 0, time.Now().Unix())
sort.Slice((*notifications), func(i, j int) bool {
return (*notifications)[i].NotificationID < (*notifications)[j].NotificationID
})
notification := &(*notifications)[0]
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand All @@ -233,9 +237,9 @@ func (a *AlertService) RequestAuthzNotification(ctx context.Context, req *alert.
}
switch notification.Type {
case "slack":
err = a.sendSlackRequestAuthzNotification(ctx, a.baseURL, notification.NotifySetting, a.defaultLocale, req.ProjectName, req.UserName)
err = a.sendSlackRequestAuthzNotification(ctx, a.baseURL, notification.NotifySetting, a.defaultLocale, req.UserName, req.ProjectName, req.ProjectId)
if err != nil {
a.logger.Errorf(ctx, "Error occured when sending test slack notification. err: %v", err)
a.logger.Errorf(ctx, "Error occured when sending request authz slack notification. err: %v", err)
return nil, err
}
default:
Expand Down
23 changes: 12 additions & 11 deletions pkg/server/alert/alert_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ const (
slackNotificationAttachmentEn = "Please check all %d Findings from <%s/alert/alert?project_id=%d&from=slack|Alert screen>."
slackNotificationTestMessageJa = "RISKENからのテスト通知です"
slackNotificationTestMessageEn = "This is a test notification from RISKEN"
slackRequestAuthzNotificationMessageJa = `%sさんが
あなたのプロジェクト%sへの権限を申請しました。
問題なければ次のリンクから%sさんを追加してください。`
slackRequestAuthzNotificationMessageEn = `%s %s %s`
slackRequestAuthzNotificationMessageJa = `@here %sさんが
あなたのプロジェクト%sへのアクセスをリクエストしました。
問題がなければ<%s/iam/user?project_id=%d&from=slack|ユーザー一覧>から%sさんを招待してください。`
slackRequestAuthzNotificationMessageEn = `@here %s has requested access to your Project %s.
If there are no issues, please check <%s/iam/user?project_id=%d&from=slack|the user list> and invite Person %s.`
)

func (a *AlertService) sendSlackNotification(
Expand Down Expand Up @@ -115,7 +116,7 @@ func (a *AlertService) sendSlackTestNotification(ctx context.Context, url, notif
return nil
}

func (a *AlertService) sendSlackRequestAuthzNotification(ctx context.Context, url, notifySetting, defaultLocale, userName, projectName string) error {
func (a *AlertService) sendSlackRequestAuthzNotification(ctx context.Context, url, notifySetting, defaultLocale, userName, projectName string, projectID uint32) error {
var setting slackNotifySetting
if err := json.Unmarshal([]byte(notifySetting), &setting); err != nil {
return err
Expand All @@ -132,13 +133,13 @@ func (a *AlertService) sendSlackRequestAuthzNotification(ctx context.Context, ur
}

if setting.WebhookURL != "" {
webhookMsg := getRequestAuthzWebhookMessage(setting.Data.Channel, locale, userName, projectName)
webhookMsg := getRequestAuthzWebhookMessage(setting.Data.Channel, locale, userName, projectName, url, projectID)
if err := slack.PostWebhook(setting.WebhookURL, webhookMsg); err != nil {
return fmt.Errorf("failed to send slack(webhookurl): %w", err)
}
} else if setting.ChannelID != "" {
if err := a.postMessageSlackWithRetry(ctx,
setting.ChannelID, slack.MsgOptionText(getRequestAuthzSlackMessageText(locale, userName, projectName), false)); err != nil {
setting.ChannelID, slack.MsgOptionText(getRequestAuthzSlackMessageText(locale, userName, projectName, url, projectID), false)); err != nil {
return fmt.Errorf("failed to send slack(postmessage): %w", err)
}
}
Expand Down Expand Up @@ -284,9 +285,9 @@ func getTestSlackMessageText(locale string) string {
return msgText
}

func getRequestAuthzWebhookMessage(channel, locale, userName, projectName string) *slack.WebhookMessage {
func getRequestAuthzWebhookMessage(channel, locale, projectName, userName, url string, projectID uint32) *slack.WebhookMessage {
msg := slack.WebhookMessage{
Text: getRequestAuthzSlackMessageText(locale, userName, projectName),
Text: getRequestAuthzSlackMessageText(locale, userName, projectName, url, projectID),
}
// override message
if channel != "" {
Expand All @@ -295,15 +296,15 @@ func getRequestAuthzWebhookMessage(channel, locale, userName, projectName string
return &msg
}

func getRequestAuthzSlackMessageText(locale, userName, projectName string) string {
func getRequestAuthzSlackMessageText(locale, projectName, userName, url string, projectID uint32) string {
var msgText string
switch locale {
case LocaleJa:
msgText = slackRequestAuthzNotificationMessageJa
default:
msgText = slackRequestAuthzNotificationMessageEn
}
return fmt.Sprintf(msgText, userName, projectName, userName)
return fmt.Sprintf(msgText, userName, projectName, url, projectID, userName)
}

func getColor(severity string) string {
Expand Down

0 comments on commit cc06917

Please sign in to comment.