Skip to content

Commit

Permalink
Add context arg to DecisionFunc (#255)
Browse files Browse the repository at this point in the history
* Optionally pass the context argument down to the OnDemand decision func

* Remove the "compatibility shims"

This "breaks" the API here, but the change should be trivially obvious to an implementor
and it gives a lot less headache later.
  • Loading branch information
ankon committed Oct 30, 2023
1 parent 560847b commit f0038ff
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion certmagic.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ type OnDemandConfig struct {
// whether a certificate can be obtained or renewed
// for the given name. If an error is returned, the
// request will be denied.
DecisionFunc func(name string) error
DecisionFunc func(ctx context.Context, name string) error

// Sources for getting new, unmanaged certificates.
// They will be invoked only during TLS handshakes
Expand Down
8 changes: 4 additions & 4 deletions handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (cfg *Config) getCertDuringHandshake(ctx context.Context, hello *tls.Client
// Make sure a certificate is allowed for the given name. If not, it doesn't
// make sense to try loading one from storage (issue #185), getting it from a
// certificate manager, or obtaining one from an issuer.
if err := cfg.checkIfCertShouldBeObtained(name, false); err != nil {
if err := cfg.checkIfCertShouldBeObtained(ctx, name, false); err != nil {
return Certificate{}, fmt.Errorf("certificate is not allowed for server name %s: %v", name, err)
}

Expand Down Expand Up @@ -438,7 +438,7 @@ func (cfg *Config) optionalMaintenance(ctx context.Context, log *zap.Logger, cer
// checkIfCertShouldBeObtained checks to see if an on-demand TLS certificate
// should be obtained for a given domain based upon the config settings. If
// a non-nil error is returned, do not issue a new certificate for name.
func (cfg *Config) checkIfCertShouldBeObtained(name string, requireOnDemand bool) error {
func (cfg *Config) checkIfCertShouldBeObtained(ctx context.Context, name string, requireOnDemand bool) error {
if requireOnDemand && cfg.OnDemand == nil {
return fmt.Errorf("not configured for on-demand certificate issuance")
}
Expand All @@ -447,7 +447,7 @@ func (cfg *Config) checkIfCertShouldBeObtained(name string, requireOnDemand bool
}
if cfg.OnDemand != nil {
if cfg.OnDemand.DecisionFunc != nil {
if err := cfg.OnDemand.DecisionFunc(name); err != nil {
if err := cfg.OnDemand.DecisionFunc(ctx, name); err != nil {
return fmt.Errorf("decision func: %w", err)
}
return nil
Expand Down Expand Up @@ -685,7 +685,7 @@ func (cfg *Config) renewDynamicCertificate(ctx context.Context, hello *tls.Clien
defer cancel()

// Make sure a certificate for this name should be renewed on-demand
err := cfg.checkIfCertShouldBeObtained(name, true)
err := cfg.checkIfCertShouldBeObtained(ctx, name, true)
if err != nil {
// if not, remove from cache (it will be deleted from storage later)
cfg.certCache.mu.Lock()
Expand Down

0 comments on commit f0038ff

Please sign in to comment.