forked from minio/madmin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster-commands.go
395 lines (332 loc) · 10.9 KB
/
cluster-commands.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// MinIO Object Storage (c) 2021 MinIO, Inc.
//
// 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.
//
package madmin
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
// PeerSite - represents a cluster/site to be added to the set of replicated
// sites.
type PeerSite struct {
Name string `json:"name"`
Endpoint string `json:"endpoints"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
// Meaningful values for ReplicateAddStatus.Status
const (
ReplicateAddStatusSuccess = "Requested sites were configured for replication successfully."
ReplicateAddStatusPartial = "Some sites could not be configured for replication."
)
// ReplicateAddStatus - returns status of add request.
type ReplicateAddStatus struct {
Success bool `json:"success"`
Status string `json:"status"`
ErrDetail string `json:"errorDetail,omitempty"`
InitialSyncErrorMessage string `json:"initialSyncErrorMessage,omitempty"`
}
// SiteReplicationAdd - sends the SR add API call.
func (adm *AdminClient) SiteReplicationAdd(ctx context.Context, sites []PeerSite) (ReplicateAddStatus, error) {
sitesBytes, err := json.Marshal(sites)
if err != nil {
return ReplicateAddStatus{}, nil
}
encBytes, err := EncryptData(adm.getSecretKey(), sitesBytes)
if err != nil {
return ReplicateAddStatus{}, err
}
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/add",
content: encBytes,
}
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return ReplicateAddStatus{}, err
}
if resp.StatusCode != http.StatusOK {
return ReplicateAddStatus{}, httpRespToErrorResponse(resp)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ReplicateAddStatus{}, err
}
var res ReplicateAddStatus
if err = json.Unmarshal(b, &res); err != nil {
return ReplicateAddStatus{}, err
}
return res, nil
}
// SiteReplicationInfo - contains cluster replication information.
type SiteReplicationInfo struct {
Enabled bool `json:"enabled"`
Name string `json:"name,omitempty"`
Sites []PeerInfo `json:"sites,omitempty"`
ServiceAccountAccessKey string `json:"serviceAccountAccessKey,omitempty"`
}
// SiteReplicationInfo - returns cluster replication information.
func (adm *AdminClient) SiteReplicationInfo(ctx context.Context) (info SiteReplicationInfo, err error) {
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/info",
}
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return info, err
}
if resp.StatusCode != http.StatusOK {
return info, httpRespToErrorResponse(resp)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return info, err
}
err = json.Unmarshal(b, &info)
return info, err
}
// SRInternalJoinReq - arg body for SRInternalJoin
type SRInternalJoinReq struct {
SvcAcctAccessKey string `json:"svcAcctAccessKey"`
SvcAcctSecretKey string `json:"svcAcctSecretKey"`
SvcAcctParent string `json:"svcAcctParent"`
Peers map[string]PeerInfo `json:"peers"`
}
// PeerInfo - contains some properties of a cluster peer.
type PeerInfo struct {
Endpoint string `json:"endpoint"`
Name string `json:"name"`
// Deployment ID is useful as it is immutable - though endpoint may
// change.
DeploymentID string `json:"deploymentID"`
}
// SRInternalJoin - used only by minio server to send SR join requests to peer
// servers.
func (adm *AdminClient) SRInternalJoin(ctx context.Context, r SRInternalJoinReq) error {
b, err := json.Marshal(r)
if err != nil {
return err
}
encBuf, err := EncryptData(adm.getSecretKey(), b)
if err != nil {
return err
}
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/peer/join",
content: encBuf,
}
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
}
return nil
}
// BktOp represents the bucket operation being requested.
type BktOp string
// BktOp value constants.
const (
// make bucket and enable versioning
MakeWithVersioningBktOp BktOp = "make-with-versioning"
// add replication configuration
ConfigureReplBktOp BktOp = "configure-replication"
// delete bucket (forceDelete = off)
DeleteBucketBktOp BktOp = "delete-bucket"
// delete bucket (forceDelete = on)
ForceDeleteBucketBktOp BktOp = "force-delete-bucket"
)
// SRInternalBucketOps - tells peers to create bucket and setup replication.
func (adm *AdminClient) SRInternalBucketOps(ctx context.Context, bucket string, op BktOp, opts map[string]string) error {
v := url.Values{}
v.Add("bucket", bucket)
v.Add("operation", string(op))
// For make-bucket, bucket options may be sent via `opts`
if op == MakeWithVersioningBktOp {
for k, val := range opts {
v.Add(k, val)
}
}
reqData := requestData{
queryValues: v,
relPath: adminAPIPrefix + "/site-replication/peer/bucket-ops",
}
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
}
return nil
}
// SRIAMItem.Type constants.
const (
SRIAMItemPolicy = "policy"
SRIAMItemSvcAcc = "service-account"
SRIAMItemSTSAcc = "sts-account"
SRIAMItemPolicyMapping = "policy-mapping"
)
// SRSvcAccCreate - create operation
type SRSvcAccCreate struct {
Parent string `json:"parent"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Groups []string `json:"groups"`
Claims map[string]interface{} `json:"claims"`
SessionPolicy json.RawMessage `json:"sessionPolicy"`
Status string `json:"status"`
}
// SRSvcAccUpdate - update operation
type SRSvcAccUpdate struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
Status string `json:"status"`
SessionPolicy json.RawMessage `json:"sessionPolicy"`
}
// SRSvcAccDelete - delete operation
type SRSvcAccDelete struct {
AccessKey string `json:"accessKey"`
}
// SRSvcAccChange - sum-type to represent an svc account change.
type SRSvcAccChange struct {
Create *SRSvcAccCreate `json:"crSvcAccCreate"`
Update *SRSvcAccUpdate `json:"crSvcAccUpdate"`
Delete *SRSvcAccDelete `json:"crSvcAccDelete"`
}
// SRPolicyMapping - represents mapping of a policy to a user or group.
type SRPolicyMapping struct {
UserOrGroup string `json:"userOrGroup"`
IsGroup bool `json:"isGroup"`
Policy string `json:"policy"`
}
// SRSTSCredential - represents an STS credential to be replicated.
type SRSTSCredential struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
SessionToken string `json:"sessionToken"`
}
// SRIAMItem - represents an IAM object that will be copied to a peer.
type SRIAMItem struct {
Type string `json:"type"`
// Name and Policy below are used when Type == SRIAMItemPolicy
Name string `json:"name"`
Policy json.RawMessage `json:"policy"`
// Used when Type == SRIAMItemPolicyMapping
PolicyMapping *SRPolicyMapping `json:"policyMapping"`
// Used when Type == SRIAMItemSvcAcc
SvcAccChange *SRSvcAccChange `json:"serviceAccountChange"`
// Used when Type = SRIAMItemSTSAcc
STSCredential *SRSTSCredential `json:"stsCredential"`
}
// SRInternalReplicateIAMItem - copies an IAM object to a peer cluster.
func (adm *AdminClient) SRInternalReplicateIAMItem(ctx context.Context, item SRIAMItem) error {
b, err := json.Marshal(item)
if err != nil {
return err
}
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/peer/iam-item",
content: b,
}
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
}
return nil
}
// SRBucketMeta.Type constants
const (
SRBucketMetaTypePolicy = "policy"
SRBucketMetaTypeTags = "tags"
SRBucketMetaTypeObjectLockConfig = "object-lock-config"
SRBucketMetaTypeSSEConfig = "sse-config"
)
// SRBucketMeta - represents a bucket metadata change that will be copied to a
// peer.
type SRBucketMeta struct {
Type string `json:"type"`
Bucket string `json:"bucket"`
Policy json.RawMessage `json:"policy,omitempty"`
// Since tags does not have a json representation, we use its xml byte
// representation directly.
Tags *string `json:"tags,omitempty"`
// Since object lock does not have a json representation, we use its xml
// byte representation.
ObjectLockConfig *string `json:"objectLockConfig,omitempty"`
// Since SSE config does not have a json representation, we use its xml
// byte respresentation.
SSEConfig *string `json:"sseConfig,omitempty"`
}
// SRInternalReplicateBucketMeta - copies a bucket metadata change to a peer
// cluster.
func (adm *AdminClient) SRInternalReplicateBucketMeta(ctx context.Context, item SRBucketMeta) error {
b, err := json.Marshal(item)
if err != nil {
return err
}
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/peer/bucket-meta",
content: b,
}
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return httpRespToErrorResponse(resp)
}
return nil
}
// IDPSettings contains key IDentity Provider settings to validate that all
// peers have the same configuration.
type IDPSettings struct {
IsLDAPEnabled bool
LDAPUserDNSearchBase string
LDAPUserDNSearchFilter string
LDAPGroupSearchBase string
LDAPGroupSearchFilter string
}
// SRInternalGetIDPSettings - fetches IDP settings from the server.
func (adm *AdminClient) SRInternalGetIDPSettings(ctx context.Context) (info IDPSettings, err error) {
reqData := requestData{
relPath: adminAPIPrefix + "/site-replication/peer/idp-settings",
}
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return info, err
}
if resp.StatusCode != http.StatusOK {
return info, httpRespToErrorResponse(resp)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return info, err
}
err = json.Unmarshal(b, &info)
return info, err
}