From 78f844a3571a504eca35ec905159f7bc933373d8 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Mon, 27 Jun 2022 00:05:31 +0530 Subject: [PATCH] Save license at the time of registration (#4123) Currently we only extract the `api_key` from SUBNET's registration api response and store it in the minio subnet config. The response contains license also, and it can be useful in getting information about the subscription. So this PR enhances the register command to extract and store the license also. --- cmd/subnet-utils.go | 55 ++++++++++++++++++++++++++++------------- cmd/support-diag.go | 2 -- cmd/support-register.go | 31 +++++++++++++++-------- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index ba1f3a4c..4c916342 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -291,24 +291,44 @@ func minioConfigSupportsSubSys(client *madmin.AdminClient, subSys string) bool { return false } -func setSubnetAPIKeyConfig(alias string, apiKey string) { - _, supported := getKeyFromMinIOConfig(alias, "subnet", "api_key") - if supported { - // Create a new MinIO Admin Client - client, err := newAdminClient(alias) - fatalIf(err, "Unable to initialize admin connection.") - - configStr := "subnet license= api_key=" + apiKey - _, e := client.SetConfigKV(globalContext, configStr) - fatalIf(probe.NewError(e), "Unable to set SUBNET API key config on MinIO") - return - } +func setSubnetCredsInMcConfig(alias string, apiKey string, lic string) { mcCfg := mcConfig() aliasCfg := mcCfg.Aliases[alias] - aliasCfg.APIKey = apiKey + if len(apiKey) > 0 { + aliasCfg.APIKey = apiKey + } + if len(lic) > 0 { + aliasCfg.License = lic + } setAlias(alias, aliasCfg) } +func setSubnetCreds(alias string, apiKey string, lic string) { + if len(apiKey) == 0 && len(lic) == 0 { + fatal(errDummy().Trace(), "At least one of api key and license must be passed.") + } + + _, apiKeySupported := getKeyFromMinIOConfig(alias, "subnet", "api_key") + _, licSupported := getKeyFromMinIOConfig(alias, "subnet", "license") + if !(apiKeySupported || licSupported) { + setSubnetCredsInMcConfig(alias, apiKey, lic) + return + } + + client, err := newAdminClient(alias) + fatalIf(err, "Unable to initialize admin connection.") + + configStr := "subnet" + if apiKeySupported && len(apiKey) > 0 { + configStr += " api_key=" + apiKey + } + if licSupported && len(lic) > 0 { + configStr += " license=" + lic + } + _, e := client.SetConfigKV(globalContext, configStr) + fatalIf(probe.NewError(e), "Unable to set SUBNET license/api_key config on MinIO") +} + func getClusterRegInfo(admInfo madmin.InfoMessage, clusterName string) ClusterRegistrationInfo { noOfPools := 1 noOfDrives := 0 @@ -469,10 +489,11 @@ func getSubnetCreds(alias string) (string, string, error) { return apiKey, lic, nil } -// extractAndSaveAPIKey - extract api key from response and set it in minio config -func extractAndSaveAPIKey(alias string, resp string) { +// extractAndSaveSubnetCreds - extract license / api key from response and set it in minio config +func extractAndSaveSubnetCreds(alias string, resp string) { subnetAPIKey := gjson.Parse(resp).Get("api_key").String() - if len(subnetAPIKey) > 0 { - setSubnetAPIKeyConfig(alias, subnetAPIKey) + subnetLic := gjson.Parse(resp).Get("license").String() + if len(subnetAPIKey) > 0 || len(subnetLic) > 0 { + setSubnetCreds(alias, subnetAPIKey, subnetLic) } } diff --git a/cmd/support-diag.go b/cmd/support-diag.go index f1f2d599..7c77ea77 100644 --- a/cmd/support-diag.go +++ b/cmd/support-diag.go @@ -284,8 +284,6 @@ func uploadDiagReport(alias string, filename string, reqURL string, headers map[ return e } - extractAndSaveAPIKey(alias, resp) - // Delete the report after successful upload os.Remove(filename) diff --git a/cmd/support-register.go b/cmd/support-register.go index dbf98127..dc6a3ca6 100644 --- a/cmd/support-register.go +++ b/cmd/support-register.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/fatih/color" + "github.com/golang-jwt/jwt/v4" "github.com/google/uuid" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" @@ -173,19 +174,29 @@ func registerOffline(clusterRegInfo ClusterRegistrationInfo, alias string) { ` + regToken + ` -Step 2: Enter the API key generated by SUBNET: `) +Step 2: Enter the key generated by SUBNET: `) reader := bufio.NewReader(os.Stdin) - apiKey, e := reader.ReadString('\n') - fatalIf(probe.NewError(e), "Error in reading API key") - apiKey = strings.TrimSpace(apiKey) + key, e := reader.ReadString('\n') + fatalIf(probe.NewError(e), "Error in reading the key") + key = strings.TrimSpace(key) - if len(apiKey) > 0 { - _, e := uuid.Parse(apiKey) - fatalIf(probe.NewError(e), "Invalid API key specified:") - setSubnetAPIKeyConfig(alias, apiKey) + 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 API key specified. Please run the command again with a valid SUBNET API key to complete registration.") + console.Fatalln("Invalid key specified. Please run the command again with a valid key to complete registration.") } } @@ -193,5 +204,5 @@ func registerOnline(clusterRegInfo ClusterRegistrationInfo, alias string, cluste resp, e := registerClusterOnSubnet(alias, clusterRegInfo) fatalIf(probe.NewError(e), "Could not register cluster with SUBNET:") - extractAndSaveAPIKey(alias, resp) + extractAndSaveSubnetCreds(alias, resp) }