2017-08-28 20:44:35 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-07-12 22:38:18 +00:00
|
|
|
"os"
|
2017-08-28 20:44:35 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-08-29 04:24:22 +00:00
|
|
|
"sync"
|
2017-08-28 20:44:35 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2017-08-28 20:44:35 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
type Predict struct {
|
|
|
|
client *api.Client
|
|
|
|
clientOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPredict() *Predict {
|
|
|
|
return &Predict{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Predict) Client() *api.Client {
|
|
|
|
p.clientOnce.Do(func() {
|
|
|
|
if p.client == nil { // For tests
|
2017-09-05 03:55:08 +00:00
|
|
|
client, _ := api.NewClient(nil)
|
|
|
|
|
|
|
|
if client.Token() == "" {
|
|
|
|
helper, err := DefaultTokenHelper()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
token, err := helper.Get()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
}
|
|
|
|
|
2018-07-12 22:38:18 +00:00
|
|
|
// Turn off retries for prediction
|
|
|
|
if os.Getenv(api.EnvVaultMaxRetries) == "" {
|
|
|
|
client.SetMaxRetries(0)
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:08 +00:00
|
|
|
p.client = client
|
2017-08-29 04:24:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return p.client
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:44:35 +00:00
|
|
|
// defaultPredictVaultMounts is the default list of mounts to return to the
|
|
|
|
// user. This is a best-guess, given we haven't communicated with the Vault
|
|
|
|
// server. If the user has no token or if the token does not have the default
|
|
|
|
// policy attached, it won't be able to read cubbyhole/, but it's a better UX
|
|
|
|
// that returning nothing.
|
|
|
|
var defaultPredictVaultMounts = []string{"cubbyhole/"}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// predictClient is the API client to use for prediction. We create this at the
|
|
|
|
// beginning once, because completions are generated for each command (and this
|
|
|
|
// doesn't change), and the only way to configure the predict/autocomplete
|
|
|
|
// client is via environment variables. Even if the user specifies a flag, we
|
|
|
|
// can't parse that flag until after the command is submitted.
|
2021-04-08 16:43:39 +00:00
|
|
|
var (
|
|
|
|
predictClient *api.Client
|
|
|
|
predictClientOnce sync.Once
|
|
|
|
)
|
2017-08-29 04:24:22 +00:00
|
|
|
|
|
|
|
// PredictClient returns the cached API client for the predictor.
|
|
|
|
func PredictClient() *api.Client {
|
|
|
|
predictClientOnce.Do(func() {
|
|
|
|
if predictClient == nil { // For tests
|
|
|
|
predictClient, _ = api.NewClient(nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return predictClient
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// PredictVaultAvailableMounts returns a predictor for the available mounts in
|
2018-03-20 18:54:10 +00:00
|
|
|
// Vault. For now, there is no way to programmatically get this list. If, in the
|
2017-09-05 03:55:19 +00:00
|
|
|
// future, such a list exists, we can adapt it here. Until then, it's
|
|
|
|
// hard-coded.
|
|
|
|
func (b *BaseCommand) PredictVaultAvailableMounts() complete.Predictor {
|
|
|
|
// This list does not contain deprecated backends. At present, there is no
|
|
|
|
// API that lists all available secret backends, so this is hard-coded :(.
|
|
|
|
return complete.PredictSet(
|
|
|
|
"aws",
|
|
|
|
"consul",
|
|
|
|
"database",
|
2017-09-08 01:58:39 +00:00
|
|
|
"generic",
|
2017-09-05 03:55:19 +00:00
|
|
|
"pki",
|
|
|
|
"plugin",
|
|
|
|
"rabbitmq",
|
|
|
|
"ssh",
|
|
|
|
"totp",
|
|
|
|
"transit",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PredictVaultAvailableAuths returns a predictor for the available auths in
|
2018-03-20 18:54:10 +00:00
|
|
|
// Vault. For now, there is no way to programmatically get this list. If, in the
|
2017-09-05 03:55:19 +00:00
|
|
|
// future, such a list exists, we can adapt it here. Until then, it's
|
|
|
|
// hard-coded.
|
|
|
|
func (b *BaseCommand) PredictVaultAvailableAuths() complete.Predictor {
|
|
|
|
return complete.PredictSet(
|
|
|
|
"app-id",
|
|
|
|
"approle",
|
|
|
|
"aws",
|
|
|
|
"cert",
|
|
|
|
"gcp",
|
|
|
|
"github",
|
|
|
|
"ldap",
|
|
|
|
"okta",
|
|
|
|
"plugin",
|
|
|
|
"radius",
|
|
|
|
"userpass",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-28 21:05:09 +00:00
|
|
|
// PredictVaultFiles returns a predictor for Vault mounts and paths based on the
|
2017-08-28 20:44:35 +00:00
|
|
|
// configured client for the base command. Unfortunately this happens pre-flag
|
|
|
|
// parsing, so users must rely on environment variables for autocomplete if they
|
|
|
|
// are not using Vault at the default endpoints.
|
2017-08-28 21:05:09 +00:00
|
|
|
func (b *BaseCommand) PredictVaultFiles() complete.Predictor {
|
2017-08-29 04:24:22 +00:00
|
|
|
return NewPredict().VaultFiles()
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 21:05:09 +00:00
|
|
|
// PredictVaultFolders returns a predictor for "folders". See PredictVaultFiles
|
|
|
|
// for more information and restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultFolders() complete.Predictor {
|
2017-08-29 04:24:22 +00:00
|
|
|
return NewPredict().VaultFolders()
|
2017-08-28 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:38:39 +00:00
|
|
|
// PredictVaultNamespaces returns a predictor for "namespaces". See PredictVaultFiles
|
|
|
|
// for more information an restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultNamespaces() complete.Predictor {
|
|
|
|
return NewPredict().VaultNamespaces()
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// PredictVaultMounts returns a predictor for "folders". See PredictVaultFiles
|
|
|
|
// for more information and restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultMounts() complete.Predictor {
|
|
|
|
return NewPredict().VaultMounts()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PredictVaultAudits returns a predictor for "folders". See PredictVaultFiles
|
|
|
|
// for more information and restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultAudits() complete.Predictor {
|
|
|
|
return NewPredict().VaultAudits()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PredictVaultAuths returns a predictor for "folders". See PredictVaultFiles
|
|
|
|
// for more information and restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultAuths() complete.Predictor {
|
|
|
|
return NewPredict().VaultAuths()
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
// PredictVaultPlugins returns a predictor for installed plugins.
|
2018-11-07 01:21:24 +00:00
|
|
|
func (b *BaseCommand) PredictVaultPlugins(pluginTypes ...consts.PluginType) complete.Predictor {
|
|
|
|
return NewPredict().VaultPlugins(pluginTypes...)
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// PredictVaultPolicies returns a predictor for "folders". See PredictVaultFiles
|
|
|
|
// for more information and restrictions.
|
|
|
|
func (b *BaseCommand) PredictVaultPolicies() complete.Predictor {
|
|
|
|
return NewPredict().VaultPolicies()
|
|
|
|
}
|
|
|
|
|
2019-10-15 22:39:19 +00:00
|
|
|
func (b *BaseCommand) PredictVaultDebugTargets() complete.Predictor {
|
|
|
|
return complete.PredictSet(
|
|
|
|
"config",
|
|
|
|
"host",
|
|
|
|
"metrics",
|
|
|
|
"pprof",
|
|
|
|
"replication-status",
|
|
|
|
"server-status",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// VaultFiles returns a predictor for Vault "files". This is a public API for
|
|
|
|
// consumers, but you probably want BaseCommand.PredictVaultFiles instead.
|
|
|
|
func (p *Predict) VaultFiles() complete.Predictor {
|
|
|
|
return p.vaultPaths(true)
|
2017-08-28 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// VaultFolders returns a predictor for Vault "folders". This is a public
|
2017-08-28 21:05:09 +00:00
|
|
|
// API for consumers, but you probably want BaseCommand.PredictVaultFolders
|
|
|
|
// instead.
|
2017-08-29 04:24:22 +00:00
|
|
|
func (p *Predict) VaultFolders() complete.Predictor {
|
|
|
|
return p.vaultPaths(false)
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:38:39 +00:00
|
|
|
// VaultNamespaces returns a predictor for Vault "namespaces". This is a public
|
|
|
|
// API for consumers, but you probably want BaseCommand.PredictVaultNamespaces
|
|
|
|
// instead.
|
|
|
|
func (p *Predict) VaultNamespaces() complete.Predictor {
|
|
|
|
return p.filterFunc(p.namespaces)
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// VaultMounts returns a predictor for Vault "folders". This is a public
|
|
|
|
// API for consumers, but you probably want BaseCommand.PredictVaultMounts
|
|
|
|
// instead.
|
|
|
|
func (p *Predict) VaultMounts() complete.Predictor {
|
|
|
|
return p.filterFunc(p.mounts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VaultAudits returns a predictor for Vault "folders". This is a public API for
|
|
|
|
// consumers, but you probably want BaseCommand.PredictVaultAudits instead.
|
|
|
|
func (p *Predict) VaultAudits() complete.Predictor {
|
|
|
|
return p.filterFunc(p.audits)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VaultAuths returns a predictor for Vault "folders". This is a public API for
|
|
|
|
// consumers, but you probably want BaseCommand.PredictVaultAuths instead.
|
|
|
|
func (p *Predict) VaultAuths() complete.Predictor {
|
|
|
|
return p.filterFunc(p.auths)
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
// VaultPlugins returns a predictor for Vault's plugin catalog. This is a public
|
|
|
|
// API for consumers, but you probably want BaseCommand.PredictVaultPlugins
|
|
|
|
// instead.
|
2018-11-07 01:21:24 +00:00
|
|
|
func (p *Predict) VaultPlugins(pluginTypes ...consts.PluginType) complete.Predictor {
|
|
|
|
filterFunc := func() []string {
|
|
|
|
return p.plugins(pluginTypes...)
|
|
|
|
}
|
|
|
|
return p.filterFunc(filterFunc)
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// VaultPolicies returns a predictor for Vault "folders". This is a public API for
|
|
|
|
// consumers, but you probably want BaseCommand.PredictVaultPolicies instead.
|
|
|
|
func (p *Predict) VaultPolicies() complete.Predictor {
|
|
|
|
return p.filterFunc(p.policies)
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// vaultPaths parses the CLI options and returns the "best" list of possible
|
|
|
|
// paths. If there are any errors, this function returns an empty result. All
|
|
|
|
// errors are suppressed since this is a prediction function.
|
|
|
|
func (p *Predict) vaultPaths(includeFiles bool) complete.PredictFunc {
|
2017-08-28 20:44:35 +00:00
|
|
|
return func(args complete.Args) []string {
|
|
|
|
// Do not predict more than one paths
|
2017-08-29 04:24:22 +00:00
|
|
|
if p.hasPathArg(args.All) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path := args.Last
|
|
|
|
|
2020-02-07 14:58:30 +00:00
|
|
|
// Trim path with potential mount
|
|
|
|
var relativePath string
|
|
|
|
for _, mount := range p.mounts() {
|
|
|
|
if strings.HasPrefix(path, mount) {
|
|
|
|
relativePath = strings.TrimPrefix(path, mount+"/")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Predict path or mount depending on path separator
|
2017-08-28 20:44:35 +00:00
|
|
|
var predictions []string
|
2020-02-07 14:58:30 +00:00
|
|
|
if strings.Contains(relativePath, "/") {
|
2017-08-29 04:24:22 +00:00
|
|
|
predictions = p.paths(path, includeFiles)
|
2017-08-28 20:44:35 +00:00
|
|
|
} else {
|
2017-09-05 03:55:19 +00:00
|
|
|
predictions = p.filter(p.mounts(), path)
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Either no results or many results, so return.
|
|
|
|
if len(predictions) != 1 {
|
|
|
|
return predictions
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is not a "folder", do not try to recurse.
|
|
|
|
if !strings.HasSuffix(predictions[0], "/") {
|
|
|
|
return predictions
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the prediction is the same as the last guess, return it (we have no
|
|
|
|
// new information and we won't get anymore).
|
|
|
|
if predictions[0] == args.Last {
|
|
|
|
return predictions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-predict with the remaining path
|
|
|
|
args.Last = predictions[0]
|
2017-08-29 04:24:22 +00:00
|
|
|
return p.vaultPaths(includeFiles).Predict(args)
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// paths predicts all paths which start with the given path.
|
|
|
|
func (p *Predict) paths(path string, includeFiles bool) []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:44:35 +00:00
|
|
|
// Vault does not support listing based on a sub-key, so we have to back-pedal
|
|
|
|
// to the last "/" and return all paths on that "folder". Then we perform
|
|
|
|
// client-side filtering.
|
|
|
|
root := path
|
|
|
|
idx := strings.LastIndex(root, "/")
|
|
|
|
if idx > 0 && idx < len(root) {
|
|
|
|
root = root[:idx+1]
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
paths := p.listPaths(root)
|
2017-08-28 20:44:35 +00:00
|
|
|
|
|
|
|
var predictions []string
|
|
|
|
for _, p := range paths {
|
|
|
|
// Calculate the absolute "path" for matching.
|
|
|
|
p = root + p
|
|
|
|
|
|
|
|
if strings.HasPrefix(p, path) {
|
2017-08-28 21:05:09 +00:00
|
|
|
// Ensure this is a directory or we've asked to include files.
|
|
|
|
if includeFiles || strings.HasSuffix(p, "/") {
|
|
|
|
predictions = append(predictions, p)
|
|
|
|
}
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add root to the path
|
|
|
|
if len(predictions) == 0 {
|
|
|
|
predictions = append(predictions, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return predictions
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// audits returns a sorted list of the audit backends for Vault server for
|
|
|
|
// which the client is configured to communicate with.
|
|
|
|
func (p *Predict) audits() []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
audits, err := client.Sys().ListAudit()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(audits))
|
|
|
|
for m := range audits {
|
|
|
|
list = append(list, m)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
// auths returns a sorted list of the enabled auth provides for Vault server for
|
|
|
|
// which the client is configured to communicate with.
|
|
|
|
func (p *Predict) auths() []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
auths, err := client.Sys().ListAuth()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(auths))
|
|
|
|
for m := range auths {
|
|
|
|
list = append(list, m)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
// plugins returns a sorted list of the plugins in the catalog.
|
2018-11-07 01:21:24 +00:00
|
|
|
func (p *Predict) plugins(pluginTypes ...consts.PluginType) []string {
|
|
|
|
// This method's signature doesn't enforce that a pluginType must be passed in.
|
|
|
|
// If it's not, it's likely the caller's intent is go get a list of all of them,
|
|
|
|
// so let's help them out.
|
|
|
|
if len(pluginTypes) == 0 {
|
|
|
|
pluginTypes = append(pluginTypes, consts.PluginTypeUnknown)
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-07 01:21:24 +00:00
|
|
|
var plugins []string
|
|
|
|
pluginsAdded := make(map[string]bool)
|
|
|
|
for _, pluginType := range pluginTypes {
|
|
|
|
result, err := client.Sys().ListPlugins(&api.ListPluginsInput{Type: pluginType})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-12 20:36:28 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-07 01:21:24 +00:00
|
|
|
for _, names := range result.PluginsByType {
|
|
|
|
for _, name := range names {
|
|
|
|
if _, ok := pluginsAdded[name]; !ok {
|
|
|
|
plugins = append(plugins, name)
|
|
|
|
pluginsAdded[name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
sort.Strings(plugins)
|
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
// policies returns a sorted list of the policies stored in this Vault
|
|
|
|
// server.
|
|
|
|
func (p *Predict) policies() []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := client.Sys().ListPolicies()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sort.Strings(policies)
|
|
|
|
return policies
|
|
|
|
}
|
|
|
|
|
|
|
|
// mounts returns a sorted list of the mount paths for Vault server for
|
2017-08-29 04:24:22 +00:00
|
|
|
// which the client is configured to communicate with. This function returns the
|
|
|
|
// default list of mounts if an error occurs.
|
2017-09-05 03:55:19 +00:00
|
|
|
func (p *Predict) mounts() []string {
|
2017-08-29 04:24:22 +00:00
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts, err := client.Sys().ListMounts()
|
2017-08-28 20:44:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return defaultPredictVaultMounts
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(mounts))
|
|
|
|
for m := range mounts {
|
|
|
|
list = append(list, m)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2020-02-09 18:38:39 +00:00
|
|
|
// namespaces returns a sorted list of the namespace paths for Vault server for
|
|
|
|
// which the client is configured to communicate with. This function returns
|
|
|
|
// an empty list in any error occurs.
|
|
|
|
func (p *Predict) namespaces() []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().List("sys/namespaces")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
namespaces, ok := extractListData(secret)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(namespaces))
|
|
|
|
for _, n := range namespaces {
|
|
|
|
s, ok := n.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
list = append(list, s)
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// listPaths returns a list of paths (HTTP LIST) for the given path. This
|
2017-08-28 20:44:35 +00:00
|
|
|
// function returns an empty list of any errors occur.
|
2017-08-29 04:24:22 +00:00
|
|
|
func (p *Predict) listPaths(path string) []string {
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().List(path)
|
2017-08-28 20:44:35 +00:00
|
|
|
if err != nil || secret == nil || secret.Data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paths, ok := secret.Data["keys"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0, len(paths))
|
|
|
|
for _, p := range paths {
|
|
|
|
if str, ok := p.(string); ok {
|
|
|
|
list = append(list, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
// hasPathArg determines if the args have already accepted a path.
|
|
|
|
func (p *Predict) hasPathArg(args []string) bool {
|
2017-08-28 20:44:35 +00:00
|
|
|
var nonFlags []string
|
|
|
|
for _, a := range args {
|
|
|
|
if !strings.HasPrefix(a, "-") {
|
|
|
|
nonFlags = append(nonFlags, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(nonFlags) > 2
|
|
|
|
}
|
2017-09-05 03:55:19 +00:00
|
|
|
|
|
|
|
// filterFunc is used to compose a complete predictor that filters an array
|
|
|
|
// of strings as per the filter function.
|
|
|
|
func (p *Predict) filterFunc(f func() []string) complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(args complete.Args) []string {
|
|
|
|
if p.hasPathArg(args.All) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
client := p.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.filter(f(), args.Last)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter filters the given list for items that start with the prefix.
|
|
|
|
func (p *Predict) filter(list []string, prefix string) []string {
|
|
|
|
var predictions []string
|
|
|
|
for _, item := range list {
|
|
|
|
if strings.HasPrefix(item, prefix) {
|
|
|
|
predictions = append(predictions, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return predictions
|
|
|
|
}
|