Skip to content

Commit

Permalink
send result as attachment in email
Browse files Browse the repository at this point in the history
  • Loading branch information
gnmahanth committed Jul 1, 2024
1 parent eb0a5b9 commit 2a37d06
Showing 1 changed file with 20 additions and 52 deletions.
72 changes: 20 additions & 52 deletions deepfence_server/pkg/integration/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"

"github.com/deepfence/ThreatMapper/deepfence_server/model"
Expand Down Expand Up @@ -35,69 +33,39 @@ func New(ctx context.Context, b []byte) (*Email, error) {
return &h, nil
}

func (e Email) FormatMessage(message []map[string]interface{}) string {
var entiremsg strings.Builder
entiremsg.WriteString("*")
entiremsg.WriteString(e.Resource)
entiremsg.WriteString("*\n\n")

// Prepare the sorted keys so that the output has the records in same order
var keys []string
if len(message) > 0 {
keys = make([]string, 0, len(message[0]))
for key := range message[0] {
keys = append(keys, key)
}

sort.Strings(keys)
func (e Email) FormatMessage(message []map[string]interface{},

Check failure on line 36 in deepfence_server/pkg/integration/email/email.go

View workflow job for this annotation

GitHub Actions / lint-server

ST1016: methods on the same type should have the same receiver name (seen 1x "a", 5x "e") (stylecheck)
extras map[string]interface{}) (string, map[string][]byte) {

var msg strings.Builder
var attachments map[string][]byte = map[string][]byte{}

Check failure on line 40 in deepfence_server/pkg/integration/email/email.go

View workflow job for this annotation

GitHub Actions / lint-server

ST1023: should omit type map[string][]byte from declaration; it will be inferred from the right-hand side (stylecheck)

for k, v := range extras {
msg.WriteString(fmt.Sprintf("%s: %v", k, v))
}

for k, v := range message {
entiremsg.WriteString("#")
entiremsg.WriteString(strconv.Itoa(k + 1))
entiremsg.WriteString("\n")
for _, key := range keys {
if val, ok := v[key]; ok {
fmtVal := ""
if val != nil {
fmtVal = fmt.Sprintf("%v", val)
}
entiremsg.WriteString(key)
entiremsg.WriteString(": ")
entiremsg.WriteString(fmtVal)
entiremsg.WriteString("\n")
delete(v, key)
}
}

// This is to handle if we have unprocessed data in the map
// Possilbe if all the records are not uniform
for key, val := range v {
fmtVal := ""
if val != nil {
fmtVal = fmt.Sprintf("%v", val)
}
entiremsg.WriteString(key)
entiremsg.WriteString(": ")
entiremsg.WriteString(fmtVal)
entiremsg.WriteString("\n")
}
entiremsg.WriteString("\n")
r, err := json.Marshal(message)
if err != nil {
log.Error().Err(err).Msg("failed to marshal results")
}
return entiremsg.String()

attachments["scan-results.json"] = r

return msg.String(), attachments
}

func (e Email) SendNotification(ctx context.Context, message []map[string]interface{}, extras map[string]interface{}) error {
func (e Email) SendNotification(ctx context.Context,
message []map[string]interface{}, extras map[string]interface{}) error {

_, span := telemetry.NewSpan(ctx, "integrations", "email-send-notification")
defer span.End()

m := e.FormatMessage(message)
m, a := e.FormatMessage(message, extras)
emailSender, err := sendemail.NewEmailSender(ctx)
if err != nil {
return err
}
return emailSender.Send([]string{e.Config.EmailID}, "Deepfence Subscription", m, "", nil)
return emailSender.Send([]string{e.Config.EmailID},
fmt.Sprintf("Deepfence %s Subscription", e.Resource), m, "", a)
}

func (e Email) IsEmailConfigured(ctx context.Context) bool {
Expand Down

0 comments on commit 2a37d06

Please sign in to comment.