-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpath_roles.go
179 lines (156 loc) · 4.65 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
175
176
177
178
179
package wireguard
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
const (
rolesStoragePath = "roles/"
)
type roleEntry struct {
Name string `json:"name" mapstructure:"name"`
Permissions []string `json:"permissions" mapstructure:"permissions"`
OrgID string `json:"org_id" mapstructure:"org_id"`
TTL int `json:"ttl" mapstructure:"ttl"`
MaxTTL int `json:"max_ttl" mapstructure:"max_ttl"`
}
func (b *backend) pathRoles() []*framework.Path {
return []*framework.Path{
{
Pattern: rolesStoragePath + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeLowerCaseString,
Description: "Name of the role.",
},
"permissions": {
Type: framework.TypeStringSlice,
Description: "List of permissions to give the token",
},
"org_id": {
Type: framework.TypeString,
Description: "Organization ID in which to create the token",
},
"ttl": {
Type: framework.TypeDurationSecond,
Description: "Default lease for generated credentials. If not set or set to 0, will use system default.",
},
"max_ttl": {
Type: framework.TypeDurationSecond,
Description: "Maximum time a service principal. If not set or set to 0, will use system default.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathRoleCreateUpdate,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathRoleCreateUpdate,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathRoleRead,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathRoleDelete,
},
},
HelpSynopsis: roleHelpSyn,
HelpDescription: roleHelpDesc,
},
{
Pattern: "roles/?",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathRoleList,
},
},
HelpSynopsis: roleListHelpSyn,
HelpDescription: roleListHelpDesc,
},
}
}
func (b *backend) pathRoleDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
b.Lock()
defer b.Unlock()
err := req.Storage.Delete(ctx, rolesStoragePath+name)
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathRoleRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
entry, err := req.Storage.Get(ctx, rolesStoragePath+name)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
var result roleEntry
if err := entry.DecodeJSON(&result); err != nil {
return nil, err
}
var roleMap map[string]interface{}
err = mapstructure.Decode(result, &roleMap)
if err != nil {
return nil, err
}
return &logical.Response{
Data: roleMap,
}, nil
}
func (b *backend) pathRoleCreateUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := data.Validate()
if err != nil {
return nil, errors.New("Failing validation: " + err.Error())
}
name := data.Get("name").(string)
b.Lock()
defer b.Unlock()
orgID := data.Get("org_id").(string)
if orgID == "" {
return logical.ErrorResponse("OrgID is a required field to manage a influxdb v2 role"), nil
}
permissions := data.Get("permissions").([]string)
if len(permissions) == 0 {
return logical.ErrorResponse("permissions is a required field to manage a influxdb v2 role"), nil
}
ttl := data.Get("ttl").(int)
maxTTL := data.Get("max_ttl").(int)
roleEntry := &roleEntry{
Name: name,
Permissions: permissions,
OrgID: orgID,
TTL: ttl,
MaxTTL: maxTTL,
}
entry, err := logical.StorageEntryJSON(rolesStoragePath+name, roleEntry)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// Respond with a 204.
return nil, nil
}
func (b *backend) pathRoleList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
rolesets, err := req.Storage.List(ctx, rolesStoragePath)
if err != nil {
return nil, err
}
return logical.ListResponse(rolesets), nil
}
const roleHelpSyn = "Manage the Vault roles used to Manage access to Influxdb2."
const roleHelpDesc = `
Roles allow to define which permissions a given token will have
`
const roleListHelpSyn = "asdas"
const roleListHelpDesc = `
asdasda
asdasd
`