2016-05-30 18:30:01 +00:00
|
|
|
package approle
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-05-30 18:30:01 +00:00
|
|
|
"fmt"
|
2018-07-11 19:45:09 +00:00
|
|
|
"net/http"
|
2016-05-30 18:30:01 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2020-03-03 21:43:24 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/locksutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-05-30 18:30:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func pathTidySecretID(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "tidy/secret-id$",
|
|
|
|
|
2023-01-24 18:12:41 +00:00
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
|
|
|
Callback: b.pathTidySecretIDUpdate,
|
|
|
|
Responses: map[int][]framework.Response{
|
|
|
|
http.StatusAccepted: {{
|
|
|
|
Description: http.StatusText(http.StatusAccepted),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
2016-05-30 18:30:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathTidySecretIDSyn,
|
|
|
|
HelpDescription: pathTidySecretIDDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tidySecretID is used to delete entries in the whitelist that are expired.
|
2018-07-11 19:45:09 +00:00
|
|
|
func (b *backend) tidySecretID(ctx context.Context, req *logical.Request) (*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
|
|
|
|
}
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
if !atomic.CompareAndSwapUint32(b.tidySecretIDCASGuard, 0, 1) {
|
|
|
|
resp := &logical.Response{}
|
|
|
|
resp.AddWarning("Tidy operation already in progress.")
|
|
|
|
return resp, nil
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
go b.tidySecretIDinternal(req.Storage)
|
2018-07-11 19:45:09 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
resp := &logical.Response{}
|
|
|
|
resp.AddWarning("Tidy operation successfully started. Any information from the operation will be printed to Vault's server logs.")
|
|
|
|
return logical.RespondWithStatusCode(resp, req, http.StatusAccepted)
|
|
|
|
}
|
2018-04-23 20:02:55 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
type tidyHelperSecretIDAccessor struct {
|
|
|
|
secretIDAccessorStorageEntry
|
|
|
|
saltedSecretIDAccessor string
|
|
|
|
}
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
func (b *backend) tidySecretIDinternal(s logical.Storage) {
|
|
|
|
defer atomic.StoreUint32(b.tidySecretIDCASGuard, 0)
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
logger := b.Logger().Named("tidy")
|
|
|
|
|
|
|
|
checkCount := 0
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
logger.Trace("done checking entries", "num_entries", checkCount)
|
|
|
|
}()
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// Don't cancel when the original client request goes away
|
|
|
|
ctx := context.Background()
|
2018-04-23 20:02:55 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
salt, err := b.Salt(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error tidying secret IDs", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tidyFunc := func(secretIDPrefixToUse, accessorIDPrefixToUse string) error {
|
|
|
|
logger.Trace("listing accessors", "prefix", accessorIDPrefixToUse)
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// List all the accessors and add them all to a map
|
|
|
|
// These hashes are the result of salting the accessor id.
|
|
|
|
accessorHashes, err := s.List(ctx, accessorIDPrefixToUse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
skipHashes := make(map[string]bool, len(accessorHashes))
|
|
|
|
accHashesByLockID := make([][]tidyHelperSecretIDAccessor, 256)
|
|
|
|
for _, accessorHash := range accessorHashes {
|
|
|
|
var entry secretIDAccessorStorageEntry
|
|
|
|
entryIndex := accessorIDPrefixToUse + accessorHash
|
|
|
|
se, err := s.Get(ctx, entryIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if se == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = se.DecodeJSON(&entry)
|
2016-05-30 18:30:01 +00:00
|
|
|
if err != nil {
|
2018-06-16 22:21:33 +00:00
|
|
|
return err
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
lockIdx := locksutil.LockIndexForKey(entry.SecretIDHMAC)
|
|
|
|
accHashesByLockID[lockIdx] = append(accHashesByLockID[lockIdx], tidyHelperSecretIDAccessor{
|
|
|
|
secretIDAccessorStorageEntry: entry,
|
|
|
|
saltedSecretIDAccessor: accessorHash,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
secretIDCleanupFunc := func(secretIDHMAC, roleNameHMAC, secretIDPrefixToUse string) error {
|
|
|
|
checkCount++
|
|
|
|
lock := b.secretIDLock(secretIDHMAC)
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
entryIndex := fmt.Sprintf("%s%s%s", secretIDPrefixToUse, roleNameHMAC, secretIDHMAC)
|
|
|
|
secretIDEntry, err := s.Get(ctx, entryIndex)
|
2018-06-16 22:21:33 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error fetching SecretID %q: %w", secretIDHMAC, err)
|
2018-04-24 20:17:24 +00:00
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
|
|
|
|
if secretIDEntry == nil {
|
|
|
|
logger.Error("entry for secret id was nil", "secret_id_hmac", secretIDHMAC)
|
|
|
|
return nil
|
2018-04-24 20:17:24 +00:00
|
|
|
}
|
2016-05-30 18:30:01 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
if secretIDEntry.Value == nil || len(secretIDEntry.Value) == 0 {
|
|
|
|
return fmt.Errorf("found entry for SecretID %q but actual SecretID is empty", secretIDHMAC)
|
|
|
|
}
|
2018-07-24 21:00:53 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
var result secretIDStorageEntry
|
|
|
|
if err := secretIDEntry.DecodeJSON(&result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-24 20:17:24 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// If a secret ID entry does not have a corresponding accessor
|
|
|
|
// entry, revoke the secret ID immediately
|
|
|
|
accessorEntry, err := b.secretIDAccessorEntry(ctx, s, result.SecretIDAccessor, secretIDPrefixToUse)
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("failed to read secret ID accessor entry: %w", err)
|
2020-03-03 21:43:24 +00:00
|
|
|
}
|
|
|
|
if accessorEntry == nil {
|
|
|
|
logger.Trace("found nil accessor")
|
|
|
|
if err := s.Delete(ctx, entryIndex); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error deleting secret ID %q from storage: %w", secretIDHMAC, err)
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// ExpirationTime not being set indicates non-expiring SecretIDs
|
|
|
|
if !result.ExpirationTime.IsZero() && time.Now().After(result.ExpirationTime) {
|
|
|
|
logger.Trace("found expired secret ID")
|
|
|
|
// Clean up the accessor of the secret ID first
|
|
|
|
err = b.deleteSecretIDAccessorEntry(ctx, s, result.SecretIDAccessor, secretIDPrefixToUse)
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("failed to delete secret ID accessor entry: %w", err)
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
if err := s.Delete(ctx, entryIndex); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error deleting SecretID %q from storage: %w", secretIDHMAC, err)
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-05 20:12:11 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// At this point, the secret ID is not expired and is valid. Flag
|
|
|
|
// the corresponding accessor as not needing attention.
|
|
|
|
skipHashes[salt.SaltID(result.SecretIDAccessor)] = true
|
2018-06-16 22:21:33 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
logger.Trace("listing role HMACs", "prefix", secretIDPrefixToUse)
|
2018-06-16 22:21:33 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
roleNameHMACs, err := s.List(ctx, secretIDPrefixToUse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-06 20:44:48 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
for _, roleNameHMAC := range roleNameHMACs {
|
|
|
|
logger.Trace("listing secret ID HMACs", "role_hmac", roleNameHMAC)
|
|
|
|
secretIDHMACs, err := s.List(ctx, fmt.Sprintf("%s%s", secretIDPrefixToUse, roleNameHMAC))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, secretIDHMAC := range secretIDHMACs {
|
|
|
|
err = secretIDCleanupFunc(secretIDHMAC, roleNameHMAC, secretIDPrefixToUse)
|
2018-06-16 22:21:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-23 20:02:55 +00:00
|
|
|
}
|
2018-04-24 20:17:24 +00:00
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
}
|
2018-04-24 20:17:24 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
// Accessor indexes were not getting cleaned up until 0.9.3. This is a fix
|
|
|
|
// to clean up the dangling accessor entries.
|
|
|
|
if len(accessorHashes) > len(skipHashes) {
|
|
|
|
// There is some raciness here because we're querying secretids for
|
|
|
|
// roles without having a lock while doing so. Because
|
|
|
|
// accHashesByLockID was populated previously, at worst this may
|
|
|
|
// mean that we fail to clean up something we ought to.
|
2021-04-08 16:43:39 +00:00
|
|
|
allSecretIDHMACs := make(map[string]struct{})
|
2018-06-16 22:21:33 +00:00
|
|
|
for _, roleNameHMAC := range roleNameHMACs {
|
2020-03-03 21:43:24 +00:00
|
|
|
secretIDHMACs, err := s.List(ctx, secretIDPrefixToUse+roleNameHMAC)
|
2018-06-16 22:21:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
for _, v := range secretIDHMACs {
|
|
|
|
allSecretIDHMACs[v] = struct{}{}
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
2018-04-24 20:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
tidyEntries := func(entries []tidyHelperSecretIDAccessor) error {
|
|
|
|
for _, entry := range entries {
|
|
|
|
// Don't clean up accessor index entry if secretid cleanup func
|
|
|
|
// determined that it should stay.
|
|
|
|
if _, ok := skipHashes[entry.saltedSecretIDAccessor]; ok {
|
|
|
|
continue
|
2018-07-24 21:00:53 +00:00
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
|
|
|
|
// Don't clean up accessor index entry if referenced in role.
|
|
|
|
if _, ok := allSecretIDHMACs[entry.SecretIDHMAC]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Delete(context.Background(), accessorIDPrefixToUse+entry.saltedSecretIDAccessor); err != nil {
|
|
|
|
return err
|
2018-07-24 21:00:53 +00:00
|
|
|
}
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
2020-03-03 21:43:24 +00:00
|
|
|
return nil
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
2018-02-06 20:44:48 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
for lockIdx, entries := range accHashesByLockID {
|
|
|
|
// Ideally, locking on accessors should be performed here too
|
|
|
|
// but for that, accessors are required in plaintext, which are
|
|
|
|
// not available.
|
|
|
|
// ...
|
|
|
|
// The lock is held when writing accessor/secret so if we have
|
|
|
|
// the lock we know we're not in a
|
|
|
|
// wrote-accessor-but-not-yet-secret case, which can be racy.
|
|
|
|
b.secretIDLocks[lockIdx].Lock()
|
|
|
|
err = tidyEntries(entries)
|
|
|
|
b.secretIDLocks[lockIdx].Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
2018-04-23 20:02:55 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-06 20:44:48 +00:00
|
|
|
|
2020-03-03 21:43:24 +00:00
|
|
|
err = tidyFunc(secretIDPrefix, secretIDAccessorPrefix)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error tidying global secret IDs", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tidyFunc(secretIDLocalPrefix, secretIDAccessorLocalPrefix)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error tidying local secret IDs", "error", err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathTidySecretIDUpdate is used to delete the expired SecretID entries
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathTidySecretIDUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-07-11 19:45:09 +00:00
|
|
|
return b.tidySecretID(ctx, req)
|
2016-05-30 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
const (
|
|
|
|
pathTidySecretIDSyn = "Trigger the clean-up of expired SecretID entries."
|
|
|
|
pathTidySecretIDDesc = `SecretIDs will have expiration time attached to them. The periodic function
|
2016-05-30 18:30:01 +00:00
|
|
|
of the backend will look for expired entries and delete them. This happens once in a minute. Invoking
|
|
|
|
this endpoint will trigger the clean-up action, without waiting for the backend's periodic function.`
|
2021-04-08 16:43:39 +00:00
|
|
|
)
|