certmagic
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
Easy and Powerful TLS Automation
The same library used by theCaddy Web Server
Caddy'sautomagic TLS features—now for your own Go programs—in one powerful and easy-to-use library!
CertMagic is the most mature, robust, and powerful ACME client integration for Go... and perhaps ever.
With CertMagic, you can add one line to your Go application to serve securely over TLS, without ever having to touch certificates.
Instead of:
// plaintext HTTP, gross 🤢http.ListenAndServe(":80", mux)Use CertMagic:
// encrypted HTTPS with HTTP->HTTPS redirects - yay! 🔒😍certmagic.HTTPS([]string{"example.com"}, mux)That line of code will serve your HTTP routermux over HTTPS, complete with HTTP->HTTPS redirects. It obtains and renews the TLS certificates. It staples OCSP responses for greater privacy and security. As long as your domain name points to your server, CertMagic will keep its connections secure.
Compared to other ACME client libraries for Go, only CertMagic supports the full suite of ACME features, and no other library matches CertMagic's maturity and reliability.
CertMagic - Automatic HTTPS using Let's Encrypt
Menu
- Features
- Requirements
- Installation
- Usage
- The ACME Challenges
- On-Demand TLS
- Storage
- Cache
- Events
- ZeroSSL
- FAQ
- Contributing
- Project History
- Credits and License
Features
- Fully automated certificate management including issuance and renewal
- One-line, fully managed HTTPS servers
- Full control over almost every aspect of the system
- HTTP->HTTPS redirects
- Multiple issuers supported: get certificates from multiple sources/CAs for redundancy and resiliency
- Solves all 3 common ACME challenges: HTTP, TLS-ALPN, and DNS (and capable of others)
- Most robust error handling ofany ACME client
- Challenges are randomized to avoid accidental dependence
- Challenges are rotated to overcome certain network blockages
- Robust retries for up to 30 days
- Exponential backoff with carefully-tuned intervals
- Retries with optional test/staging CA endpoint instead of production, to avoid rate limits
- Written in Go, a language with memory-safety guarantees
- Powered byACMEz,the premier ACME client library for Go
- Alllibdns DNS providers work out-of-the-box
- Pluggable storage backends (default: file system)
- Pluggable key sources
- Wildcard certificates
- Automatic OCSP stapling (done right)keeps your sites online!
- Willautomatically attempt to replacerevoked certificates!
- Staples stored to disk in case of responder outages
- Distributed solving of all challenges (works behind load balancers)
- Highly efficient, coordinated management in a fleet
- Active locking
- Smart queueing
- Supports "on-demand" issuance of certificates (during TLS handshakes!)
- Caddy / CertMagic pioneered this technology
- Custom decision functions to regulate and throttle on-demand behavior
- Optional event hooks for observation
- One-time private keys by default (new key for each cert) to discourage pinning and reduce scope of key compromise
- Works with any certificate authority (CA) compliant with the ACME specification RFC 8555
- Certificate revocation (please, only if private key is compromised)
- Must-Staple (optional; not default)
- Cross-platform support! Mac, Windows, Linux, BSD, Android...
- Scales to hundreds of thousands of names/certificates per instance
- Use in conjunction with your own certificates
- Full support forRFC 9773 (ACME Renewal Information; ARI) extension
Requirements
- ACME server (can be a publicly-trusted CA, or your own)
- Public DNS name(s) you control
- Server reachable from public Internet
- Or use the DNS challenge to waive this requirement
- Control over port 80 (HTTP) and/or 443 (HTTPS)
- Or they can be forwarded to other ports you control
- Or use the DNS challenge to waive this requirement
- (This is a requirement of the ACME protocol, not a library limitation)
- Persistent storage
- Typically the local file system (default)
- Other integrations available/possible
- Go 1.21 or newer
Before using this library, your domain names MUST be pointed (A/AAAA records) at your server (unless you use the DNS challenge)!
Installation
$ go get github.com/caddyserver/certmagicUsage
Package Overview
Certificate authority
This library uses Let's Encrypt by default, but you can use any certificate authority that conforms to the ACME specification. Known/common CAs are provided as consts in the package, for exampleLetsEncryptStagingCA andLetsEncryptProductionCA.
TheConfig type
Thecertmagic.Config struct is how you can wield the power of this fully armed and operational battle station. However, an empty/uninitializedConfig isnot a valid one! In time, you will learn to use the force ofcertmagic.NewDefault() as I have.
Defaults
The defaultConfig value is calledcertmagic.Default. Change its fields to suit your needs, then callcertmagic.NewDefault() when you need a validConfig value. In other words,certmagic.Default is a template and is not valid for use directly.
You can set the default values easily, for example:certmagic.Default.Issuer = ....
Similarly, to configure ACME-specific defaults, usecertmagic.DefaultACME.
The high-level functions in this package (HTTPS(),Listen(),ManageSync(), andManageAsync()) use the default config exclusively. This is how most of you will interact with the package. This is suitable when all your certificates are managed the same way. However, if you need to manage certificates differently depending on their name, you will need to make your own cache and configs (keep reading).
Providing an email address
Although not strictly required, this is highly recommended best practice. It allows you to receive expiration emails if your certificates are expiring for some reason, and also allows the CA's engineers to potentially get in touch with you if something is wrong. I recommend settingcertmagic.DefaultACME.Email or always setting theEmail field of a newConfig struct.
Rate limiting
To avoid firehosing the CA's servers, CertMagic has built-in rate limiting. Currently, its default limit is up to 10 transactions (obtain or renew) every 1 minute (sliding window). This can be changed by setting theRateLimitEvents andRateLimitEventsWindow variables, if desired.
The CA may still enforce their own rate limits, and there's nothing (well, nothing ethical) CertMagic can do to bypass them for you.
Additionally, CertMagic will retry failed validations with exponential backoff for up to 30 days, with a reasonable maximum interval between attempts (an "attempt" means trying each enabled challenge type once).
Development and Testing
Note that Let's Encrypt imposesstrict rate limits at its production endpoint, so using it while developing your application may lock you out for a few days if you aren't careful!
While developing your application and testing it, usetheir staging endpoint which has much higher rate limits. Even then, don't hammer it: but it's much safer for when you're testing. When deploying, though, use their production CA because their staging CA doesn't issue trusted certificates.
To use staging, setcertmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA or setCA of everyACMEIssuer struct.
Examples
There are many ways to use this library. We'll start with the highest-level (simplest) and work down (more control).
All these high-level examples usecertmagic.Default andcertmagic.DefaultACME for the config and the default cache and storage for serving up certificates.
First, we'll follow best practices and do the following:
// read and agree to your CA's legal documentscertmagic.DefaultACME.Agreed = true// provide an email addresscertmagic.DefaultACME.Email = "you@yours.com"// use the staging endpoint while we're developingcertmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCAFor fully-functional program examples, check outthis X thread (or read itunrolled into a single post). (Note that the package API has changed slightly since these posts.)
Serving HTTP handlers with HTTPS
err := certmagic.HTTPS([]string{"example.com", "www.example.com"}, mux)if err != nil {return err}This starts HTTP and HTTPS listeners and redirects HTTP to HTTPS!
Starting a TLS listener
ln, err := certmagic.Listen([]string{"example.com"})if err != nil {return err}Getting a tls.Config
tlsConfig, err := certmagic.TLS([]string{"example.com"})if err != nil {return err}// be sure to customize NextProtos if serving a specific// application protocol after the TLS handshake, for example:tlsConfig.NextProtos = append([]string{"h2", "http/1.1"}, tlsConfig.NextProtos...)Advanced use
For more control (particularly, if you need a different way of managing each certificate), you'll make and use aCache and aConfig like so:
// First make a pointer to a Cache as we need to reference the same Cache in// GetConfigForCert below.var cache *certmagic.Cachecache = certmagic.NewCache(certmagic.CacheOptions{GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {// Here we use New to get a valid Config associated with the same cache.// The provided Config is used as a template and will be completed with// any defaults that are set in the Default config.return certmagic.New(cache, certmagic.Config{// ...}), nil},...})magic := certmagic.New(cache, certmagic.Config{// any customizations you need go here})myACME := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{CA: certmagic.LetsEncryptStagingCA,Email: "you@yours.com",Agreed: true,// plus any other customizations you need})magic.Issuers = []certmagic.Issuer{myACME}// this obtains certificates or renews them if necessaryerr := magic.ManageSync(context.TODO(), []string{"example.com", "sub.example.com"})if err != nil {return err}// to use its certificates and solve the TLS-ALPN challenge,// you can get a TLS config to use in a TLS listener!tlsConfig := magic.TLSConfig()// be sure to customize NextProtos if serving a specific// application protocol after the TLS handshake, for example:tlsConfig.NextProtos = append([]string{"h2", "http/1.1"}, tlsConfig.NextProtos...)//// OR ////// if you already have a TLS config you don't want to replace,// we can simply set its GetCertificate field and append the// TLS-ALPN challenge protocol to the NextProtosmyTLSConfig.GetCertificate = magic.GetCertificatemyTLSConfig.NextProtos = append(myTLSConfig.NextProtos, acmez.ACMETLS1Protocol)// the HTTP challenge has to be handled by your HTTP server;// if you don't have one, you should have disabled it earlier// when you made the certmagic.ConfighttpMux = myACME.HTTPChallengeHandler(httpMux)Great! This example grants you much more flexibility for advanced programs. However,the vast majority of you will only use the high-level functions described earlier, especially since you can still customize them by setting the package-levelDefault config.
Wildcard certificates
At time of writing (December 2018), Let's Encrypt only issues wildcard certificates with the DNS challenge. You can easily enable the DNS challenge with CertMagic for numerous providers (see the relevant section in the docs).
Behind a load balancer (or in a cluster)
CertMagic runs effectively behind load balancers and/or in cluster/fleet environments. In other words, you can have 10 or 1,000 servers all serving the same domain names, all sharing certificates and OCSP staples.
To do so, simply ensure that each instance is using the same Storage. That is the sole criteria for determining whether an instance is part of a cluster.
The default Storage is implemented using the file system, so mounting the same shared folder is sufficient (seeStorage for more on that)! If you need an alternate Storage implementation, feel free to use one, provided that all the instances use thesame one. :)
SeeStorage and the associatedpkg.go.dev for more information!
The ACME Challenges
This section describes how to solve the ACME challenges. Challenges are how you demonstrate to the certificate authority some control over your domain name, thus authorizing them to grant you a certificate for that name.The great innovation of ACME is that verification by CAs can now be automated, rather than having to click links in emails (who ever thought that was a good idea??).
If you're using the high-level convenience functions likeHTTPS(),Listen(), orTLS(), the HTTP and/or TLS-ALPN challenges are solved for you because they also start listeners. However, if you're making aConfig and you start your own server manually, you'll need to be sure the ACME challenges can be solved so certificates can be renewed.
The HTTP and TLS-ALPN challenges are the defaults because they don't require configuration from you, but they require that your server is accessible from external IPs on low ports. If that is not possible in your situation, you can enable the DNS challenge, which will disable the HTTP and TLS-ALPN challenges and use the DNS challenge exclusively.
Technically, only one challenge needs to be enabled for things to work, but using multiple is good for reliability in case a challenge is discontinued by the CA. This happened to the TLS-SNI challenge in early 2018—many popular ACME clients such as Traefik and Autocert broke, resulting in downtime for some sites, until new releases were made and patches deployed, because they used only one challenge; Caddy, however—this library's forerunner—was unaffected because it also used the HTTP challenge. If multiple challenges are enabled, they are chosen randomly to help prevent false reliance on a single challenge type. And if one fails, any remaining enabled challenges are tried before giving up.
HTTP Challenge
Per the ACME spec, the HTTP challenge requires port 80, or at least packet forwarding from port 80. It works by serving a specific HTTP response that only the genuine server would have to a normal HTTP request at a special endpoint.
If you are running an HTTP server, solving this challenge is very easy: just wrap your handler inHTTPChallengeHandleror callSolveHTTPChallenge() inside your ownServeHTTP() method.
For example, if you're using the standard library:
mux := http.NewServeMux()mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {fmt.Fprintf(w, "Lookit my cool website over HTTPS!")})http.ListenAndServe(":80", myACME.HTTPChallengeHandler(mux))If wrapping your handler is not a good solution, try this inside yourServeHTTP() instead:
magic := certmagic.NewDefault()myACME := certmagic.NewACMEIssuer(magic, certmagic.DefaultACME)func ServeHTTP(w http.ResponseWriter, req *http.Request) {if myACME.HandleHTTPChallenge(w, r) {return // challenge handled; nothing else to do}...}If you are not running an HTTP server, you should disable the HTTP challengeor run an HTTP server whose sole job it is to solve the HTTP challenge.
TLS-ALPN Challenge
Per the ACME spec, the TLS-ALPN challenge requires port 443, or at least packet forwarding from port 443. It works by providing a special certificate using a standard TLS extension, Application Layer Protocol Negotiation (ALPN), having a special value. This is the most convenient challenge type because it usually requires no extra configuration and uses the standard TLS port which is where the certificates are used, also.
This challenge is easy to solve: just use the providedtls.Config when you make your TLS listener:
// use this to configure a TLS listenertlsConfig := magic.TLSConfig()Or make two simple changes to an existingtls.Config:
myTLSConfig.GetCertificate = magic.GetCertificatemyTLSConfig.NextProtos = append(myTLSConfig.NextProtos, acmez.ACMETLS1Protocol}Then just make sure your TLS listener is listening on port 443:
ln, err := tls.Listen("tcp", ":443", myTLSConfig)DNS Challenge
The DNS challenge is perhaps the most useful challenge because it allows you to obtain certificates without your server needing to be publicly accessible on the Internet, and it's the only challenge by which Let's Encrypt will issue wildcard certificates.
This challenge works by setting a special record in the domain's zone. To do this automatically, your DNS provider needs to offer an API by which changes can be made to domain names, and the changes need to take effect immediately for best results. CertMagic supportsall DNS providers withlibdns implementations! It always cleans up the temporary record after the challenge completes.
To enable it, just set theDNS01Solver field on acertmagic.ACMEIssuer struct, or set the defaultcertmagic.ACMEIssuer.DNS01Solver variable. For example, if my domains' DNS was served by Cloudflare:
import "github.com/libdns/cloudflare"certmagic.DefaultACME.DNS01Solver = &certmagic.DNS01Solver{DNSManager: certmagic.DNSManager{DNSProvider: &cloudflare.Provider{APIToken: "topsecret",},},}Now the DNS challenge will be used by default, and I can obtain certificates for wildcard domains, too. Enabling the DNS challenge disables the other challenges for thatcertmagic.ACMEIssuer instance.
On-Demand TLS
Normally, certificates are obtained and renewed before a listener starts serving, and then those certificates are maintained throughout the lifetime of the program. In other words, the certificate names are static. But sometimes you don't know all the names ahead of time, or you don't want to manage all the certificates up front. This is where On-Demand TLS shines.
Originally invented for use in Caddy (which was the first program to use such technology), On-Demand TLS makes it possible and easy to serve certificates for arbitrary or specific names during the lifetime of the server. When a TLS handshake is received, CertMagic will read the Server Name Indication (SNI) value and either load and present that certificate in the ServerHello, or if one does not exist, it will obtain it from a CA right then-and-there.
Of course, this has some obvious security implications. You don't want to DoS a CA or allow arbitrary clients to fill your storage with spammy TLS handshakes. That's why, when you enable On-Demand issuance, you should set limits or policy to allow getting certificates. CertMagic has an implicit whitelist built-in which is sufficient for nearly everyone, but also has a more advanced way to control on-demand issuance.
The simplest way to enable on-demand issuance is to set the OnDemand field of a Config (or the default package-level value):
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)By setting this to a non-nil value, on-demand TLS is enabled for that config. For convenient security, CertMagic's high-level abstraction functions such asHTTPS(),TLS(),ManageSync(),ManageAsync(), andListen() (which all accept a list of domain names) will whitelist those names automatically so only certificates for those names can be obtained when using the Default config. Usually this is sufficient for most users.
However, if you require advanced control over which domains can be issued certificates on-demand (for example, if you do not know which domain names you are managing, or just need to defer their operations until later), you should implement your own DecisionFunc:
// if the decision function returns an error, a certificate// may not be obtained for that name at that timecertmagic.Default.OnDemand = &certmagic.OnDemandConfig{DecisionFunc: func(name string) error {if name != "example.com" {return fmt.Errorf("not allowed")}return nil},}Thepkg.go.dev describes how to use this in full detail, so please check it out!
Storage
CertMagic relies on storage to store certificates and other TLS assets (OCSP staple cache, coordinating locks, etc). Persistent storage is a requirement when using CertMagic: ephemeral storage will likely lead to rate limiting on the CA-side as CertMagic will always have to get new certificates.
By default, CertMagic stores assets on the local file system in$HOME/.local/share/certmagic (and honors$XDG_DATA_HOME if set). CertMagic will create the directory if it does not exist. If writes are denied, things will not be happy, so make sure CertMagic can write to it!
The notion of a "cluster" or "fleet" of instances that may be serving the same site and sharing certificates, etc, is tied to storage. Simply, any instances that use the same storage facilities are considered part of the cluster. So if you deploy 100 instances of CertMagic behind a load balancer, they are all part of the same cluster if they share the same storage configuration. Sharing storage could be mounting a shared folder, or implementing some other distributed storage system such as a database server or KV store.
The easiest way to change the storage being used is to setcertmagic.Default.Storage to a value that satisfies theStorage interface. Keep in mind that a validStorage must be able to implement some operations atomically in order to provide locking and synchronization.
If you write a Storage implementation, please add it to theproject wiki so people can find it!
Cache
All of the certificates in use are de-duplicated and cached in memory for optimal performance at handshake-time. This cache must be backed by persistent storage as described above.
Most applications will not need to interact with certificate caches directly. Usually, the closest you will come is to set the package-widecertmagic.Default.Storage variable (before attempting to create any Configs) which defines how the cache is persisted. However, if your use case requires using different storage facilities for different Configs (that's highly unlikely and NOT recommended! Even Caddy doesn't get that crazy), you will need to callcertmagic.NewCache() and pass in the storage you want to use, then get newConfig structs withcertmagic.NewWithCache() and pass in the cache.
Again, if you're needing to do this, you've probably over-complicated your application design.
Events
(Events are new and still experimental, so they may change.)
CertMagic emits events when possible things of interest happen. Set theOnEvent field of yourConfig to subscribe to events; ignore the ones you aren't interested in. Here are the events currently emitted along with their metadata you can use:
cached_unmanaged_certAn unmanaged certificate was cachedsans: The subject names on the certificate
cert_obtainingA certificate is about to be obtainedrenewal: Whether this is a renewalidentifier: The name on the certificateforced: Whether renewal is being forced (if renewal)remaining: Time left on the certificate (if renewal)issuer: The previous or current issuer
cert_obtainedA certificate was successfully obtainedrenewal: Whether this is a renewalidentifier: The name on the certificateremaining: Time left on the certificate (if renewal)issuer: The previous or current issuerstorage_path: The path to the folder containing the cert resources within storageprivate_key_path: The path to the private key file in storagecertificate_path: The path to the public key file in storagemetadata_path: The path to the metadata file in storage
cert_failedAn attempt to obtain a certificate failedrenewal: Whether this is a renewalidentifier: The name on the certificateremaining: Time left on the certificate (if renewal)issuers: The issuer(s) triederror: The (final) error message
tls_get_certificateThe GetCertificate phase of a TLS handshake is under wayclient_hello: The tls.ClientHelloInfo struct
cert_ocsp_revokedA certificate's OCSP indicates it has been revokedsubjects: The subject names on the certificatecertificate: The Certificate structreason: The OCSP revocation reasonrevoked_at: When the certificate was revoked
OnEvent can return an error. Some events may be aborted by returning an error. For example, returning an error fromcert_obtained can cancel obtaining the certificate. Only return an error fromOnEvent if you want to abort program flow.
ZeroSSL
ZeroSSL has both ACME and HTTP API services for getting certificates. CertMagic works with both of them.
To use ZeroSSL's ACME server, configure CertMagic with anACMEIssuer like you would with any other ACME CA (just adjust the directory URL). External Account Binding (EAB) is required for ZeroSSL. You can use theZeroSSL API to generate one, or your account dashboard.
To use ZeroSSL's API instead, use theZeroSSLIssuer. Here is a simple example:
magic := certmagic.NewDefault()magic.Issuers = []certmagic.Issuer{certmagic.ZeroSSLIssuer{APIKey: "<your ZeroSSL API key>",}),}err := magic.ManageSync(ctx, []string{"example.com"})FAQ
Can I use some of my own certificates while using CertMagic?
Yes, just call the relevant method on theConfig to add your own certificate to the cache:
Keep in mind that unmanaged certificates are (obviously) not renewed for you, so you'll have to replace them when you do. However, OCSP stapling is performed even for unmanaged certificates that qualify.
Does CertMagic obtain SAN certificates?
Technically all certificates these days are SAN certificates because CommonName is deprecated. But if you're asking whether CertMagic issues and manages certificates with multiple SANs, the answer is no. But it does support serving them, if you provide your own.
How can I listen on ports 80 and 443? Do I have to run as root?
On Linux, you can usesetcap to grant your binary the permission to bind low ports:
$ sudo setcap cap_net_bind_service=+ep /path/to/your/binaryand then you will not need to run with root privileges.
Contributing
We welcome your contributions! Please see ourcontributing guidelines for instructions.
Project History
CertMagic is the core of Caddy's advanced TLS automation code, extracted into a library. The underlying ACME client implementation isACMEz. CertMagic's code was originally a central part of Caddy even before Let's Encrypt entered public beta in 2015.
In the years since then, Caddy's TLS automation techniques have been widely adopted, tried and tested in production, and served millions of sites and secured trillions of connections.
Now, CertMagic isthe actual library used by Caddy. It's incredibly powerful and feature-rich, but also easy to use for simple Go programs: one line of code can enable fully-automated HTTPS applications with HTTP->HTTPS redirects.
Caddy is known for its robust HTTPS+ACME features. When ACME certificate authorities have had outages, in some cases Caddy was the only major client that didn't experience any downtime. Caddy can weather OCSP outages lasting days, or CA outages lasting weeks, without taking your sites offline.
Caddy was also the first to sport "on-demand" issuance technology, which obtains certificates during the first TLS handshake for an allowed SNI name.
Consequently, CertMagic brings all these (and more) features and capabilities right into your own Go programs.
You canwatch a 2016 dotGo talk by the author of this library about using ACME to automate certificate management in Go programs:
Credits and License
CertMagic is a project byMatthew Holt, who is the author; and various contributors, who are credited in the commit history of either CertMagic or Caddy.
CertMagic is licensed under Apache 2.0, an open source license. For convenience, its main points are summarized as follows (but this is no replacement for the actual license text):
- The author owns the copyright to this code
- Use, distribute, and modify the software freely
- Private and internal use is allowed
- License text and copyright notices must stay intact and be included with distributions
- Any and all changes to the code must be documented
Documentation¶
Overview¶
Package certmagic automates the obtaining and renewal of TLS certificates,including TLS & HTTPS best practices such as robust OCSP stapling, caching,HTTP->HTTPS redirects, and more.
Its high-level API serves your HTTP handlers over HTTPS if you simply givethe domain name(s) and the http.Handler; CertMagic will create and runthe HTTPS server for you, fully managing certificates during the lifetimeof the server. Similarly, it can be used to start TLS listeners or returna ready-to-use tls.Config -- whatever layer you need TLS for, CertMagicmakes it easy. See the HTTPS, Listen, and TLS functions for that.
If you need more control, create a Cache using NewCache() and then makea Config using New(). You can then call Manage() on the config. But ifyou use this lower-level API, you'll have to be sure to solve the HTTPand TLS-ALPN challenges yourself (unless you disabled them or use theDNS challenge) by using the provided Config.GetCertificate functionin your tls.Config and/or Config.HTTPChallengeHandler in your HTTPhandler.
See the package's README for more instruction.
Index¶
- Constants
- Variables
- func CleanStorage(ctx context.Context, storage Storage, opts CleanStorageOptions) error
- func CleanUpOwnLocks(ctx context.Context, logger *zap.Logger)
- func FindZoneByFQDN(ctx context.Context, logger *zap.Logger, fqdn string, nameservers []string) (string, error)
- func HTTPS(domainNames []string, mux http.Handler) error
- func Listen(domainNames []string) (net.Listener, error)
- func LooksLikeHTTPChallenge(r *http.Request) bool
- func LooksLikeZeroSSLHTTPValidation(r *http.Request) bool
- func ManageAsync(ctx context.Context, domainNames []string) error
- func ManageSync(ctx context.Context, domainNames []string) error
- func MatchWildcard(subject, wildcard string) bool
- func PEMDecodePrivateKey(keyPEMBytes []byte) (crypto.Signer, error)
- func PEMEncodePrivateKey(key crypto.PrivateKey) ([]byte, error)
- func RecursiveNameservers(custom []string) []string
- func SolveHTTPChallenge(logger *zap.Logger, w http.ResponseWriter, r *http.Request, ...) bool
- func SubjectIsIP(subj string) bool
- func SubjectIsInternal(subj string) bool
- func SubjectQualifiesForCert(subj string) bool
- func SubjectQualifiesForPublicCert(subj string) bool
- func TLS(domainNames []string) (*tls.Config, error)
- type ACMEIssuer
- func (am *ACMEIssuer) GetAccount(ctx context.Context, privateKeyPEM []byte) (acme.Account, error)
- func (iss *ACMEIssuer) GetRenewalInfo(ctx context.Context, cert Certificate) (acme.RenewalInfo, error)
- func (am *ACMEIssuer) HTTPChallengeHandler(h http.Handler) http.Handler
- func (am *ACMEIssuer) HandleHTTPChallenge(w http.ResponseWriter, r *http.Request) bool
- func (am *ACMEIssuer) Issue(ctx context.Context, csr *x509.CertificateRequest) (*IssuedCertificate, error)
- func (am *ACMEIssuer) IssuerKey() string
- func (am *ACMEIssuer) PreCheck(ctx context.Context, names []string, interactive bool) error
- func (am *ACMEIssuer) Revoke(ctx context.Context, cert CertificateResource, reason int) error
- type Cache
- func (certCache *Cache) AllMatchingCertificates(name string) []Certificate
- func (certCache *Cache) Remove(hashes []string)
- func (certCache *Cache) RemoveManaged(subjects []SubjectIssuer)
- func (certCache *Cache) RenewManagedCertificates(ctx context.Context) error
- func (certCache *Cache) SetOptions(opts CacheOptions)
- func (certCache *Cache) Stop()
- type CacheOptions
- type Certificate
- type CertificateResource
- type CertificateSelector
- type ChainPreference
- type Challenge
- type CleanStorageOptions
- type Config
- func (cfg *Config) CacheManagedCertificate(ctx context.Context, domain string) (Certificate, error)
- func (cfg *Config) CacheUnmanagedCertificatePEMBytes(ctx context.Context, certBytes, keyBytes []byte, tags []string) (string, error)
- func (cfg *Config) CacheUnmanagedCertificatePEMBytesAsReplacement(ctx context.Context, certBytes, keyBytes []byte, tags, sans []string) (string, error)
- func (cfg *Config) CacheUnmanagedCertificatePEMFile(ctx context.Context, certFile, keyFile string, tags []string) (string, error)
- func (cfg *Config) CacheUnmanagedTLSCertificate(ctx context.Context, tlsCert tls.Certificate, tags []string) (string, error)
- func (cfg *Config) ClientCredentials(ctx context.Context, identifiers []string) ([]tls.Certificate, error)
- func (cfg *Config) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error)
- func (cfg *Config) GetCertificateWithContext(ctx context.Context, clientHello *tls.ClientHelloInfo) (*tls.Certificate, error)
- func (cfg *Config) ManageAsync(ctx context.Context, domainNames []string) error
- func (cfg *Config) ManageSync(ctx context.Context, domainNames []string) error
- func (cfg *Config) ObtainCertAsync(ctx context.Context, name string) error
- func (cfg *Config) ObtainCertSync(ctx context.Context, name string) error
- func (cfg *Config) RenewCertAsync(ctx context.Context, name string, force bool) error
- func (cfg *Config) RenewCertSync(ctx context.Context, name string, force bool) error
- func (cfg *Config) RevokeCert(ctx context.Context, domain string, reason int, interactive bool) error
- func (cfg *Config) TLSConfig() *tls.Config
- type ConfigGetter
- type DNS01Solver
- type DNSManager
- type DNSProvider
- type ErrNoRetry
- type FileStorage
- func (s *FileStorage) Delete(_ context.Context, key string) error
- func (s *FileStorage) Exists(_ context.Context, key string) bool
- func (s *FileStorage) Filename(key string) string
- func (s *FileStorage) List(ctx context.Context, prefix string, recursive bool) ([]string, error)
- func (s *FileStorage) Load(_ context.Context, key string) ([]byte, error)
- func (s *FileStorage) Lock(ctx context.Context, name string) error
- func (s *FileStorage) Stat(_ context.Context, key string) (KeyInfo, error)
- func (s *FileStorage) Store(_ context.Context, key string, value []byte) error
- func (s *FileStorage) String() string
- func (s *FileStorage) Unlock(_ context.Context, name string) error
- type IssuedCertificate
- type Issuer
- type IssuerPolicy
- type KeyBuilder
- func (keys KeyBuilder) CertsPrefix(issuerKey string) string
- func (keys KeyBuilder) CertsSitePrefix(issuerKey, domain string) string
- func (keys KeyBuilder) OCSPStaple(cert *Certificate, pemBundle []byte) string
- func (keys KeyBuilder) Safe(str string) string
- func (keys KeyBuilder) SiteCert(issuerKey, domain string) string
- func (keys KeyBuilder) SiteMeta(issuerKey, domain string) string
- func (keys KeyBuilder) SitePrivateKey(issuerKey, domain string) string
- type KeyGenerator
- type KeyInfo
- type KeyType
- type Locker
- type Manager
- type OCSPConfig
- type OnDemandConfig
- type PreChecker
- type RenewalInfoGetter
- type Revoker
- type RingBufferRateLimiter
- func (r *RingBufferRateLimiter) Allow() bool
- func (r *RingBufferRateLimiter) MaxEvents() int
- func (r *RingBufferRateLimiter) SetMaxEvents(maxEvents int)
- func (r *RingBufferRateLimiter) SetWindow(window time.Duration)
- func (r *RingBufferRateLimiter) Stop()
- func (r *RingBufferRateLimiter) Wait(ctx context.Context) error
- func (r *RingBufferRateLimiter) Window() time.Duration
- type StandardKeyGenerator
- type Storage
- type SubjectIssuer
- type ZeroSSLIssuer
- func (iss *ZeroSSLIssuer) HTTPValidationHandler(h http.Handler) http.Handler
- func (iss *ZeroSSLIssuer) HandleZeroSSLHTTPValidation(w http.ResponseWriter, r *http.Request) bool
- func (iss *ZeroSSLIssuer) Issue(ctx context.Context, csr *x509.CertificateRequest) (*IssuedCertificate, error)
- func (iss *ZeroSSLIssuer) IssuerKey() string
- func (iss *ZeroSSLIssuer) Revoke(ctx context.Context, cert CertificateResource, reason int) error
Examples¶
Constants¶
const (LetsEncryptStagingCA = "https://acme-staging-v02.api.letsencrypt.org/directory"//https://letsencrypt.org/docs/staging-environment/LetsEncryptProductionCA = "https://acme-v02.api.letsencrypt.org/directory"//https://letsencrypt.org/getting-started/ZeroSSLProductionCA = "https://acme.zerossl.com/v2/DV90"//https://zerossl.com/documentation/acme/GoogleTrustStagingCA = "https://dv.acme-v02.test-api.pki.goog/directory"//https://cloud.google.com/certificate-manager/docs/public-ca-tutorialGoogleTrustProductionCA = "https://dv.acme-v02.api.pki.goog/directory"//https://cloud.google.com/certificate-manager/docs/public-ca-tutorial)
Some well-known CA endpoints available to use. Seethe documentation for each service; some may requireExternal Account Binding (EAB) and possibly payment.COMPATIBILITY NOTICE: These constants refer to externalresources and are thus subject to change or removalwithout a major version bump.
const (// UseFirstIssuer uses the first issuer that// successfully returns a certificate.UseFirstIssuer = "first"// UseFirstRandomIssuer shuffles the list of// configured issuers, then uses the first one// that successfully returns a certificate.UseFirstRandomIssuer = "first_random")
Supported issuer policies. These are subject to change.
const (// HTTPChallengePort is the officially-designated port for// the HTTP challenge according to the ACME spec.HTTPChallengePort = 80// TLSALPNChallengePort is the officially-designated port for// the TLS-ALPN challenge according to the ACME spec.TLSALPNChallengePort = 443)
const (ED25519 =KeyType("ed25519")P256 =KeyType("p256")P384 =KeyType("p384")RSA2048 =KeyType("rsa2048")RSA4096 =KeyType("rsa4096")RSA8192 =KeyType("rsa8192"))
Constants for all key types we support.
const (// DefaultRenewCheckInterval is how often to check certificates for expiration.// Scans are very lightweight, so this can be semi-frequent. This default should// be smaller than <Minimum Cert Lifetime>*DefaultRenewalWindowRatio/3, which// gives certificates plenty of chance to be renewed on time.DefaultRenewCheckInterval = 10 *time.Minute// DefaultRenewalWindowRatio is how much of a certificate's lifetime becomes the// renewal window. The renewal window is the span of time at the end of the// certificate's validity period in which it should be renewed. A default value// of ~1/3 is pretty safe and recommended for most certificates.DefaultRenewalWindowRatio = 1.0 / 3.0// DefaultOCSPCheckInterval is how often to check if OCSP stapling needs updating.DefaultOCSPCheckInterval = 1 *time.Hour)
const ClientHelloInfoCtxKey helloInfoCtxKey = "certmagic:ClientHelloInfo"ClientHelloInfoCtxKey is the key by which the ClientHelloInfo can be extracted froma context.Context within a DecisionFunc. However, be advised that it is best practicethat the decision whether to obtain a certificate is be based solely on the name,not other properties of the specific connection/client requesting the connection.For example, it is not advisable to use a client's IP address to decide whether toallow a certificate. Instead, the ClientHello can be useful for logging, etc.
Variables¶
var (// RateLimitEvents is how many new events can be allowed// in RateLimitEventsWindow.RateLimitEvents = 10// RateLimitEventsWindow is the size of the sliding// window that throttles events.RateLimitEventsWindow = 10 *time.Second)
These internal rate limits are designed to prevent accidentallyfirehosing a CA's ACME endpoints. They are not intended toreplace or replicate the CA's actual rate limits.
Let's Encrypt's rate limits can be found here:https://letsencrypt.org/docs/rate-limits/
Currently (as of December 2019), Let's Encrypt's most relevantrate limit for large deployments is 300 new orders per accountper 3 hours (on average, or best case, that's about 1 every 36seconds, or 2 every 72 seconds, etc.); but it's not reasonableto try to assume that our internal state is the same as the CA's(due to process restarts, config changes, failed validations,etc.) and ultimately, only the CA's actual rate limiter is theauthority. Thus, our own rate limiters do not attempt to enforceexternal rate limits. Doing so causes problems when the domainsare not in our control (i.e. serving customer sites) and/or lotsof domains fail validation: they clog our internal rate limiterand nearly starve out (or at least slow down) the other domainsthat need certificates. Failed transactions are already retriedwith exponential backoff, so adding in rate limiting can slowthings down even more.
Instead, the point of our internal rate limiter is to avoidhammering the CA's endpoint when there are thousands or evenmillions of certificates under management. Our goal is toallow small bursts in a relatively short timeframe so as tonot block any one domain for too long, without unleashingthousands of requests to the CA at once.
var (UserAgentstringHTTPTimeout = 30 *time.Second)
Some default values passed down to the underlying ACME client.
var (// HTTPPort is the port on which to serve HTTP// and, as such, the HTTP challenge (unless// Default.AltHTTPPort is set).HTTPPort = 80// HTTPSPort is the port on which to serve HTTPS// and, as such, the TLS-ALPN challenge// (unless Default.AltTLSALPNPort is set).HTTPSPort = 443)
Port variables must remain their defaults unless youforward packets from the defaults to whatever theseare set to; otherwise ACME challenges will fail.
var AttemptsCtxKey retryStateCtxKeyAttemptsCtxKey is the context key for the valuethat holds the attempt counter. The value countshow many times the operation has been attempted.A value of 0 means first attempt.
var Default =Config{RenewalWindowRatio:DefaultRenewalWindowRatio,Storage: defaultFileStorage,KeySource:DefaultKeyGenerator,Logger: defaultLogger,}
Default contains the package defaults for thevarious Config fields. This is used as a templatewhen creating your own Configs with New() orNewDefault(), and it is also used as the Configby all the high-level functions in this packagethat abstract away most configuration (HTTPS(),TLS(), Listen(), etc).
The fields of this value will be used for Configfields which are unset. Feel free to modify thesedefaults, but do not use this Config by itself: itis only a template. Valid configurations can beobtained by calling New() (if you have your owncertificate cache) or NewDefault() (if you onlyneed a single config and want to use the defaultcache).
Even if the Issuers or Storage fields are not set,defaults will be applied in the call to New().
var DefaultACME =ACMEIssuer{CA:LetsEncryptProductionCA,TestCA:LetsEncryptStagingCA,Logger: defaultLogger,HTTPProxy:http.ProxyFromEnvironment,}
DefaultACME specifies default settings to use for ACMEIssuers.Using this value is optional but can be convenient.
var DefaultKeyGenerator =StandardKeyGenerator{KeyType:P256}DefaultKeyGenerator is the default key source.
var ErrNoOCSPServerSpecified =errors.New("no OCSP server specified in certificate")ErrNoOCSPServerSpecified indicates that OCSP information could not bestapled because the certificate does not support OCSP.
Functions¶
funcCleanStorage¶
func CleanStorage(ctxcontext.Context, storageStorage, optsCleanStorageOptions)error
CleanStorage removes assets which are no longer useful,according to opts.
funcCleanUpOwnLocks¶
CleanUpOwnLocks immediately cleans up allcurrent locks obtained by this process. Sincethis does not cancel the operations thatthe locks are synchronizing, this should becalled only immediately before process exit.Errors are only reported if a logger is given.
funcFindZoneByFQDN¶added inv0.22.0
func FindZoneByFQDN(ctxcontext.Context, logger *zap.Logger, fqdnstring, nameservers []string) (string,error)
FindZoneByFQDN determines the zone apex for the given fully-qualifieddomain name (FQDN) by recursing up the domain labels until the nameserverreturns a SOA record in the answer section. The logger must be non-nil.
EXPERIMENTAL: This API was previously unexported, and may be changed orunexported again in the future. Do not rely on it at this time.
funcHTTPS¶
HTTPS serves mux for all domainNames using the HTTPand HTTPS ports, redirecting all HTTP requests to HTTPS.It uses the Default config and a background context.
This high-level convenience function is opinionated andapplies sane defaults for production use, includingtimeouts for HTTP requests and responses. To allow verylong-lived connections, you should make your ownhttp.Server values and use this package's Listen(), TLS(),or Config.TLSConfig() functions to customize to your needs.For example, servers which need to support large uploads ordownloads with slow clients may need to use longer timeouts,thus this function is not suitable.
Calling this function signifies your acceptance tothe CA's Subscriber Agreement and/or Terms of Service.
Example¶
This is the simplest way for HTTP servers to use this package.Call HTTPS() with your domain names and your handler (or nilfor the http.DefaultMux), and CertMagic will do the rest.
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {fmt.Fprintf(w, "Hello, HTTPS visitor!")})err := HTTPS([]string{"example.com", "www.example.com"}, nil)if err != nil {log.Fatal(err)}funcListen¶
Listen manages certificates for domainName and returns aTLS listener. It uses the Default config.
Because this convenience function returns only a TLS-enabledlistener and does not presume HTTP is also being served,the HTTP challenge will be disabled. The package variableDefault is modified so that the HTTP challenge is disabled.
Calling this function signifies your acceptance tothe CA's Subscriber Agreement and/or Terms of Service.
funcLooksLikeHTTPChallenge¶
LooksLikeHTTPChallenge returns true if r looks like an ACMEHTTP challenge request from an ACME server.
funcLooksLikeZeroSSLHTTPValidation¶added inv0.21.0
LooksLikeZeroSSLHTTPValidation returns true if the request appears to bedomain validation from a ZeroSSL/Sectigo CA. NOTE: This API isnon-standard and is subject to change.
funcManageAsync¶
ManageAsync is the same as ManageSync, except thatcertificates are managed asynchronously. This meansthat the function will return before certificatesare ready, and errors that occur during certificateobtain or renew operations are only logged. It isvital that you monitor the logs if using this method,which is only recommended for automated/non-interactiveenvironments.
funcManageSync¶
ManageSync obtains certificates for domainNames and keeps themrenewed using the Default config.
This is a slightly lower-level function; you will need towire up support for the ACME challenges yourself. You canobtain a Config to help you do that by calling NewDefault().
You will need to ensure that you use a TLS config that getscertificates from this Config and that the HTTP and TLS-ALPNchallenges can be solved. The easiest way to do this is touse NewDefault().TLSConfig() as your TLS config and to wrapyour HTTP handler with NewDefault().HTTPChallengeHandler().If you don't have an HTTP server, you will need to disablethe HTTP challenge.
If you already have a TLS config you want to use, you cansimply set its GetCertificate field toNewDefault().GetCertificate.
Calling this function signifies your acceptance tothe CA's Subscriber Agreement and/or Terms of Service.
funcMatchWildcard¶added inv0.10.5
MatchWildcard returns true if subject (a candidate DNS name)matches wildcard (a reference DNS name), mostly according toRFC 6125-compliant wildcard rules. See alsoRFC 2818 whichstates that IP addresses must match exactly, but this functiondoes not attempt to distinguish IP addresses from internal orexternal DNS names that happen to look like IP addresses.It uses DNS wildcard matching logic and is case-insensitive.https://tools.ietf.org/html/rfc2818#section-3.1
funcPEMDecodePrivateKey¶added inv0.15.4
PEMDecodePrivateKey loads a PEM-encoded ECC/RSA private key from an array of bytes.Borrowed from Go standard library, to handle various private key and PEM block types.
funcPEMEncodePrivateKey¶added inv0.15.4
func PEMEncodePrivateKey(keycrypto.PrivateKey) ([]byte,error)
PEMEncodePrivateKey marshals a private key into a PEM-encoded block.The private key must be one of *ecdsa.PrivateKey, *rsa.PrivateKey, or*ed25519.PrivateKey.
funcRecursiveNameservers¶added inv0.22.0
RecursiveNameservers are used to pre-check DNS propagation. Itpicks user-configured nameservers (custom) OR the defaultsobtained from resolv.conf and defaultNameservers if none isconfigured and ensures that all server addresses have a port value.
EXPERIMENTAL: This API was previously unexported, and may bebe unexported again in the future. Do not rely on it at this time.
funcSolveHTTPChallenge¶added inv0.13.0
func SolveHTTPChallenge(logger *zap.Logger, whttp.ResponseWriter, r *http.Request, challengeacme.Challenge)bool
SolveHTTPChallenge solves the HTTP challenge. It should be used only on HTTP requests that arefrom ACME servers trying to validate an identifier (i.e. LooksLikeHTTPChallenge() == true). Itreturns true if the request criteria check out and it answered with key authentication, in whichcase no further handling of the request is necessary.
funcSubjectIsIP¶added inv0.13.0
SubjectIsIP returns true if subj is an IP address.
funcSubjectIsInternal¶added inv0.13.0
SubjectIsInternal returns true if subj is an internal-facinghostname or address, including localhost/loopback hosts.Ports are ignored, if present.
funcSubjectQualifiesForCert¶added inv0.10.2
SubjectQualifiesForCert returns true if subj is a name which,as a quick sanity check, looks like it could be the subjectof a certificate. Requirements are:- must not be empty- must not start or end with a dot (RFC 1034;RFC 6066 section 3)- must not contain common accidental special characters
funcSubjectQualifiesForPublicCert¶added inv0.10.1
SubjectQualifiesForPublicCert returns true if the subjectname appears eligible for automagic TLS with a publicCA such as Let's Encrypt. For example: internal IP addressesand localhost are not eligible because we cannot obtain certsfor those names with a public CA. Wildcard names areallowed, as long as they conform to CABF requirements (onlyone wildcard label, and it must be the left-most label).
funcTLS¶
TLS enables management of certificates for domainNamesand returns a valid tls.Config. It uses the Defaultconfig.
Because this is a convenience function that returnsonly a tls.Config, it does not assume HTTP is beingserved on the HTTP port, so the HTTP challenge isdisabled (no HTTPChallengeHandler is necessary). Thepackage variable Default is modified so that theHTTP challenge is disabled.
Calling this function signifies your acceptance tothe CA's Subscriber Agreement and/or Terms of Service.
Types¶
typeACMEIssuer¶added inv0.16.0
type ACMEIssuer struct {// The endpoint of the directory for the ACME// CA we are to useCAstring// TestCA is the endpoint of the directory for// an ACME CA to use to test domain validation,// but any certs obtained from this CA are// discarded; it should perform real and valid// ACME verifications, but probably should not// issue real, publicly-trusted certificatesTestCAstring// The email address to use when creating or// selecting an existing ACME server accountEmailstring// The PEM-encoded private key of the ACME// account to use; only needed if the account// is already created on the server and// can be looked up with the ACME protocolAccountKeyPEMstring// Set to true if agreed to the CA's// subscriber agreementAgreedbool// An optional external account to associate// with this ACME accountExternalAccount *acme.EAB// Optionally select an ACME profile offered// by the ACME server. The list of supported// profile names can be obtained from the ACME// server's directory endpoint. For details://https://datatracker.ietf.org/doc/draft-aaron-acme-profiles///// (EXPERIMENTAL: Subject to change.)Profilestring// Optionally specify the validity period of// the certificate(s) here as offsets from the// approximate time of certificate issuance,// but note that not all CAs support this// (EXPERIMENTAL: Subject to change)NotBefore, NotAftertime.Duration// Disable all HTTP challengesDisableHTTPChallengebool// Disable all TLS-ALPN challengesDisableTLSALPNChallengebool// The host (ONLY the host, not port) to listen// on if necessary to start a listener to solve// an ACME challengeListenHoststring// The alternate port to use for the ACME HTTP// challenge; if non-empty, this port will be// used instead of HTTPChallengePort to spin up// a listener for the HTTP challengeAltHTTPPortint// The alternate port to use for the ACME// TLS-ALPN challenge; the system must forward// TLSALPNChallengePort to this port for// challenge to succeedAltTLSALPNPortint// The solver for the dns-01 challenge;// usually this is a DNS01Solver value// from this packageDNS01Solver acmez.Solver// TrustedRoots specifies a pool of root CA// certificates to trust when communicating// over a network to a peer.TrustedRoots *x509.CertPool// The maximum amount of time to allow for// obtaining a certificate. If empty, the// default from the underlying ACME lib is// used. If set, it must not be too low so// as to cancel challenges too early.CertObtainTimeouttime.Duration// Address of custom DNS resolver to be used// when communicating with ACME serverResolverstring// Callback function that is called before a// new ACME account is registered with the CA;// it allows for last-second config changes// of the ACMEIssuer and the Account.// (TODO: this feature is still EXPERIMENTAL and subject to change)NewAccountFunc func(context.Context, *ACMEIssuer,acme.Account) (acme.Account,error)// Preferences for selecting alternate// certificate chainsPreferredChainsChainPreference// Set a logger to configure logging; a default// logger must always be set; if no logging is// desired, set this to zap.NewNop().Logger *zap.Logger// Set a http proxy to use when issuing a certificate.// Default is http.ProxyFromEnvironmentHTTPProxy func(*http.Request) (*url.URL,error)// contains filtered or unexported fields}ACMEIssuer gets certificates using ACME. It implements the PreChecker,Issuer, and Revoker interfaces.
It is NOT VALID to use an ACMEIssuer without calling NewACMEIssuer().It fills in any default values from DefaultACME as well as setting upinternal state that is necessary for valid use. Always callNewACMEIssuer() to get a valid ACMEIssuer value.
funcNewACMEIssuer¶added inv0.16.0
func NewACMEIssuer(cfg *Config, templateACMEIssuer) *ACMEIssuer
NewACMEIssuer constructs a valid ACMEIssuer based on a templateconfiguration; any empty values will be filled in by defaults inDefaultACME, and if any required values are still empty, sensibledefaults will be used.
Typically, you'll create the Config first with New() or NewDefault(),then call NewACMEIssuer(), then assign the return value to the Issuersfield of the Config.
func (*ACMEIssuer)GetAccount¶added inv0.16.0
GetAccount first tries loading the account with the associated private key from storage.If it does not exist in storage, it will be retrieved from the ACME server and added to storage.The account must already exist; it does not create a new account.
func (*ACMEIssuer)GetRenewalInfo¶added inv0.21.3
func (iss *ACMEIssuer) GetRenewalInfo(ctxcontext.Context, certCertificate) (acme.RenewalInfo,error)
GetRenewalInfo gets the ACME Renewal Information (ARI) for the certificate.
func (*ACMEIssuer)HTTPChallengeHandler¶added inv0.16.0
func (am *ACMEIssuer) HTTPChallengeHandler(hhttp.Handler)http.Handler
HTTPChallengeHandler wraps h in a handler that can solve the ACMEHTTP challenge. cfg is required, and it must have a certificatecache backed by a functional storage facility, since that is wherethe challenge state is stored between initiation and solution.
If a request is not an ACME HTTP challenge, h will be invoked.
func (*ACMEIssuer)HandleHTTPChallenge¶added inv0.16.0
func (am *ACMEIssuer) HandleHTTPChallenge(whttp.ResponseWriter, r *http.Request)bool
HandleHTTPChallenge uses am to solve challenge requests from an ACMEserver that were initiated by this instance or any other instance inthis cluster (being, any instances using the same storage am does).
If the HTTP challenge is disabled, this function is a no-op.
If am is nil or if am does not have a certificate cache backed byusable storage, solving the HTTP challenge will fail.
It returns true if it handled the request; if so, the response hasalready been written. If false is returned, this call was a no-op andthe request has not been handled.
func (*ACMEIssuer)Issue¶added inv0.16.0
func (am *ACMEIssuer) Issue(ctxcontext.Context, csr *x509.CertificateRequest) (*IssuedCertificate,error)
Issue implements the Issuer interface. It obtains a certificate for the given csr usingthe ACME configuration am.
func (*ACMEIssuer)IssuerKey¶added inv0.16.0
func (am *ACMEIssuer) IssuerKey()string
IssuerKey returns the unique issuer key for theconfigured CA endpoint.
func (*ACMEIssuer)PreCheck¶added inv0.16.0
PreCheck performs a few simple checks before obtaining orrenewing a certificate with ACME, and returns whether thisbatch is eligible for certificates. It also ensures that anemail address is available if possible.
IP certificates via ACME are defined inRFC 8738.
func (*ACMEIssuer)Revoke¶added inv0.16.0
func (am *ACMEIssuer) Revoke(ctxcontext.Context, certCertificateResource, reasonint)error
Revoke implements the Revoker interface. It revokes the given certificate.
typeCache¶
type Cache struct {// contains filtered or unexported fields}Cache is a structure that stores certificates in memory.A Cache indexes certificates by name for quick accessduring TLS handshakes, and avoids duplicating certificatesin memory. Generally, there should only be one per process.However, that is not a strict requirement; but using morethan one is a code smell, and may indicate anover-engineered design.
An empty cache is INVALID and must not be used. Be sureto call NewCache to get a valid value.
These should be very long-lived values and must not becopied. Before all references leave scope to be garbagecollected, ensure you call Stop() to stop maintenance onthe certificates stored in this cache and release locks.
Caches are not usually manipulated directly; create aConfig value with a pointer to a Cache, and then usethe Config to interact with the cache. Caches areagnostic of any particular storage or ACME config,since each certificate may be managed and storeddifferently.
funcNewCache¶
func NewCache(optsCacheOptions) *Cache
NewCache returns a new, valid Cache for efficientlyaccessing certificates in memory. It also begins amaintenance goroutine to tend to the certificatesin the cache. Call Stop() when you are done with thecache so it can clean up locks and stuff.
Most users of this package will not need to call thisbecause a default certificate cache is created for you.Only advanced use cases require creating a new cache.
This function panics if opts.GetConfigForCert is notset. The reason is that a cache absolutely needs tobe able to get a Config with which to manage TLSassets, and it is not safe to assume that the Defaultconfig is always the correct one, since you havecreated the cache yourself.
See the godoc for Cache to use it properly. Whenno longer needed, caches should be stopped withStop() to clean up resources even if the processis being terminated, so that it can clean upany locks for other processes to unblock!
func (*Cache)AllMatchingCertificates¶
func (certCache *Cache) AllMatchingCertificates(namestring) []Certificate
AllMatchingCertificates returns a list of all certificates that couldbe used to serve the given SNI name, including exact SAN matches andwildcard matches.
func (*Cache)Remove¶added inv0.19.0
Remove removes certificates with the given hashes from the cache.This is effectively used to unload manually-loaded certificates.
func (*Cache)RemoveManaged¶added inv0.19.0
func (certCache *Cache) RemoveManaged(subjects []SubjectIssuer)
RemoveManaged removes managed certificates for the given subjects from the cache.This effectively stops maintenance of those certificates. If an IssuerKey isspecified alongside the subject, only certificates for that subject from thespecified issuer will be removed.
func (*Cache)RenewManagedCertificates¶
RenewManagedCertificates renews managed certificates,including ones loaded on-demand. Note that this is doneautomatically on a regular basis; normally you will notneed to call this. This method assumes non-interactivemode (i.e. operating in the background).
func (*Cache)SetOptions¶added inv0.19.0
func (certCache *Cache) SetOptions(optsCacheOptions)
typeCacheOptions¶
type CacheOptions struct {// REQUIRED. A function that returns a configuration// used for managing a certificate, or for accessing// that certificate's asset storage (e.g. for// OCSP staples, etc). The returned Config MUST// be associated with the same Cache as the caller,// use New to obtain a valid Config.//// The reason this is a callback function, dynamically// returning a Config (instead of attaching a static// pointer to a Config on each certificate) is because// the config for how to manage a domain's certificate// might change from maintenance to maintenance. The// cache is so long-lived, we cannot assume that the// host's situation will always be the same; e.g. the// certificate might switch DNS providers, so the DNS// challenge (if used) would need to be adjusted from// the last time it was run ~8 weeks ago.GetConfigForCertConfigGetter// How often to check certificates for renewal;// if unset, DefaultOCSPCheckInterval will be used.OCSPCheckIntervaltime.Duration// How often to check certificates for renewal;// if unset, DefaultRenewCheckInterval will be used.RenewCheckIntervaltime.Duration// Maximum number of certificates to allow in the cache.// If reached, certificates will be randomly evicted to// make room for new ones. 0 means unlimited.Capacityint// Set a logger to enable loggingLogger *zap.Logger}CacheOptions is used to configure certificate caches.Once a cache has been created with certain options,those settings cannot be changed.
typeCertificate¶
type Certificate struct {tls.Certificate// Names is the list of subject names this// certificate is signed for.Names []string// Optional; user-provided, and arbitrary.Tags []string// contains filtered or unexported fields}Certificate is a tls.Certificate with associated metadata tacked on.Even if the metadata can be obtained by parsing the certificate,we are more efficient by extracting the metadata onto this struct,but at the cost of slightly higher memory use.
funcDefaultCertificateSelector¶added inv0.10.7
func DefaultCertificateSelector(hello *tls.ClientHelloInfo, choices []Certificate) (Certificate,error)
DefaultCertificateSelector is the default certificate selection logicgiven a choice of certificates. If there is at least one certificate inchoices, it always returns a certificate without error. It chooses thefirst non-expired certificate that the client supports if possible,otherwise it returns an expired certificate that the client supports,otherwise it just returns the first certificate in the list of choices.
func (Certificate)Empty¶added inv0.15.4
func (certCertificate) Empty()bool
Empty returns true if the certificate struct is not filled out; atleast the tls.Certificate.Certificate field is expected to be set.
func (Certificate)Expired¶added inv0.12.0
func (certCertificate) Expired()bool
Expired returns true if the certificate has expired.
func (Certificate)HasTag¶added inv0.10.7
func (certCertificate) HasTag(tagstring)bool
HasTag returns true if cert.Tags has tag.
func (Certificate)Hash¶added inv0.19.0
func (certCertificate) Hash()string
Hash returns a checksum of the certificate chain's DER-encoded bytes.
func (Certificate)Lifetime¶added inv0.21.5
func (certCertificate) Lifetime()time.Duration
Lifetime returns the duration of the certificate's validity.
func (Certificate)NeedsRenewal¶
func (certCertificate) NeedsRenewal(cfg *Config)bool
NeedsRenewal returns true if the certificate is expiringsoon (according to ARI and/or cfg) or has expired.
typeCertificateResource¶
type CertificateResource struct {// The list of names on the certificate;// for convenience only.SANs []string `json:"sans,omitempty"`// The PEM-encoding of DER-encoded ASN.1 data// for the cert or chain.CertificatePEM []byte `json:"-"`// The PEM-encoding of the certificate's private key.PrivateKeyPEM []byte `json:"-"`// Any extra information associated with the certificate,// usually provided by the issuer implementation.IssuerDatajson.RawMessage `json:"issuer_data,omitempty"`// contains filtered or unexported fields}CertificateResource associates a certificate with its privatekey and other useful information, for use in maintaining thecertificate.
func (*CertificateResource)NamesKey¶
func (cr *CertificateResource) NamesKey()string
NamesKey returns the list of SANs as a single string,truncated to some ridiculously long size limit. Itcan act as a key for the set of names on the resource.
typeCertificateSelector¶
type CertificateSelector interface {SelectCertificate(*tls.ClientHelloInfo, []Certificate) (Certificate,error)}CertificateSelector is a type which can select a certificate to use given multiple choices.
typeChainPreference¶added inv0.13.0
type ChainPreference struct {// Prefer chains with the fewest number of bytes.Smallest *bool// Select first chain having a root with one of// these common names.RootCommonName []string// Select first chain that has any issuer with one// of these common names.AnyCommonName []string}ChainPreference describes the client's preferred certificate chain,useful if the CA offers alternate chains. The first matching chainwill be selected.
typeChallenge¶added inv0.13.0
Challenge is an ACME challenge, but optionally paired withdata that can make it easier or more efficient to solve.
funcGetACMEChallenge¶added inv0.13.0
GetACMEChallenge returns an active ACME challenge for the given identifier,or false if no active challenge for that identifier is known.
typeCleanStorageOptions¶
type CleanStorageOptions struct {// Optional custom logger.Logger *zap.Logger// Optional ID of the instance initiating the cleaning.InstanceIDstring// If set, cleaning will be skipped if it was performed// more recently than this interval.Intervaltime.Duration// Whether to clean cached OCSP staples.OCSPStaplesbool// Whether to cleanup expired certificates, and if so,// how long to let them stay after they've expired.ExpiredCertsboolExpiredCertGracePeriodtime.Duration}CleanStorageOptions specifies how to clean up a storage unit.
typeConfig¶
type Config struct {// How much of a certificate's lifetime becomes the// renewal window, which is the span of time at the// end of the certificate's validity period in which// it should be renewed; for most certificates, the// global default is good, but for extremely short-// lived certs, you may want to raise this to ~0.5.// Ratio is remaining:total lifetime.RenewalWindowRatiofloat64// An optional event callback clients can set// to subscribe to certain things happening// internally by this config; invocations are// synchronous, so make them return quickly!// Functions should honor context cancellation.//// An error should only be returned to advise// the emitter to abort or cancel an upcoming// event. Some events, especially those that have// already happened, cannot be aborted. For example,// cert_obtaining can be canceled, but// cert_obtained cannot. Emitters may choose to// ignore returned errors.OnEvent func(ctxcontext.Context, eventstring, data map[string]any)error// DefaultServerName specifies a server name// to use when choosing a certificate if the// ClientHello's ServerName field is empty.DefaultServerNamestring// FallbackServerName specifies a server name// to use when choosing a certificate if the// ClientHello's ServerName field doesn't match// any available certificate.// EXPERIMENTAL: Subject to change or removal.FallbackServerNamestring// The state needed to operate on-demand TLS;// if non-nil, on-demand TLS is enabled and// certificate operations are deferred to// TLS handshakes (or as-needed).// TODO: Can we call this feature "Reactive/Lazy/Passive TLS" instead?OnDemand *OnDemandConfig// Adds the must staple TLS extension to the CSR.MustStaplebool// Sources for getting new, managed certificates;// the default Issuer is ACMEIssuer. If multiple// issuers are specified, they will be tried in// turn until one succeeds.Issuers []Issuer// How to select which issuer to use.// Default: UseFirstIssuer (subject to change).IssuerPolicyIssuerPolicy// If true, private keys already existing in storage// will be reused. Otherwise, a new key will be// created for every new certificate to mitigate// pinning and reduce the scope of key compromise.// Default: false (do not reuse keys).ReusePrivateKeysbool// The source of new private keys for certificates;// the default KeySource is StandardKeyGenerator.KeySourceKeyGenerator// CertSelection chooses one of the certificates// with which the ClientHello will be completed;// if not set, DefaultCertificateSelector will// be used.CertSelectionCertificateSelector// OCSP configures how OCSP is handled. By default,// OCSP responses are fetched for every certificate// with a responder URL, and cached on disk. Changing// these defaults is STRONGLY discouraged unless you// have a compelling reason to put clients at greater// risk and reduce their privacy.OCSPOCSPConfig// The storage to access when storing or loading// TLS assets. Default is the local file system.StorageStorage// CertMagic will verify the storage configuration// is acceptable before obtaining a certificate// to avoid information loss after an expensive// operation. If you are absolutely 100% sure your// storage is properly configured and has sufficient// space, you can disable this check to reduce I/O// if that is expensive for you.// EXPERIMENTAL: Subject to change or removal.DisableStorageCheckbool// SubjectTransformer is a hook that can transform the// subject (SAN) of a certificate being loaded or issued.// For example, a common use case is to replace the// left-most label with an asterisk (*) to become a// wildcard certificate.// EXPERIMENTAL: Subject to change or removal.SubjectTransformer func(ctxcontext.Context, domainstring)string// Disables both ARI fetching and the use of ARI for renewal decisions.// TEMPORARY: Will likely be removed in the future.DisableARIbool// Set a logger to enable logging. If not set,// a default logger will be created.Logger *zap.Logger// contains filtered or unexported fields}Config configures a certificate manager instance.An empty Config is not valid: use New() to obtaina valid Config.
funcNew¶
New makes a new, valid config based on cfg anduses the provided certificate cache. certCacheMUST NOT be nil or this function will panic.
Use this method when you have an advanced use casethat requires a custom certificate cache and configthat may differ from the Default. For example, ifnot all certificates are managed/renewed the sameway, you need to make your own Cache value with aGetConfigForCert callback that returns the correctconfiguration for each certificate. However, forthe vast majority of cases, there will be only asingle Config, thus the default cache (which alwaysuses the default Config) and default config willsuffice, and you should use NewDefault() instead.
funcNewDefault¶
func NewDefault() *Config
NewDefault makes a valid config based on the packageDefault config. Most users will call this functioninstead of New() since most use cases require only asingle config for any and all certificates.
If your requirements are more advanced (for example,multiple configs depending on the certificate), then useNew() instead. (You will need to make your own Cachefirst.) If you only need a single Config to manage yourcerts (even if that config changes, as long as it is theonly one), customize the Default package variable beforecalling NewDefault().
All calls to NewDefault() will return configs that use thesame, default certificate cache. All configs returnedby NewDefault() are based on the values of the fields ofDefault at the time it is called.
This is the only way to get a config that uses thedefault certificate cache.
func (*Config)CacheManagedCertificate¶
CacheManagedCertificate loads the certificate for domain into thecache, from the TLS storage for managed certificates. It returns acopy of the Certificate that was put into the cache.
This is a lower-level method; normally you'll call Manage() instead.
This method is safe for concurrent use.
func (*Config)CacheUnmanagedCertificatePEMBytes¶
func (cfg *Config) CacheUnmanagedCertificatePEMBytes(ctxcontext.Context, certBytes, keyBytes []byte, tags []string) (string,error)
CacheUnmanagedCertificatePEMBytes makes a certificate out of the PEM bytesof the certificate and key, then caches it in memory, and returns the hash,which is useful for removing from the cache.
This method is safe for concurrent use.
func (*Config)CacheUnmanagedCertificatePEMBytesAsReplacement¶added inv0.24.0
func (cfg *Config) CacheUnmanagedCertificatePEMBytesAsReplacement(ctxcontext.Context, certBytes, keyBytes []byte, tags, sans []string) (string,error)
CacheUnmanagedCertificatePEMBytesAsReplacement is the same as CacheUnmanagedCertificatePEMBytes,but it also removes any other loaded certificates for the SANs on the certificate being cached.This has the effect of using this certificate exclusively and immediately for its SANs. The SANsfor which the certificate should apply may optionally be passed in as well. By default, a certis used for any of its SANs.
This method is safe for concurrent use.
EXPERIMENTAL: Subject to change/removal.
func (*Config)CacheUnmanagedCertificatePEMFile¶
func (cfg *Config) CacheUnmanagedCertificatePEMFile(ctxcontext.Context, certFile, keyFilestring, tags []string) (string,error)
CacheUnmanagedCertificatePEMFile loads a certificate for host using certFileand keyFile, which must be in PEM format. It stores the certificate inthe in-memory cache and returns the hash, useful for removing from the cache.
This method is safe for concurrent use.
func (*Config)CacheUnmanagedTLSCertificate¶
func (cfg *Config) CacheUnmanagedTLSCertificate(ctxcontext.Context, tlsCerttls.Certificate, tags []string) (string,error)
CacheUnmanagedTLSCertificate adds tlsCert to the certificate cache
and returns the hash, useful for removing from the cache.
It staples OCSP if possible.
This method is safe for concurrent use.
func (*Config)ClientCredentials¶added inv0.13.0
func (cfg *Config) ClientCredentials(ctxcontext.Context, identifiers []string) ([]tls.Certificate,error)
ClientCredentials returns a list of TLS client certificate chains for the given identifiers.The return value can be used in a tls.Config to enable client authentication using managed certificates.Any certificates that need to be obtained or renewed for these identifiers will be managed accordingly.
func (*Config)GetCertificate¶
func (cfg *Config) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate,error)
GetCertificate gets a certificate to satisfy clientHello. In gettingthe certificate, it abides the rules and settings defined in the Configthat matches clientHello.ServerName. It tries to get certificates inthis order:
1. Exact match in the in-memory cache2. Wildcard match in the in-memory cache3. Managers (if any)4. Storage (if on-demand is enabled)5. Issuers (if on-demand is enabled)
This method is safe for use as a tls.Config.GetCertificate callback.
GetCertificate will run in a new context, use GetCertificateWithContext to providea context.
func (*Config)GetCertificateWithContext¶added inv0.18.0
func (cfg *Config) GetCertificateWithContext(ctxcontext.Context, clientHello *tls.ClientHelloInfo) (*tls.Certificate,error)
func (*Config)ManageAsync¶
ManageAsync is the same as ManageSync, except that ACMEoperations are performed asynchronously (in the background).This method returns before certificates are ready. It iscrucial that the administrator monitors the logs and isnotified of any errors so that corrective action can betaken as soon as possible. Any errors returned from thismethod occurred before ACME transactions started.
As long as logs are monitored, this method is typicallyrecommended for non-interactive environments.
If there are failures loading, obtaining, or renewing acertificate, it will be retried with exponential backofffor up to about 30 days, with a maximum interval of about24 hours. Cancelling ctx will cancel retries and shut downany goroutines spawned by ManageAsync.
func (*Config)ManageSync¶
ManageSync causes the certificates for domainNames to be managedaccording to cfg. If cfg.OnDemand is not nil, then this simplyallowlists the domain names and defers the certificate operationsto when they are needed. Otherwise, the certificates for eachname are loaded from storage or obtained from the CA if not alreadyin the cache associated with the Config. If loaded from storage,they are renewed if they are expiring or expired. It then cachesthe certificate in memory and is prepared to serve them up duringTLS handshakes. To change how an already-loaded certificate ismanaged, update the cache options relating to getting a config fora cert.
Note that name allowlisting for on-demand management only takeseffect if cfg.OnDemand.DecisionFunc is not set (is nil); it willnot overwrite an existing DecisionFunc, nor will it overwriteits decision; i.e. the implicit allowlist is only used if noDecisionFunc is set.
This method is synchronous, meaning that certificates for alldomainNames must be successfully obtained (or renewed) beforeit returns. It returns immediately on the first error for anyof the given domainNames. This behavior is recommended forinteractive use (i.e. when an administrator is present) sothat errors can be reported and fixed immediately.
func (*Config)ObtainCertAsync¶added inv0.14.0
ObtainCertAsync is the same as ObtainCertSync(), except it runs in thebackground; i.e. non-interactively, and with retries if it fails.
func (*Config)ObtainCertSync¶added inv0.14.0
ObtainCertSync generates a new private key and obtains a certificate forname using cfg in the foreground; i.e. interactively and without retries.It stows the renewed certificate and its assets in storage if successful.It DOES NOT load the certificate into the in-memory cache. This methodis a no-op if storage already has a certificate for name.
func (*Config)RenewCertAsync¶added inv0.14.0
RenewCertAsync is the same as RenewCertSync(), except it runs in thebackground; i.e. non-interactively, and with retries if it fails.
func (*Config)RenewCertSync¶added inv0.14.0
RenewCertSync renews the certificate for name using cfg in the foreground;i.e. interactively and without retries. It stows the renewed certificateand its assets in storage if successful. It DOES NOT update the in-memorycache with the new certificate. The certificate will not be renewed if itis not close to expiring unless force is true.
func (*Config)RevokeCert¶
RevokeCert revokes the certificate for domain via ACME protocol. It requiresthat cfg.Issuers is properly configured with the same issuer that issued thecertificate being revoked. SeeRFC 5280 §5.3.1 for reason codes.
The certificate assets are deleted from storage after successful revocationto prevent reuse.
func (*Config)TLSConfig¶
TLSConfig is an opinionated method that returns a recommended, modernTLS configuration that can be used to configure TLS listeners. Asidefrom safe, modern defaults, this method sets two critical fields on theTLS config which are required to enable automatic certificatemanagement: GetCertificate and NextProtos.
The GetCertificate field is necessary to get certificates from memoryor storage, including both manual and automated certificates. Youshould only change this field if you know what you are doing.
The NextProtos field is pre-populated with a special value to enablesolving the TLS-ALPN ACME challenge. Because this method does notassume any particular protocols after the TLS handshake is completed,you will likely need to customize the NextProtos field by prependingyour application's protocols to the slice. For example, to serveHTTP, you will need to prepend "h2" and "http/1.1" values. Be sure toleave the acmez.ACMETLS1Protocol value intact, however, or TLS-ALPNchallenges will fail (which may be acceptable if you are not usingACME, or specifically, the TLS-ALPN challenge).
Unlike the package TLS() function, this method does not, by itself,enable certificate management for any domain names.
typeConfigGetter¶
type ConfigGetter func(Certificate) (*Config,error)
ConfigGetter is a function that returns a prepared,valid config that should be used when managing thegiven certificate or its assets.
typeDNS01Solver¶added inv0.12.0
type DNS01Solver struct {DNSManager}DNS01Solver is a type that makes libdns providers usable as ACME dns-01challenge solvers. Seehttps://github.com/libdns/libdns
Note that challenges may be solved concurrently by some clients (such asacmez, which CertMagic uses), meaning that multiple TXT records may becreated in a DNS zone simultaneously, and in some cases distinct TXT recordsmay have the same name. For example, solving challenges for both example.comand *.example.com create a TXT record named _acme_challenge.example.com,but with different tokens as their values. This solver distinguishesbetween different records with the same name by looking at their values.DNS provider APIs and implementations of the libdns interfaces must alsosupport multiple same-named TXT records.
func (*DNS01Solver)CleanUp¶added inv0.12.0
CleanUp deletes the DNS TXT record created in Present().
We ignore the context because cleanup is often/likely performed aftera context cancellation, and properly-implemented DNS providers shouldhonor cancellation, which would result in cleanup being aborted.Cleanup must always occur.
typeDNSManager¶added inv0.21.0
type DNSManager struct {// The implementation that interacts with the DNS// provider to set or delete records. (REQUIRED)DNSProviderDNSProvider// The TTL for the temporary challenge records.TTLtime.Duration// How long to wait before starting propagation checks.// Default: 0 (no wait).PropagationDelaytime.Duration// Maximum time to wait for temporary DNS record to appear.// Set to -1 to disable propagation checks.// Default: 2 minutes.PropagationTimeouttime.Duration// Preferred DNS resolver(s) to use when doing DNS lookups.Resolvers []string// Override the domain to set the TXT record on. This is// to delegate the challenge to a different domain. Note// that the solver doesn't follow CNAME/NS record.OverrideDomainstring// An optional logger.Logger *zap.Logger// contains filtered or unexported fields}DNSManager is a type that makes libdns providers usable for performingDNS verification. Seehttps://github.com/libdns/libdns
Note that records may be manipulated concurrently by some clients (such asacmez, which CertMagic uses), meaning that multiple records may be createdin a DNS zone simultaneously, and in some cases distinct records of the sametype may have the same name. For example, solving ACME challenges for both example.comand *.example.com create a TXT record named _acme_challenge.example.com,but with different tokens as their values. This solver distinguishes betweendifferent records with the same type and name by looking at their values.
typeDNSProvider¶added inv0.21.0
type DNSProvider interface {libdns.RecordAppenderlibdns.RecordDeleter}DNSProvider defines the set of operations required forACME challenges or other sorts of domain verification.A DNS provider must be able to append and delete recordsin order to solve ACME challenges. Find one you can useathttps://github.com/libdns. If your provider isn'timplemented yet, feel free to contribute!
typeErrNoRetry¶
type ErrNoRetry struct{ Errerror }ErrNoRetry is an error type which signalsto stop retries early.
func (ErrNoRetry)Error¶
func (eErrNoRetry) Error()string
typeFileStorage¶
type FileStorage struct {Pathstring}FileStorage facilitates forming file paths derived from a rootdirectory. It is used to get file paths in a consistent,cross-platform way or persisting ACME assets on the file system.The presence of a lock file for a given key indicates a lockis held and is thus unavailable.
Locks are created atomically by relying on the file system toenforce the O_EXCL flag. Acquirers that are forcefully terminatedwill not have a chance to clean up their locks before they exit,so locks may become stale. That is why, while a lock is activelyheld, the contents of the lockfile are updated with the currenttimestamp periodically. If another instance tries to acquire thelock but fails, it can see if the timestamp within is still fresh.If so, it patiently waits by polling occasionally. Otherwise,the stale lockfile is deleted, essentially forcing an unlock.
While locking is atomic, unlocking is not perfectly atomic. Filesystems offer native atomic operations when creating files, butnot necessarily when deleting them. It is theoretically possiblefor two instances to discover the same stale lock and both proceedto delete it, but if one instance is able to delete the lockfileand create a new one before the other one calls delete, then thenew lock file created by the first instance will get deleted bymistake. This does mean that mutual exclusion is not guaranteedto be perfectly enforced in the presence of stale locks. Onealternative is to lock the unlock operation by using ".unlock"files; and we did this for some time, but those files themselvesmay become stale, leading applications into infinite loops ifthey always expect the unlock file to be deleted by the instancethat created it. We instead prefer the simpler solution thatimplies imperfect mutual exclusion if locks become stale, butthat is probably less severe a consequence than infinite loops.
Seehttps://github.com/caddyserver/caddy/issues/4448 for discussion.See commit 468bfd25e452196b140148928cdd1f1a2285ae4b for where weswitched away from using .unlock files.
func (*FileStorage)Delete¶
func (s *FileStorage) Delete(_context.Context, keystring)error
Delete deletes the value at key.
func (*FileStorage)Exists¶
func (s *FileStorage) Exists(_context.Context, keystring)bool
Exists returns true if key exists in s.
func (*FileStorage)Filename¶
func (s *FileStorage) Filename(keystring)string
Filename returns the key as a path on the filesystem prefixed by s.Path.
func (*FileStorage)Lock¶
func (s *FileStorage) Lock(ctxcontext.Context, namestring)error
Lock obtains a lock named by the given name. It blocksuntil the lock can be obtained or an error is returned.
func (*FileStorage)String¶
func (s *FileStorage) String()string
typeIssuedCertificate¶
type IssuedCertificate struct {// The PEM-encoding of DER-encoded ASN.1 data.Certificate []byte// Any extra information to serialize alongside the// certificate in storage. It MUST be serializable// as JSON in order to be preserved.Metadataany}IssuedCertificate represents a certificate that was just issued.
typeIssuer¶
type Issuer interface {// Issue obtains a certificate for the given CSR. It// must honor context cancellation if it is long-running.// It can also use the context to find out if the current// call is part of a retry, via AttemptsCtxKey.Issue(ctxcontext.Context, request *x509.CertificateRequest) (*IssuedCertificate,error)// IssuerKey must return a string that uniquely identifies// this particular configuration of the Issuer such that// any certificates obtained by this Issuer will be treated// as identical if they have the same SANs.//// Certificates obtained from Issuers with the same IssuerKey// will overwrite others with the same SANs. For example, an// Issuer might be able to obtain certificates from different// CAs, say A and B. It is likely that the CAs have different// use cases and purposes (e.g. testing and production), so// their respective certificates should not overwrite eaach// other.IssuerKey()string}Issuer is a type that can issue certificates.
typeIssuerPolicy¶added inv0.18.1
type IssuerPolicystring
IssuerPolicy is a type that enumerates how tochoose which issuer to use. EXPERIMENTAL andsubject to change.
typeKeyBuilder¶
type KeyBuilder struct{}KeyBuilder provides a namespace for methods thatbuild keys and key prefixes, for addressing itemsin a Storage implementation.
var StorageKeysKeyBuilderStorageKeys provides methods for accessingkeys and key prefixes for items in a Storage.Typically, you will not need to use thisbecause accessing storage is abstracted awayfor most cases. Only use this if you need todirectly access TLS assets in your application.
func (KeyBuilder)CertsPrefix¶
func (keysKeyBuilder) CertsPrefix(issuerKeystring)string
CertsPrefix returns the storage key prefix forthe given certificate issuer.
func (KeyBuilder)CertsSitePrefix¶
func (keysKeyBuilder) CertsSitePrefix(issuerKey, domainstring)string
CertsSitePrefix returns a key prefix for items associated withthe site given by domain using the given issuer key.
func (KeyBuilder)OCSPStaple¶
func (keysKeyBuilder) OCSPStaple(cert *Certificate, pemBundle []byte)string
OCSPStaple returns a key for the OCSP staple associatedwith the given certificate. If you have the PEM bundlehandy, pass that in to save an extra encoding step.
func (KeyBuilder)Safe¶
func (keysKeyBuilder) Safe(strstring)string
Safe standardizes and sanitizes str for use asa single component of a storage key. This methodis idempotent.
func (KeyBuilder)SiteCert¶
func (keysKeyBuilder) SiteCert(issuerKey, domainstring)string
SiteCert returns the path to the certificate file for domainthat is associated with the issuer with the given issuerKey.
func (KeyBuilder)SiteMeta¶
func (keysKeyBuilder) SiteMeta(issuerKey, domainstring)string
SiteMeta returns the path to the metadata file for domain thatis associated with the certificate from the given issuer withthe given issuerKey.
func (KeyBuilder)SitePrivateKey¶
func (keysKeyBuilder) SitePrivateKey(issuerKey, domainstring)string
SitePrivateKey returns the path to the private key file for domainthat is associated with the certificate from the given issuer withthe given issuerKey.
typeKeyGenerator¶
type KeyGenerator interface {// GenerateKey generates a private key. The returned// PrivateKey must be able to expose its associated// public key.GenerateKey() (crypto.PrivateKey,error)}KeyGenerator can generate a private key.
typeKeyInfo¶
type KeyInfo struct {KeystringModifiedtime.TimeSizeint64IsTerminalbool// false for directories (keys that act as prefix for other keys)}KeyInfo holds information about a key in storage.Key and IsTerminal are required; Modified and Sizeare optional if the storage implementation is notable to get that information. Setting them willmake certain operations more consistent orpredictable, but it is not crucial to basicfunctionality.
typeLocker¶
type Locker interface {// Lock acquires the lock for name, blocking until the lock// can be obtained or an error is returned. Only one lock// for the given name can exist at a time. A call to Lock for// a name which already exists blocks until the named lock// is released or becomes stale.//// If the named lock represents an idempotent operation, callers// should always check to make sure the work still needs to be// completed after acquiring the lock. You never know if another// process already completed the task while you were waiting to// acquire it.//// Implementations should honor context cancellation.Lock(ctxcontext.Context, namestring)error// Unlock releases named lock. This method must ONLY be called// after a successful call to Lock, and only after the critical// section is finished, even if it errored or timed out. Unlock// cleans up any resources allocated during Lock. Unlock should// only return an error if the lock was unable to be released.Unlock(ctxcontext.Context, namestring)error}Locker facilitates synchronization across machines and networks.It essentially provides a distributed named-mutex service sothat multiple consumers can coordinate tasks and share resources.
If possible, a Locker should implement a coordinated distributedlocking mechanism by generating fencing tokens (seehttps://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html).This typically requires a central server or consensus algorithmHowever, if that is not feasible, Lockers may implement analternative mechanism that uses timeouts to detect node or networkfailures and avoid deadlocks. For example, the default FileStoragewrites a timestamp to the lock file every few seconds, and if anothernode acquiring the lock sees that timestamp is too old, it mayassume the lock is stale.
As not all Locker implementations use fencing tokens, code relyingupon Locker must be tolerant of some mis-synchronizations but canexpect them to be rare.
This interface should only be used for coordinating expensiveoperations across nodes in a cluster; not for internal, extremelyshort-lived, or high-contention locks.
typeManager¶added inv0.16.0
type Manager interface {// GetCertificate returns the certificate to use to complete the handshake.// Since this is called during every TLS handshake, it must be very fast and not block.// Returning any non-nil value indicates that this Manager manages a certificate// for the described handshake. Returning (nil, nil) is valid and is simply treated as// a no-op Return (nil, nil) when the Manager has no certificate for this handshake.// Return an error or a certificate only if the Manager is supposed to get a certificate// for this handshake. Returning (nil, nil) other Managers or Issuers to try to get// a certificate for the handshake.GetCertificate(context.Context, *tls.ClientHelloInfo) (*tls.Certificate,error)}Manager is a type that manages certificates (keeps them renewed) suchthat we can get certificates during TLS handshakes to immediately serveto clients.
TODO: This is an EXPERIMENTAL API. It is subject to change/removal.
typeOCSPConfig¶added inv0.13.0
type OCSPConfig struct {// Disable automatic OCSP stapling; strongly// discouraged unless you have a good reason.// Disabling this puts clients at greater risk// and reduces their privacy.DisableStaplingbool// A map of OCSP responder domains to replacement// domains for querying OCSP servers. Used for// overriding the OCSP responder URL that is// embedded in certificates. Mapping to an empty// URL will disable OCSP from that responder.ResponderOverrides map[string]string// Optionally specify a function that can return the URL// for an HTTP proxy to use for OCSP-related HTTP requests.HTTPProxy func(*http.Request) (*url.URL,error)}OCSPConfig configures how OCSP is handled.
typeOnDemandConfig¶
type OnDemandConfig struct {// If set, this function will be called to determine// whether a certificate can be obtained or renewed// for the given name. If an error is returned, the// request will be denied. IDNs will be given as// punycode.DecisionFunc func(ctxcontext.Context, namestring)error// Sources for getting new, unmanaged certificates.// They will be invoked only during TLS handshakes// before on-demand certificate management occurs,// for certificates that are not already loaded into// the in-memory cache.//// TODO: EXPERIMENTAL: subject to change and/or removal.Managers []Manager// contains filtered or unexported fields}OnDemandConfig configures on-demand TLS (certificateoperations as-needed, like during TLS handshakes,rather than immediately).
When this package's high-level convenience functionsare used (HTTPS, Manage, etc., where the Defaultconfig is used as a template), this struct regulatescertificate operations using an implicit whitelistcontaining the names passed into those functions ifno DecisionFunc is set. This ensures some degree ofcontrol by default to avoid certificate operations forarbitrary domain names. To override this whitelist,manually specify a DecisionFunc. To impose rate limits,specify your own DecisionFunc.
typePreChecker¶
PreChecker is an interface that can be optionally implemented byIssuers. Pre-checks are performed before each call (or batch ofidentical calls) to Issue(), giving the issuer the option to ensureit has all the necessary information/state.
typeRenewalInfoGetter¶added inv0.21.3
type RenewalInfoGetter interface {GetRenewalInfo(context.Context,Certificate) (acme.RenewalInfo,error)}RenewalInfoGetter is a type that can get ACME Renewal Information (ARI).Users of this package that wrap the ACMEIssuer or use any other issuerthat supports ARI will need to implement this so that CertMagic canupdate ARI which happens outside the normal issuance flow and is thusnot required by the Issuer interface (a type assertion is performed).
typeRevoker¶
type Revoker interface {Revoke(ctxcontext.Context, certCertificateResource, reasonint)error}Revoker can revoke certificates. Reason codes are definedbyRFC 5280 §5.3.1:https://tools.ietf.org/html/rfc5280#section-5.3.1and are available as constants in our ACME library.
typeRingBufferRateLimiter¶
type RingBufferRateLimiter struct {// contains filtered or unexported fields}RingBufferRateLimiter uses a ring to enforce rate limitsconsisting of a maximum number of events within a singlesliding window of a given duration. An empty value isnot valid; use NewRateLimiter to get one.
funcNewRateLimiter¶
func NewRateLimiter(maxEventsint, windowtime.Duration) *RingBufferRateLimiter
NewRateLimiter returns a rate limiter that allows up to maxEventsin a sliding window of size window. If maxEvents and window areboth 0, or if maxEvents is non-zero and window is 0, rate limitingis disabled. This function panics if maxEvents is less than 0 orif maxEvents is 0 and window is non-zero, which is considered to bean invalid configuration, as it would never allow events.
func (*RingBufferRateLimiter)Allow¶
func (r *RingBufferRateLimiter) Allow()bool
Allow returns true if the event is allowed tohappen right now. It does not wait. If the eventis allowed, a ticket is claimed.
func (*RingBufferRateLimiter)MaxEvents¶
func (r *RingBufferRateLimiter) MaxEvents()int
MaxEvents returns the maximum number of events thatare allowed within the sliding window.
func (*RingBufferRateLimiter)SetMaxEvents¶
func (r *RingBufferRateLimiter) SetMaxEvents(maxEventsint)
SetMaxEvents changes the maximum number of events that areallowed in the sliding window. If the new limit is lower,the oldest events will be forgotten. If the new limit ishigher, the window will suddenly have capacity for newreservations. It panics if maxEvents is 0 and window sizeis not zero; if setting both the events limit and thewindow size to 0, call SetWindow() first.
func (*RingBufferRateLimiter)SetWindow¶
func (r *RingBufferRateLimiter) SetWindow(windowtime.Duration)
SetWindow changes r's sliding window duration to window.Goroutines that are already blocked on a call to Wait()will not be affected. It panics if window is non-zerobut the max event limit is 0.
func (*RingBufferRateLimiter)Stop¶
func (r *RingBufferRateLimiter) Stop()
Stop cleans up r's scheduling goroutine.
func (*RingBufferRateLimiter)Wait¶
func (r *RingBufferRateLimiter) Wait(ctxcontext.Context)error
Wait blocks until the event is allowed to occur. It returns anerror if the context is cancelled.
func (*RingBufferRateLimiter)Window¶
func (r *RingBufferRateLimiter) Window()time.Duration
Window returns the size of the sliding window.
typeStandardKeyGenerator¶
type StandardKeyGenerator struct {// The type of keys to generate.KeyTypeKeyType}StandardKeyGenerator is the standard, in-memory key sourcethat uses crypto/rand.
func (StandardKeyGenerator)GenerateKey¶
func (kgStandardKeyGenerator) GenerateKey() (crypto.PrivateKey,error)
GenerateKey generates a new private key according to kg.KeyType.
typeStorage¶
type Storage interface {// Locker enables the storage backend to synchronize// operational units of work.//// The use of Locker is NOT employed around every// Storage method call (Store, Load, etc), as these// should already be thread-safe. Locker is used for// high-level jobs or transactions that need// synchronization across a cluster; it's a simple// distributed lock. For example, CertMagic uses the// Locker interface to coordinate the obtaining of// certificates.Locker// Store puts value at key. It creates the key if it does// not exist and overwrites any existing value at this key.Store(ctxcontext.Context, keystring, value []byte)error// Load retrieves the value at key.Load(ctxcontext.Context, keystring) ([]byte,error)// Delete deletes the named key. If the name is a// directory (i.e. prefix of other keys), all keys// prefixed by this key should be deleted. An error// should be returned only if the key still exists// when the method returns.Delete(ctxcontext.Context, keystring)error// Exists returns true if the key exists either as// a directory (prefix to other keys) or a file,// and there was no error checking.Exists(ctxcontext.Context, keystring)bool// List returns all keys in the given path.//// If recursive is true, non-terminal keys// will be enumerated (i.e. "directories"// should be walked); otherwise, only keys// prefixed exactly by prefix will be listed.List(ctxcontext.Context, pathstring, recursivebool) ([]string,error)// Stat returns information about key.Stat(ctxcontext.Context, keystring) (KeyInfo,error)}Storage is a type that implements a key-value store withbasic file system (folder path) semantics. Keys use theforward slash '/' to separate path components and have noleading or trailing slashes.
A "prefix" of a key is defined on a component basis,e.g. "a" is a prefix of "a/b" but not "ab/c".
A "file" is a key with a value associated with it.
A "directory" is a key with no value, but which may bethe prefix of other keys.
Keys passed into Load and Store always have "file" semantics,whereas "directories" are only implicit by leading up to thefile.
The Load, Delete, List, and Stat methods should returnfs.ErrNotExist if the key does not exist.
Processes running in a cluster should use the same Storagevalue (with the same configuration) in order to sharecertificates and other TLS resources with the cluster.
Implementations of Storage MUST be safe for concurrent useand honor context cancellations. Methods should block untiltheir operation is complete; that is, Load() should alwaysreturn the value from the last call to Store() for a givenkey, and concurrent calls to Store() should not corrupt afile.
For simplicity, this is not a streaming API and is notsuitable for very large files.
typeSubjectIssuer¶added inv0.21.0
type SubjectIssuer struct {Subject, IssuerKeystring}SubjectIssuer pairs a subject name with an issuer ID/key.
typeZeroSSLIssuer¶added inv0.21.0
type ZeroSSLIssuer struct {// The API key (or "access key") for using the ZeroSSL API.// REQUIRED.APIKeystring// Where to store verification material temporarily.// All instances in a cluster should have the same// Storage value to enable distributed verification.// REQUIRED. (TODO: Make it optional for those not// operating in a cluster. For now, it's simpler to// put info in storage whether distributed or not.)StorageStorage// How many days the certificate should be valid for.ValidityDaysint// The host to bind to when opening a listener for// verifying domain names (or IPs).ListenHoststring// If HTTP is forwarded from port 80, specify the// forwarded port here.AltHTTPPortint// To use CNAME validation instead of HTTP// validation, set this field.CNAMEValidation *DNSManager// Delay between poll attempts.PollIntervaltime.Duration// An optional (but highly recommended) logger.Logger *zap.Logger}ZeroSSLIssuer can get certificates from ZeroSSL's API. (To use ZeroSSL's ACMEendpoint, use the ACMEIssuer instead.) Note that use of the API is restrictedby payment tier.
func (*ZeroSSLIssuer)HTTPValidationHandler¶added inv0.21.0
func (iss *ZeroSSLIssuer) HTTPValidationHandler(hhttp.Handler)http.Handler
HTTPValidationHandler wraps the ZeroSSL HTTP validation handler such thatit can pass verification checks from ZeroSSL's API.
If a request is not a ZeroSSL HTTP validation request, h will be invoked.
func (*ZeroSSLIssuer)HandleZeroSSLHTTPValidation¶added inv0.21.0
func (iss *ZeroSSLIssuer) HandleZeroSSLHTTPValidation(whttp.ResponseWriter, r *http.Request)bool
HandleZeroSSLHTTPValidation is to ZeroSSL API HTTP validation requests like HandleHTTPChallengeis to ACME HTTP challenge requests.
func (*ZeroSSLIssuer)Issue¶added inv0.21.0
func (iss *ZeroSSLIssuer) Issue(ctxcontext.Context, csr *x509.CertificateRequest) (*IssuedCertificate,error)
Issue obtains a certificate for the given csr.
func (*ZeroSSLIssuer)IssuerKey¶added inv0.21.0
func (iss *ZeroSSLIssuer) IssuerKey()string
IssuerKey returns the unique issuer key for ZeroSSL.
func (*ZeroSSLIssuer)Revoke¶added inv0.21.0
func (iss *ZeroSSLIssuer) Revoke(ctxcontext.Context, certCertificateResource, reasonint)error
Revoke revokes the given certificate. Only do this if there is a security or trustconcern with the certificate.
Source Files¶
Directories¶
| Path | Synopsis |
|---|---|
internal | |
atomicfile Package atomicfile provides a mechanism (on Unix-like platforms) to present a consistent view of a file to separate processes even while the file is being written. | Package atomicfile provides a mechanism (on Unix-like platforms) to present a consistent view of a file to separate processes even while the file is being written. |

