forked from cockroachdb/cockroach
/
config.go
308 lines (284 loc) · 10.1 KB
/
config.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Bram Gruneir (bram.gruneir@gmail.com)
package server
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
gogoproto "github.com/gogo/protobuf/proto"
)
// Gets a friendly name for output based on the passed in config prefix.
func getFriendlyNameFromPrefix(prefix string) string {
switch prefix {
case acctPathPrefix:
return "accounting"
case permPathPrefix:
return "permission"
case zonePathPrefix:
return "zone"
default:
return "unknown"
}
}
// runGetConfig invokes the REST API with GET action and key prefix as path.
func runGetConfig(ctx *Context, prefix, keyPrefix string) {
friendlyName := getFriendlyNameFromPrefix(prefix)
req, err := http.NewRequest("GET", fmt.Sprintf("%s://%s%s/%s", ctx.RequestScheme(), ctx.Addr, prefix, keyPrefix), nil)
if err != nil {
log.Errorf("unable to create request to admin REST endpoint: %s", err)
return
}
req.Header.Add("Accept", "text/yaml")
b, err := sendAdminRequest(ctx, req)
if err != nil {
log.Errorf("admin REST request failed: %s", err)
return
}
fmt.Fprintf(os.Stdout, "%s config for key prefix %q:\n%s\n", friendlyName, keyPrefix, string(b))
}
// RunGetAcct gets the account from the given key.
func RunGetAcct(ctx *Context, keyPrefix string) {
runGetConfig(ctx, acctPathPrefix, keyPrefix)
}
// RunGetPerm gets the permission from the given key.
func RunGetPerm(ctx *Context, keyPrefix string) {
runGetConfig(ctx, permPathPrefix, keyPrefix)
}
// RunGetZone gets the zone from the given key.
func RunGetZone(ctx *Context, keyPrefix string) {
runGetConfig(ctx, zonePathPrefix, keyPrefix)
}
// runLsConfigs invokes the REST API with GET action and no path, which
// fetches a list of all configuration prefixes.
// The type of config that is listed is based on the passed in prefix.
// The optional regexp is applied to the complete list and matching prefixes
// displayed.
func runLsConfigs(ctx *Context, prefix, pattern string) {
friendlyName := getFriendlyNameFromPrefix(prefix)
req, err := http.NewRequest("GET", fmt.Sprintf("%s://%s%s", ctx.RequestScheme(), ctx.Addr, prefix), nil)
if err != nil {
log.Errorf("unable to create request to admin REST endpoint: %s", err)
return
}
b, err := sendAdminRequest(ctx, req)
if err != nil {
log.Errorf("admin REST request failed: %s", err)
return
}
var prefixes []string
if err = json.Unmarshal(b, &prefixes); err != nil {
log.Errorf("unable to parse admin REST response: %s", err)
return
}
var re *regexp.Regexp
if len(pattern) > 0 {
if re, err = regexp.Compile(pattern); err != nil {
log.Warningf("invalid regular expression %q; skipping regexp match and listing all %s prefixes", pattern, friendlyName)
re = nil
}
}
for _, prefix := range prefixes {
if re != nil {
unescaped, err := url.QueryUnescape(prefix)
if err != nil || !re.MatchString(unescaped) {
continue
}
}
if prefix == "" {
prefix = "[default]"
}
fmt.Fprintf(os.Stdout, "%s\n", prefix)
}
}
// RunLsAcct lists accounts.
func RunLsAcct(ctx *Context, pattern string) {
runLsConfigs(ctx, acctPathPrefix, pattern)
}
// RunLsPerm lists permissions.
func RunLsPerm(ctx *Context, pattern string) {
runLsConfigs(ctx, permPathPrefix, pattern)
}
// RunLsZone lists zones.
func RunLsZone(ctx *Context, pattern string) {
runLsConfigs(ctx, zonePathPrefix, pattern)
}
// runRmConfig invokes the REST API with DELETE action and key prefix as path.
// The type of config that is removed is based on the passed in prefix.
func runRmConfig(ctx *Context, prefix, keyPrefix string) {
friendlyName := getFriendlyNameFromPrefix(prefix)
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s://%s%s/%s", ctx.RequestScheme(), ctx.Addr, prefix, keyPrefix),
nil)
if err != nil {
log.Errorf("unable to create request to admin REST endpoint: %s", err)
return
}
_, err = sendAdminRequest(ctx, req)
if err != nil {
log.Errorf("admin REST request failed: %s", err)
return
}
fmt.Fprintf(os.Stdout, "removed %s config for key prefix %q\n", friendlyName, keyPrefix)
}
// RunRmAcct removes the account with the given key.
func RunRmAcct(ctx *Context, keyPrefix string) {
runRmConfig(ctx, acctPathPrefix, keyPrefix)
}
// RunRmPerm removes the permission with the given key.
func RunRmPerm(ctx *Context, keyPrefix string) {
runRmConfig(ctx, permPathPrefix, keyPrefix)
}
// RunRmZone removes the zone with the given key.
func RunRmZone(ctx *Context, keyPrefix string) {
runRmConfig(ctx, zonePathPrefix, keyPrefix)
}
// runSetConfig invokes the REST API with POST action and key prefix as
// path. The specified configuration file is read from disk and sent
// as the POST body.
// The type of config that is set is based on the passed in prefix.
func runSetConfig(ctx *Context, prefix, keyPrefix, configFileName string) {
friendlyName := getFriendlyNameFromPrefix(prefix)
// Read in the config file.
body, err := ioutil.ReadFile(configFileName)
if err != nil {
log.Errorf("unable to read %s config file %q: %s", friendlyName, configFileName, err)
return
}
// Send to admin REST API.
req, err := http.NewRequest("POST", fmt.Sprintf("%s://%s%s/%s", ctx.RequestScheme(), ctx.Addr, prefix, keyPrefix),
bytes.NewReader(body))
if err != nil {
log.Errorf("unable to create request to admin REST endpoint: %s", err)
return
}
req.Header.Add("Content-Type", "text/yaml")
_, err = sendAdminRequest(ctx, req)
if err != nil {
log.Errorf("admin REST request failed: %s", err)
return
}
fmt.Fprintf(os.Stdout, "set %s config for key prefix %q\n", friendlyName, keyPrefix)
}
// RunSetAcct sets the account to the key given the yaml filename.
func RunSetAcct(ctx *Context, keyPrefix, configFileName string) {
runSetConfig(ctx, acctPathPrefix, keyPrefix, configFileName)
}
// RunSetPerm sets the permission to the key given the yaml filename.
func RunSetPerm(ctx *Context, keyPrefix, configFileName string) {
runSetConfig(ctx, permPathPrefix, keyPrefix, configFileName)
}
// RunSetZone sets the zone to the key given the yaml filename.
func RunSetZone(ctx *Context, keyPrefix, configFileName string) {
runSetConfig(ctx, zonePathPrefix, keyPrefix, configFileName)
}
// putConfig writes a config for the specified key prefix (which is
// treated as a key). The config is parsed from the input "body". The
// config is stored proto-encoded. The specified body must validly
// parse into a config struct and must pass a given validation check (if
// validate is not nil).
func putConfig(db *client.KV, configPrefix proto.Key, config gogoproto.Message,
path string, body []byte, r *http.Request,
validate func(gogoproto.Message) error) error {
if len(path) == 0 {
return util.Errorf("no path specified for Put")
}
if err := util.UnmarshalRequest(r, body, config, util.AllEncodings); err != nil {
return util.Errorf("config has invalid format: %+v: %s", config, err)
}
if validate != nil {
if err := validate(config); err != nil {
return err
}
}
key := engine.MakeKey(configPrefix, proto.Key(path[1:]))
if err := db.Run(client.PutProto(key, config)); err != nil {
return err
}
return nil
}
// getConfig retrieves the configuration for the specified key. If the
// key is empty, all configurations are returned. Otherwise, the
// leading "/" path delimiter is stripped and the configuration
// matching the remainder is retrieved. Note that this will retrieve
// the default config if "key" is equal to "/", and will list all
// configs if "key" is equal to "". The body result contains a listing
// of keys and retrieval of a config. The output format is determined
// by the request header.
func getConfig(db *client.KV, configPrefix proto.Key, config gogoproto.Message,
path string, r *http.Request) (body []byte, contentType string, err error) {
// Scan all configs if the key is empty.
if len(path) == 0 {
sr := &proto.ScanResponse{}
if err = db.Run(client.Call{
Args: &proto.ScanRequest{
RequestHeader: proto.RequestHeader{
Key: configPrefix,
EndKey: configPrefix.PrefixEnd(),
User: storage.UserRoot,
},
MaxResults: maxGetResults,
},
Reply: sr}); err != nil {
return
}
if len(sr.Rows) == maxGetResults {
log.Warningf("retrieved maximum number of results (%d); some may be missing", maxGetResults)
}
var prefixes []string
for _, kv := range sr.Rows {
trimmed := bytes.TrimPrefix(kv.Key, configPrefix)
prefixes = append(prefixes, url.QueryEscape(string(trimmed)))
}
// Encode the response.
body, contentType, err = util.MarshalResponse(r, prefixes, util.AllEncodings)
} else {
configkey := engine.MakeKey(configPrefix, proto.Key(path[1:]))
if err = db.Run(client.GetProto(configkey, config)); err != nil {
return
}
body, contentType, err = util.MarshalResponse(r, config, util.AllEncodings)
}
return
}
// deleteConfig removes the config specified by key.
func deleteConfig(db *client.KV, configPrefix proto.Key, path string, r *http.Request) error {
if len(path) == 0 {
return util.Errorf("no path specified for config Delete")
}
if path == "/" {
return util.Errorf("the default configuration cannot be deleted")
}
configKey := engine.MakeKey(configPrefix, proto.Key(path[1:]))
return db.Run(client.Call{
Args: &proto.DeleteRequest{
RequestHeader: proto.RequestHeader{
Key: configKey,
User: storage.UserRoot,
},
},
Reply: &proto.DeleteResponse{}})
}