mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 02:57:59 +00:00
command/policy-list
This commit is contained in:
78
command/policy_list.go
Normal file
78
command/policy_list.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PolicyListCommand is a Command that enables a new endpoint.
|
||||
type PolicyListCommand struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (c *PolicyListCommand) Run(args []string) int {
|
||||
flags := c.Meta.FlagSet("policy-list", FlagSetDefault)
|
||||
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
args = flags.Args()
|
||||
if len(args) != 0 {
|
||||
flags.Usage()
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"\npolicy-list expects zero arguments"))
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing client: %s", err))
|
||||
return 2
|
||||
}
|
||||
|
||||
policies, err := client.Sys().ListPolicies()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
for _, p := range policies {
|
||||
c.Ui.Output(p)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *PolicyListCommand) Synopsis() string {
|
||||
return "List the policies on the server"
|
||||
}
|
||||
|
||||
func (c *PolicyListCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: vault policy-list [options]
|
||||
|
||||
List the policies that are available.
|
||||
|
||||
This command lists the policies that are written to the Vault server.
|
||||
|
||||
General Options:
|
||||
|
||||
-address=TODO The address of the Vault server.
|
||||
|
||||
-ca-cert=path Path to a PEM encoded CA cert file to use to
|
||||
verify the Vault server SSL certificate.
|
||||
|
||||
-ca-path=path Path to a directory of PEM encoded CA cert files
|
||||
to verify the Vault server SSL certificate. If both
|
||||
-ca-cert and -ca-path are specified, -ca-path is used.
|
||||
|
||||
-insecure Do not verify TLS certificate. This is highly
|
||||
not recommended. This is especially not recommended
|
||||
for unsealing a vault.
|
||||
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
30
command/policy_list_test.go
Normal file
30
command/policy_list_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/vault/http"
|
||||
"github.com/hashicorp/vault/vault"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func TestPolicyList(t *testing.T) {
|
||||
core, _, token := vault.TestCoreUnsealed(t)
|
||||
ln, addr := http.TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &PolicyListCommand{
|
||||
Meta: Meta{
|
||||
ClientToken: token,
|
||||
Ui: ui,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-address", addr,
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,12 @@ func init() {
|
||||
}, nil
|
||||
},
|
||||
|
||||
"policy-list": func() (cli.Command, error) {
|
||||
return &command.PolicyListCommand{
|
||||
Meta: meta,
|
||||
}, nil
|
||||
},
|
||||
|
||||
"read": func() (cli.Command, error) {
|
||||
return &command.ReadCommand{
|
||||
Meta: meta,
|
||||
|
||||
Reference in New Issue
Block a user