forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/crypto/acme/autocert/renewal.go

276 lines
4.3 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2016 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package autocert
import (
"context"
"crypto"
"sync"
"time"
)
// renewJitter is the maximum deviation from Manager.RenewBefore.
2024-02-18 10:42:21 +00:00
const renewJitter = time.Hour
// domainRenewal tracks the state used by the periodic timers
2024-02-18 10:42:21 +00:00
// renewing a single domain's cert.
2024-02-18 10:42:21 +00:00
type domainRenewal struct {
m *Manager
ck certKey
2024-02-18 10:42:21 +00:00
key crypto.Signer
timerMu sync.Mutex
timer *time.Timer
2024-02-18 10:42:21 +00:00
timerClose chan struct{} // if non-nil, renew closes this channel (and nils out the timer fields) instead of running
2024-02-18 10:42:21 +00:00
}
// start starts a cert renewal timer at the time
2024-02-18 10:42:21 +00:00
// defined by the certificate expiration time exp.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If the timer is already started, calling start is a noop.
2024-02-18 10:42:21 +00:00
func (dr *domainRenewal) start(exp time.Time) {
2024-02-18 10:42:21 +00:00
dr.timerMu.Lock()
2024-02-18 10:42:21 +00:00
defer dr.timerMu.Unlock()
2024-02-18 10:42:21 +00:00
if dr.timer != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dr.timer = time.AfterFunc(dr.next(exp), dr.renew)
2024-02-18 10:42:21 +00:00
}
// stop stops the cert renewal timer and waits for any in-flight calls to renew
2024-02-18 10:42:21 +00:00
// to complete. If the timer is already stopped, calling stop is a noop.
2024-02-18 10:42:21 +00:00
func (dr *domainRenewal) stop() {
2024-02-18 10:42:21 +00:00
dr.timerMu.Lock()
2024-02-18 10:42:21 +00:00
defer dr.timerMu.Unlock()
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if dr.timer == nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if dr.timer.Stop() {
2024-02-18 10:42:21 +00:00
dr.timer = nil
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// dr.timer fired, and we acquired dr.timerMu before the renew callback did.
2024-02-18 10:42:21 +00:00
// (We know this because otherwise the renew callback would have reset dr.timer!)
2024-02-18 10:42:21 +00:00
timerClose := make(chan struct{})
2024-02-18 10:42:21 +00:00
dr.timerClose = timerClose
2024-02-18 10:42:21 +00:00
dr.timerMu.Unlock()
2024-02-18 10:42:21 +00:00
<-timerClose
2024-02-18 10:42:21 +00:00
dr.timerMu.Lock()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// renew is called periodically by a timer.
2024-02-18 10:42:21 +00:00
// The first renew call is kicked off by dr.start.
2024-02-18 10:42:21 +00:00
func (dr *domainRenewal) renew() {
2024-02-18 10:42:21 +00:00
dr.timerMu.Lock()
2024-02-18 10:42:21 +00:00
defer dr.timerMu.Unlock()
2024-02-18 10:42:21 +00:00
if dr.timerClose != nil {
2024-02-18 10:42:21 +00:00
close(dr.timerClose)
2024-02-18 10:42:21 +00:00
dr.timer, dr.timerClose = nil, nil
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
2024-02-18 10:42:21 +00:00
defer cancel()
2024-02-18 10:42:21 +00:00
// TODO: rotate dr.key at some point?
2024-02-18 10:42:21 +00:00
next, err := dr.do(ctx)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
next = renewJitter / 2
2024-02-18 10:42:21 +00:00
next += time.Duration(pseudoRand.int63n(int64(next)))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
testDidRenewLoop(next, err)
2024-02-18 10:42:21 +00:00
dr.timer = time.AfterFunc(next, dr.renew)
2024-02-18 10:42:21 +00:00
}
// updateState locks and replaces the relevant Manager.state item with the given
2024-02-18 10:42:21 +00:00
// state. It additionally updates dr.key with the given state's key.
2024-02-18 10:42:21 +00:00
func (dr *domainRenewal) updateState(state *certState) {
2024-02-18 10:42:21 +00:00
dr.m.stateMu.Lock()
2024-02-18 10:42:21 +00:00
defer dr.m.stateMu.Unlock()
2024-02-18 10:42:21 +00:00
dr.key = state.key
2024-02-18 10:42:21 +00:00
dr.m.state[dr.ck] = state
2024-02-18 10:42:21 +00:00
}
// do is similar to Manager.createCert but it doesn't lock a Manager.state item.
2024-02-18 10:42:21 +00:00
// Instead, it requests a new certificate independently and, upon success,
2024-02-18 10:42:21 +00:00
// replaces dr.m.state item with a new one and updates cache for the given domain.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It may lock and update the Manager.state if the expiration date of the currently
2024-02-18 10:42:21 +00:00
// cached cert is far enough in the future.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned value is a time interval after which the renewal should occur again.
2024-02-18 10:42:21 +00:00
func (dr *domainRenewal) do(ctx context.Context) (time.Duration, error) {
2024-02-18 10:42:21 +00:00
// a race is likely unavoidable in a distributed environment
2024-02-18 10:42:21 +00:00
// but we try nonetheless
2024-02-18 10:42:21 +00:00
if tlscert, err := dr.m.cacheGet(ctx, dr.ck); err == nil {
2024-02-18 10:42:21 +00:00
next := dr.next(tlscert.Leaf.NotAfter)
2024-02-18 10:42:21 +00:00
if next > dr.m.renewBefore()+renewJitter {
2024-02-18 10:42:21 +00:00
signer, ok := tlscert.PrivateKey.(crypto.Signer)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
state := &certState{
key: signer,
2024-02-18 10:42:21 +00:00
cert: tlscert.Certificate,
2024-02-18 10:42:21 +00:00
leaf: tlscert.Leaf,
}
2024-02-18 10:42:21 +00:00
dr.updateState(state)
2024-02-18 10:42:21 +00:00
return next, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
der, leaf, err := dr.m.authorizedCert(ctx, dr.key, dr.ck)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
state := &certState{
key: dr.key,
2024-02-18 10:42:21 +00:00
cert: der,
2024-02-18 10:42:21 +00:00
leaf: leaf,
}
2024-02-18 10:42:21 +00:00
tlscert, err := state.tlscert()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err := dr.m.cachePut(ctx, dr.ck, tlscert); err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dr.updateState(state)
2024-02-18 10:42:21 +00:00
return dr.next(leaf.NotAfter), nil
2024-02-18 10:42:21 +00:00
}
func (dr *domainRenewal) next(expiry time.Time) time.Duration {
2024-02-18 10:42:21 +00:00
d := expiry.Sub(dr.m.now()) - dr.m.renewBefore()
2024-02-18 10:42:21 +00:00
// add a bit of randomness to renew deadline
2024-02-18 10:42:21 +00:00
n := pseudoRand.int63n(int64(renewJitter))
2024-02-18 10:42:21 +00:00
d -= time.Duration(n)
2024-02-18 10:42:21 +00:00
if d < 0 {
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return d
2024-02-18 10:42:21 +00:00
}
var testDidRenewLoop = func(next time.Duration, err error) {}