2016-04-06 00:42:26 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2016-04-25 00:50:59 +00:00
|
|
|
func pathTidyIdentities(b *backend) *framework.Path {
|
2016-04-06 00:42:26 +00:00
|
|
|
return &framework.Path{
|
2016-04-25 00:50:59 +00:00
|
|
|
Pattern: "tidy/identities$",
|
2016-04-06 00:42:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"safety_buffer": &framework.FieldSchema{
|
|
|
|
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.`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-04-25 00:50:59 +00:00
|
|
|
logical.UpdateOperation: b.pathTidyIdentitiesUpdate,
|
2016-04-06 00:42:26 +00:00
|
|
|
},
|
|
|
|
|
2016-04-25 00:50:59 +00:00
|
|
|
HelpSynopsis: pathTidyIdentitiesSyn,
|
|
|
|
HelpDescription: pathTidyIdentitiesDesc,
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
// tidyWhitelistIdentity is used to delete entries in the whitelist that are expired.
|
|
|
|
func tidyWhitelistIdentity(s logical.Storage, safety_buffer int) error {
|
2016-04-06 00:42:26 +00:00
|
|
|
bufferDuration := time.Duration(safety_buffer) * time.Second
|
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
identities, err := s.List("whitelist/identity/")
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return err
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, instanceID := range identities {
|
2016-04-19 01:06:26 +00:00
|
|
|
identityEntry, err := s.Get("whitelist/identity/" + instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
if err != nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return fmt.Errorf("error fetching identity of instanceID %s: %s", instanceID, err)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry == nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return fmt.Errorf("identity entry for instanceID %s is nil", instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if identityEntry.Value == nil || len(identityEntry.Value) == 0 {
|
2016-04-19 01:06:26 +00:00
|
|
|
return fmt.Errorf("found identity entry for instanceID %s but actual identity is empty", instanceID)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result whitelistIdentity
|
|
|
|
if err := identityEntry.DecodeJSON(&result); err != nil {
|
2016-04-19 01:06:26 +00:00
|
|
|
return err
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().After(result.ExpirationTime.Add(bufferDuration)) {
|
2016-04-19 01:06:26 +00:00
|
|
|
if err := s.Delete("whitelist/identity" + instanceID); err != nil {
|
|
|
|
return fmt.Errorf("error deleting identity of instanceID %s from storage: %s", instanceID, err)
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:06:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:50:59 +00:00
|
|
|
// pathTidyIdentitiesUpdate is used to delete entries in the whitelist that are expired.
|
|
|
|
func (b *backend) pathTidyIdentitiesUpdate(
|
2016-04-19 01:06:26 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
return nil, tidyWhitelistIdentity(req.Storage, data.Get("safety_buffer").(int))
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:50:59 +00:00
|
|
|
const pathTidyIdentitiesSyn = `
|
2016-04-06 00:42:26 +00:00
|
|
|
Clean-up the whitelisted instance identity entries.
|
|
|
|
`
|
|
|
|
|
2016-04-25 00:50:59 +00:00
|
|
|
const pathTidyIdentitiesDesc = `
|
2016-04-06 00:42:26 +00:00
|
|
|
When an instance identity is whitelisted, the expiration time of the whitelist
|
|
|
|
entry is set to the least amont 'max_ttl' of the registered AMI, 'max_ttl' of the
|
|
|
|
role tag and 'max_ttl' of the backend mount.
|
|
|
|
|
|
|
|
When this endpoint is invoked all the entries that are expired will be deleted.
|
|
|
|
|
|
|
|
A 'safety_buffer' (duration in seconds) can be provided, to ensure deletion of
|
|
|
|
only those entries that are expired before 'safety_buffer' seconds.
|
|
|
|
`
|