2017-04-24 19:15:50 +00:00
|
|
|
package awsauth
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-04-06 00:42:26 +00:00
|
|
|
"fmt"
|
2018-07-11 19:45:09 +00:00
|
|
|
"net/http"
|
2016-05-05 18:28:46 +00:00
|
|
|
"sync/atomic"
|
2016-04-06 00:42:26 +00:00
|
|
|
"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"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2016-04-06 00:42:26 +00:00
|
|
|
)
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
func (b *backend) pathTidyIdentityAccessList() *framework.Path {
|
2016-04-06 00:42:26 +00:00
|
|
|
return &framework.Path{
|
2021-02-26 07:23:34 +00:00
|
|
|
Pattern: "tidy/identity-accesslist$",
|
2016-04-06 00:42:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2019-07-31 23:11:35 +00:00
|
|
|
"safety_buffer": {
|
2016-04-06 00:42:26 +00:00
|
|
|
Type: framework.TypeDurationSecond,
|
|
|
|
Default: 259200,
|
|
|
|
Description: `The amount of extra time that must have passed beyond the identity's
|
|
|
|
expiration, before it is removed from the backend storage.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-07-31 23:11:35 +00:00
|
|
|
Operations: map[logical.Operation]framework.OperationHandler{
|
|
|
|
logical.UpdateOperation: &framework.PathOperation{
|
2021-02-26 07:23:34 +00:00
|
|
|
Callback: b.pathTidyIdentityAccessListUpdate,
|
2019-07-31 23:11:35 +00:00
|
|
|
},
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
HelpSynopsis: pathTidyIdentityAccessListSyn,
|
|
|
|
HelpDescription: pathTidyIdentityAccessListDesc,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
// tidyAccessListIdentity is used to delete entries in the access list that are expired.
|
|
|
|
func (b *backend) tidyAccessListIdentity(ctx context.Context, req *logical.Request, safetyBuffer int) (*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
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
if !atomic.CompareAndSwapUint32(b.tidyAccessListCASGuard, 0, 1) {
|
2018-06-16 22:21:33 +00:00
|
|
|
resp := &logical.Response{}
|
|
|
|
resp.AddWarning("Tidy operation already in progress.")
|
|
|
|
return resp, nil
|
2016-05-05 18:28:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
s := req.Storage
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
go func() {
|
2021-02-26 07:23:34 +00:00
|
|
|
defer atomic.StoreUint32(b.tidyAccessListCASGuard, 0)
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
// Don't cancel when the original client request goes away
|
|
|
|
ctx = context.Background()
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
logger := b.Logger().Named("wltidy")
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2018-06-20 00:58:12 +00:00
|
|
|
bufferDuration := time.Duration(safetyBuffer) * time.Second
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
doTidy := func() error {
|
2021-02-26 07:23:34 +00:00
|
|
|
identities, err := s.List(ctx, identityAccessListStorage)
|
2018-06-16 22:21:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, instanceID := range identities {
|
2021-02-26 07:23:34 +00:00
|
|
|
identityEntry, err := s.Get(ctx, identityAccessListStorage+instanceID)
|
2018-06-16 22:21:33 +00:00
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error fetching identity of instanceID %q: %w", instanceID, err)
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry == nil {
|
|
|
|
return fmt.Errorf("identity entry for instanceID %q is nil", instanceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry.Value == nil || len(identityEntry.Value) == 0 {
|
|
|
|
return fmt.Errorf("found identity entry for instanceID %q but actual identity is empty", instanceID)
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
var result accessListIdentity
|
2018-06-16 22:21:33 +00:00
|
|
|
if err := identityEntry.DecodeJSON(&result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().After(result.ExpirationTime.Add(bufferDuration)) {
|
2021-02-26 07:23:34 +00:00
|
|
|
if err := s.Delete(ctx, identityAccessListStorage+instanceID); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return fmt.Errorf("error deleting identity of instanceID %q from storage: %w", instanceID, err)
|
2018-06-16 22:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
return nil
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
if err := doTidy(); err != nil {
|
2021-02-26 07:23:34 +00:00
|
|
|
logger.Error("error running access list tidy", "error", err)
|
2018-06-16 22:21:33 +00:00
|
|
|
return
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
}()
|
2016-04-06 00:42:26 +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-04-19 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
// pathTidyIdentityAccessListUpdate is used to delete entries in the access list that are expired.
|
|
|
|
func (b *backend) pathTidyIdentityAccessListUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
return b.tidyAccessListIdentity(ctx, req, data.Get("safety_buffer").(int))
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
const pathTidyIdentityAccessListSyn = `
|
|
|
|
Clean-up the access list instance identity entries.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|
|
|
|
|
2021-02-26 07:23:34 +00:00
|
|
|
const pathTidyIdentityAccessListDesc = `
|
|
|
|
When an instance identity is in the access list, the expiration time of the access list
|
2016-05-12 11:19:29 +00:00
|
|
|
entry is set based on the maximum 'max_ttl' value set on: the role, the role tag
|
2016-04-28 02:03:11 +00:00
|
|
|
and the backend's mount.
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2016-04-28 02:03:11 +00:00
|
|
|
When this endpoint is invoked, all the entries that are expired will be deleted.
|
2016-04-06 00:42:26 +00:00
|
|
|
A 'safety_buffer' (duration in seconds) can be provided, to ensure deletion of
|
|
|
|
only those entries that are expired before 'safety_buffer' seconds.
|
|
|
|
`
|