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{
|
|
|
|
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),
|
|
|
|
pathConfigTidyBlacklistRoleTag(b),
|
|
|
|
pathConfigTidyWhitelistIdentity(b),
|
|
|
|
pathListCertificates(b),
|
|
|
|
pathBlacklistRoleTag(b),
|
|
|
|
pathListBlacklistRoleTags(b),
|
|
|
|
pathBlacklistRoleTagTidy(b),
|
|
|
|
pathWhitelistIdentity(b),
|
|
|
|
pathWhitelistIdentityTidy(b),
|
|
|
|
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-19 18:21:27 +00:00
|
|
|
configMutex sync.RWMutex
|
|
|
|
tidyCooldownPeriod time.Duration
|
|
|
|
nextTidyTime time.Time
|
2016-04-07 18:13:19 +00:00
|
|
|
|
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.
|
|
|
|
// The tasks being done are to cleanup the expired entries of both blacklist
|
|
|
|
// and whitelist.
|
|
|
|
func (b *backend) periodicFunc(req *logical.Request) error {
|
2016-04-19 01:06:26 +00:00
|
|
|
b.configMutex.Lock()
|
|
|
|
defer b.configMutex.Unlock()
|
2016-04-19 18:21:27 +00:00
|
|
|
if b.nextTidyTime.IsZero() || !time.Now().Before(b.nextTidyTime) {
|
|
|
|
// safety_buffer defaults to 72h
|
|
|
|
safety_buffer := 259200
|
|
|
|
tidyBlacklistConfigEntry, err := configTidyBlacklistRoleTag(req.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
skipBlacklistTidy := false
|
|
|
|
if tidyBlacklistConfigEntry != nil {
|
|
|
|
if tidyBlacklistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipBlacklistTidy = true
|
|
|
|
}
|
|
|
|
safety_buffer = tidyBlacklistConfigEntry.SafetyBuffer
|
|
|
|
}
|
|
|
|
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
|
|
|
|
tidyWhitelistConfigEntry, err := configTidyWhitelistIdentity(req.Storage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-19 01:06:26 +00:00
|
|
|
}
|
2016-04-19 18:21:27 +00:00
|
|
|
skipWhitelistTidy := false
|
|
|
|
if tidyWhitelistConfigEntry != nil {
|
|
|
|
if tidyWhitelistConfigEntry.DisablePeriodicTidy {
|
|
|
|
skipWhitelistTidy = true
|
|
|
|
}
|
|
|
|
safety_buffer = tidyWhitelistConfigEntry.SafetyBuffer
|
|
|
|
}
|
|
|
|
if !skipWhitelistTidy {
|
|
|
|
tidyWhitelistIdentity(req.Storage, safety_buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the lastTidyTime
|
|
|
|
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 = `
|
|
|
|
AWS auth backend takes in a AWS EC2 instance identity document, its PKCS#7 signature
|
|
|
|
and a client created nonce to authenticates the instance with Vault.
|
|
|
|
|
|
|
|
Authentication is backed by a preconfigured association of AMIs to Vault's policies
|
2016-04-08 06:05:57 +00:00
|
|
|
through 'image/<ami_id>' endpoint. For instances that share an AMI, an instance tag can
|
|
|
|
be created through 'image/<ami_id>/tag'. This tag should be attached to the EC2 instance
|
2016-04-06 00:42:26 +00:00
|
|
|
before the instance attempts to login to Vault.
|
|
|
|
`
|