-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
217 lines (184 loc) · 5.29 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
package main
import (
"context"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
const liveDir = "/etc/letsencrypt/live"
type config struct {
AcmeServer string
AlternativeNames string
CertName string
CommonName string
DNSWaitSeconds int
GCSBucket string
GCSPrefix string
GoogleCredentialsFile string
GoogleProject string
RenewalDays int
}
func main() {
if err := mainCmd(); err != nil {
log.Fatalf("certifier: %v", err)
}
}
func mainCmd() error {
var cfg config
flag.StringVar(&cfg.AcmeServer, "acme-server", "https://acme-v02.api.letsencrypt.org/directory", "")
flag.StringVar(&cfg.AlternativeNames, "alternative-names", "", "")
flag.StringVar(&cfg.CertName, "cert-name", "", "")
flag.StringVar(&cfg.CommonName, "common-name", "", "")
flag.IntVar(&cfg.DNSWaitSeconds, "dns-wait-seconds", 240, "")
flag.StringVar(&cfg.GCSBucket, "gcs-bucket", "", "")
flag.StringVar(&cfg.GCSPrefix, "gcs-prefix", "", "")
flag.IntVar(&cfg.RenewalDays, "renewal-days", 15, "")
flag.StringVar(&cfg.GoogleProject, "gcp-project-name", "", "")
flag.Parse()
googleCredentialsFile, found := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
if !found {
return errors.New("GOOGLE_APPLICATION_CREDENTIALS not set")
}
cfg.GoogleCredentialsFile = googleCredentialsFile
files := []string{
filepath.Join(cfg.CertName, "cert.pem"),
filepath.Join(cfg.CertName, "chain.pem"),
filepath.Join(cfg.CertName, "fullchain.pem"),
filepath.Join(cfg.CertName, "privkey.pem"),
}
gcs := gcs{
bucket: cfg.GCSBucket,
prefix: cfg.GCSPrefix,
}
live := live{
liveDir: liveDir,
}
err := gcs.test(context.Background())
if err != nil {
log.Printf("Error from GCS test: %v", err)
return err
}
// Find existing certificate (if any) and determine if it needs to be renewed.
renew := func() bool {
certPath := filepath.Join(cfg.CertName, "cert.pem")
body, err := gcs.load(context.Background(), certPath)
if err != nil {
log.Printf("Failed to load certificate from GCS")
return true
}
renew, err := shouldRenew(body, cfg.RenewalDays)
if err != nil {
log.Printf("Failed to parse certificate data")
return true
}
return renew
}()
if !renew {
log.Printf("Not renewing certificate")
return nil
}
log.Printf("Renewing certificate")
// Run certbot to recreate our certs
cmd := buildCertbotCommand(cfg)
if err := cmd.Run(); err != nil {
catCertbotLog()
return err
}
catCertbotLog()
// Copy all generated files to GCS
var privkey, fullchain []byte
for _, file := range files {
log.Printf("Loading %q from disk", file)
data, err := live.load(file)
if err != nil {
return err
}
log.Printf("Loaded %q from disk", file)
log.Printf("Saving %q to GCS", file)
if err := gcs.save(context.Background(), file, data); err != nil {
return err
}
log.Printf("Saved %q to GCS", file)
if strings.Contains(file, "privkey.pem") {
privkey = data
}
if strings.Contains(file, "fullchain.pem") {
fullchain = data
}
}
file := filepath.Join(cfg.CertName, "privkey-plus-fullchain.pem")
log.Printf("Saving %q to GCS", file)
if err := gcs.save(context.Background(), file, append(privkey, fullchain...)); err != nil {
return err
}
log.Printf("Saved %q to GCS", file)
return nil
}
func buildCertbotCommand(cfg config) *exec.Cmd {
args := []string{
"certonly",
"--agree-tos",
"--break-my-certs",
"--cert-name", cfg.CertName,
"--dns-google",
"--dns-google-credentials", cfg.GoogleCredentialsFile,
"--dns-google-propagation-seconds", strconv.Itoa(cfg.DNSWaitSeconds),
"--domain", cfg.CommonName,
"--email", "[email protected]",
"--force-renewal",
"--non-interactive",
"--preferred-challenges", "dns",
"--server", cfg.AcmeServer,
}
if len(cfg.AlternativeNames) != 0 {
args = append(args, "--domains", cfg.AlternativeNames)
}
if cfg.GoogleProject != "" {
args = append(args, "--dns-google-project", cfg.GoogleProject)
}
cmd := exec.Command("certbot", args...)
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func catCertbotLog() {
cmd := exec.Command("cat", "/var/log/letsencrypt/letsencrypt.log")
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Println(">>> Certbot Log <<<")
if err := cmd.Run(); err != nil {
log.Printf("Error dumping log: %v\n", err)
}
log.Println(">>> End Certbot Log <<<")
}
// Check if the current cert is close enough to its expiration date to warrant renewal.
func shouldRenew(certBytes []byte, days int) (bool, error) {
block, _ := pem.Decode(certBytes)
if block == nil {
return false, fmt.Errorf("failed to parse pem file")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return false, err
}
log.Printf("Parsed cert for %+v (expires on %v)\n", cert.Subject, cert.NotAfter)
timeRemaining := time.Until(cert.NotAfter)
timeGrace := time.Duration(days*24) * time.Hour
if timeRemaining <= timeGrace {
log.Printf("Renewing certificate since time remaining (%v) is less than the grace period (%v)\n", timeRemaining, timeGrace)
return true, nil
}
log.Printf("Not renewing certificate since time remaining (%v) is greater than the grace period (%v)\n", timeRemaining, timeGrace)
return false, nil
}