vault/builtin/logical/pki/secret_certs.go
Steven Clark cbf6dc2c4f
PKI refactoring to start breaking apart monolith into sub-packages (#24406)
* PKI refactoring to start breaking apart monolith into sub-packages

 - This was broken down by commit within enterprise for ease of review
   but would be too difficult to bring back individual commits back
   to the CE repository. (they would be squashed anyways)
 - This change was created by exporting a patch of the enterprise PR
   and applying it to CE repository

* Fix TestBackend_OID_SANs to not be rely on map ordering
2023-12-07 09:22:53 -05:00

87 lines
2.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package pki
import (
"context"
"crypto/x509"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// SecretCertsType is the name used to identify this type
const SecretCertsType = "pki"
func secretCerts(b *backend) *framework.Secret {
return &framework.Secret{
Type: SecretCertsType,
Fields: map[string]*framework.FieldSchema{
"certificate": {
Type: framework.TypeString,
Description: `The PEM-encoded concatenated certificate and
issuing certificate authority`,
},
"private_key": {
Type: framework.TypeString,
Description: "The PEM-encoded private key for the certificate",
},
"serial": {
Type: framework.TypeString,
Description: `The serial number of the certificate, for handy
reference`,
},
},
Revoke: b.secretCredsRevoke,
}
}
func (b *backend) secretCredsRevoke(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
if req.Secret == nil {
return nil, fmt.Errorf("secret is nil in request")
}
serialInt, ok := req.Secret.InternalData["serial_number"]
if !ok {
return nil, fmt.Errorf("could not find serial in internal secret data")
}
b.GetRevokeStorageLock().Lock()
defer b.GetRevokeStorageLock().Unlock()
sc := b.makeStorageContext(ctx, req.Storage)
serial := serialInt.(string)
certEntry, err := fetchCertBySerial(sc, "certs/", serial)
if err != nil {
return nil, err
}
if certEntry == nil {
// We can't write to revoked/ or update the CRL anyway because we don't have the cert,
// and there's no reason to expect this will work on a subsequent
// retry. Just give up and let the lease get deleted.
b.Logger().Warn("expired certificate revoke failed because not found in storage, treating as success", "serial", serial)
return nil, nil
}
cert, err := x509.ParseCertificate(certEntry.Value)
if err != nil {
return nil, fmt.Errorf("error parsing certificate: %w", err)
}
// Compatibility: Don't revoke CAs if they had leases. New CAs going forward aren't issued leases.
if cert.IsCA {
return nil, nil
}
config, err := sc.Backend.CrlBuilder().getConfigWithUpdate(sc)
if err != nil {
return nil, fmt.Errorf("error revoking serial: %s: failed reading config: %w", serial, err)
}
return revokeCert(sc, config, cert)
}