/
admin-bucket-remote-edit.go
180 lines (160 loc) · 5.86 KB
/
admin-bucket-remote-edit.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
// 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/>.
package cmd
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/madmin-go"
"github.com/filswan/fs3-mc/pkg/probe"
"github.com/minio/minio/pkg/console"
)
var adminBucketRemoteEditFlags = []cli.Flag{
cli.StringFlag{
Name: "arn",
Usage: "ARN of target",
}}
var adminBucketRemoteEditCmd = cli.Command{
Name: "edit",
Usage: "edit credentials for existing remote target",
Action: mainAdminBucketRemoteEdit,
Before: setGlobalsFromContext,
OnUsageError: onUsageError,
Flags: append(globalFlags, adminBucketRemoteEditFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET http(s)://ACCESSKEY:SECRETKEY@DEST_URL/DEST_BUCKET --arn arn
TARGET:
Also called as alias/sourcebucketname
DEST_BUCKET:
Also called as remote target bucket.
DEST_URL:
Also called as remote endpoint.
ACCESSKEY:
Also called as username.
SECRETKEY:
Also called as password.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Edit credentials for existing remote target with arn where a remote target has been configured between sourcebucket on sitea to targetbucket on siteb.
{{.DisableHistory}}
{{.Prompt}} {{.HelpName}} sitea/sourcebucket \
https://foobar:newpassword@minio.siteb.example.com/targetbucket \
--arn "arn:minio:replication:us-west-1:993bc6b6-accd-45e3-884f-5f3e652aed2a:dest1"
{{.EnableHistory}}
`,
}
// checkAdminBucketRemoteEditSyntax - validate all the passed arguments
func checkAdminBucketRemoteEditSyntax(ctx *cli.Context) {
argsNr := len(ctx.Args())
if argsNr != 2 {
cli.ShowCommandHelpAndExit(ctx, ctx.Command.Name, 1) // last argument is exit code
}
}
// fetchRemoteEditTarget - returns the dest bucket, dest endpoint, access and secret key
func fetchRemoteEditTarget(cli *cli.Context) (bktTarget *madmin.BucketTarget) {
args := cli.Args()
_, sourceBucket := url2Alias(args[0])
TargetURL := args[1]
parts := targetKeys.FindStringSubmatch(TargetURL)
if len(parts) != 6 {
fatalIf(probe.NewError(fmt.Errorf("invalid url format")), "Malformed Remote target URL")
}
accessKey := parts[2]
secretKey := parts[3]
parsedURL := fmt.Sprintf("%s%s", parts[1], parts[4])
TargetBucket := strings.TrimSuffix(parts[5], slashSeperator)
TargetBucket = strings.TrimPrefix(TargetBucket, slashSeperator)
u, cerr := url.Parse(parsedURL)
if cerr != nil {
fatalIf(probe.NewError(cerr), "Malformed Remote target URL")
}
secure := u.Scheme == "https"
host := u.Host
if u.Port() == "" {
port := 80
if secure {
port = 443
}
host = host + ":" + strconv.Itoa(port)
}
console.SetColor(cred, color.New(color.FgYellow, color.Italic))
creds := &madmin.Credentials{AccessKey: accessKey, SecretKey: secretKey}
bktTarget = &madmin.BucketTarget{
SourceBucket: sourceBucket,
TargetBucket: TargetBucket,
Secure: secure,
Credentials: creds,
Endpoint: host,
API: "s3v4",
Region: cli.String("region"),
Arn: cli.String("arn"),
}
return bktTarget
}
// mainAdminBucketRemoteEdit is the handle for "mc admin bucket remote edit" command.
func mainAdminBucketRemoteEdit(ctx *cli.Context) error {
checkAdminBucketRemoteEditSyntax(ctx)
console.SetColor("RemoteMessage", color.New(color.FgGreen))
// Get the alias parameter from cli
args := ctx.Args()
aliasedURL := args.Get(0)
// Create a new MinIO Admin Client
client, cerr := newAdminClient(aliasedURL)
fatalIf(cerr, "Unable to initialize admin connection.")
bktTarget := fetchRemoteEditTarget(ctx)
targets, e := client.ListRemoteTargets(globalContext, bktTarget.SourceBucket, "")
fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch remote target.")
var found bool
for _, t := range targets {
if t.Arn == bktTarget.Arn {
if t.Endpoint != bktTarget.Endpoint {
fatalIf(errInvalidArgument().Trace(args...), "configured Endpoint `"+t.Endpoint+"` does not match "+bktTarget.Endpoint+"` for this ARN `"+bktTarget.Arn+"`")
}
if t.TargetBucket != bktTarget.TargetBucket {
fatalIf(errInvalidArgument().Trace(args...), "configured remote target bucket `"+t.TargetBucket+"` does not match "+bktTarget.TargetBucket+"` for this ARN `"+bktTarget.Arn+"`")
}
if t.SourceBucket != bktTarget.SourceBucket {
fatalIf(errInvalidArgument().Trace(args...), "configured source bucket `"+t.SourceBucket+"` does not match "+bktTarget.SourceBucket+"` for this ARN `"+bktTarget.Arn+"`")
}
found = true
break
}
}
if !found {
fatalIf(errInvalidArgument().Trace(args...), "Unable to edit remote target - `"+bktTarget.Arn+"` is not a valid Arn")
}
arn, e := client.UpdateRemoteTarget(globalContext, bktTarget)
if e != nil {
fatalIf(probe.NewError(e).Trace(args...), "Unable to update credentials for remote target `"+bktTarget.Endpoint+"` from `"+bktTarget.SourceBucket+"` -> `"+bktTarget.TargetBucket+"`")
}
printMsg(RemoteMessage{
op: ctx.Command.Name,
TargetURL: bktTarget.URL().String(),
TargetBucket: bktTarget.TargetBucket,
AccessKey: bktTarget.Credentials.AccessKey,
SourceBucket: bktTarget.SourceBucket,
RemoteARN: arn,
})
return nil
}