This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (124 loc) · 3.74 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
package main
import(
"os"
"log"
"fmt"
"strings"
"io/ioutil"
"encoding/json"
"github.com/nlopes/slack"
"github.com/jessevdk/go-flags"
"github.com/AckeeDevOps/vault-renovator/renovator"
)
type TokenFileNames struct {
Names []string `json:"fileNames"`
}
func main() {
//disable timestamp in the log output
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
log.Print("Running vault-renovator ...")
args := handleInput(os.Args[1:])
decryptor := getDecryptor(args)
tokens := getTokens(args)
tokensPlainText, err := decryptTokens(tokens, decryptor) // start here
if err != nil {
log.Fatal(err)
}
client := renovator.NewClient(args.VaultAddr)
statusList := []renovator.OutputRenewalStatus{}
for _, v := range tokensPlainText {
status := client.CheckOrRenew(v, args.ThresholdTTL, args.IncrementTTL)
statusList = append(statusList, status)
}
// notify slack if SlackWebhookUrl has been set
if(args.SlackWebhookUrl != ""){
notifySlackFinal(statusList, args.SlackWebhookUrl)
}
}
func notifySlackFinal(statusList []renovator.OutputRenewalStatus, url string) {
attachments := statusListToAttachments(statusList)
msg := slack.WebhookMessage{Text: "Vault token renewal status", Attachments: attachments}
err := slack.PostWebhook(url, &msg)
if err != nil {
log.Fatal(err)
}
}
func handleInput(args []string) *renovator.ProgramOptions {
opts := renovator.ProgramOptions{}
parser := flags.NewParser(&opts, (flags.HelpFlag | flags.PassDoubleDash))
_, err := parser.ParseArgs(args);
if err != nil {
log.Fatal(err)
}
return &opts
}
func handleSpecsFile(path string) []string {
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
fileNames := TokenFileNames{}
json.Unmarshal(byteValue, &fileNames) // returns empty TokenFileNames on error
return fileNames.Names
}
func getTokens(args *renovator.ProgramOptions) [][]byte {
fileNames := handleSpecsFile(args.SpecsPath)
provider := renovator.NewTokenProvider(args.Bucket, fileNames)
tokens, err := provider.GetTokens()
if err != nil {
log.Fatal(err) //stop here
}
return tokens
}
func getDecryptor(args *renovator.ProgramOptions) *renovator.Decryptor {
decryptor, err := renovator.NewDecryptor(args.ProjectID, args.Location, args.Keyring, args.Key)
if err != nil {
log.Fatal(err) //stop here
}
return decryptor
}
func decryptTokens(tokens [][]byte, decryptor *renovator.Decryptor) ([]string, error) {
results := []string{}
for _, v := range tokens {
decodedToken, err := decryptor.Decrypt(v)
if err != nil {
return nil, err
}
results = append(results, strings.TrimSpace(string(decodedToken[:])))
}
return results, nil
}
// transform vault client output to the Slack Attachments
func statusListToAttachments(list []renovator.OutputRenewalStatus) []slack.Attachment{
attachments := []slack.Attachment{}
for _, v := range list {
color := ""
switch v.StatusMessage {
case "RENEWAL_DONE": color = "#008000"
case "RENEWAL_NOT_NEEDED": color = "#008000"
case "RENEWAL_FAILED": color = "#FF0000"
default: color = "#808080"
}
msg := "display name: %s\n" +
"expire time: %s\n" +
"issue time: %s\n" +
"current TTL: %d\n" +
"current status: %s"
msg = fmt.Sprintf(
msg,
v.TokenDetails.DisplayName,
v.TokenDetails.ExpireTime,
v.TokenDetails.IssueTime,
v.TokenDetails.TTL,
v.StatusMessage,
)
attachment := slack.Attachment{Color: color, Title: v.TokenDetails.Accessor, Text: msg}
attachments = append(attachments, attachment)
}
return attachments
}