From 9de421ffd4c56d7434632f045ea3beea4b242d05 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Wed, 27 Jul 2022 11:51:06 +0530 Subject: [PATCH] New offline registration flow (#4156) Instead of asking the user to - open the subnet register page - paste the registered token - copy the generated license token - paste it back on mc Now we will simply ask the user to visit a subnet URL (which contains the registration token as a query parameter). On visiting this page, subnet will automatically register the cluster and generate a license for it. User can then optionally update the license on mc using the `mc license update` command --- cmd/license-register.go | 92 ++++++++++++++++++++--------------------- cmd/subnet-utils.go | 4 ++ 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/cmd/license-register.go b/cmd/license-register.go index f2457ee4..93df1abb 100644 --- a/cmd/license-register.go +++ b/cmd/license-register.go @@ -18,16 +18,13 @@ package cmd import ( - "bufio" "errors" "fmt" - "os" - "strings" "github.com/fatih/color" - "github.com/golang-jwt/jwt/v4" "github.com/google/uuid" "github.com/minio/cli" + json "github.com/minio/colorjson" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" ) @@ -68,6 +65,35 @@ EXAMPLES: `, } +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 = 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 += li.URL + } + return console.Colorize(licUpdateMsgTag, 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 { @@ -172,58 +198,28 @@ func mainLicenseRegister(ctx *cli.Context) error { alreadyRegistered = true } + lrm := licRegisterMessage{Status: "success", Alias: alias} if offline { - registerOffline(regInfo, alias) + lrm.Type = "offline" + + regToken, e := generateRegToken(regInfo) + fatalIf(probe.NewError(e), "Unable to generate registration token") + + lrm.URL = subnetOfflineRegisterURL(regToken) } else { + lrm.Type = "online" registerOnline(regInfo, alias, accAPIKey) + + lrm.Action = "registered" + if alreadyRegistered { + lrm.Action = "updated" + } } - action := "registered" - if alreadyRegistered { - action = "updated" - } - - msg := console.Colorize("RegisterSuccessMessage", fmt.Sprintf("%s %s successfully.", clusterName, action)) - fmt.Println(msg) + printMsg(lrm) return nil } -func registerOffline(clusterRegInfo ClusterRegistrationInfo, alias string) { - regToken, e := generateRegToken(clusterRegInfo) - fatalIf(probe.NewError(e), "Unable to generate registration token") - - subnetRegisterPageURL := "https://subnet.min.io/cluster/register" - - fmt.Print(`Step 1: Use the following token to register your cluster at ` + subnetRegisterPageURL + ` - -` + regToken + ` - -Step 2: Enter the key generated by SUBNET: `) - - reader := bufio.NewReader(os.Stdin) - key, e := reader.ReadString('\n') - fatalIf(probe.NewError(e), "Error in reading the key") - key = strings.TrimSpace(key) - - if len(key) > 0 { - _, e := uuid.Parse(key) - if e == nil { - // key is the api key - setSubnetCreds(alias, key, "") - return - } - - // Not api-key (uuid). Must be license (jwt) - _, _, e = new(jwt.Parser).ParseUnverified(key, jwt.MapClaims{}) - if e != nil { - fatalIf(probe.NewError(e), "Invalid key specified:") - } - setSubnetCreds(alias, "", key) - } else { - console.Fatalln("Invalid key specified. Please run the command again with a valid key to complete registration.") - } -} - func registerOnline(clusterRegInfo ClusterRegistrationInfo, alias string, accAPIKey string) { var resp string var e error diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index 85991e05..f192263d 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -104,6 +104,10 @@ func subnetRegisterURL() string { return subnetBaseURL() + "/api/cluster/register" } +func subnetOfflineRegisterURL(regToken string) string { + return subnetBaseURL() + "/cluster/register?token=" + regToken +} + func subnetLoginURL() string { return subnetBaseURL() + "/api/auth/login" }