-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpath_servers.go
184 lines (161 loc) · 4.87 KB
/
path_servers.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
180
181
182
183
184
package wireguard
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
const (
serverStoragePath = "servers/"
)
type serverEntry 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) pathServers() []*framework.Path {
return []*framework.Path{
{
Pattern: serverStoragePath + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeLowerCaseString,
Description: "Name of the server.",
},
"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.pathServerCreateUpdate,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathServerCreateUpdate,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathServerRead,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathServerDelete,
},
},
HelpSynopsis: roleHelpSyn,
HelpDescription: roleHelpDesc,
},
{
Pattern: "servers/?",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathServerList,
},
},
HelpSynopsis: roleListHelpSyn,
HelpDescription: roleListHelpDesc,
},
}
}
func (b *backend) pathServerDelete(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, serverStoragePath+name)
if err != nil {
return nil, err
}
return nil, nil
}
func (b *backend) pathServerRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
entry, err := req.Storage.Get(ctx, serverStoragePath+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) pathServerCreateUpdate(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(serverStoragePath+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) pathServerList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
serversets, err := req.Storage.List(ctx, serverStoragePath)
if err != nil {
return nil, err
}
return logical.ListResponse(serversets), nil
}
const serverHelpSyn = "Manage the Vault servers used to Manage access to Influxdb2."
const serverHelpDesc = `
servers allow to define which permissions a given token will have
`
const serverListHelpSyn = "asdas"
const serverListHelpDesc = `
asdasda
asdasd
`
// import "github.com/hashicorp/vault/sdk/database/helper/credsutil"
// password, err := credsutil.RandomAlphaNumeric(20, true)
// if err != nil {
// return "", err
// }