forked from minio/mc
/
license-register.go
266 lines (223 loc) · 7.83 KB
/
license-register.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
// Copyright (c) 2015-2022 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"
"net/url"
"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/trinet2005/oss-admin-go"
"github.com/trinet2005/oss-go-sdk/pkg/set"
"github.com/trinet2005/oss-mc/pkg/probe"
"github.com/trinet2005/oss-pkg/console"
)
const (
licRegisterMsgTag = "licenseRegisterMessage"
licRegisterLinkTag = "licenseRegisterLink"
)
var licenseRegisterFlags = append([]cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "Specify the name to associate to this MinIO cluster in SUBNET",
},
}, subnetCommonFlags...)
var licenseRegisterCmd = cli.Command{
Name: "register",
Usage: "register with MinIO Subscription Network",
OnUsageError: onUsageError,
Action: mainLicenseRegister,
Before: setGlobalsFromContext,
Flags: licenseRegisterFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Register MinIO cluster at alias 'play' on SUBNET, using api key for auth
{{.Prompt}} {{.HelpName}} play --api-key 08efc836-4289-dbd4-ad82-b5e8b6d25577
2. Register MinIO cluster at alias 'play' on SUBNET, using api key for auth,
and "play-cluster" as the preferred name for the cluster on SUBNET.
{{.Prompt}} {{.HelpName}} play --api-key 08efc836-4289-dbd4-ad82-b5e8b6d25577 --name play-cluster
3. Register MinIO cluster at alias 'play' on SUBNET in an airgapped environment
{{.Prompt}} {{.HelpName}} play --airgap
4. Register MinIO cluster at alias 'play' on SUBNET, using alias as the cluster name.
This asks for SUBNET credentials if the cluster is not already registered.
{{.Prompt}} {{.HelpName}} play
`,
}
type licRegisterMessage struct {
Status string `json:"status"`
Alias string `json:"-"`
Action string `json:"action,omitempty"`
Type string `json:"type"`
URL string `json:"url,omitempty"`
}
// String colorized license register message
func (li licRegisterMessage) String() string {
var msg string
switch li.Type {
case "online":
msg = console.Colorize(licRegisterMsgTag, fmt.Sprintf("%s %s successfully.", li.Alias, li.Action))
case "offline":
msg = fmt.Sprintln("Open the following URL in the browser to register", li.Alias, "on SUBNET:")
msg = console.Colorize(licRegisterMsgTag, msg) + console.Colorize(licRegisterLinkTag, li.URL)
}
return msg
}
// JSON jsonified license register message
func (li licRegisterMessage) JSON() string {
jsonBytes, e := json.MarshalIndent(li, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonBytes)
}
// checkLicenseRegisterSyntax - validate arguments passed by a user
func checkLicenseRegisterSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
}
// ClusterRegistrationReq - JSON payload of the subnet api for cluster registration
// Contains a registration token created by base64 encoding of the registration info
type ClusterRegistrationReq struct {
Token string `json:"token"`
}
// ClusterRegistrationInfo - Information stored in the cluster registration token
type ClusterRegistrationInfo struct {
DeploymentID string `json:"deployment_id"`
ClusterName string `json:"cluster_name"`
UsedCapacity uint64 `json:"used_capacity"`
Info ClusterInfo `json:"info"`
}
// ClusterInfo - The "info" sub-node of the cluster registration information struct
// Intended to be extensible i.e. more fields will be added as and when required
type ClusterInfo struct {
MinioVersion string `json:"minio_version"`
NoOfServerPools int `json:"no_of_server_pools"`
NoOfServers int `json:"no_of_servers"`
NoOfDrives int `json:"no_of_drives"`
NoOfBuckets uint64 `json:"no_of_buckets"`
NoOfObjects uint64 `json:"no_of_objects"`
TotalDriveSpace uint64 `json:"total_drive_space"`
UsedDriveSpace uint64 `json:"used_drive_space"`
}
// SubnetLoginReq - JSON payload of the SUBNET login api
type SubnetLoginReq struct {
Username string `json:"username"`
Password string `json:"password"`
}
// SubnetMFAReq - JSON payload of the SUBNET mfa api
type SubnetMFAReq struct {
Username string `json:"username"`
OTP string `json:"otp"`
Token string `json:"token"`
}
func isPlay(endpoint url.URL) (bool, error) {
playEndpoint := "https://play.min.io"
if globalAirgapped {
return endpoint.String() == playEndpoint, nil
}
aliasIPs, e := net.LookupHost(endpoint.Hostname())
if e != nil {
return false, e
}
aliasIPSet := set.CreateStringSet(aliasIPs...)
playURL, e := url.Parse(playEndpoint)
if e != nil {
return false, e
}
playIPs, e := net.LookupHost(playURL.Hostname())
if e != nil {
return false, e
}
playIPSet := set.CreateStringSet(playIPs...)
return !aliasIPSet.Intersection(playIPSet).IsEmpty(), nil
}
func validateNotPlay(aliasedURL string) {
client := getClient(aliasedURL)
endpoint := client.GetEndpointURL()
if endpoint == nil {
fatal(errDummy().Trace(), "invalid endpoint on alias "+aliasedURL)
return
}
isplay, e := isPlay(*endpoint)
fatalIf(probe.NewError(e), "error checking if endpoint is play:")
if isplay {
fatal(errDummy().Trace(), "play is a public demo cluster; cannot be registered")
}
}
func mainLicenseRegister(ctx *cli.Context) error {
console.SetColor(licRegisterMsgTag, color.New(color.FgGreen, color.Bold))
console.SetColor(licRegisterLinkTag, color.New(color.FgWhite, color.Bold))
checkLicenseRegisterSyntax(ctx)
// Get the alias parameter from cli
aliasedURL := ctx.Args().Get(0)
validateNotPlay(aliasedURL)
alias, accAPIKey := initSubnetConnectivity(ctx, aliasedURL, true)
clusterName := ctx.String("name")
if len(clusterName) == 0 {
clusterName = alias
} else {
if globalAirgapped {
fatalIf(errInvalidArgument(), "'--name' is not allowed in airgapped mode")
}
}
regInfo := getClusterRegInfo(getAdminInfo(aliasedURL), clusterName)
lrm := licRegisterMessage{Status: "success", Alias: alias}
if globalAirgapped {
lrm.Type = "offline"
regToken, e := generateRegToken(regInfo)
fatalIf(probe.NewError(e), "Unable to generate registration token")
lrm.URL = subnetOfflineRegisterURL(regToken)
} else {
alreadyRegistered := false
if len(accAPIKey) == 0 {
apiKey, _, e := getSubnetCreds(alias)
fatalIf(probe.NewError(e), "Error in fetching subnet API Key")
if len(apiKey) > 0 {
alreadyRegistered = true
accAPIKey = apiKey
}
} else {
apiKey := getSubnetAPIKeyFromConfig(alias)
if len(apiKey) > 0 {
alreadyRegistered = true
}
}
lrm.Type = "online"
_, _, e := registerClusterOnSubnet(regInfo, alias, accAPIKey)
fatalIf(probe.NewError(e), "Could not register cluster with SUBNET:")
lrm.Action = "registered"
if alreadyRegistered {
lrm.Action = "updated"
}
}
printMsg(lrm)
return nil
}
func getAdminInfo(aliasedURL string) madmin.InfoMessage {
// Create a new MinIO Admin Client
client := getClient(aliasedURL)
// Fetch info of all servers (cluster or single server)
admInfo, e := client.ServerInfo(globalContext)
fatalIf(probe.NewError(e), "Unable to fetch cluster info")
return admInfo
}