/
client.go
227 lines (188 loc) · 6.28 KB
/
client.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
/*
Copyright 2015-2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"net/url"
"strings"
"text/template"
"time"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/logger"
"github.com/gravitational/trace"
)
var reviewReplyTemplate = template.Must(template.New("review reply").Parse(
`{{.Author}} reviewed the request at {{.Created.Format .TimeFormat}}.
Resolution: {{.ProposedStateEmoji}} {{.ProposedState}}.
{{if .Reason}}Reason: {{.Reason}}.{{end}}`,
))
// Client is a email client that works with access.Request
type Client struct {
clusterName string
mailer Mailer
webProxyURL *url.URL
}
// NewClient initializes the new Email message client
func NewClient(ctx context.Context, conf Config, clusterName, webProxyAddr string) (Client, error) {
var (
webProxyURL *url.URL
err error
mailer Mailer
)
if webProxyAddr != "" {
if webProxyURL, err = lib.AddrToURL(webProxyAddr); err != nil {
return Client{}, trace.Wrap(err)
}
}
if conf.Mailgun != nil {
mailer = NewMailgunMailer(*conf.Mailgun, conf.Delivery.Sender, clusterName)
logger.Get(ctx).WithField("domain", conf.Mailgun.Domain).Info("Using Mailgun as email transport")
}
if conf.SMTP != nil {
mailer = NewSMTPMailer(*conf.SMTP, conf.Delivery.Sender, clusterName)
logger.Get(ctx).WithFields(logger.Fields{
"host": conf.SMTP.Host,
"port": conf.SMTP.Port,
"username": conf.SMTP.Username,
}).Info("Using SMTP as email transport")
}
return Client{
mailer: mailer,
clusterName: clusterName,
webProxyURL: webProxyURL,
}, nil
}
// SendNewThreads sends emails on new requests. Returns EmailData.
func (c *Client) SendNewThreads(ctx context.Context, recipients []string, reqID string, reqData RequestData) ([]EmailThread, error) {
var threads []EmailThread
var errors []error
body := c.buildBody(reqID, reqData, "You have a new Role Request")
for _, email := range recipients {
id, err := c.mailer.Send(ctx, reqID, email, body, "")
if err != nil {
errors = append(errors, err)
continue
}
threads = append(threads, EmailThread{Email: email, Timestamp: time.Now().String(), MessageID: id})
}
return threads, trace.NewAggregate(errors...)
}
// SendReview sends new AccessReview message to the given threads
func (c *Client) SendReview(ctx context.Context, threads []EmailThread, reqID string, reqData RequestData, review types.AccessReview) ([]EmailThread, error) {
var proposedStateEmoji string
var threadsSent = make([]EmailThread, 0)
switch review.ProposedState {
case types.RequestState_APPROVED:
proposedStateEmoji = "✅"
case types.RequestState_DENIED:
proposedStateEmoji = "❌"
}
var builder strings.Builder
err := reviewReplyTemplate.Execute(&builder, struct {
types.AccessReview
ProposedState string
ProposedStateEmoji string
TimeFormat string
}{
review,
review.ProposedState.String(),
proposedStateEmoji,
time.RFC822,
})
if err != nil {
return threadsSent, trace.Wrap(err)
}
body := builder.String()
errors := make([]error, 0)
for _, thread := range threads {
_, err = c.mailer.Send(ctx, reqID, thread.Email, body, thread.MessageID)
if err != nil {
errors = append(errors, trace.Wrap(err))
continue
}
threadsSent = append(threadsSent, thread)
}
return threadsSent, trace.NewAggregate(errors...)
}
// SendResolution sends message on a request status update (review, decline)
func (c *Client) SendResolution(ctx context.Context, threads []EmailThread, reqID string, reqData RequestData) ([]EmailThread, error) {
var errors []error
var threadsSent = make([]EmailThread, 0)
body := c.buildBody(reqID, reqData, "Role Request has been resolved")
for _, thread := range threads {
_, err := c.mailer.Send(ctx, reqID, thread.Email, body, thread.MessageID)
if err != nil {
errors = append(errors, err)
continue
}
threadsSent = append(threads, thread)
}
return threadsSent, trace.NewAggregate(errors...)
}
// buildBody builds a email message for create/resolve events
func (c *Client) buildBody(reqID string, reqData RequestData, subject string) string {
var builder strings.Builder
builder.Grow(128)
builder.WriteString(fmt.Sprintf("%v:\n\n", subject))
resolution := reqData.Resolution
msgFieldToBuilder(&builder, "ID", reqID)
msgFieldToBuilder(&builder, "Cluster", c.clusterName)
if len(reqData.User) > 0 {
msgFieldToBuilder(&builder, "User", reqData.User)
}
if reqData.Roles != nil {
msgFieldToBuilder(&builder, "Role(s)", strings.Join(reqData.Roles, ","))
}
if reqData.RequestReason != "" {
msgFieldToBuilder(&builder, "Reason", reqData.RequestReason)
}
if c.webProxyURL != nil {
reqURL := *c.webProxyURL
reqURL.Path = lib.BuildURLPath("web", "requests", reqID)
msgFieldToBuilder(&builder, "Link", reqURL.String())
} else {
if resolution.Tag == Unresolved {
msgFieldToBuilder(&builder, "Approve", fmt.Sprintf("tsh request review --approve %s", reqID))
msgFieldToBuilder(&builder, "Deny", fmt.Sprintf("tsh request review --deny %s", reqID))
}
}
var statusEmoji string
status := string(resolution.Tag)
switch resolution.Tag {
case Unresolved:
status = "PENDING"
statusEmoji = "⏳"
case ResolvedApproved:
statusEmoji = "✅"
case ResolvedDenied:
statusEmoji = "❌"
case ResolvedExpired:
statusEmoji = "⌛"
}
statusText := fmt.Sprintf("Status: %s %s", statusEmoji, status)
if resolution.Reason != "" {
statusText += fmt.Sprintf(" (%s)", resolution.Reason)
}
builder.WriteString("\n")
builder.WriteString(statusText)
return builder.String()
}
// msgFieldToBuilder utility string builder method
func msgFieldToBuilder(b *strings.Builder, field, value string) {
b.WriteString(field)
b.WriteString(": ")
b.WriteString(value)
b.WriteString("\n")
}