-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_email_user.go
134 lines (111 loc) · 3.76 KB
/
notification_email_user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"fmt"
"net/mail"
"text/template"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func sendEmailUserNotifications(images *map[string]ImageData, ctx *cli.Context) {
//check that --sendEmail flag is send and user want to send emails. If not return early.
if !ctx.Bool(sendEmailUserFlag.Name) {
return
}
var err error
// check sender is a valid email address
from := ctx.String(smtpSenderAddressFlag.Name)
_, err = mail.ParseAddress(from)
if err != nil {
//log error
log.Errorf("Value of Sender Email Adress '%s' is not a valid email address", from)
//exit programm
cli.Exit("Value of Sender Email Adress is not a valid email address", 1)
}
//reorganize result data to loop over and send emails
result := generateNotificationDataModel(images)
for recipient, outdatedImages := range result.Notifications {
// Check if valid recipient email address
_, err = mail.ParseAddress(recipient)
if err != nil {
//skip this address
//log error
log.Errorf("Value from Namespace Annotation for contact email address '%s' is not a valid email address", recipient)
//exit programm
cli.Exit("Value from Namespace Annotation for contact email address is not a valid email address", 1)
}
// build email content
emailBodyContent := templateUserEmailBodyContent(outdatedImages, ctx.String(emailUserContentPrefixFilePathFlag.Name), ctx.String(emailUserContentSuffixFilePathFlag.Name))
request := Mail{
Sender: from,
To: []string{recipient},
Subject: fmt.Sprintf("Outdated container images older than %s in use", ctx.String(ageFlag.Name)),
Body: emailBodyContent.Bytes(),
}
msg := buildMessage(request)
// send email
err = sendEmail(
ctx.String(smtpUsernameFlag.Name),
ctx.String(smtpPasswordFlag.Name),
ctx.String(smtpServerAddressFlag.Name),
&request,
msg)
if err != nil {
log.Error("Error sending User report via email", err)
//cli.Exit("Error sending User report via email", 1)
} else {
log.Debugf("Successful sent User email to '%s' via '%s'", request.To[0], ctx.String(smtpServerAddressFlag.Name))
}
}
}
func templateUserEmailBodyContent(outdatedImages ResultGroupedByEmailOutdatedImages, prefixContentFlagValue string, suffixContentFlagValue string) bytes.Buffer {
tmpl := template.Must(template.New("emailUserNotificationTemplate").Parse(emailUserNotificationTemplate))
var emailBodyContent bytes.Buffer
emailBodyContent.WriteString("\n")
emailBodyContent.WriteString("<html>\n")
emailBodyContent.WriteString(emailHTMLHeader)
emailBodyContent.WriteString("<body>\n")
//write prefix content
addEmailContentOrDefault(prefixContentFlagValue, emailUserDefaultPrefixContent, &emailBodyContent)
//write result in html table
if err := tmpl.Execute(&emailBodyContent, outdatedImages); err != nil {
log.Error(err.Error())
cli.Exit("Error during building notification user email content", 1)
}
//write suffix content
addEmailContentOrDefault(suffixContentFlagValue, emailUserDefaultSuffixContent, &emailBodyContent)
emailBodyContent.WriteString("</body>\n")
emailBodyContent.WriteString("</html>\n")
return emailBodyContent
}
var emailUserNotificationTemplate = `<table>
<tr>
<th>Image</th>
<th>BuildTimestamp</th>
<th>Namespace</th>
<th>PodName</th>
</tr>
{{ range $image, $resultContentData := .Images}}
{{ range $resultContentData.Findings }}
<tr>
<td>{{ $image }}</td>
<td>{{ $resultContentData.BuildTimestamp }}</td>
<td>{{ .Namespace }}</td>
<td>{{ .PodName }}</td>
</tr>
{{ end }}
{{ end }}
</table>
`
var emailUserDefaultPrefixContent = `<p>
The following container images are outdated.
</p>
<p>
</p>
`
var emailUserDefaultSuffixContent = `<p>
</p>
<p>
Please update or rebuild your application immediatly.
</p>
`