2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
package madmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-07-30 22:55:22 -04:00
|
|
|
"strings"
|
2021-01-12 01:36:51 -05:00
|
|
|
"time"
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
)
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// ServiceType represents service type
|
|
|
|
type ServiceType string
|
2020-07-28 14:50:47 -04:00
|
|
|
|
|
|
|
const (
|
2020-08-06 20:10:21 -04:00
|
|
|
// ReplicationService specifies replication service
|
|
|
|
ReplicationService ServiceType = "replication"
|
2020-07-28 14:50:47 -04:00
|
|
|
)
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// IsValid returns true if ARN type represents replication
|
2020-08-06 20:10:21 -04:00
|
|
|
func (t ServiceType) IsValid() bool {
|
2021-04-19 13:30:42 -04:00
|
|
|
return t == ReplicationService
|
2020-07-30 22:55:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ARN is a struct to define arn.
|
|
|
|
type ARN struct {
|
2020-08-06 20:10:21 -04:00
|
|
|
Type ServiceType
|
2020-07-30 22:55:22 -04:00
|
|
|
ID string
|
|
|
|
Region string
|
|
|
|
Bucket string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns true if arn struct is empty
|
|
|
|
func (a ARN) Empty() bool {
|
|
|
|
return !a.Type.IsValid()
|
|
|
|
}
|
|
|
|
func (a ARN) String() string {
|
|
|
|
return fmt.Sprintf("arn:minio:%s:%s:%s:%s", a.Type, a.Region, a.ID, a.Bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseARN return ARN struct from string in arn format.
|
|
|
|
func ParseARN(s string) (*ARN, error) {
|
|
|
|
// ARN must be in the format of arn:minio:<Type>:<REGION>:<ID>:<remote-bucket>
|
|
|
|
if !strings.HasPrefix(s, "arn:minio:") {
|
|
|
|
return nil, fmt.Errorf("Invalid ARN %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := strings.Split(s, ":")
|
|
|
|
if len(tokens) != 6 {
|
|
|
|
return nil, fmt.Errorf("Invalid ARN %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokens[4] == "" || tokens[5] == "" {
|
|
|
|
return nil, fmt.Errorf("Invalid ARN %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ARN{
|
2020-08-06 20:10:21 -04:00
|
|
|
Type: ServiceType(tokens[2]),
|
2020-07-30 22:55:22 -04:00
|
|
|
Region: tokens[3],
|
|
|
|
ID: tokens[4],
|
|
|
|
Bucket: tokens[5],
|
|
|
|
}, nil
|
2020-07-28 14:50:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BucketTarget represents the target bucket and site association.
|
|
|
|
type BucketTarget struct {
|
2021-01-12 01:36:51 -05:00
|
|
|
SourceBucket string `json:"sourcebucket"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
Credentials *auth.Credentials `json:"credentials"`
|
|
|
|
TargetBucket string `json:"targetbucket"`
|
|
|
|
Secure bool `json:"secure"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
API string `json:"api,omitempty"`
|
|
|
|
Arn string `json:"arn,omitempty"`
|
|
|
|
Type ServiceType `json:"type"`
|
|
|
|
Region string `json:"omitempty"`
|
|
|
|
BandwidthLimit int64 `json:"bandwidthlimit,omitempty"`
|
|
|
|
ReplicationSync bool `json:"replicationSync"`
|
2021-04-19 13:30:42 -04:00
|
|
|
StorageClass string `json:"storageclass,omitempty"`
|
2021-01-12 01:36:51 -05:00
|
|
|
HealthCheckDuration time.Duration `json:"healthCheckDuration,omitempty"`
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// Clone returns shallow clone of BucketTarget without secret key in credentials
|
|
|
|
func (t *BucketTarget) Clone() BucketTarget {
|
|
|
|
return BucketTarget{
|
2021-01-12 01:36:51 -05:00
|
|
|
SourceBucket: t.SourceBucket,
|
|
|
|
Endpoint: t.Endpoint,
|
|
|
|
TargetBucket: t.TargetBucket,
|
|
|
|
Credentials: &auth.Credentials{AccessKey: t.Credentials.AccessKey},
|
|
|
|
Secure: t.Secure,
|
|
|
|
Path: t.Path,
|
|
|
|
API: t.Path,
|
|
|
|
Arn: t.Arn,
|
|
|
|
Type: t.Type,
|
|
|
|
Region: t.Region,
|
|
|
|
BandwidthLimit: t.BandwidthLimit,
|
|
|
|
ReplicationSync: t.ReplicationSync,
|
2021-04-19 13:30:42 -04:00
|
|
|
StorageClass: t.StorageClass, // target storage class
|
2021-01-12 01:36:51 -05:00
|
|
|
HealthCheckDuration: t.HealthCheckDuration,
|
2020-08-06 20:10:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
// URL returns target url
|
2020-11-25 14:24:50 -05:00
|
|
|
func (t BucketTarget) URL() *url.URL {
|
2020-07-21 20:49:56 -04:00
|
|
|
scheme := "http"
|
2020-07-28 14:50:47 -04:00
|
|
|
if t.Secure {
|
2020-07-21 20:49:56 -04:00
|
|
|
scheme = "https"
|
|
|
|
}
|
2020-11-25 14:24:50 -05:00
|
|
|
return &url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: t.Endpoint,
|
2020-09-17 14:09:50 -04:00
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns true if struct is empty.
|
2020-07-28 14:50:47 -04:00
|
|
|
func (t BucketTarget) Empty() bool {
|
2020-07-21 20:49:56 -04:00
|
|
|
return t.String() == "" || t.Credentials == nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:50:47 -04:00
|
|
|
func (t *BucketTarget) String() string {
|
2020-07-21 20:49:56 -04:00
|
|
|
return fmt.Sprintf("%s %s", t.Endpoint, t.TargetBucket)
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
// BucketTargets represents a slice of bucket targets by type and endpoint
|
|
|
|
type BucketTargets struct {
|
|
|
|
Targets []BucketTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty returns true if struct is empty.
|
|
|
|
func (t BucketTargets) Empty() bool {
|
|
|
|
if len(t.Targets) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
empty := true
|
|
|
|
for _, t := range t.Targets {
|
|
|
|
if !t.Empty() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// ListRemoteTargets - gets target(s) for this bucket
|
|
|
|
func (adm *AdminClient) ListRemoteTargets(ctx context.Context, bucket, arnType string) (targets []BucketTarget, err error) {
|
2020-07-21 20:49:56 -04:00
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("bucket", bucket)
|
2020-07-30 22:55:22 -04:00
|
|
|
queryValues.Set("type", arnType)
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
reqData := requestData{
|
2020-08-06 20:10:21 -04:00
|
|
|
relPath: adminAPIPrefix + "/list-remote-targets",
|
2020-07-21 20:49:56 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// Execute GET on /minio/admin/v3/list-remote-targets
|
2020-07-21 20:49:56 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
|
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return targets, err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-07-30 22:55:22 -04:00
|
|
|
return targets, httpRespToErrorResponse(resp)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return targets, err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
if err = json.Unmarshal(b, &targets); err != nil {
|
|
|
|
return targets, err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
return targets, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// SetRemoteTarget sets up a remote target for this bucket
|
|
|
|
func (adm *AdminClient) SetRemoteTarget(ctx context.Context, bucket string, target *BucketTarget) (string, error) {
|
2020-07-21 20:49:56 -04:00
|
|
|
data, err := json.Marshal(target)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return "", err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
encData, err := EncryptData(adm.getSecretKey(), data)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return "", err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("bucket", bucket)
|
|
|
|
|
|
|
|
reqData := requestData{
|
2020-08-06 20:10:21 -04:00
|
|
|
relPath: adminAPIPrefix + "/set-remote-target",
|
2020-07-21 20:49:56 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
content: encData,
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// Execute PUT on /minio/admin/v3/set-remote-target to set a target for this bucket of specific arn type.
|
2020-07-21 20:49:56 -04:00
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
|
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2020-11-24 22:09:05 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return "", httpRespToErrorResponse(resp)
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var arn string
|
|
|
|
if err = json.Unmarshal(b, &arn); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return arn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRemoteTarget updates credentials for a remote bucket target
|
|
|
|
func (adm *AdminClient) UpdateRemoteTarget(ctx context.Context, target *BucketTarget) (string, error) {
|
|
|
|
if target == nil {
|
|
|
|
return "", fmt.Errorf("target cannot be nil")
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(target)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
encData, err := EncryptData(adm.getSecretKey(), data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
queryValues := url.Values{}
|
|
|
|
queryValues.Set("bucket", target.SourceBucket)
|
|
|
|
queryValues.Set("update", "true")
|
|
|
|
|
|
|
|
reqData := requestData{
|
|
|
|
relPath: adminAPIPrefix + "/set-remote-target",
|
|
|
|
queryValues: queryValues,
|
|
|
|
content: encData,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute PUT on /minio/admin/v3/set-remote-target to set a target for this bucket of specific arn type.
|
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
|
|
|
|
|
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return "", err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-07-30 22:55:22 -04:00
|
|
|
return "", httpRespToErrorResponse(resp)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var arn string
|
|
|
|
if err = json.Unmarshal(b, &arn); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return arn, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// RemoveRemoteTarget removes a remote target associated with particular ARN for this bucket
|
|
|
|
func (adm *AdminClient) RemoveRemoteTarget(ctx context.Context, bucket, arn string) error {
|
2020-07-21 20:49:56 -04:00
|
|
|
queryValues := url.Values{}
|
2020-07-30 22:55:22 -04:00
|
|
|
queryValues.Set("bucket", bucket)
|
|
|
|
queryValues.Set("arn", arn)
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
reqData := requestData{
|
2020-08-06 20:10:21 -04:00
|
|
|
relPath: adminAPIPrefix + "/remove-remote-target",
|
2020-07-21 20:49:56 -04:00
|
|
|
queryValues: queryValues,
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// Execute PUT on /minio/admin/v3/remove-remote-target to remove a target for this bucket
|
2020-07-30 22:55:22 -04:00
|
|
|
// with specific ARN
|
|
|
|
resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
|
2020-07-21 20:49:56 -04:00
|
|
|
defer closeResponse(resp)
|
|
|
|
if err != nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
return httpRespToErrorResponse(resp)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
return nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|