open-vault/builtin/logical/nomad/path_config_access.go

94 lines
2.1 KiB
Go
Raw Normal View History

2017-09-20 20:59:35 +00:00
package nomad
import (
2017-12-15 22:06:56 +00:00
"github.com/hashicorp/errwrap"
2017-09-20 20:59:35 +00:00
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
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{
"address": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Nomad server address",
},
"token": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Token for API calls",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
2017-11-06 21:34:20 +00:00
logical.ReadOperation: b.pathConfigAccessRead,
logical.UpdateOperation: b.pathConfigAccessWrite,
2017-09-20 20:59:35 +00:00
},
}
}
2017-11-06 21:34:20 +00:00
func (b *backend) readConfigAccess(storage logical.Storage) (*accessConfig, error) {
2017-09-20 20:59:35 +00:00
entry, err := storage.Get("config/access")
if err != nil {
return nil, err
2017-09-20 20:59:35 +00:00
}
if entry == nil {
return nil, nil
2017-09-20 20:59:35 +00:00
}
conf := &accessConfig{}
if err := entry.DecodeJSON(conf); err != nil {
2017-12-15 22:06:56 +00:00
return nil, errwrap.Wrapf("error reading nomad access configuration: {{err}}", err)
2017-09-20 20:59:35 +00:00
}
return conf, nil
2017-09-20 20:59:35 +00:00
}
2017-11-06 21:34:20 +00:00
func (b *backend) pathConfigAccessRead(
2017-09-20 20:59:35 +00:00
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2017-11-06 21:36:37 +00:00
conf, err := b.readConfigAccess(req.Storage)
if err != nil {
return nil, err
2017-09-20 20:59:35 +00:00
}
if conf == nil {
return nil, nil
2017-09-20 20:59:35 +00:00
}
return &logical.Response{
Data: map[string]interface{}{
"address": conf.Address,
},
}, nil
}
2017-11-06 21:34:20 +00:00
func (b *backend) pathConfigAccessWrite(
2017-09-20 20:59:35 +00:00
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
address := data.Get("address").(string)
if address == "" {
return logical.ErrorResponse("missing nomad server address"), nil
}
token := data.Get("token").(string)
if token == "" {
return logical.ErrorResponse("missing nomad management token"), nil
}
2017-09-20 20:59:35 +00:00
entry, err := logical.StorageEntryJSON("config/access", accessConfig{
Address: address,
Token: token,
2017-09-20 20:59:35 +00:00
})
if err != nil {
return nil, err
}
if err := req.Storage.Put(entry); err != nil {
return nil, err
}
return nil, nil
}
type accessConfig struct {
Address string `json:"address"`
Token string `json:"token"`
}