-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathprocessor.go
822 lines (750 loc) · 24.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// Copyright 2016 Google Inc. All Rights Reserved.
// Copyright 2016 Palm Stone Games, 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/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"log"
"sort"
"strings"
"sync"
"time"
"github.com/boltdb/bolt"
"github.com/pkg/errors"
"github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns/azure"
"github.com/xenolf/lego/providers/dns/cloudflare"
"github.com/xenolf/lego/providers/dns/cloudxns"
"github.com/xenolf/lego/providers/dns/digitalocean"
"github.com/xenolf/lego/providers/dns/dnsimple"
"github.com/xenolf/lego/providers/dns/dnsmadeeasy"
"github.com/xenolf/lego/providers/dns/dnspod"
"github.com/xenolf/lego/providers/dns/dyn"
"github.com/xenolf/lego/providers/dns/gandi"
"github.com/xenolf/lego/providers/dns/gandiv5"
"github.com/xenolf/lego/providers/dns/googlecloud"
"github.com/xenolf/lego/providers/dns/linode"
"github.com/xenolf/lego/providers/dns/namecheap"
"github.com/xenolf/lego/providers/dns/ovh"
"github.com/xenolf/lego/providers/dns/pdns"
"github.com/xenolf/lego/providers/dns/rfc2136"
"github.com/xenolf/lego/providers/dns/route53"
"github.com/xenolf/lego/providers/dns/vultr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/pkg/labels"
"k8s.io/client-go/pkg/selection"
"k8s.io/client-go/rest"
)
// CertProcessor holds the shared configuration, state, and locks
type CertProcessor struct {
acmeURL string
certSecretPrefix string
certNamespace string
tagPrefix string
namespaces []string
class string
defaultProvider string
defaultEmail string
db *bolt.DB
Lock sync.Mutex
HTTPLock sync.Mutex
TLSLock sync.Mutex
k8s K8sClient
renewBeforeDays int
}
// NewCertProcessor creates and populates a CertProcessor
func NewCertProcessor(
k8s *kubernetes.Clientset,
certClient *rest.RESTClient,
acmeURL string,
certSecretPrefix string,
certNamespace string,
tagPrefix string,
namespaces []string,
class string,
defaultProvider string,
defaultEmail string,
db *bolt.DB,
renewBeforeDays int) *CertProcessor {
return &CertProcessor{
k8s: K8sClient{c: k8s, certClient: certClient},
acmeURL: acmeURL,
certSecretPrefix: certSecretPrefix,
certNamespace: certNamespace,
tagPrefix: tagPrefix,
namespaces: namespaces,
class: class,
defaultProvider: defaultProvider,
defaultEmail: defaultEmail,
db: db,
renewBeforeDays: renewBeforeDays,
}
}
func (p *CertProcessor) newACMEClient(acmeUser acme.User, provider string) (*acme.Client, *sync.Mutex, error) {
acmeClient, err := acme.NewClient(p.acmeURL, acmeUser, acme.RSA2048)
if err != nil {
return nil, nil, errors.Wrap(err, "Error while generating acme client")
}
initDNSProvider := func(p acme.ChallengeProvider, err error) (*acme.Client, *sync.Mutex, error) {
if err != nil {
return nil, nil, errors.Wrapf(err, "Error while initializing challenge provider %v", provider)
}
if err := acmeClient.SetChallengeProvider(acme.DNS01, p); err != nil {
return nil, nil, errors.Wrapf(err, "Error while setting challenge provider %v for dns-01", provider)
}
acmeClient.ExcludeChallenges([]acme.Challenge{acme.HTTP01, acme.TLSSNI01})
return acmeClient, nil, nil
}
switch provider {
case "http":
acmeClient.SetHTTPAddress(":8080")
acmeClient.ExcludeChallenges([]acme.Challenge{acme.DNS01, acme.TLSSNI01})
return acmeClient, &p.HTTPLock, nil
case "tls":
acmeClient.SetTLSAddress(":8081")
acmeClient.ExcludeChallenges([]acme.Challenge{acme.HTTP01, acme.DNS01})
return acmeClient, &p.TLSLock, nil
case "azure":
return initDNSProvider(azure.NewDNSProvider())
case "cloudflare":
return initDNSProvider(cloudflare.NewDNSProvider())
case "cloudxns":
return initDNSProvider(cloudxns.NewDNSProvider())
case "digitalocean":
return initDNSProvider(digitalocean.NewDNSProvider())
case "dnsimple":
return initDNSProvider(dnsimple.NewDNSProvider())
case "dnsmadeeasy":
return initDNSProvider(dnsmadeeasy.NewDNSProvider())
case "dnspod":
return initDNSProvider(dnspod.NewDNSProvider())
case "dyn":
return initDNSProvider(dyn.NewDNSProvider())
case "gandi":
return initDNSProvider(gandi.NewDNSProvider())
case "gandiv5":
return initDNSProvider(gandiv5.NewDNSProvider())
case "googlecloud":
return initDNSProvider(googlecloud.NewDNSProvider())
case "linode":
return initDNSProvider(linode.NewDNSProvider())
case "namecheap":
return initDNSProvider(namecheap.NewDNSProvider())
case "ovh":
return initDNSProvider(ovh.NewDNSProvider())
case "pdns":
return initDNSProvider(pdns.NewDNSProvider())
case "rfc2136":
return initDNSProvider(rfc2136.NewDNSProvider())
case "route53":
return initDNSProvider(route53.NewDNSProvider())
case "vultr":
return initDNSProvider(vultr.NewDNSProvider())
default:
return nil, nil, errors.Errorf("Unknown provider %v", provider)
}
}
func (p *CertProcessor) syncCertificates() error {
p.Lock.Lock()
defer p.Lock.Unlock()
certificates, err := p.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 := p.processCertificate(cert)
if err != nil {
log.Printf("Error while processing certificate during sync: %v", err)
}
}(cert)
}
wg.Wait()
return nil
}
func (p *CertProcessor) getSecrets() ([]v1.Secret, error) {
var secrets []v1.Secret
if len(p.namespaces) == 0 {
var err error
secrets, err = p.k8s.getSecrets(v1.NamespaceAll, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching secret list")
}
} else {
for _, namespace := range p.namespaces {
s, err := p.k8s.getSecrets(namespace, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching secret list")
}
secrets = append(secrets, s...)
}
}
return secrets, nil
}
func (p *CertProcessor) getCertificates() ([]Certificate, error) {
var certificates []Certificate
if len(p.namespaces) == 0 {
var err error
certificates, err = p.k8s.getCertificates(v1.NamespaceAll, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching certificate list")
}
} else {
for _, namespace := range p.namespaces {
certs, err := p.k8s.getCertificates(namespace, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching certificate list")
}
certificates = append(certificates, certs...)
}
}
return certificates, nil
}
func (p *CertProcessor) getIngresses() ([]v1beta1.Ingress, error) {
var ingresses []v1beta1.Ingress
if len(p.namespaces) == 0 {
var err error
if err != nil {
return nil, errors.Wrap(err, "Error creating API URL for ingress list")
}
ingresses, err = p.k8s.getIngresses(v1.NamespaceAll, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching ingress list")
}
} else {
for _, namespace := range p.namespaces {
igs, err := p.k8s.getIngresses(namespace, p.getLabelSelector())
if err != nil {
return nil, errors.Wrap(err, "Error while fetching ingress list")
}
ingresses = append(ingresses, igs...)
}
}
return ingresses, nil
}
func (p *CertProcessor) syncIngresses() error {
p.Lock.Lock()
defer p.Lock.Unlock()
ingresses, err := p.getIngresses()
if err != nil {
return err
}
var wg sync.WaitGroup
for _, ingress := range ingresses {
wg.Add(1)
go func(ingress v1beta1.Ingress) {
p.processIngress(ingress)
wg.Done()
}(ingress)
}
wg.Wait()
return nil
}
func (p *CertProcessor) watchKubernetesEvents(namespace string, selector labels.Selector, wg *sync.WaitGroup, doneChan <-chan struct{}) {
if namespace == v1.NamespaceAll {
log.Printf("Watching certificates and ingresses in all namespaces")
} else {
log.Printf("Watchining certificates and ingresses in namespace %s", namespace)
}
certEvents := p.k8s.monitorCertificateEvents(namespace, selector, doneChan)
ingressEvents := p.k8s.monitorIngressEvents(namespace, selector, doneChan)
for {
select {
case event := <-certEvents:
err := p.processCertificateEvent(event)
if err != nil {
log.Printf("Error while processing certificate event: %v", err)
}
case event := <-ingressEvents:
p.processIngressEvent(event)
case <-doneChan:
wg.Done()
log.Println("Stopped certificate event watcher.")
return
}
}
}
func (p *CertProcessor) maintenance(syncInterval time.Duration, wg *sync.WaitGroup, doneChan <-chan struct{}) {
for {
select {
case <-time.After(syncInterval):
if err := p.syncCertificates(); err != nil {
log.Printf("Error while synchronizing certificates during refresh: %s", err)
}
if err := p.syncIngresses(); err != nil {
log.Printf("Error while synchronizing ingresses during refresh: %s", err)
}
if err := p.gcSecrets(); err != nil {
log.Printf("Error cleaning up secrets: %s", err)
}
case <-doneChan:
wg.Done()
log.Println("Stopped refresh loop.")
return
}
}
}
func (p *CertProcessor) processCertificateEvent(c CertificateEvent) error {
p.Lock.Lock()
defer p.Lock.Unlock()
switch c.Type {
case "ADDED", "MODIFIED":
_, err := p.processCertificate(c.Object)
return err
}
return nil
}
func (p *CertProcessor) secretName(cert Certificate) string {
if cert.Spec.SecretName != "" {
return cert.Spec.SecretName
}
return p.certSecretPrefix + cert.Spec.Domain
}
// normalizeHostnames returns a copy of the hostnames array where all hostnames are lower
// cased and the array sorted.
// This allows the input to have changed order or different casing between runs,
// but a new certificate will only be created if a certificate is added or removed.
func normalizeHostnames(hostnames []string) []string {
arr := make([]string, len(hostnames))
copy(arr, hostnames)
for i, hostname := range arr {
arr[i] = strings.ToLower(hostname)
}
sort.Strings(arr)
return arr
}
func (p *CertProcessor) getStoredAltNames(cert Certificate) ([]string, error) {
var altNamesRaw []byte
err := p.db.View(func(tx *bolt.Tx) error {
altNamesRaw = tx.Bucket([]byte("domain-altnames")).Get([]byte(cert.Spec.Domain))
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "Error while fetching altnames from database for domain %v", cert.Spec.Domain)
}
if altNamesRaw == nil {
return nil, nil
}
var altNames []string
err = json.Unmarshal(altNamesRaw, &altNames)
if err != nil {
return nil, errors.Wrapf(err, "Error while unmarshalling altnames from database for domain %v", cert.Spec.Domain)
}
return altNames, nil
}
func equalAltNames(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// processCertificate creates or renews the corresponding secret
// processCertificate will create new ACME users if necessary, and complete ACME challenges
// processCertificate caches ACME user and certificate information in boltdb for reuse
func (p *CertProcessor) processCertificate(cert Certificate) (processed bool, err error) {
var (
acmeUserInfo ACMEUserData
acmeCertDetails ACMECertDetails
acmeCert ACMECertData
acmeClient *acme.Client
acmeClientMutex *sync.Mutex
)
namespace := certificateNamespace(cert)
// Fetch current certificate data from k8s
s, err := p.k8s.getSecret(namespace, p.secretName(cert))
if err != nil {
return false, errors.Wrapf(err, "Error while fetching certificate acme data for domain %v", cert.Spec.Domain)
}
altNames := normalizeHostnames(cert.Spec.AltNames)
storedAltNames, err := p.getStoredAltNames(cert)
if err != nil {
return false, errors.Wrap(err, "Error while getting stored alternative names")
}
sameAltNames := equalAltNames(altNames, storedAltNames)
// If a cert exists, and altNames are correct check its expiry and expected altNames
if s != nil && getDomainFromLabel(s, p.tagPrefix) == cert.Spec.Domain && sameAltNames {
acmeCert, err = NewACMECertDataFromSecret(s, p.tagPrefix)
if err != nil {
return false, errors.Wrapf(err, "Error while decoding acme certificate from secret for existing domain %v", cert.Spec.Domain)
}
// Decode cert
pemBlock, _ := pem.Decode(acmeCert.Cert)
if pemBlock == nil {
return false, errors.Wrapf(err, "Got nil back when decoding x509 encoded certificate for existing domain %v", cert.Spec.Domain)
}
parsedCert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
return false, errors.Wrapf(err, "Error while parsing x509 encoded certificate for existing domain %v", cert.Spec.Domain)
}
// If certificate expires after now + p.renewBeforeDays, don't renew
if parsedCert.NotAfter.After(time.Now().Add(time.Hour * time.Duration(24*p.renewBeforeDays))) {
return false, nil
}
log.Printf("[%v] Expiry for cert is in less than %v days (%v), attempting renewal", cert.Spec.Domain, p.renewBeforeDays, parsedCert.NotAfter.String())
}
// Fetch acme user data and cert details from bolt
var userInfoRaw, certDetailsRaw []byte
err = p.db.View(func(tx *bolt.Tx) error {
userInfoRaw = tx.Bucket([]byte("user-info")).Get([]byte(cert.Spec.Domain))
certDetailsRaw = tx.Bucket([]byte("cert-details")).Get([]byte(cert.Spec.Domain))
return nil
})
if err != nil {
return false, errors.Wrapf(err, "Error while running bolt view transaction for domain %v", cert.Spec.Domain)
}
provider := valueOrDefault(cert.Spec.Provider, p.defaultProvider)
email := valueOrDefault(cert.Spec.Email, p.defaultEmail)
// Handle user information
if userInfoRaw != nil { // Use existing user
if err := json.Unmarshal(userInfoRaw, &acmeUserInfo); err != nil {
return false, errors.Wrapf(err, "Error while unmarshalling user info for %v", cert.Spec.Domain)
}
log.Printf("Creating ACME client for %v provider for %v", provider, cert.Spec.Domain)
acmeClient, acmeClientMutex, err = p.newACMEClient(&acmeUserInfo, provider)
if err != nil {
return false, errors.Wrapf(err, "Error while creating ACME client for %v provider for %v", provider, cert.Spec.Domain)
}
// Some acme providers require locking, if the mutex is specified, lock it
if acmeClientMutex != nil {
acmeClientMutex.Lock()
defer acmeClientMutex.Unlock()
}
} else { // Generate a new ACME user
userKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return false, errors.Wrapf(err, "Error while generating rsa key for new user for domain %v", cert.Spec.Domain)
}
acmeUserInfo.Email = email
acmeUserInfo.Key = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(userKey),
})
log.Printf("Creating ACME client for %v provider for %v", provider, cert.Spec.Domain)
acmeClient, acmeClientMutex, err = p.newACMEClient(&acmeUserInfo, provider)
if err != nil {
return false, errors.Wrapf(err, "Error while creating ACME client for %v", cert.Spec.Domain)
}
// Some acme providers require locking, if the mutex is specified, lock it
if acmeClientMutex != nil {
acmeClientMutex.Lock()
defer acmeClientMutex.Unlock()
}
// Register
acmeUserInfo.Registration, err = acmeClient.Register()
if err != nil {
return false, errors.Wrapf(err, "Error while registering user for new domain %v", cert.Spec.Domain)
}
// Agree to TOS
if err := acmeClient.AgreeToTOS(); err != nil {
return false, errors.Wrapf(err, "Error while agreeing to acme TOS for new domain %v", cert.Spec.Domain)
}
}
domains := append([]string{cert.Spec.Domain}, altNames...)
// If we have cert details stored with expected altNames, do a renewal, otherwise, obtain from scratch
if certDetailsRaw == nil || acmeCert.DomainName == "" || !sameAltNames {
acmeCert.DomainName = cert.Spec.Domain
// Obtain a cert
certRes, errs := acmeClient.ObtainCertificate(domains, true, nil, false)
for _, domain := range domains {
if errs[domain] != nil {
return false, errors.Wrapf(errs[domain], "Error while obtaining certificate for new domain %v", domain)
}
}
// fill in data
acmeCert.Cert = certRes.Certificate
acmeCert.PrivateKey = certRes.PrivateKey
acmeCertDetails = NewACMECertDetailsFromResource(certRes)
} else {
if err := json.Unmarshal(certDetailsRaw, &acmeCertDetails); err != nil {
return false, errors.Wrapf(err, "Error while unmarshalling cert details for existing domain %v", cert.Spec.Domain)
}
// Fill in cert resource
certRes := acmeCertDetails.ToCertResource()
certRes.Certificate = acmeCert.Cert
certRes.PrivateKey = acmeCert.PrivateKey
certRes, err = acmeClient.RenewCertificate(certRes, true, false)
if err != nil {
return false, errors.Wrapf(err, "Error while renewing certificate for existing domain %v", cert.Spec.Domain)
}
// Fill in details
acmeCert.Cert = certRes.Certificate
acmeCert.PrivateKey = certRes.PrivateKey
acmeCertDetails = NewACMECertDetailsFromResource(certRes)
}
// Serialize acmeCertDetails and acmeUserInfo
certDetailsRaw, err = json.Marshal(&acmeCertDetails)
if err != nil {
return false, errors.Wrapf(err, "Error while marshalling cert details for domain %v", cert.Spec.Domain)
}
userInfoRaw, err = json.Marshal(&acmeUserInfo)
if err != nil {
return false, errors.Wrapf(err, "Error while marshalling user info for domain %v", cert.Spec.Domain)
}
altNamesRaw, err := json.Marshal(altNames)
if err != nil {
return false, errors.Wrapf(err, "Error while marshalling altNames for domain %v", cert.Spec.Domain)
}
// Save cert details and user info to bolt
err = p.db.Update(func(tx *bolt.Tx) error {
key := []byte(cert.Spec.Domain)
tx.Bucket([]byte("user-info")).Put(key, userInfoRaw)
tx.Bucket([]byte("cert-details")).Put(key, certDetailsRaw)
tx.Bucket([]byte("domain-altnames")).Put(key, altNamesRaw)
return nil
})
if err != nil {
return false, errors.Wrapf(err, "Error while saving data to bolt for domain %v", cert.Spec.Domain)
}
// Convert cert data to k8s secret
isUpdate := s != nil
s = acmeCert.ToSecret(p.tagPrefix, p.class)
s.Name = p.secretName(cert)
if isUpdate {
log.Printf("Updating secret %v in namespace %v for domain %v", s.Name, namespace, cert.Spec.Domain)
} else {
log.Printf("Creating secret %v in namespace %v for domain %v", s.Name, namespace, cert.Spec.Domain)
}
// Save the k8s secret
if err := p.k8s.saveSecret(namespace, s, isUpdate); err != nil {
return false, errors.Wrapf(err, "Error while saving secret for domain %v", cert.Spec.Domain)
}
msg := "Created certificate"
if isUpdate {
msg = "Updated certificate"
}
p.k8s.createEvent(v1.Event{
ObjectMeta: v1.ObjectMeta{
Namespace: namespace,
},
InvolvedObject: v1.ObjectReference{
Kind: "Secret",
Namespace: namespace,
Name: s.Name,
},
Reason: "ACMEUpdated",
Message: msg,
Source: v1.EventSource{
Component: "kube-cert-manager",
},
Type: "Normal",
})
return true, nil
}
func (p *CertProcessor) gcSecrets() error {
p.Lock.Lock()
defer p.Lock.Unlock()
// Fetch secrets before certificates. That way, if a race occurs,
// we will only fail to delete a secret, not accidentally delete
// one that's still referenced.
secrets, err := p.getSecrets()
if err != nil {
return err
}
certs, err := p.getCertificates()
if err != nil {
return err
}
ingresses, err := p.getIngresses()
if err != nil {
return err
}
for _, ingress := range ingresses {
certs = append(certs, ingressCertificates(p, ingress)...)
}
usedSecrets := map[string]bool{}
for _, cert := range certs {
usedSecrets[cert.Metadata.Namespace+" "+p.secretName(cert)] = true
}
for _, secret := range secrets {
// Only check for the deprecated "enabled" annotation if not using the "class" feature
if p.class == "" && secret.Annotations[addTagPrefix(p.tagPrefix, "enabled")] != "true" {
continue
}
if usedSecrets[secret.Namespace+" "+secret.Name] {
continue
}
log.Printf("Deleting unused secret %s in namespace %s", secret.Name, secret.Namespace)
if err := p.k8s.deleteSecret(secret.Namespace, secret.Name); err != nil {
return err
}
}
return nil
}
func (p *CertProcessor) processIngressEvent(c IngressEvent) {
p.Lock.Lock()
defer p.Lock.Unlock()
switch c.Type {
case "ADDED", "MODIFIED":
p.processIngress(c.Object)
}
}
func ingressCertificates(p *CertProcessor, ingress v1beta1.Ingress) []Certificate {
// The enabled annotation is deprecated when a class label is used
if p.class == "" && ingress.Annotations[addTagPrefix(p.tagPrefix, "enabled")] != "true" {
return nil
}
var certs []Certificate
provider := valueOrDefault(ingress.Annotations[addTagPrefix(p.tagPrefix, "provider")], p.defaultProvider)
email := valueOrDefault(ingress.Annotations[addTagPrefix(p.tagPrefix, "email")], p.defaultEmail)
if provider == "" || email == "" {
return nil
}
for _, tls := range ingress.Spec.TLS {
if len(tls.Hosts) < 1 {
continue
}
cert := Certificate{
TypeMeta: unversioned.TypeMeta{
APIVersion: "v1",
Kind: "Certificate",
},
Metadata: api.ObjectMeta{
Namespace: ingress.Namespace,
},
Spec: CertificateSpec{
Domain: tls.Hosts[0],
Provider: provider,
Email: email,
SecretName: tls.SecretName,
AltNames: tls.Hosts[1:],
},
}
certs = append(certs, cert)
}
return certs
}
func (p *CertProcessor) processIngress(ingress v1beta1.Ingress) {
if p.class == "" && ingress.Annotations[addTagPrefix(p.tagPrefix, "enabled")] != "true" {
return
}
source := v1.EventSource{
Component: "kube-cert-manager",
}
var certs []Certificate
provider := valueOrDefault(ingress.Annotations[addTagPrefix(p.tagPrefix, "provider")], p.defaultProvider)
email := valueOrDefault(ingress.Annotations[addTagPrefix(p.tagPrefix, "email")], p.defaultEmail)
for _, tls := range ingress.Spec.TLS {
if len(tls.Hosts) == 0 {
continue
}
altNames := tls.Hosts[1:]
cert := Certificate{
TypeMeta: unversioned.TypeMeta{
APIVersion: "v1",
Kind: "Certificate",
},
Metadata: api.ObjectMeta{
Namespace: ingress.Namespace,
},
Spec: CertificateSpec{
Domain: tls.Hosts[0],
Provider: provider,
Email: email,
SecretName: tls.SecretName,
AltNames: altNames,
},
}
certs = append(certs, cert)
}
if len(certs) > 0 && (provider == "" || email == "") {
p.k8s.createEvent(v1.Event{
ObjectMeta: v1.ObjectMeta{
Namespace: ingress.Namespace,
},
InvolvedObject: ingressReference(ingress, ""),
Reason: "ACMEMissingAnnotation",
Message: "Couldn't create certificates: missing email or provider annotation",
Source: source,
Type: "Warning",
})
return
}
for _, cert := range certs {
processed, err := p.processCertificate(cert)
if err != nil {
p.k8s.createEvent(v1.Event{
ObjectMeta: v1.ObjectMeta{
Namespace: ingress.Namespace,
},
InvolvedObject: ingressReference(ingress, ""),
Reason: "ACMEError",
Message: fmt.Sprintf("Couldn't create certificate for secret %s: %s", cert.Spec.SecretName, err),
Source: source,
Type: "Warning",
})
continue
}
if processed {
p.k8s.createEvent(v1.Event{
ObjectMeta: v1.ObjectMeta{
Namespace: ingress.Namespace,
},
InvolvedObject: ingressReference(ingress, ""),
Reason: "ACMEProcessed",
Message: fmt.Sprintf("Processed ACME certificate for secret: %s", cert.Spec.SecretName),
Source: source,
Type: "Normal",
})
}
}
}
func certificateNamespace(c Certificate) string {
if c.Metadata.Namespace != "" {
return c.Metadata.Namespace
}
return "default"
}
func (p *CertProcessor) getLabelSelector() labels.Selector {
if p.class != "" {
r, err := labels.NewRequirement(
addTagPrefix(p.tagPrefix, "class"),
selection.Equals,
[]string{p.class},
)
if err != nil {
log.Fatalf("unable to create class-equals requirement: %v", err)
}
return labels.NewSelector().Add(*r)
}
return nil
}
func addTagPrefix(prefix, tag string) string {
if prefix == "" {
return tag
} else if strings.HasSuffix(prefix, ".") {
// Support the deprecated "stable.k8s.psg.io/kcm." prefix
return prefix + tag
}
return prefix + "/" + tag
}
func valueOrDefault(a, b string) string {
if a != "" {
return a
}
return b
}