2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2016-05-24 18:23:27 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-04-20 22:16:15 +00:00
|
|
|
"github.com/hashicorp/consul/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"
|
2015-03-21 16:19:37 +00:00
|
|
|
)
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func pathConfigAccess(b *backend) *framework.Path {
|
2015-03-21 16:19:37 +00:00
|
|
|
return &framework.Path{
|
2015-04-19 05:28:53 +00:00
|
|
|
Pattern: "config/access",
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"address": {
|
2015-03-21 16:19:37 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Consul server address",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"scheme": {
|
2015-03-21 16:19:37 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "URI scheme for the Consul address",
|
|
|
|
|
|
|
|
// https would be a better default but Consul on its own
|
|
|
|
// defaults to HTTP access, and when HTTPS is enabled it
|
|
|
|
// disables HTTP, so there isn't really any harm done here.
|
|
|
|
Default: "http",
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"token": {
|
2015-03-21 16:19:37 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Token for API calls",
|
|
|
|
},
|
2020-01-29 08:44:35 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"ca_cert": {
|
2020-01-29 08:44:35 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `CA certificate to use when verifying Consul server certificate,
|
|
|
|
must be x509 PEM encoded.`,
|
|
|
|
},
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
"client_cert": {
|
2020-01-29 08:44:35 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Client certificate used for Consul'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-29 08:44:35 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `Client key used for Consul's TLS communication,
|
|
|
|
must be x509 PEM encoded and if this is set you need to also set client_cert.`,
|
|
|
|
},
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2018-11-02 14:44:12 +00:00
|
|
|
logical.ReadOperation: b.pathConfigAccessRead,
|
|
|
|
logical.UpdateOperation: b.pathConfigAccessWrite,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) readConfigAccess(ctx context.Context, storage logical.Storage) (*accessConfig, error, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
entry, err := storage.Get(ctx, "config/access")
|
2016-05-24 18:23:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("access credentials for the backend itself haven't been configured; please configure them at the '/config/access' endpoint"), nil
|
2016-05-24 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := &accessConfig{}
|
|
|
|
if err := entry.DecodeJSON(conf); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, nil, fmt.Errorf("error reading consul access configuration: %w", err)
|
2016-05-24 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) pathConfigAccessRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
conf, userErr, intErr := b.readConfigAccess(ctx, req.Storage)
|
2016-05-24 18:23:27 +00:00
|
|
|
if intErr != nil {
|
|
|
|
return nil, intErr
|
|
|
|
}
|
|
|
|
if userErr != nil {
|
|
|
|
return logical.ErrorResponse(userErr.Error()), nil
|
|
|
|
}
|
|
|
|
if conf == nil {
|
2016-05-24 21:28:19 +00:00
|
|
|
return nil, fmt.Errorf("no user error reported but consul access configuration not found")
|
2016-05-24 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 21:28:19 +00:00
|
|
|
return &logical.Response{
|
2016-05-24 18:23:27 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"address": conf.Address,
|
|
|
|
"scheme": conf.Scheme,
|
|
|
|
},
|
2016-05-24 21:28:19 +00:00
|
|
|
}, nil
|
2016-05-24 18:23:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 14:44:12 +00:00
|
|
|
func (b *backend) pathConfigAccessWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2022-04-20 22:16:15 +00:00
|
|
|
config := accessConfig{
|
2020-01-29 08:44:35 +00:00
|
|
|
Address: data.Get("address").(string),
|
|
|
|
Scheme: data.Get("scheme").(string),
|
|
|
|
Token: data.Get("token").(string),
|
|
|
|
CACert: data.Get("ca_cert").(string),
|
|
|
|
ClientCert: data.Get("client_cert").(string),
|
|
|
|
ClientKey: data.Get("client_key").(string),
|
2022-04-20 22:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a token has not been given by the user, we try to boostrap the ACL
|
|
|
|
// support
|
|
|
|
if config.Token == "" {
|
|
|
|
consulConf := config.NewConfig()
|
|
|
|
client, err := api.NewClient(consulConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token, _, err := client.ACL().Bootstrap()
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse("Token not provided and failed to bootstrap ACLs"), err
|
|
|
|
}
|
|
|
|
config.Token = token.SecretID
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON("config/access", config)
|
2015-03-21 16:19:37 +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 {
|
2015-03-21 16:19:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:28:53 +00:00
|
|
|
type accessConfig struct {
|
2020-01-29 08:44:35 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Scheme string `json:"scheme"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
CACert string `json:"ca_cert"`
|
|
|
|
ClientCert string `json:"client_cert"`
|
|
|
|
ClientKey string `json:"client_key"`
|
2015-03-21 16:19:37 +00:00
|
|
|
}
|
2022-04-20 22:16:15 +00:00
|
|
|
|
|
|
|
func (conf *accessConfig) NewConfig() *api.Config {
|
|
|
|
consulConf := api.DefaultNonPooledConfig()
|
|
|
|
consulConf.Address = conf.Address
|
|
|
|
consulConf.Scheme = conf.Scheme
|
|
|
|
consulConf.Token = conf.Token
|
|
|
|
consulConf.TLSConfig.CAPem = []byte(conf.CACert)
|
|
|
|
consulConf.TLSConfig.CertPEM = []byte(conf.ClientCert)
|
|
|
|
consulConf.TLSConfig.KeyPEM = []byte(conf.ClientKey)
|
|
|
|
|
|
|
|
return consulConf
|
|
|
|
}
|