forked from Caiyeon/goldfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
79 lines (68 loc) · 1.59 KB
/
policy.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
package vault
import (
"errors"
api "github.com/hashicorp/vault/vault"
)
func (auth AuthInfo) ListPolicies() ([]string, error) {
client, err := auth.Client()
if err != nil {
return nil, err
}
return client.Sys().ListPolicies()
}
func (auth AuthInfo) GetPolicy(name string) (string, error) {
client, err := auth.Client()
if err != nil {
return "", err
}
if name == "" {
return "", errors.New("Empty policy name")
}
return client.Sys().GetPolicy(name)
}
func (auth AuthInfo) DeletePolicy(name string) error {
client, err := auth.Client()
if err != nil {
return err
}
if name == "" {
return errors.New("Empty policy name")
}
return client.Sys().DeletePolicy(name)
}
func (auth AuthInfo) PutPolicy(name, rules string) error {
client, err := auth.Client()
if err != nil {
return err
}
if name == "" {
return errors.New("Empty policy name")
}
return client.Sys().PutPolicy(name, rules)
}
func (auth AuthInfo) PolicyCapabilities(policyName, path string) ([]string, error) {
client, err := auth.Client()
if err != nil {
return []string{}, err
}
// the "root" policy is unchangeable and unremovable, so short circuit
if policyName == "root" {
return []string{api.RootCapability}, nil
}
// fetch policy rules
rules, err := client.Sys().GetPolicy(policyName)
if err != nil {
return []string{}, err
}
// construct ACL
policy, err := api.ParseACLPolicy(rules)
if err != nil {
return []string{}, err
}
acl, err := api.NewACL([]*api.Policy{policy})
if err != nil {
return []string{}, err
}
// read capabilities of policy and return
return acl.Capabilities(path), nil
}