-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
318 lines (299 loc) · 9.18 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/url"
"os"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/enescakir/emoji"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/hcl/v2/hclsimple"
vault "github.com/hashicorp/vault/api"
"github.com/kevinburke/twilio-go"
"github.com/matrix-org/gomatrix"
"github.com/slack-go/slack"
)
type Config struct {
Vault VaultConfig `hcl:"vault,block"`
Defaults DefaultConfig `hcl:"defaults,block"`
SlackTargets []SlackTargetConfig `hcl:"slack_message,block"`
SMSTargets []SMSTargetConfig `hcl:"sms,block"`
MatrixTargets []MatrixTargetConfig `hcl:"matrix_message,block"`
DiscordTargets []DiscordTargetConfig `hcl:"discord_message,block"`
}
func (c Config) Validate() error {
if c.Vault.MountPath == "" {
return errors.New("vault.mount_path must be set")
}
if c.Defaults.Message == "" {
for _, smsTarget := range c.SMSTargets {
if smsTarget.Message == "" {
return fmt.Errorf("sms.%q.message must be set because defaults.message is not set.", smsTarget.Recipient)
}
}
for _, slackTarget := range c.SlackTargets {
if slackTarget.Message == "" {
return fmt.Errorf("slack_message.%q.message must be set because defaults.message is not set.", slackTarget.Recipient)
}
}
for _, matrixTarget := range c.MatrixTargets {
if matrixTarget.Message == "" {
return fmt.Errorf("matrix_message.%q.message must be set because defaults.message is not set.", matrixTarget.Recipient)
}
}
for _, discordTarget := range c.SlackTargets {
if discordTarget.Message == "" {
return fmt.Errorf("discord_message.%q.message must be set because defaults.message is not set.", discordTarget.Recipient)
}
}
}
return nil
}
type VaultConfig struct {
Address string `hcl:"address,optional"`
MountPath string `hcl:"mount_path,attr"`
}
func (v VaultConfig) getMountPath() string {
return strings.TrimPrefix(strings.TrimSuffix(v.MountPath, "/"), "/")
}
type DefaultConfig struct {
Message string `hcl:"message,optional"`
}
type SlackTargetConfig struct {
Recipient string `hcl:"recipient,label"`
ChannelID string `hcl:"channel_id,attr"`
Message string `hcl:"message,optional"`
}
type SMSTargetConfig struct {
Recipient string `hcl:"recipient,label"`
Number string `hcl:"number,attr"`
Message string `hcl:"message,optional"`
}
type MatrixTargetConfig struct {
Recipient string `hcl:"recipient,label"`
RoomID string `hcl:"room_id,attr"`
Message string `hcl:"message,optional"`
}
type DiscordTargetConfig struct {
Recipient string `hcl:"recipient,label"`
ChannelID string `hcl:"channel_id,attr"`
Message string `hcl:"message,optional"`
}
func main() {
var config Config
flag.Parse()
if flag.Arg(0) == "" {
log.Println("Usage: ./babybutton /path/to/config.hcl")
os.Exit(1)
}
err := hclsimple.DecodeFile(flag.Arg(0), nil, &config)
if err != nil {
log.Printf("Error parsing config file %s: %s", flag.Arg(0), err)
os.Exit(1)
}
err = config.Validate()
if err != nil {
log.Printf("Invalid config file %s: %s", flag.Arg(0), err)
os.Exit(1)
}
vaultClient, err := vault.NewClient(&vault.Config{
Address: config.Vault.Address,
})
if err != nil {
log.Println("Error creating vault client:", err)
os.Exit(1)
}
vaultToken := os.Getenv("VAULT_TOKEN")
if vaultToken == "" {
log.Println("VAULT_TOKEN must be set")
os.Exit(1)
}
vaultClient.SetToken(vaultToken)
if len(config.SlackTargets) > 0 {
sendSlackMessages(vaultClient, config)
}
if len(config.SMSTargets) > 0 {
sendTextMessages(vaultClient, config)
}
if len(config.MatrixTargets) > 0 {
sendMatrixMessages(vaultClient, config)
}
if len(config.DiscordTargets) > 0 {
sendDiscordMessages(vaultClient, config)
}
}
func getVaultString(vals map[string]interface{}, key, source string) (string, error) {
dataI, ok := vals["data"]
if !ok {
return "", fmt.Errorf("No data key in response from Vault for %s", source)
}
data, ok := dataI.(map[string]interface{})
if !ok {
return "", fmt.Errorf("Data key in response from Vault for %s wasn't map[string]interface{}, it was %T", source, dataI)
}
iface, ok := data[key]
if !ok {
return "", fmt.Errorf("No %s data set in %s in vault", key, source)
}
val, ok := iface.(string)
if !ok {
return "", fmt.Errorf("%s data set in %s in vault wasn't string, was %T", key, source, iface)
}
return val, nil
}
func sendSlackMessages(vaultClient *vault.Client, config Config) {
secrets, err := vaultClient.Logical().Read(config.Vault.getMountPath() + "/data/slack")
if err != nil {
log.Println("Error retrieving Slack credentials from Vault:", err)
return
}
authToken, err := getVaultString(secrets.Data, "auth_token", config.Vault.getMountPath()+"/slack")
if err != nil {
log.Println(err)
return
}
client := slack.New(authToken)
for _, target := range config.SlackTargets {
msg := config.Defaults.Message
if target.Message != "" {
msg = target.Message
}
_, _, err := client.PostMessageContext(context.Background(), target.ChannelID,
slack.MsgOptionText(emoji.Parse(msg), false),
slack.MsgOptionAsUser(true), slack.MsgOptionParse(true))
if err != nil {
log.Printf("Error sending Slack message to %s: %s", target.Recipient, err)
continue
}
log.Println("Sent slack message to", target.Recipient+".")
}
}
func sendTextMessages(vaultClient *vault.Client, config Config) {
secrets, err := vaultClient.Logical().Read(config.Vault.getMountPath() + "/data/twilio")
if err != nil {
log.Println("Error retrieving Twilio credentials from Vault:", err)
return
}
accountSID, err := getVaultString(secrets.Data, "account_sid", config.Vault.getMountPath()+"/twilio")
if err != nil {
log.Println(err)
return
}
authToken, err := getVaultString(secrets.Data, "auth_token", config.Vault.getMountPath()+"/twilio")
if err != nil {
log.Println(err)
return
}
fromNumber, err := getVaultString(secrets.Data, "number", config.Vault.getMountPath()+"/twilio")
if err != nil {
log.Println(err)
return
}
client := twilio.NewClient(accountSID, authToken, cleanhttp.DefaultPooledClient())
for _, target := range config.SMSTargets {
values := url.Values{}
values.Set("From", fromNumber)
values.Set("To", target.Number)
if target.Message != "" {
values.Set("Body", emoji.Parse(target.Message))
} else {
values.Set("Body", emoji.Parse(config.Defaults.Message))
}
_, err := client.Messages.Create(context.Background(), values)
if err != nil {
log.Printf("Error sending text message to %s: %s", target.Recipient, err)
continue
}
log.Println("Sent text message to", target.Recipient+".")
}
}
func sendMatrixMessages(vaultClient *vault.Client, config Config) {
secrets, err := vaultClient.Logical().Read(config.Vault.getMountPath() + "/data/matrix")
if err != nil {
log.Println("Error retrieving Matrix credentials from Vault:", err)
return
}
homeserverURL, err := getVaultString(secrets.Data, "homeserver_url", config.Vault.getMountPath()+"/matrix")
if err != nil {
log.Println(err)
return
}
userID, err := getVaultString(secrets.Data, "user_id", config.Vault.getMountPath()+"/matrix")
if err != nil {
log.Println(err)
return
}
accessToken, err := getVaultString(secrets.Data, "access_token", config.Vault.getMountPath()+"/matrix")
if err != nil {
log.Println(err)
return
}
client, err := gomatrix.NewClient(homeserverURL, userID, accessToken)
if err != nil {
log.Printf("Error making matrix client: %s", err)
return
}
joinedRooms := map[string]struct{}{}
jrResp, err := client.JoinedRooms()
if err != nil {
log.Printf("Error looking up joined rooms: %v", err)
return
}
for _, room := range jrResp.JoinedRooms {
joinedRooms[room] = struct{}{}
}
log.Printf("Joined rooms: %v", jrResp.JoinedRooms)
for _, target := range config.MatrixTargets {
if _, ok := joinedRooms[target.RoomID]; !ok {
log.Printf("Joining %s", target.RoomID)
_, err = client.JoinRoom(target.RoomID, "", nil)
if err != nil {
log.Printf("Error joining %s: %s", target.RoomID, err)
continue
}
}
msg := config.Defaults.Message
if target.Message != "" {
msg = target.Message
}
_, err := client.SendText(target.RoomID, emoji.Parse(msg))
if err != nil {
log.Printf("Error sending text message to %s: %s", target.Recipient, err)
continue
}
log.Println("Sent matrix message to", target.Recipient+".")
}
}
func sendDiscordMessages(vaultClient *vault.Client, config Config) {
secrets, err := vaultClient.Logical().Read(config.Vault.getMountPath() + "/data/discord")
if err != nil {
log.Println("Error retrieving Discord credentials from Vault:", err)
return
}
token, err := getVaultString(secrets.Data, "token", config.Vault.getMountPath()+"/discord")
if err != nil {
log.Println(err)
return
}
session, err := discordgo.New("Bot " + token)
if err != nil {
log.Println("Error creating discord client:", err)
return
}
for _, target := range config.DiscordTargets {
msg := config.Defaults.Message
if target.Message != "" {
msg = target.Message
}
_, err := session.ChannelMessageSend(target.ChannelID, emoji.Parse(msg))
if err != nil {
log.Printf("Error sending discord message to %s: %s", target.Recipient, err)
continue
}
log.Println("Sent discord message to", target.Recipient+".")
}
}