-
Notifications
You must be signed in to change notification settings - Fork 4
/
letsencrypt.go
55 lines (46 loc) · 1.71 KB
/
letsencrypt.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
package watcher
import (
"context"
"time"
"github.com/nstapelbroek/envoy-swarm-control-plane/pkg/acme"
"github.com/nstapelbroek/envoy-swarm-control-plane/pkg/logger"
"github.com/nstapelbroek/envoy-swarm-control-plane/pkg/snapshot"
)
// LetsEncrypt is a poor man's interval trigger to issue any missing certificates at LetsEncrypt.
// I tried hooking this up with gRPC server callbacks but that lead to a lot of coupling
// between several control plane components. This is probably good enough for now :)
type LetsEncrypt struct {
integration *acme.Integration
logger logger.Logger
}
func ForLetsEncrypt(integration *acme.Integration, log logger.Logger) *LetsEncrypt {
return &LetsEncrypt{
integration: integration,
logger: log,
}
}
func (l *LetsEncrypt) Start(ctx context.Context, dispatchChannel chan snapshot.UpdateReason) {
const IssueInterval = 60
const CheckForRenewalInterval = 86370
reissueInterval := time.After(IssueInterval * time.Second)
renewalInterval := time.After(CheckForRenewalInterval * time.Second)
for {
select {
case <-reissueInterval:
l.logger.Debugf("Running LetsEncrypt certificate issuing")
if reloadRequired, _ := l.integration.IssueCertificates(); reloadRequired {
dispatchChannel <- "new LetsEncrypt certificate rotated"
}
reissueInterval = time.After(IssueInterval * time.Second)
case <-renewalInterval:
l.logger.Debugf("Running LetsEncrypt renewal check")
if reloadRequired := l.integration.ScheduleRenewals(); reloadRequired {
dispatchChannel <- "LetsEncrypt renewal scheduled"
}
renewalInterval = time.After(CheckForRenewalInterval * time.Second)
case <-ctx.Done():
l.logger.Debugf("Stopping certificate issuing")
return
}
}
}