2017-09-20 20:59:35 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
|
|
|
|
2017-11-03 07:19:49 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-09-20 20:59:35 +00:00
|
|
|
)
|
|
|
|
|
2018-08-16 19:48:23 +00:00
|
|
|
// Factory returns a Nomad backend that satisfies the logical.Backend interface
|
2018-01-19 06:44:44 +00:00
|
|
|
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
|
2017-09-20 20:59:35 +00:00
|
|
|
b := Backend()
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.Setup(ctx, conf); err != nil {
|
2017-09-20 20:59:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2018-08-16 19:48:23 +00:00
|
|
|
// Backend returns the configured Nomad backend
|
2017-09-20 20:59:35 +00:00
|
|
|
func Backend() *backend {
|
|
|
|
var b backend
|
|
|
|
b.Backend = &framework.Backend{
|
2017-11-29 21:53:21 +00:00
|
|
|
PathsSpecial: &logical.Paths{
|
|
|
|
SealWrapStorage: []string{
|
|
|
|
"config/access",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-09-20 20:59:35 +00:00
|
|
|
Paths: []*framework.Path{
|
2017-11-06 21:34:20 +00:00
|
|
|
pathConfigAccess(&b),
|
2017-11-06 15:09:56 +00:00
|
|
|
pathConfigLease(&b),
|
2017-09-20 20:59:35 +00:00
|
|
|
pathListRoles(&b),
|
2017-11-06 21:34:20 +00:00
|
|
|
pathRoles(&b),
|
2017-11-07 14:58:19 +00:00
|
|
|
pathCredsCreate(&b),
|
2017-09-20 20:59:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Secrets: []*framework.Secret{
|
|
|
|
secretToken(&b),
|
|
|
|
},
|
|
|
|
BackendType: logical.TypeLogical,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
type backend struct {
|
|
|
|
*framework.Backend
|
2017-11-03 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) client(ctx context.Context, s logical.Storage) (*api.Client, error) {
|
|
|
|
conf, err := b.readConfigAccess(ctx, s)
|
2017-11-06 21:34:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-03 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 15:51:39 +00:00
|
|
|
nomadConf := api.DefaultConfig()
|
2017-12-17 14:10:56 +00:00
|
|
|
if conf != nil {
|
|
|
|
if conf.Address != "" {
|
|
|
|
nomadConf.Address = conf.Address
|
|
|
|
}
|
|
|
|
if conf.Token != "" {
|
|
|
|
nomadConf.SecretID = conf.Token
|
|
|
|
}
|
2020-01-15 10:03:38 +00:00
|
|
|
if conf.CACert != "" {
|
|
|
|
nomadConf.TLSConfig.CACertPEM = []byte(conf.CACert)
|
|
|
|
}
|
|
|
|
if conf.ClientCert != "" {
|
|
|
|
nomadConf.TLSConfig.ClientCertPEM = []byte(conf.ClientCert)
|
|
|
|
}
|
|
|
|
if conf.ClientKey != "" {
|
|
|
|
nomadConf.TLSConfig.ClientKeyPEM = []byte(conf.ClientKey)
|
|
|
|
}
|
2017-11-29 11:01:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 21:34:20 +00:00
|
|
|
client, err := api.NewClient(nomadConf)
|
2017-11-03 07:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-16 19:48:23 +00:00
|
|
|
|
2017-11-06 21:34:20 +00:00
|
|
|
return client, nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|