2017-09-20 20:59:35 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2021-04-22 15:20:59 +00:00
|
|
|
"fmt"
|
2018-01-08 18:31:38 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2017-12-16 00:18:32 +00:00
|
|
|
const configAccessKey = "config/access"
|
|
|
|
|
2017-11-06 21:34:20 +00:00
|
|
|
func pathConfigAccess(b *backend) *framework.Path {
|
2017-09-20 20:59:35 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/access",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"address": {
|
2017-09-20 20:59:35 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Nomad server address",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"token": {
|
2017-09-20 20:59:35 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Token for API calls",
|
|
|
|
},
|
2018-08-16 19:48:23 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"max_token_name_length": {
|
2018-08-16 19:48:23 +00:00
|
|
|
Type: framework.TypeInt,
|
|
|
|
Description: "Max length for name of generated Nomad tokens",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"ca_cert": {
|
2020-01-15 10:03:38 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `CA certificate to use when verifying Nomad server certificate,
|
|
|
|
must be x509 PEM encoded.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"client_cert": {
|
2020-01-15 10:03:38 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Client certificate used for Nomad's TLS communication,
|
|
|
|
must be x509 PEM encoded and if this is set you need to also set client_key.`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"client_key": {
|
2020-01-15 10:03:38 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Client key used for Nomad's TLS communication,
|
|
|
|
must be x509 PEM encoded and if this is set you need to also set client_cert.`,
|
|
|
|
},
|
2017-09-20 20:59:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2017-11-06 21:34:20 +00:00
|
|
|
logical.ReadOperation: b.pathConfigAccessRead,
|
2017-12-16 00:18:32 +00:00
|
|
|
logical.CreateOperation: b.pathConfigAccessWrite,
|
2017-11-06 21:34:20 +00:00
|
|
|
logical.UpdateOperation: b.pathConfigAccessWrite,
|
2017-12-16 00:18:32 +00:00
|
|
|
logical.DeleteOperation: b.pathConfigAccessDelete,
|
2017-09-20 20:59:35 +00:00
|
|
|
},
|
2017-12-16 00:18:32 +00:00
|
|
|
|
|
|
|
ExistenceCheck: b.configExistenceCheck,
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) configExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := b.readConfigAccess(ctx, req.Storage)
|
2017-12-16 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry != nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) readConfigAccess(ctx context.Context, storage logical.Storage) (*accessConfig, error) {
|
|
|
|
entry, err := storage.Get(ctx, configAccessKey)
|
2017-09-20 20:59:35 +00:00
|
|
|
if err != nil {
|
2017-11-01 08:49:31 +00:00
|
|
|
return nil, err
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
2017-11-29 11:15:54 +00:00
|
|
|
return nil, nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := &accessConfig{}
|
|
|
|
if err := entry.DecodeJSON(conf); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("error reading nomad access configuration: %w", err)
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 08:49:31 +00:00
|
|
|
return conf, nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigAccessRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
conf, err := b.readConfigAccess(ctx, req.Storage)
|
2017-11-06 21:36:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
if conf == nil {
|
2017-11-29 11:15:54 +00:00
|
|
|
return nil, nil
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2018-08-16 19:48:23 +00:00
|
|
|
"address": conf.Address,
|
|
|
|
"max_token_name_length": conf.MaxTokenNameLength,
|
2022-06-10 14:09:54 +00:00
|
|
|
"ca_cert": conf.CACert,
|
|
|
|
"client_cert": conf.ClientCert,
|
2017-09-20 20:59:35 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigAccessWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
conf, err := b.readConfigAccess(ctx, req.Storage)
|
2017-12-16 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if conf == nil {
|
|
|
|
conf = &accessConfig{}
|
|
|
|
}
|
|
|
|
|
|
|
|
address, ok := data.GetOk("address")
|
|
|
|
if ok {
|
|
|
|
conf.Address = address.(string)
|
2017-11-29 10:36:34 +00:00
|
|
|
}
|
2017-12-16 00:18:32 +00:00
|
|
|
token, ok := data.GetOk("token")
|
|
|
|
if ok {
|
|
|
|
conf.Token = token.(string)
|
2017-11-29 10:36:34 +00:00
|
|
|
}
|
2020-01-15 10:03:38 +00:00
|
|
|
caCert, ok := data.GetOk("ca_cert")
|
|
|
|
if ok {
|
|
|
|
conf.CACert = caCert.(string)
|
|
|
|
}
|
|
|
|
clientCert, ok := data.GetOk("client_cert")
|
|
|
|
if ok {
|
|
|
|
conf.ClientCert = clientCert.(string)
|
|
|
|
}
|
|
|
|
clientKey, ok := data.GetOk("client_key")
|
|
|
|
if ok {
|
|
|
|
conf.ClientKey = clientKey.(string)
|
|
|
|
}
|
2017-12-16 00:18:32 +00:00
|
|
|
|
2022-04-20 18:06:25 +00:00
|
|
|
if conf.Token == "" {
|
|
|
|
client, err := clientFromConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse("Token not provided and failed to constuct client"), err
|
|
|
|
}
|
|
|
|
token, _, err := client.ACLTokens().Bootstrap(nil)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse("Token not provided and failed to bootstrap ACLs"), err
|
|
|
|
}
|
|
|
|
conf.Token = token.SecretID
|
|
|
|
}
|
|
|
|
|
2018-08-16 19:48:23 +00:00
|
|
|
conf.MaxTokenNameLength = data.Get("max_token_name_length").(int)
|
|
|
|
|
2017-12-16 00:18:32 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/access", conf)
|
2017-09-20 20:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2017-09-20 20:59:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConfigAccessDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Delete(ctx, configAccessKey); err != nil {
|
2017-12-16 00:18:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-09-20 20:59:35 +00:00
|
|
|
type accessConfig struct {
|
2018-08-16 19:48:23 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
MaxTokenNameLength int `json:"max_token_name_length"`
|
2020-01-15 10:03:38 +00:00
|
|
|
CACert string `json:"ca_cert"`
|
|
|
|
ClientCert string `json:"client_cert"`
|
|
|
|
ClientKey string `json:"client_key"`
|
2017-09-20 20:59:35 +00:00
|
|
|
}
|