2016-04-06 00:42:26 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-04-07 18:13:19 +00:00
|
|
|
"sync"
|
2016-04-19 18:21:27 +00:00
|
|
|
"time"
|
2016-04-07 18:13:19 +00:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-04-06 00:42:26 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
b, err := Backend(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Setup(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Backend(conf *logical.BackendConfig) (*framework.Backend, error) {
|
|
|
|
salt, err := salt.NewSalt(conf.StorageView, &salt.Config{
|
|
|
|
HashFunc: salt.SHA256Hash,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
b := &backend{
|
2016-04-28 00:01:39 +00:00
|
|
|
// Setting the periodic func to be run once in an hour.
|
|
|
|
// If there is a real need, this can be made configurable.
|
2016-04-19 18:21:27 +00:00
|
|
|
tidyCooldownPeriod: time.Hour,
|
|
|
|
Salt: salt,
|
|
|
|
EC2ClientsMap: make(map[string]*ec2.EC2),
|
|
|
|
}
|
2016-04-06 00:42:26 +00:00
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
b.Backend = &framework.Backend{
|
|
|
|
PeriodicFunc: b.periodicFunc,
|
|
|
|
AuthRenew: b.pathLoginRenew,
|
|
|
|
Help: backendHelp,
|
2016-04-06 00:42:26 +00:00
|
|
|
PathsSpecial: &logical.Paths{
|
|
|
|
Unauthenticated: []string{
|
|
|
|
"login",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Paths: append([]*framework.Path{
|
2016-04-19 18:21:27 +00:00
|
|
|
pathLogin(b),
|
|
|
|
pathImage(b),
|
|
|
|
pathListImages(b),
|
|
|
|
pathImageTag(b),
|
|
|
|
pathConfigClient(b),
|
|
|
|
pathConfigCertificate(b),
|
2016-04-25 00:50:59 +00:00
|
|
|
pathConfigTidyRoleTags(b),
|
|
|
|
pathConfigTidyIdentities(b),
|
2016-04-19 18:21:27 +00:00
|
|
|
pathListCertificates(b),
|
|
|
|
pathBlacklistRoleTag(b),
|
|
|
|
pathListBlacklistRoleTags(b),
|
2016-04-25 00:50:59 +00:00
|
|
|
pathTidyRoleTags(b),
|
2016-04-19 18:21:27 +00:00
|
|
|
pathWhitelistIdentity(b),
|
2016-04-25 00:50:59 +00:00
|
|
|
pathTidyIdentities(b),
|
2016-04-19 18:21:27 +00:00
|
|
|
pathListWhitelistIdentities(b),
|
2016-04-06 00:42:26 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Backend, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type backend struct {
|
|
|
|
*framework.Backend
|
|
|
|
Salt *salt.Salt
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2016-04-28 00:01:39 +00:00
|
|
|
// Lock to make changes to any of the backend's configuration endpoints.
|
|
|
|
configMutex sync.RWMutex
|
|
|
|
|
|
|
|
// Duration after which the periodic function of the backend needs to be
|
|
|
|
// executed.
|
2016-04-19 18:21:27 +00:00
|
|
|
tidyCooldownPeriod time.Duration
|
2016-04-07 18:13:19 +00:00
|
|
|
|
2016-04-28 00:01:39 +00:00
|
|
|
// Var that holds the time at which the periodic func should initiatite
|
|
|
|
// the tidy operations.
|
|
|
|
nextTidyTime time.Time
|
|
|
|
|
|
|
|
// Map to hold the EC2 client objects indexed by region. This avoids the
|
|
|
|
// overhead of creating a client object for every login request.
|
2016-04-14 04:11:17 +00:00
|
|
|
EC2ClientsMap map[string]*ec2.EC2
|
2016-04-06 00:42:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
// periodicFunc performs the tasks that the backend wishes to do periodically.
|
|
|
|
// Currently this will be triggered once in a minute by the RollbackManager.
|
2016-04-28 00:01:39 +00:00
|
|
|
//
|
2016-04-19 18:21:27 +00:00
|
|
|
// The tasks being done are to cleanup the expired entries of both blacklist
|
2016-04-28 00:01:39 +00:00
|
|
|
// and whitelist. Tidying is done not once in a minute, but once in an hour.
|
|
|
|
// Tidying of blacklist and whitelist are by default enabled. This can be
|
|
|
|
// changed using `config/tidy/roletags` and `config/tidy/identities` endpoints.
|
2016-04-19 18:21:27 +00:00
|
|
|
func (b *backend) periodicFunc(req *logical.Request) error {
|
|
|
|
if b.nextTidyTime.IsZero() || !time.Now().Before(b.nextTidyTime) {
|
|
|
|
// safety_buffer defaults to 72h
|
|
|
|
safety_buffer := 259200
|
2016-04-28 04:35:49 +00:00
|
|
|
tidyBlacklistConfigEntry, err := b.configTidyRoleTags(req.Storage)
|
2016-04-19 18:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
skipBlacklistTidy := false
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if tidying of role tags was configured
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyBlacklistConfigEntry != nil {
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if periodic tidying of role tags was disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyBlacklistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipBlacklistTidy = true
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// overwrite the default safety_buffer with the configured value
|
2016-04-19 18:21:27 +00:00
|
|
|
safety_buffer = tidyBlacklistConfigEntry.SafetyBuffer
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// tidy role tags if explicitly not disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if !skipBlacklistTidy {
|
|
|
|
tidyBlacklistRoleTag(req.Storage, safety_buffer)
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 18:21:27 +00:00
|
|
|
// reset the safety_buffer to 72h
|
|
|
|
safety_buffer = 259200
|
2016-04-28 04:35:49 +00:00
|
|
|
tidyWhitelistConfigEntry, err := b.configTidyIdentities(req.Storage)
|
2016-04-19 18:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
2016-04-19 18:21:27 +00:00
|
|
|
skipWhitelistTidy := false
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if tidying of identities was configured
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyWhitelistConfigEntry != nil {
|
2016-04-28 00:01:39 +00:00
|
|
|
// check if periodic tidying of identities was disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if tidyWhitelistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipWhitelistTidy = true
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// overwrite the default safety_buffer with the configured value
|
2016-04-19 18:21:27 +00:00
|
|
|
safety_buffer = tidyWhitelistConfigEntry.SafetyBuffer
|
|
|
|
}
|
2016-04-28 00:01:39 +00:00
|
|
|
// tidy identities if explicitly not disabled
|
2016-04-19 18:21:27 +00:00
|
|
|
if !skipWhitelistTidy {
|
|
|
|
tidyWhitelistIdentity(req.Storage, safety_buffer)
|
|
|
|
}
|
|
|
|
|
2016-04-28 00:01:39 +00:00
|
|
|
// Update the nextTidyTime
|
2016-04-19 18:21:27 +00:00
|
|
|
b.nextTidyTime = time.Now().Add(b.tidyCooldownPeriod)
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:42:26 +00:00
|
|
|
const backendHelp = `
|
2016-04-28 00:01:39 +00:00
|
|
|
AWS auth backend takes in PKCS#7 signature of an AWS EC2 instance and a client
|
|
|
|
created nonce to authenticates the EC2 instance with Vault.
|
2016-04-06 00:42:26 +00:00
|
|
|
|
|
|
|
Authentication is backed by a preconfigured association of AMIs to Vault's policies
|
2016-04-28 00:01:39 +00:00
|
|
|
through 'image/<ami_id>' endpoint. All the instances that are using this AMI will
|
|
|
|
get the policies configured on the AMI.
|
|
|
|
|
|
|
|
If there is need to further restrict the policies set on the AMI, 'role_tag' option
|
|
|
|
can be enabled on the AMI and a tag can be generated using 'image/<ami_id>/roletag'
|
|
|
|
endpoint. This tag represents the subset of capabilities set on the AMI. When the
|
|
|
|
'role_tag' option is enabled on the AMI, the login operation requires that a respective
|
|
|
|
role tag is attached to the EC2 instance that is performing the login.
|
2016-04-06 00:42:26 +00:00
|
|
|
`
|