forked from splunk/vault-plugin-splunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_roles.go
171 lines (153 loc) · 4.96 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
package splunk
import (
"context"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
const rolesPrefix = "roles/"
func (b *backend) pathRoles() *framework.Path {
return &framework.Path{
Pattern: rolesPrefix + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the role",
},
"connection": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Name of the Splunk connection this role acts on",
},
"default_ttl": {
Type: framework.TypeDurationSecond,
Description: "Default TTL for role",
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: "Maximum time a credential is valid for",
},
"roles": &framework.FieldSchema{
Type: framework.TypeCommaStringSlice,
Description: "Comma-separated string or list of Splunk roles.",
},
"default_app": &framework.FieldSchema{
Type: framework.TypeString,
Description: trimIndent(`
User default app. Overrides the default app inherited from the user roles.`),
},
"email": &framework.FieldSchema{
Type: framework.TypeString,
Description: "User email address.",
},
"tz": &framework.FieldSchema{
Type: framework.TypeString,
Description: "User time zone.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.rolesReadHandler,
logical.CreateOperation: b.rolesWriteHandler,
logical.UpdateOperation: b.rolesWriteHandler,
logical.DeleteOperation: b.rolesDeleteHandler,
},
ExistenceCheck: b.rolesExistenceCheckHandler,
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
}
// Returning 'true' forces an UpdateOperation, CreateOperation otherwise.
func (b *backend) rolesExistenceCheckHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (bool, error) {
name := d.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return false, err
}
return role != nil, nil
}
func (b *backend) rolesReadHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
return nil, nil
}
resp := &logical.Response{
Data: role.toResponseData(),
}
return resp, nil
}
func (b *backend) rolesWriteHandler(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
role, err := roleConfigLoad(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if role == nil {
role = &roleConfig{}
}
if connRaw, ok := getValue(data, req.Operation, "connection"); ok {
role.Connection = connRaw.(string)
}
if role.Connection == "" {
return logical.ErrorResponse("empty Splunk connection name"), nil
}
if defaultTTLRaw, ok := getValue(data, req.Operation, "default_ttl"); ok {
role.DefaultTTL = time.Duration(defaultTTLRaw.(int)) * time.Second
}
if maxTTLRaw, ok := getValue(data, req.Operation, "max_ttl"); ok {
role.MaxTTL = time.Duration(maxTTLRaw.(int)) * time.Second
}
if roles, ok := getValue(data, req.Operation, "roles"); ok {
role.Roles = roles.([]string)
}
if len(role.Roles) == 0 {
return logical.ErrorResponse("roles cannot be empty"), nil
}
if defaultAppRaw, ok := getValue(data, req.Operation, "default_app"); ok {
role.DefaultApp = defaultAppRaw.(string)
}
if emailRaw, ok := getValue(data, req.Operation, "email"); ok {
role.Email = emailRaw.(string)
}
if tzRaw, ok := getValue(data, req.Operation, "tz"); ok {
role.TZ = tzRaw.(string)
}
if err := role.store(ctx, req.Storage, name); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) rolesDeleteHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := d.Get("name").(string)
if err := req.Storage.Delete(ctx, rolesPrefix+name); err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathRolesList() *framework.Path {
return &framework.Path{
Pattern: rolesPrefix + "?$",
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.rolesListHandler,
},
HelpSynopsis: pathRoleHelpSyn,
HelpDescription: pathRoleHelpDesc,
}
}
func (b *backend) rolesListHandler(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, rolesPrefix)
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
const pathRoleHelpSyn = `
Manage the roles that can be created with this backend.
`
const pathRoleHelpDesc = `
This path lets you manage the roles that can be created with this backend.
See the documentation for roles/name for a full list of accepted
connection details.
`