-
Notifications
You must be signed in to change notification settings - Fork 134
/
processor.go
233 lines (203 loc) · 5.86 KB
/
processor.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
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/x509"
"encoding/pem"
"errors"
"log"
"sync"
"time"
"github.com/boltdb/bolt"
"golang.org/x/crypto/acme"
)
// processorLock ensures that Certificate reconciliation and Certificate
// event processing does not happen at the same time.
var processorLock = &sync.Mutex{}
func reconcileCertificates(interval int, db *bolt.DB, done chan struct{}, wg *sync.WaitGroup) {
go func() {
for {
select {
case <-time.After(time.Duration(interval) * time.Second):
err := syncCertificates(db)
if err != nil {
log.Println(err)
}
case <-done:
wg.Done()
log.Println("Stopped reconciliation loop.")
return
}
}
}()
}
func watchCertificateEvents(db *bolt.DB, done chan struct{}, wg *sync.WaitGroup) {
events, watchErrs := monitorCertificateEvents()
go func() {
for {
select {
case event := <-events:
err := processCertificateEvent(event, db)
if err != nil {
log.Println(err)
}
case err := <-watchErrs:
log.Println(err)
case <-done:
wg.Done()
log.Println("Stopped certificate event watcher.")
return
}
}
}()
}
func syncCertificates(db *bolt.DB) error {
processorLock.Lock()
defer processorLock.Unlock()
certificates, err := getCertificates()
if err != nil {
return err
}
var wg sync.WaitGroup
for _, cert := range certificates {
wg.Add(1)
go func(cert Certificate) {
defer wg.Done()
err := processCertificate(cert, db)
if err != nil {
log.Println(err)
}
}(cert)
}
wg.Wait()
return nil
}
func processCertificateEvent(c CertificateEvent, db *bolt.DB) error {
processorLock.Lock()
defer processorLock.Unlock()
switch {
case c.Type == "ADDED":
return processCertificate(c.Object, db)
case c.Type == "DELETED":
return deleteCertificate(c.Object, db)
}
return nil
}
func deleteCertificate(c Certificate, db *bolt.DB) error {
log.Printf("Deleting Let's Encrypt account: %s", c.Spec.Domain)
err := deleteAccount(c.Spec.Domain, db)
if err != nil {
return errors.New("Error deleting the Let's Encrypt account " + err.Error())
}
log.Printf("Deleting Kubernetes TLS secret: %s", c.Spec.Domain)
return deleteKubernetesSecret(c)
}
func processCertificate(c Certificate, db *bolt.DB) error {
account, err := findAccount(c.Spec.Domain, db)
if err != nil {
return err
}
if account == nil {
log.Printf("Creating new Let's Encrypt account: %s", c.Spec.Domain)
account, err = newAccount(c.Spec.Email, c.Spec.Domain)
if err != nil {
return err
}
}
acmeClient, err := newACMEClient(discoveryURL, account.AccountKey)
if err != nil {
return errors.New("Error creating ACME client: " + err.Error())
}
if account.Account.URI == "" {
account.Account.AgreedTerms = account.Account.CurrentTerms
registeredAccount, err := acmeClient.Register(account.Account)
if err != nil {
return errors.New("Error registering account: " + err.Error())
}
account.Account = registeredAccount
err = saveAccount(account, db)
if err != nil {
return errors.New("Error saving account" + err.Error())
}
}
if account.CertificateURL != "" {
cert, err := acmeClient.RenewCert(account.CertificateURL)
if err != nil {
return errors.New("Error renewing certificate" + err.Error())
}
account.Certificate = cert
key := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(account.CertificateKey),
})
err = syncKubernetesSecret(c, account.Certificate, key)
if err != nil {
return errors.New("Error creating Kubernetes secret: " + err.Error())
}
return nil
}
authorization, challenge, err := acmeClient.Authorize(c.Spec.Domain)
if err != nil {
return errors.New("Error authorizing account: " + err.Error())
}
jwkThumbprint, err := acme.JWKThumbprint(&account.AccountKey.PublicKey)
if err != nil {
return errors.New("Error generating the JWK thumbprint: " + err.Error())
}
fqdn, value, ttl := DNSChallengeRecord(c.Spec.Domain, challenge.Token, jwkThumbprint)
dnsExecClient := &dnsClient{
c.Spec.Domain,
c.Spec.Provider,
c.Spec.Secret,
c.Spec.SecretKey,
c.Metadata.Namespace,
}
// Cleaning up the DNS challenge here creates a race between two processes
// managing DNS challenge records.
dnsExecClient.deleteRecord(fqdn, value, ttl)
err = dnsExecClient.createRecord(fqdn, value, ttl)
if err != nil {
return err
}
// We need to make sure the DNS challenge record has propagated across the
// authoritative nameservers for the fqdn before accepting the ACME challenge.
if err := dnsExecClient.monitorDNSPropagation(fqdn, value, ttl); err != nil {
return err
}
if err := acmeClient.Accept(authorization, challenge); err != nil {
return err
}
cert, certURL, err := acmeClient.CreateCert(c.Spec.Domain, account.CertificateKey)
if err != nil {
return err
}
account.Certificate = cert
account.CertificateURL = certURL
err = saveAccount(account, db)
if err != nil {
return err
}
key := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(account.CertificateKey),
})
err = syncKubernetesSecret(c, account.Certificate, key)
if err != nil {
return errors.New("Error creating Kubernetes secret: " + err.Error())
}
err = dnsExecClient.deleteRecord(fqdn, value, ttl)
if err != nil {
return err
}
return nil
}