-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add clock mock plumbing throughout the project #408
base: main
Are you sure you want to change the base?
Changes from 3 commits
e25f9b5
5191bfa
25322b2
55d0abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/jmhodges/clock" | ||
|
||
"github.com/letsencrypt/pebble/v2/acme" | ||
"github.com/letsencrypt/pebble/v2/core" | ||
"github.com/letsencrypt/pebble/v2/db" | ||
|
@@ -34,6 +36,7 @@ type CAImpl struct { | |
log *log.Logger | ||
db *db.MemoryStore | ||
ocspResponderURL string | ||
clockSource clock.Clock | ||
|
||
chains []*chain | ||
|
||
|
@@ -118,8 +121,8 @@ func (ca *CAImpl) makeRootCert( | |
template := &x509.Certificate{ | ||
Subject: subject, | ||
SerialNumber: serial, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(30, 0, 0), | ||
NotBefore: ca.clockSource.Now(), | ||
NotAfter: ca.clockSource.Now().AddDate(30, 0, 0), | ||
|
||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, | ||
|
@@ -269,7 +272,7 @@ func (ca *CAImpl) newCertificate(domains []string, ips []net.IP, key crypto.Publ | |
return nil, fmt.Errorf("cannot create subject key ID: %s", err.Error()) | ||
} | ||
|
||
certNotBefore := time.Now() | ||
certNotBefore := ca.clockSource.Now() | ||
if notBefore != "" { | ||
certNotBefore, err = time.Parse(time.RFC3339, notBefore) | ||
if err != nil { | ||
|
@@ -342,17 +345,21 @@ func (ca *CAImpl) newCertificate(domains []string, ips []net.IP, key crypto.Publ | |
return newCert, nil | ||
} | ||
|
||
func New(log *log.Logger, db *db.MemoryStore, ocspResponderURL string, alternateRoots int, chainLength int, certificateValidityPeriod uint64) *CAImpl { | ||
func New(log *log.Logger, clockSource clock.Clock, db *db.MemoryStore, ocspResponderURL string, alternateRoots int, chainLength int, certificateValidityPeriod uint64) *CAImpl { | ||
ca := &CAImpl{ | ||
log: log, | ||
db: db, | ||
clockSource: clockSource, | ||
certValidityPeriod: defaultValidityPeriod, | ||
} | ||
|
||
if ocspResponderURL != "" { | ||
ca.ocspResponderURL = ocspResponderURL | ||
ca.log.Printf("Setting OCSP responder URL for issued certificates to %q", ca.ocspResponderURL) | ||
} | ||
if clockSource == nil { | ||
ca.clockSource = clock.New() | ||
} | ||
Comment on lines
+360
to
+362
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two comments:
|
||
|
||
intermediateSubject := pkix.Name{ | ||
CommonName: intermediateCAPrefix + hex.EncodeToString(makeSerial().Bytes()[:3]), | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |||||||
"os" | ||||||||
"strconv" | ||||||||
|
||||||||
"github.com/jmhodges/clock" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
"github.com/letsencrypt/pebble/v2/ca" | ||||||||
"github.com/letsencrypt/pebble/v2/cmd" | ||||||||
"github.com/letsencrypt/pebble/v2/db" | ||||||||
|
@@ -93,9 +94,10 @@ func main() { | |||||||
chainLength = int(val) | ||||||||
} | ||||||||
|
||||||||
db := db.NewMemoryStore() | ||||||||
ca := ca.New(logger, db, c.Pebble.OCSPResponderURL, alternateRoots, chainLength, c.Pebble.CertificateValidityPeriod) | ||||||||
va := va.New(logger, c.Pebble.HTTPPort, c.Pebble.TLSPort, *strictMode, *resolverAddress) | ||||||||
clockSource := clock.New() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on your PR description (you mention "breaking the external API") it sounds like you're using Pebble as a library, rather than as a standalone binary? In that case, your changes here make sense. But if we're going to go to the trouble of giving Pebble full fake clock support, we should give it to everyone, including people who use Pebble as a binary (which I think is most users of Pebble). To that end, let's replace this line with something a bit more like this, so that the fake clock can be initialized from an environment variable if someone wants to, or defaults to the system clock otherwise. |
||||||||
db := db.NewMemoryStore(clockSource) | ||||||||
ca := ca.New(logger, clockSource, db, c.Pebble.OCSPResponderURL, alternateRoots, chainLength, c.Pebble.CertificateValidityPeriod) | ||||||||
va := va.New(logger, clockSource, c.Pebble.HTTPPort, c.Pebble.TLSPort, *strictMode, *resolverAddress) | ||||||||
|
||||||||
for keyID, key := range c.Pebble.ExternalAccountMACKeys { | ||||||||
err := db.AddExternalAccountKeyByID(keyID, key) | ||||||||
|
@@ -107,7 +109,7 @@ func main() { | |||||||
cmd.FailOnError(err, "Failed to add domain to block list") | ||||||||
} | ||||||||
|
||||||||
wfeImpl := wfe.New(logger, db, va, ca, *strictMode, c.Pebble.ExternalAccountBindingRequired, c.Pebble.RetryAfter.Authz, c.Pebble.RetryAfter.Order) | ||||||||
wfeImpl := wfe.New(logger, clockSource, db, va, ca, *strictMode, c.Pebble.ExternalAccountBindingRequired, c.Pebble.RetryAfter.Authz, c.Pebble.RetryAfter.Order) | ||||||||
muxHandler := wfeImpl.Handler() | ||||||||
|
||||||||
if c.Pebble.ManagementListenAddress != "" { | ||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and throughout, let's rename
clockSource
toclk
, to match Boulder.