forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_roles.go
174 lines (146 loc) · 4.35 KB
/
path_roles.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package aws
import (
"bytes"
"encoding/json"
"fmt"
"errors"
"strings"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathListRoles(b *backend) *framework.Path {
return &framework.Path{
Pattern: "roles/?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathRoleList,
},
HelpSynopsis: pathListRolesHelpSyn,
HelpDescription: pathListRolesHelpDesc,
}
}
func pathRoles() *framework.Path {
return &framework.Path{
Pattern: "roles/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the policy",
},
"arn": &framework.FieldSchema{
Type: framework.TypeString,
Description: "ARN Reference to a managed policy",
},
"policy": &framework.FieldSchema{
Type: framework.TypeString,
Description: "IAM policy document",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.DeleteOperation: pathRolesDelete,
logical.ReadOperation: pathRolesRead,
logical.UpdateOperation: pathRolesWrite,
},
HelpSynopsis: pathRolesHelpSyn,
HelpDescription: pathRolesHelpDesc,
}
}
func (b *backend) pathRoleList(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List("policy/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func pathRolesDelete(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete("policy/" + d.Get("name").(string))
if err != nil {
return nil, err
}
return nil, nil
}
func pathRolesRead(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get("policy/" + d.Get("name").(string))
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
val := string(entry.Value)
if strings.HasPrefix(val, "arn:") {
return &logical.Response{
Data: map[string]interface{}{
"arn": val,
},
}, nil
}
return &logical.Response{
Data: map[string]interface{}{
"policy": val,
},
}, nil
}
func useInlinePolicy(d *framework.FieldData) (bool, error) {
bp := d.Get("policy").(string) != ""
ba := d.Get("arn").(string) != ""
if !bp && !ba {
return false, errors.New("Either policy or arn must be provided")
}
if bp && ba {
return false, errors.New("Only one of policy or arn should be provided")
}
return bp, nil
}
func pathRolesWrite(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var buf bytes.Buffer
uip, err := useInlinePolicy(d)
if err != nil {
return nil, err
}
if uip {
if err := json.Compact(&buf, []byte(d.Get("policy").(string))); err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error compacting policy: %s", err)), nil
}
// Write the policy into storage
err := req.Storage.Put(&logical.StorageEntry{
Key: "policy/" + d.Get("name").(string),
Value: buf.Bytes(),
})
if err != nil {
return nil, err
}
} else {
// Write the arn ref into storage
err := req.Storage.Put(&logical.StorageEntry{
Key: "policy/" + d.Get("name").(string),
Value: []byte(d.Get("arn").(string)),
})
if err != nil {
return nil, err
}
}
return nil, nil
}
const pathListRolesHelpSyn = `List the existing roles in this backend`
const pathListRolesHelpDesc = `Roles will be listed by the role name.`
const pathRolesHelpSyn = `
Read, write and reference IAM policies that access keys can be made for.
`
const pathRolesHelpDesc = `
This path allows you to read and write roles that are used to
create access keys. These roles are associated with IAM policies that
map directly to the route to read the access keys. For example, if the
backend is mounted at "aws" and you create a role at "aws/roles/deploy"
then a user could request access credentials at "aws/creds/deploy".
You can either supply a user inline policy (via the policy argument), or
provide a reference to an existing AWS policy by supplying the full arn
reference (via the arn argument). Inline user policies written are normal
IAM policies. Vault will not attempt to parse these except to validate
that they're basic JSON. No validation is performed on arn references.
To validate the keys, attempt to read an access key after writing the policy.
`