forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_list.go
104 lines (83 loc) · 2.31 KB
/
policy_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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) == 1 {
return c.read(args[0])
} else if len(args) == 0 {
return c.list()
} else {
flags.Usage()
c.Ui.Error(fmt.Sprintf(
"\npolicies expects zero or one arguments"))
return 1
}
}
func (c *PolicyListCommand) list() int {
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) read(n string) int {
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
rules, err := client.Sys().GetPolicy(n)
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error: %s", err))
return 1
}
c.Ui.Output(rules)
return 0
}
func (c *PolicyListCommand) Synopsis() string {
return "List the policies on the server"
}
func (c *PolicyListCommand) Help() string {
helpText := `
Usage: vault policies [options] [name]
List the policies that are available or read a single policy.
This command lists the policies that are written to the Vault server.
If a name of a policy is specified, that policy is outputted.
General Options:
-address=addr 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)
}