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.
This commit is contained in:
+38
-17
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
+21
-10
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user