-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (62 loc) · 1.79 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/AckeeDevOps/renovator/client"
"github.com/AckeeDevOps/renovator/config"
"github.com/AckeeDevOps/renovator/notifier"
)
func main() {
log.Println("Renovator started ...")
// create configuration struct
cfg, err := config.Get()
if err != nil {
log.Fatalf("could not create configuration struct: %s", err)
}
// show configuration if needed
if cfg.Debug {
log.Println(cfg)
}
// create Valt client
vaultClient, err := client.NewClient(cfg.VaultAddress, cfg.Insecure, cfg.Debug)
if err != nil {
log.Fatalf("could not initialize Vault client: %s", err)
}
// create registry
registry := notifier.NewRegistry()
// read tokens from JSON
var tokenConfig TokenConfig
jsonFile, err := os.Open(cfg.ConfigFilePath)
if err != nil {
log.Fatalf("could not open config file: %s", err)
}
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatalf("could not read config file: %s", err)
}
err = json.Unmarshal(byteValue, &tokenConfig)
if err != nil {
log.Fatalf("could not unmarshal config file: %s", err)
}
// go through all tokens
for _, t := range tokenConfig.Tokens {
status, err := vaultClient.CheckOrRenew(t.Token, cfg.TTLThreshold, cfg.TTLIncrement)
if err != nil {
message := fmt.Sprintf("Could not renew; display name: %s; error: %s", t.Name, err.Error())
registry.AddStatus(t.Token, false, message)
continue
}
// renewed token
days := status.TTL / 60 / 60 / 24
message := fmt.Sprintf("new/current TTL is %d (%d days); display name: %s", status.TTL, days, t.Name)
registry.AddStatus(t.Token, true, message)
}
err = notifier.NotifySlack(registry, cfg.SlackWebhookURL)
if err != nil {
log.Fatalf("could not send Slack message: %s", err)
}
log.Println("done.")
}