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
This commit is contained in:
Shireesh Anjal
2022-07-26 23:21:06 -07:00
committed by GitHub
parent c32a438045
commit 9de421ffd4
2 changed files with 48 additions and 48 deletions
+44 -48
View File
@@ -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
+4
View File
@@ -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"
}