-
Notifications
You must be signed in to change notification settings - Fork 6
/
input_integration_email.go
299 lines (260 loc) · 8.58 KB
/
input_integration_email.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package primitive
import (
"context"
"errors"
"fmt"
"log/slog"
"net/smtp"
"net/textproto"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/turbot/flowpipe/internal/templates"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/perr"
"github.com/turbot/pipe-fittings/schema"
kitTypes "github.com/turbot/go-kit/types"
)
type InputIntegrationEmailMessage interface {
Message() (string, error)
}
type InputIntegrationEmail struct {
InputIntegrationBase
Host *string
Port *int64
SecurePort *int64
Tls *string
To []string
Cc []string
Bcc []string
From string
Subject string
User *string
Pass *string
FormUrl string
}
func NewInputIntegrationEmail(base InputIntegrationBase) InputIntegrationEmail {
return InputIntegrationEmail{
InputIntegrationBase: base,
}
}
func (ip *InputIntegrationEmail) ValidateInputIntegrationEmail(ctx context.Context, i modconfig.Input) error {
// Validate sender's information
if i[schema.AttributeTypeFrom] == nil {
return perr.BadRequestWithMessage("Email input must define from")
}
if i[schema.AttributeTypeSmtpPassword] == nil {
return perr.BadRequestWithMessage("Email input must define sender_credential")
}
if i[schema.AttributeTypeHost] == nil {
return perr.BadRequestWithMessage("Email input must define a SMTP host")
}
if i[schema.AttributeTypePort] == nil {
return perr.BadRequestWithMessage("Email input must define a port")
}
// Validate the port input
if i[schema.AttributeTypePort] != nil {
var port int64
switch data := i[schema.AttributeTypePort].(type) {
case float64:
port = int64(data)
case int64:
port = data
default:
return perr.BadRequestWithMessage("port must be a number")
}
portInString := strconv.FormatInt(port, 10)
match, err := regexp.MatchString("^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$", portInString)
if err != nil {
return perr.BadRequestWithMessage("error while validating the port")
}
if !match {
return perr.BadRequestWithMessage(fmt.Sprintf("%s is not a valid port", portInString))
}
}
if i[schema.AttributeTypeSenderName] != nil {
if _, ok := i[schema.AttributeTypeSenderName].(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'sender_name' must be a string")
}
}
// Validate the recipients
if i[schema.AttributeTypeTo] == nil {
return perr.BadRequestWithMessage("Email input must define to")
}
if _, ok := i[schema.AttributeTypeTo].([]string); !ok {
// The given input is a string slice, but the step input stores it as an interface slice during the JSON unmarshalling?
// Check if the input is an interface slice, and the elements are strings
if data, ok := i[schema.AttributeTypeTo].([]interface{}); ok {
for _, v := range data {
if _, ok := v.(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'to' must have elements of type string")
}
}
return nil
}
return perr.BadRequestWithMessage("Email attribute 'to' must be an array")
}
var recipients []string
if _, ok := i[schema.AttributeTypeTo].([]string); ok {
recipients = i[schema.AttributeTypeTo].([]string)
}
if _, ok := i[schema.AttributeTypeTo].([]interface{}); ok {
for _, v := range i[schema.AttributeTypeTo].([]interface{}) {
recipients = append(recipients, v.(string))
}
}
if len(recipients) == 0 {
return perr.BadRequestWithMessage("Recipients must not be empty")
}
// Validate the Cc recipients
if i[schema.AttributeTypeCc] != nil {
if _, ok := i[schema.AttributeTypeCc].([]string); !ok {
// The given input is a string slice, but the step input stores it as an interface slice during the JSON unmarshalling?
// Check if the input is an interface slice, and the elements are strings
if data, ok := i[schema.AttributeTypeCc].([]interface{}); ok {
for _, v := range data {
if _, ok := v.(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'cc' must have elements of type string")
}
}
return nil
}
return perr.BadRequestWithMessage("Email attribute 'cc' must be an array")
}
}
// Validate the Bcc recipients
if i[schema.AttributeTypeBcc] != nil {
if _, ok := i[schema.AttributeTypeBcc].([]string); !ok {
// The given input is a string slice, but the step input stores it as an interface slice during the JSON unmarshalling?
// Check if the input is an interface slice, and the elements are strings
if data, ok := i[schema.AttributeTypeBcc].([]interface{}); ok {
for _, v := range data {
if _, ok := v.(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'bcc' must have elements of type string")
}
}
return nil
}
return perr.BadRequestWithMessage("Email attribute 'bcc' must be an array")
}
}
// Validate the email body
if i[schema.AttributeTypeBody] != nil {
if _, ok := i[schema.AttributeTypeBody].(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'body' must be a string")
}
}
if i[schema.AttributeTypeContentType] != nil {
if _, ok := i[schema.AttributeTypeContentType].(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'content_type' must be a string")
}
}
// validate the subject
if i[schema.AttributeTypeSubject] != nil {
if _, ok := i[schema.AttributeTypeSubject].(string); !ok {
return perr.BadRequestWithMessage("Email attribute 'subject' must be a string")
}
}
return nil
}
func (ip *InputIntegrationEmail) PostMessage(ctx context.Context, mc MessageCreator, options []InputIntegrationResponseOption) (*modconfig.Output, error) {
var err error
host := kitTypes.SafeString(ip.Host)
tls := kitTypes.SafeString(ip.Tls)
var port int64
switch tls {
case "off":
if ip.Port != nil {
port = *ip.Port
} else {
port = 25 // default
}
case "required":
if ip.SecurePort != nil {
port = *ip.SecurePort
} else {
port = 587 // default
}
default: // default is auto, this should also be used if unset, defaults to port 587
if ip.SecurePort != nil {
port = *ip.SecurePort
} else if ip.Port != nil {
port = *ip.Port
} else {
port = 587
}
}
addr := fmt.Sprintf("%s:%d", host, port)
auth := smtp.PlainAuth("", kitTypes.SafeString(ip.User), kitTypes.SafeString(ip.Pass), host)
output := modconfig.Output{
Data: map[string]interface{}{},
}
message, err := mc.EmailMessage(ip, options)
if err != nil {
return nil, perr.InternalWithMessage(fmt.Sprintf("unable to create email message: %s", err.Error()))
}
recipients := ip.To
if len(ip.Cc) > 0 {
recipients = append(recipients, ip.Cc...)
}
if len(ip.Bcc) > 0 {
recipients = append(recipients, ip.Bcc...)
}
start := time.Now().UTC()
err = smtp.SendMail(addr, auth, ip.From, recipients, []byte(message))
finish := time.Now().UTC()
output.Flowpipe = FlowpipeMetadataOutput(start, finish)
if err != nil {
var smtpError *textproto.Error
if !errors.As(err, &smtpError) {
return nil, perr.InternalWithMessage(fmt.Sprintf("unable to send email: %s", err.Error()))
}
switch {
case smtpError.Code >= 400 && smtpError.Code <= 499:
return &output, perr.BadRequestWithMessage(fmt.Sprintf("unable to send email: %d %s", smtpError.Code, smtpError.Msg))
case smtpError.Code >= 500 && smtpError.Code <= 599:
return &output, perr.ServiceUnavailableWithMessage(fmt.Sprintf("unable to send email: %d %s", smtpError.Code, smtpError.Msg))
default:
return &output, perr.InternalWithMessage(fmt.Sprintf("unable to send email: %d %s", smtpError.Code, smtpError.Msg))
}
}
return &output, nil
}
func parseEmailInputTemplate(templateFileName string, data any) (string, error) {
funcs := template.FuncMap{
"mod": func(a, b int) int { return a % b },
"sub": func(a, b int) int { return a - b },
"getColor": func(s *string) string {
if s != nil {
switch *s {
case "ok":
return "#379634"
case "alert":
return "#b32128"
default:
return "#036"
}
}
return "#036"
},
}
templateFile, err := templates.HTMLTemplate(templateFileName)
if err != nil {
return "", perr.InternalWithMessage("error while reading the email template")
}
tmpl, err := template.New("email").Funcs(funcs).Parse(string(templateFile))
if err != nil {
slog.Error("error while parsing the email template", "error", err)
return "", err
}
var body strings.Builder
err = tmpl.Execute(&body, data)
if err != nil {
slog.Error("error while executing the email template", "error", err)
return "", perr.BadRequestWithMessage("error while executing the email template")
}
tempMessage := body.String()
return tempMessage, nil
}