2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2016-05-24 18:23:27 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-04-19 05:28:53 +00:00
|
|
|
func pathConfigAccess() *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{
|
|
|
|
"address": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Consul server address",
|
|
|
|
},
|
|
|
|
|
|
|
|
"scheme": &framework.FieldSchema{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
|
|
|
|
"token": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Token for API calls",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-05-24 18:23:27 +00:00
|
|
|
logical.ReadOperation: pathConfigAccessRead,
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: pathConfigAccessWrite,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 18:23:27 +00:00
|
|
|
func readConfigAccess(storage logical.Storage) (*accessConfig, error, error) {
|
|
|
|
entry, err := storage.Get("config/access")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Access credentials for the backend itself haven't been configured. Please configure them at the '/config/access' endpoint"),
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := &accessConfig{}
|
|
|
|
if err := entry.DecodeJSON(conf); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error reading root configuration: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathConfigAccessRead(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
conf, userErr, intErr := readConfigAccess(req.Storage)
|
|
|
|
if intErr != nil {
|
|
|
|
return nil, intErr
|
|
|
|
}
|
|
|
|
if userErr != nil {
|
|
|
|
return logical.ErrorResponse(userErr.Error()), nil
|
|
|
|
}
|
|
|
|
if conf == nil {
|
|
|
|
return nil, fmt.Errorf("no user error reported but configuration not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"address": conf.Address,
|
|
|
|
"scheme": conf.Scheme,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:28:53 +00:00
|
|
|
func pathConfigAccessWrite(
|
2015-03-21 16:19:37 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-04-19 05:28:53 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/access", accessConfig{
|
2015-03-21 16:19:37 +00:00
|
|
|
Address: data.Get("address").(string),
|
|
|
|
Scheme: data.Get("scheme").(string),
|
|
|
|
Token: data.Get("token").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-19 05:28:53 +00:00
|
|
|
type accessConfig struct {
|
2015-03-21 16:19:37 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Scheme string `json:"scheme"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|