forked from janeczku/rancher-letsencrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
141 lines (114 loc) · 3.85 KB
/
manager.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
package main
import (
"os"
"strings"
"time"
"github.com/Sirupsen/logrus"
)
const (
RENEW_BEFORE_DAYS = 14
)
func (c *Context) Run() {
c.startup()
for {
<-c.timer()
c.renew()
}
}
func (c *Context) startup() {
var storedLocally, storedInRancher bool
ok, acmeCert := c.Acme.GetStoredCertificate(c.CertificateName, c.Domains)
if ok {
storedLocally = true
c.ExpiryDate = acmeCert.ExpiryDate
logrus.Infof("Found locally stored certificate '%s'", c.CertificateName)
}
rancherCert, err := c.Rancher.FindCertByName(c.CertificateName)
if err != nil {
logrus.Fatalf("Could not lookup certificate in Rancher API: %v", err)
}
if rancherCert != nil {
storedInRancher = true
c.RancherCertId = rancherCert.Id
logrus.Infof("Found existing certificate '%s' in Rancher", c.CertificateName)
}
if storedLocally && storedInRancher {
if rancherCert.SerialNumber == acmeCert.SerialNumber {
logrus.Infof("Managing renewal of certificate '%s'", c.CertificateName)
return
}
logrus.Infof("Serial number mismatch between Rancher and local certificate '%s'", c.CertificateName)
c.updateRancherCert(acmeCert.PrivateKey, acmeCert.Certificate)
return
}
if storedLocally && !storedInRancher {
logrus.Debugf("Adding certificate '%s' to Rancher", c.CertificateName)
c.addRancherCert(acmeCert.PrivateKey, acmeCert.Certificate)
return
}
logrus.Infof("Trying to obtain SSL certificate (%s) from Let's Encrypt CA", strings.Join(c.Domains, ","))
acmeCert, failures := c.Acme.Issue(c.CertificateName, c.Domains)
if len(failures) > 0 {
for k, v := range failures {
logrus.Errorf("[%s] Error obtaining certificate: %s", k, v.Error())
}
os.Exit(1)
}
logrus.Infof("Certificate obtained successfully")
c.ExpiryDate = acmeCert.ExpiryDate
if storedInRancher {
logrus.Debugf("Overwriting Rancher certificate '%s'", c.CertificateName)
c.updateRancherCert(acmeCert.PrivateKey, acmeCert.Certificate)
return
}
c.addRancherCert(acmeCert.PrivateKey, acmeCert.Certificate)
}
func (c *Context) addRancherCert(privateKey, cert []byte) {
rancherCert, err := c.Rancher.AddCertificate(c.CertificateName, CERT_DESCRIPTION, privateKey, cert)
if err != nil {
logrus.Fatalf("Failed to add Rancher certificate '%s': %v", c.CertificateName, err)
}
c.RancherCertId = rancherCert.Id
logrus.Infof("Certificate '%s' added to Rancher", c.CertificateName)
}
func (c *Context) updateRancherCert(privateKey, cert []byte) {
err := c.Rancher.UpdateCertificate(c.RancherCertId, CERT_DESCRIPTION, privateKey, cert)
if err != nil {
logrus.Fatalf("Failed to update Rancher certificate '%s': %v", c.CertificateName, err)
}
logrus.Infof("Updated Rancher certificate '%s'", c.CertificateName)
err = c.Rancher.UpgradeLoadBalancers(c.RancherCertId)
if err != nil {
logrus.Fatalf("Failed to upgrade load balancers: %v", err)
}
}
func (c *Context) renew() {
logrus.Infof("Trying to obtain renewed SSL certificate (%s) from Let's Encrypt CA", strings.Join(c.Domains, ","))
acmeCert, err := c.Acme.Renew(c.CertificateName)
if err != nil {
logrus.Fatalf("Failed to renew certificate: %v", err)
}
logrus.Infof("Certificate renewed successfully")
c.ExpiryDate = acmeCert.ExpiryDate
c.updateRancherCert(acmeCert.PrivateKey, acmeCert.Certificate)
}
func (c *Context) timer() <-chan time.Time {
now := time.Now().UTC()
next := c.getRenewalDate()
left := next.Sub(now)
if left <= 0 {
left = 10 * time.Second
}
logrus.Infof("Certificate renewal scheduled for %s", next.Format("2006/01/02 15:04 MST"))
// Debug option forces renewal
if c.Debug {
logrus.Debug("Debug mode: Forced certificate renewal in 120 seconds")
left = 120 * time.Second
}
return time.After(left)
}
func (c *Context) getRenewalDate() time.Time {
date := c.ExpiryDate.AddDate(0, 0, -RENEW_BEFORE_DAYS)
dYear, dMonth, dDay := date.Date()
return time.Date(dYear, dMonth, dDay, c.RenewalTime, 0, 0, 0, time.UTC)
}