2016-02-24 21:19:01 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-02-24 21:19:01 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2018-07-11 19:45:09 +00:00
|
|
|
"net/http"
|
2018-06-16 22:21:33 +00:00
|
|
|
"sync/atomic"
|
2016-02-24 21:19:01 +00:00
|
|
|
"time"
|
|
|
|
|
2018-03-29 19:27:51 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2018-09-17 16:53:23 +00:00
|
|
|
"github.com/hashicorp/vault/helper/consts"
|
2016-02-24 21:19:01 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathTidy(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "tidy",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"tidy_cert_store": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: `Set to true to enable tidying up
|
|
|
|
the certificate store`,
|
|
|
|
},
|
|
|
|
|
2018-09-05 16:33:31 +00:00
|
|
|
"tidy_revocation_list": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: `Deprecated; synonym for 'tidy_revoked_certs`,
|
|
|
|
},
|
|
|
|
|
2018-07-13 13:32:32 +00:00
|
|
|
"tidy_revoked_certs": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Description: `Set to true to expire all revoked
|
2018-09-05 15:47:27 +00:00
|
|
|
certificates, even if their duration has not yet passed, removing
|
|
|
|
them both from the CRL and from storage. The CRL will be rotated
|
|
|
|
if this causes any values to be removed.`,
|
2018-07-13 13:32:32 +00:00
|
|
|
},
|
|
|
|
|
2016-02-24 21:19:01 +00:00
|
|
|
"safety_buffer": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Description: `The amount of extra time that must have passed
|
|
|
|
beyond certificate expiration before it is removed
|
|
|
|
from the backend storage and/or revocation list.
|
|
|
|
Defaults to 72 hours.`,
|
|
|
|
Default: 259200, //72h, but TypeDurationSecond currently requires defaults to be int
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.UpdateOperation: b.pathTidyWrite,
|
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathTidyHelpSyn,
|
|
|
|
HelpDescription: pathTidyHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathTidyWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-09-17 16:53:23 +00:00
|
|
|
// If we are a performance standby forward the request to the active node
|
|
|
|
if b.System().ReplicationState().HasState(consts.ReplicationPerformanceStandby) {
|
|
|
|
return nil, logical.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:54:18 +00:00
|
|
|
safetyBuffer := d.Get("safety_buffer").(int)
|
2016-02-24 21:19:01 +00:00
|
|
|
tidyCertStore := d.Get("tidy_cert_store").(bool)
|
2018-07-13 13:32:32 +00:00
|
|
|
tidyRevokedCerts := d.Get("tidy_revoked_certs").(bool)
|
2018-09-05 16:33:31 +00:00
|
|
|
tidyRevocationList := d.Get("tidy_revocation_list").(bool)
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-05-28 16:08:22 +00:00
|
|
|
if safetyBuffer < 1 {
|
|
|
|
return logical.ErrorResponse("safety_buffer must be greater than zero"), nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:54:18 +00:00
|
|
|
bufferDuration := time.Duration(safetyBuffer) * time.Second
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
if !atomic.CompareAndSwapUint32(b.tidyCASGuard, 0, 1) {
|
|
|
|
resp := &logical.Response{}
|
|
|
|
resp.AddWarning("Tidy operation already in progress.")
|
|
|
|
return resp, nil
|
|
|
|
}
|
2018-03-29 19:27:51 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
// Tests using framework will screw up the storage so make a locally
|
|
|
|
// scoped req to hold a reference
|
|
|
|
req = &logical.Request{
|
|
|
|
Storage: req.Storage,
|
|
|
|
}
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
go func() {
|
|
|
|
defer atomic.StoreUint32(b.tidyCASGuard, 0)
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
// Don't cancel when the original client request goes away
|
|
|
|
ctx = context.Background()
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
logger := b.Logger().Named("tidy")
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
doTidy := func() error {
|
|
|
|
if tidyCertStore {
|
|
|
|
serials, err := req.Storage.List(ctx, "certs/")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("error fetching list of certs: {{err}}", err)
|
|
|
|
}
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
for _, serial := range serials {
|
|
|
|
certEntry, err := req.Storage.Get(ctx, "certs/"+serial)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error fetching certificate %q: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if certEntry == nil {
|
|
|
|
logger.Warn("certificate entry is nil; tidying up since it is no longer useful for any server operations", "serial", serial)
|
|
|
|
if err := req.Storage.Delete(ctx, "certs/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting nil entry with serial %s: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if certEntry.Value == nil || len(certEntry.Value) == 0 {
|
|
|
|
logger.Warn("certificate entry has no value; tidying up since it is no longer useful for any server operations", "serial", serial)
|
|
|
|
if err := req.Storage.Delete(ctx, "certs/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting entry with nil value with serial %s: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(certEntry.Value)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("unable to parse stored certificate with serial %q: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().After(cert.NotAfter.Add(bufferDuration)) {
|
|
|
|
if err := req.Storage.Delete(ctx, "certs/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting serial %q from storage: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 21:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 16:33:31 +00:00
|
|
|
if tidyRevokedCerts || tidyRevocationList {
|
2018-06-16 22:21:33 +00:00
|
|
|
b.revokeStorageLock.Lock()
|
|
|
|
defer b.revokeStorageLock.Unlock()
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
tidiedRevoked := false
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
revokedSerials, err := req.Storage.List(ctx, "revoked/")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("error fetching list of revoked certs: {{err}}", err)
|
2018-03-29 19:27:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
var revInfo revocationInfo
|
|
|
|
for _, serial := range revokedSerials {
|
|
|
|
revokedEntry, err := req.Storage.Get(ctx, "revoked/"+serial)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("unable to fetch revoked cert with serial %q: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if revokedEntry == nil {
|
|
|
|
logger.Warn("revoked entry is nil; tidying up since it is no longer useful for any server operations", "serial", serial)
|
|
|
|
if err := req.Storage.Delete(ctx, "revoked/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting nil revoked entry with serial %s: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if revokedEntry.Value == nil || len(revokedEntry.Value) == 0 {
|
|
|
|
logger.Warn("revoked entry has nil value; tidying up since it is no longer useful for any server operations", "serial", serial)
|
|
|
|
if err := req.Storage.Delete(ctx, "revoked/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting revoked entry with nil value with serial %s: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = revokedEntry.DecodeJSON(&revInfo)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error decoding revocation entry for serial %q: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
revokedCert, err := x509.ParseCertificate(revInfo.CertificateBytes)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("unable to parse stored revoked certificate with serial %q: {{err}}", serial), err)
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:47:27 +00:00
|
|
|
if time.Now().After(revokedCert.NotAfter.Add(bufferDuration)) {
|
2018-06-16 22:21:33 +00:00
|
|
|
if err := req.Storage.Delete(ctx, "revoked/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting serial %q from revoked list: {{err}}", serial), err)
|
|
|
|
}
|
2018-09-05 15:47:27 +00:00
|
|
|
if err := req.Storage.Delete(ctx, "certs/"+serial); err != nil {
|
|
|
|
return errwrap.Wrapf(fmt.Sprintf("error deleting serial %q from store when tidying revoked: {{err}}", serial), err)
|
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
tidiedRevoked = true
|
|
|
|
}
|
2018-03-29 19:27:51 +00:00
|
|
|
}
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
if tidiedRevoked {
|
2018-08-21 15:20:57 +00:00
|
|
|
if err := buildCRL(ctx, b, req, false); err != nil {
|
2018-06-16 22:21:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-02-24 21:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
|
|
|
|
return nil
|
2016-02-24 21:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
if err := doTidy(); err != nil {
|
|
|
|
logger.Error("error running tidy", "error", err)
|
|
|
|
return
|
2016-02-24 21:19:01 +00:00
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
}()
|
2016-02-24 21:19:01 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
resp := &logical.Response{}
|
|
|
|
resp.AddWarning("Tidy operation successfully started. Any information from the operation will be printed to Vault's server logs.")
|
2018-07-11 19:45:09 +00:00
|
|
|
return logical.RespondWithStatusCode(resp, req, http.StatusAccepted)
|
2016-02-24 21:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathTidyHelpSyn = `
|
|
|
|
Tidy up the backend by removing expired certificates, revocation information,
|
|
|
|
or both.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathTidyHelpDesc = `
|
|
|
|
This endpoint allows expired certificates and/or revocation information to be
|
|
|
|
removed from the backend, freeing up storage and shortening CRLs.
|
|
|
|
|
|
|
|
For safety, this function is a noop if called without parameters; cleanup from
|
|
|
|
normal certificate storage must be enabled with 'tidy_cert_store' and cleanup
|
|
|
|
from revocation information must be enabled with 'tidy_revocation_list'.
|
|
|
|
|
|
|
|
The 'safety_buffer' parameter is useful to ensure that clock skew amongst your
|
|
|
|
hosts cannot lead to a certificate being removed from the CRL while it is still
|
|
|
|
considered valid by other hosts (for instance, if their clocks are a few
|
2016-02-25 03:05:39 +00:00
|
|
|
minutes behind). The 'safety_buffer' parameter can be an integer number of
|
|
|
|
seconds or a string duration like "72h".
|
2016-02-24 21:19:01 +00:00
|
|
|
|
|
|
|
All certificates and/or revocation information currently stored in the backend
|
|
|
|
will be checked when this endpoint is hit. The expiration of the
|
|
|
|
certificate/revocation information of each certificate being held in
|
2018-03-20 18:54:10 +00:00
|
|
|
certificate storage or in revocation information will then be checked. If the
|
2016-02-24 21:19:01 +00:00
|
|
|
current time, minus the value of 'safety_buffer', is greater than the
|
|
|
|
expiration, it will be removed.
|
|
|
|
`
|