2018-03-26 17:40:33 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-07-24 22:49:55 +00:00
|
|
|
"context"
|
2018-11-07 01:21:24 +00:00
|
|
|
"errors"
|
2018-03-26 17:40:33 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-06-30 15:26:52 +00:00
|
|
|
"time"
|
2018-11-07 01:21:24 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2018-11-07 01:21:24 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-03-26 17:40:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListPluginsInput is used as input to the ListPlugins function.
|
2018-11-07 01:21:24 +00:00
|
|
|
type ListPluginsInput struct {
|
|
|
|
// Type of the plugin. Required.
|
|
|
|
Type consts.PluginType `json:"type"`
|
|
|
|
}
|
2018-03-26 17:40:33 +00:00
|
|
|
|
|
|
|
// ListPluginsResponse is the response from the ListPlugins call.
|
|
|
|
type ListPluginsResponse struct {
|
2018-11-07 01:21:24 +00:00
|
|
|
// PluginsByType is the list of plugins by type.
|
|
|
|
PluginsByType map[consts.PluginType][]string `json:"types"`
|
|
|
|
|
2018-11-19 23:23:48 +00:00
|
|
|
// Names is the list of names of the plugins.
|
|
|
|
//
|
|
|
|
// Deprecated: Newer server responses should be returning PluginsByType (json:
|
|
|
|
// "types") instead.
|
|
|
|
Names []string `json:"names"`
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPlugins lists all plugins in the catalog and returns their names as a
|
|
|
|
// list of strings.
|
|
|
|
func (c *Sys) ListPlugins(i *ListPluginsInput) (*ListPluginsResponse, error) {
|
2018-11-07 01:21:24 +00:00
|
|
|
path := ""
|
|
|
|
method := ""
|
|
|
|
if i.Type == consts.PluginTypeUnknown {
|
|
|
|
path = "/v1/sys/plugins/catalog"
|
|
|
|
method = "GET"
|
|
|
|
} else {
|
|
|
|
path = fmt.Sprintf("/v1/sys/plugins/catalog/%s", i.Type)
|
|
|
|
method = "LIST"
|
|
|
|
}
|
|
|
|
|
|
|
|
req := c.c.NewRequest(method, path)
|
2019-01-11 00:57:00 +00:00
|
|
|
if method == "LIST" {
|
|
|
|
// Set this for broader compatibility, but we use LIST above to be able
|
|
|
|
// to handle the wrapping lookup function
|
|
|
|
req.Method = "GET"
|
|
|
|
req.Params.Set("list", "true")
|
|
|
|
}
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
2018-12-12 20:33:09 +00:00
|
|
|
if err != nil && resp == nil {
|
2018-03-26 17:40:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-12 20:36:28 +00:00
|
|
|
if resp == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-03-26 17:40:33 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-12-12 20:33:09 +00:00
|
|
|
// We received an Unsupported Operation response from Vault, indicating
|
2019-01-11 00:57:00 +00:00
|
|
|
// Vault of an older version that doesn't support the GET method yet;
|
|
|
|
// switch it to a LIST.
|
|
|
|
if resp.StatusCode == 405 {
|
|
|
|
req.Params.Set("list", "true")
|
2018-11-07 01:21:24 +00:00
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var result struct {
|
|
|
|
Data struct {
|
|
|
|
Keys []string `json:"keys"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
if err := resp.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-19 23:23:48 +00:00
|
|
|
return &ListPluginsResponse{Names: result.Data.Keys}, nil
|
2018-11-07 01:21:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 20:33:09 +00:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2018-11-07 01:21:24 +00:00
|
|
|
result := &ListPluginsResponse{
|
|
|
|
PluginsByType: make(map[consts.PluginType][]string),
|
|
|
|
}
|
|
|
|
if i.Type == consts.PluginTypeUnknown {
|
|
|
|
for pluginTypeStr, pluginsRaw := range secret.Data {
|
|
|
|
pluginType, err := consts.ParsePluginType(pluginTypeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginsIfc, ok := pluginsRaw.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unable to parse plugins for %q type", pluginTypeStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins := make([]string, len(pluginsIfc))
|
|
|
|
for i, nameIfc := range pluginsIfc {
|
|
|
|
name, ok := nameIfc.(string)
|
|
|
|
if !ok {
|
|
|
|
}
|
|
|
|
plugins[i] = name
|
|
|
|
}
|
|
|
|
result.PluginsByType[pluginType] = plugins
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var respKeys []string
|
|
|
|
if err := mapstructure.Decode(secret.Data["keys"], &respKeys); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.PluginsByType[i.Type] = respKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPluginInput is used as input to the GetPlugin function.
|
|
|
|
type GetPluginInput struct {
|
|
|
|
Name string `json:"-"`
|
2018-11-07 01:21:24 +00:00
|
|
|
|
|
|
|
// Type of the plugin. Required.
|
|
|
|
Type consts.PluginType `json:"type"`
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPluginResponse is the response from the GetPlugin call.
|
|
|
|
type GetPluginResponse struct {
|
|
|
|
Args []string `json:"args"`
|
|
|
|
Builtin bool `json:"builtin"`
|
|
|
|
Command string `json:"command"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SHA256 string `json:"sha256"`
|
|
|
|
}
|
|
|
|
|
2018-11-15 22:33:11 +00:00
|
|
|
// GetPlugin retrieves information about the plugin.
|
2018-03-26 17:40:33 +00:00
|
|
|
func (c *Sys) GetPlugin(i *GetPluginInput) (*GetPluginResponse, error) {
|
2018-11-15 22:33:11 +00:00
|
|
|
path := catalogPathByType(i.Type, i.Name)
|
2018-03-26 17:40:33 +00:00
|
|
|
req := c.c.NewRequest(http.MethodGet, path)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
2018-03-26 17:40:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-07-13 17:35:08 +00:00
|
|
|
var result struct {
|
2018-11-15 22:33:11 +00:00
|
|
|
Data *GetPluginResponse
|
2018-07-13 17:35:08 +00:00
|
|
|
}
|
2018-03-26 17:40:33 +00:00
|
|
|
err = resp.DecodeJSON(&result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-15 22:33:11 +00:00
|
|
|
return result.Data, err
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPluginInput is used as input to the RegisterPlugin function.
|
|
|
|
type RegisterPluginInput struct {
|
|
|
|
// Name is the name of the plugin. Required.
|
|
|
|
Name string `json:"-"`
|
|
|
|
|
2018-11-07 01:21:24 +00:00
|
|
|
// Type of the plugin. Required.
|
|
|
|
Type consts.PluginType `json:"type"`
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
// Args is the list of args to spawn the process with.
|
|
|
|
Args []string `json:"args,omitempty"`
|
|
|
|
|
|
|
|
// Command is the command to run.
|
|
|
|
Command string `json:"command,omitempty"`
|
|
|
|
|
|
|
|
// SHA256 is the shasum of the plugin.
|
|
|
|
SHA256 string `json:"sha256,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPlugin registers the plugin with the given information.
|
|
|
|
func (c *Sys) RegisterPlugin(i *RegisterPluginInput) error {
|
2018-11-15 22:33:11 +00:00
|
|
|
path := catalogPathByType(i.Type, i.Name)
|
2018-03-26 17:40:33 +00:00
|
|
|
req := c.c.NewRequest(http.MethodPut, path)
|
2018-11-15 22:33:11 +00:00
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
if err := req.SetJSONBody(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:49:55 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
2018-03-26 17:40:33 +00:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterPluginInput is used as input to the DeregisterPlugin function.
|
|
|
|
type DeregisterPluginInput struct {
|
|
|
|
// Name is the name of the plugin. Required.
|
|
|
|
Name string `json:"-"`
|
2018-11-07 01:21:24 +00:00
|
|
|
|
|
|
|
// Type of the plugin. Required.
|
|
|
|
Type consts.PluginType `json:"type"`
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterPlugin removes the plugin with the given name from the plugin
|
|
|
|
// catalog.
|
|
|
|
func (c *Sys) DeregisterPlugin(i *DeregisterPluginInput) error {
|
2018-11-15 22:33:11 +00:00
|
|
|
path := catalogPathByType(i.Type, i.Name)
|
2018-03-26 17:40:33 +00:00
|
|
|
req := c.c.NewRequest(http.MethodDelete, path)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
2018-03-26 17:40:33 +00:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-11-15 22:33:11 +00:00
|
|
|
|
2020-05-04 18:14:23 +00:00
|
|
|
// ReloadPluginInput is used as input to the ReloadPlugin function.
|
|
|
|
type ReloadPluginInput struct {
|
|
|
|
// Plugin is the name of the plugin to reload, as registered in the plugin catalog
|
|
|
|
Plugin string `json:"plugin"`
|
|
|
|
|
|
|
|
// Mounts is the array of string mount paths of the plugin backends to reload
|
|
|
|
Mounts []string `json:"mounts"`
|
2020-06-30 15:26:52 +00:00
|
|
|
|
|
|
|
// Scope is the scope of the plugin reload
|
|
|
|
Scope string `json:"scope"`
|
2020-05-04 18:14:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 15:26:52 +00:00
|
|
|
// ReloadPlugin reloads mounted plugin backends, possibly returning
|
|
|
|
// reloadId for a cluster scoped reload
|
|
|
|
func (c *Sys) ReloadPlugin(i *ReloadPluginInput) (string, error) {
|
2020-05-04 18:14:23 +00:00
|
|
|
path := "/v1/sys/plugins/reload/backend"
|
|
|
|
req := c.c.NewRequest(http.MethodPut, path)
|
|
|
|
|
|
|
|
if err := req.SetJSONBody(i); err != nil {
|
2020-06-30 15:26:52 +00:00
|
|
|
return "", err
|
2020-05-04 18:14:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
|
|
|
if err != nil {
|
2020-06-30 15:26:52 +00:00
|
|
|
return "", err
|
2020-06-29 21:23:28 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2020-06-30 15:26:52 +00:00
|
|
|
|
|
|
|
if i.Scope == "global" {
|
|
|
|
// Get the reload id
|
|
|
|
secret, parseErr := ParseSecret(resp.Body)
|
|
|
|
if parseErr != nil {
|
2020-06-30 18:33:30 +00:00
|
|
|
return "", parseErr
|
2020-06-30 15:26:52 +00:00
|
|
|
}
|
|
|
|
if _, ok := secret.Data["reload_id"]; ok {
|
|
|
|
return secret.Data["reload_id"].(string), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadStatus is the status of an individual node's plugin reload
|
|
|
|
type ReloadStatus struct {
|
|
|
|
Timestamp time.Time `json:"timestamp" mapstructure:"timestamp"`
|
2020-06-30 21:26:38 +00:00
|
|
|
Error string `json:"error" mapstructure:"error"`
|
2020-06-30 15:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadStatusResponse is the combined response of all known completed plugin reloads
|
|
|
|
type ReloadStatusResponse struct {
|
|
|
|
ReloadID string `mapstructure:"reload_id"`
|
|
|
|
Results map[string]*ReloadStatus `mapstructure:"results"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadPluginStatusInput is used as input to the ReloadStatusPlugin function.
|
|
|
|
type ReloadPluginStatusInput struct {
|
|
|
|
// ReloadID is the ID of the reload operation
|
|
|
|
ReloadID string `json:"reload_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadPluginStatus retrieves the status of a reload operation
|
|
|
|
func (c *Sys) ReloadPluginStatus(reloadStatusInput *ReloadPluginStatusInput) (*ReloadStatusResponse, error) {
|
|
|
|
path := "/v1/sys/plugins/reload/backend/status"
|
|
|
|
req := c.c.NewRequest(http.MethodGet, path)
|
|
|
|
req.Params.Add("reload_id", reloadStatusInput.ReloadID)
|
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
resp, err := c.c.RawRequestWithContext(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp != nil {
|
|
|
|
secret, parseErr := ParseSecret(resp.Body)
|
|
|
|
if parseErr != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var r ReloadStatusResponse
|
|
|
|
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
|
|
|
|
Result: &r,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = d.Decode(secret.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-05-04 18:14:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 22:33:11 +00:00
|
|
|
// catalogPathByType is a helper to construct the proper API path by plugin type
|
|
|
|
func catalogPathByType(pluginType consts.PluginType, name string) string {
|
|
|
|
path := fmt.Sprintf("/v1/sys/plugins/catalog/%s/%s", pluginType, name)
|
|
|
|
|
|
|
|
// Backwards compat, if type is not provided then use old path
|
|
|
|
if pluginType == consts.PluginTypeUnknown {
|
|
|
|
path = fmt.Sprintf("/v1/sys/plugins/catalog/%s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|