From b6c50a92a12ed5746cbff0ec957a5e687b328712 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Thu, 9 Dec 2021 23:10:27 +0530 Subject: [PATCH] Use subnet api key instead of license (#3876) The earlier approach of using a license token for communicating with SUBNET is being replaced with a simpler mechanism of API keys. Unlike the license which is a JWT token, these API keys will be simple UUID tokens, and don't have any embedded information in them. SUBNET would generate the API key on cluster registration, and return in the API response. mc then stores it in the `subnet api_key` config of minio, to be used for subsequent communication with SUBNET. In case the api_key is not available in the config, but the cluster's license is available, it will be used as auth in the subnet API call, and api_key will be set after the call succeeds. This will help in smooth migration from older versions of MinIO. --- cmd/admin-subnet-health.go | 20 ++++---- cmd/admin-subnet-register.go | 34 +++++-------- cmd/config-v10.go | 1 + cmd/subnet-utils.go | 99 +++++++++++++++++++++--------------- go.mod | 2 +- 5 files changed, 82 insertions(+), 74 deletions(-) diff --git a/cmd/admin-subnet-health.go b/cmd/admin-subnet-health.go index 1d284f11..6d9127dd 100644 --- a/cmd/admin-subnet-health.go +++ b/cmd/admin-subnet-health.go @@ -60,8 +60,9 @@ var adminHealthFlags = append([]cli.Flag{ EnvVar: "MC_HEALTH_DEADLINE,MC_OBD_DEADLINE", }, cli.StringFlag{ - Name: "license", - Usage: "SUBNET license key", + Name: "license", + Usage: "SUBNET license key", + Hidden: true, // deprecated dec 2021 }, cli.IntFlag{ Name: "schedule", @@ -292,13 +293,18 @@ func prepareHealthUploadURL(alias string, clusterName string, filename string, l clusterName = alias } + apiKey := "" if len(license) == 0 { - license = getSubnetLicenseFromConfig(alias) + apiKey = getSubnetAPIKeyFromConfig(alias) + + if len(apiKey) == 0 { + license = getSubnetLicenseFromConfig(alias) + } } uploadURL := subnetHealthUploadURL() - reqURL, headers, e := subnetURLWithAuth(uploadURL, license) + reqURL, headers, e := subnetURLWithAuth(uploadURL, apiKey, license) fatalIf(probe.NewError(e).Trace(uploadURL), "Unable to fetch SUBNET authentication") reqURL = fmt.Sprintf("%s&clustername=%s&filename=%s", reqURL, clusterName, filename) @@ -316,11 +322,7 @@ func uploadHealthReport(alias string, filename string, reqURL string, headers ma return e } - // extract license from response and set it in minio config - subnetLic := gjson.Get(resp, "license").String() - if len(subnetLic) > 0 { - setSubnetLicenseConfig(alias, subnetLic) - } + extractAndSaveAPIKey(alias, resp) // Delete the report after successful upload deleteFile(filename) diff --git a/cmd/admin-subnet-register.go b/cmd/admin-subnet-register.go index 1e2c0094..7a6e4eb4 100644 --- a/cmd/admin-subnet-register.go +++ b/cmd/admin-subnet-register.go @@ -23,10 +23,10 @@ import ( "os" "strings" + "github.com/google/uuid" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" - "github.com/tidwall/gjson" ) var adminSubnetRegisterCmd = cli.Command{ @@ -149,29 +149,23 @@ func registerOffline(clusterRegInfo ClusterRegistrationInfo, alias string) { subnetRegisterPageURL := "https://subnet.min.io/cluster/register" - fmt.Print("The registration token for the cluster " + clusterRegInfo.ClusterName + ` is: + fmt.Print(`Step 1: Use the following token to register your cluster at ` + subnetRegisterPageURL + ` ` + regToken + ` -Please follow these steps to complete the registration: - -1) Copy the registration token shown above -2) Open ` + subnetRegisterPageURL + ` -3) Paste the registration token there and submit the form -4) Copy the license string generated -5) Paste it here: `) +Step 2: Enter the API key generated by SUBNET: `) reader := bufio.NewReader(os.Stdin) - lic, e := reader.ReadString('\n') - fatalIf(probe.NewError(e), "Error in reading license string") - lic = strings.Trim(lic, "\n") + apiKey, e := reader.ReadString('\n') + fatalIf(probe.NewError(e), "Error in reading API key") + apiKey = strings.TrimSpace(apiKey) - if len(lic) > 0 { - e := verifySubnetLicense(lic) - fatalIf(probe.NewError(e), "Invalid license specified:") - setSubnetLicenseConfig(alias, lic) + if len(apiKey) > 0 { + _, e := uuid.Parse(apiKey) + fatalIf(probe.NewError(e), "Invalid API key specified:") + setSubnetAPIKeyConfig(alias, apiKey) } else { - console.Fatalln("Invalid license specified. Please run the command again with a valid SUBNET license to complete registration.") + console.Fatalln("Invalid API key specified. Please run the command again with a valid SUBNET API key to complete registration.") } } @@ -179,9 +173,5 @@ func registerOnline(clusterRegInfo ClusterRegistrationInfo, alias string, cluste resp, e := registerClusterOnSubnet(alias, clusterRegInfo) fatalIf(probe.NewError(e), "Could not register cluster with SUBNET:") - // extract license from response and set it in minio config - subnetLic := gjson.Parse(resp).Get("license").String() - if len(subnetLic) > 0 { - setSubnetLicenseConfig(alias, subnetLic) - } + extractAndSaveAPIKey(alias, resp) } diff --git a/cmd/config-v10.go b/cmd/config-v10.go index 97329d6d..ebf21235 100644 --- a/cmd/config-v10.go +++ b/cmd/config-v10.go @@ -45,6 +45,7 @@ type aliasConfigV10 struct { API string `json:"api"` Path string `json:"path"` License string `json:"license,omitempty"` + APIKey string `json:"apiKey,omitempty"` } // configV10 config version. diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index 71df1530..8f48ca31 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -36,7 +36,6 @@ import ( "github.com/minio/cli" "github.com/minio/madmin-go" "github.com/minio/mc/pkg/probe" - "github.com/minio/pkg/licverifier" "github.com/tidwall/gjson" "golang.org/x/crypto/ssh/terminal" ) @@ -117,13 +116,16 @@ func checkURLReachable(url string) *probe.Error { return nil } -func subnetURLWithAuth(reqURL string, license string) (string, map[string]string, error) { +func subnetURLWithAuth(reqURL string, apiKey string, license string) (string, map[string]string, error) { headers := map[string]string{} - if len(license) > 0 { + if len(apiKey) > 0 { + // Add api key in url for authentication + reqURL = reqURL + "?api_key=" + apiKey + } else if len(license) > 0 { // Add license in url for authentication reqURL = reqURL + "?license=" + license } else { - // License not available in minio/mc config. + // API key not available in minio/mc config. // Ask the user to log in to get auth token token, e := subnetLogin() if e != nil { @@ -201,12 +203,12 @@ func subnetPostReq(reqURL string, payload interface{}, headers map[string]string return subnetReqDo(r, headers) } -func getSubnetLicenseFromConfig(alias string) string { +func getSubnetKeyFromMinIOConfig(alias string, key string) (bool, string) { client, err := newAdminClient(alias) fatalIf(err, "Unable to initialize admin connection.") - if minioConfigSupportsLicense(client) { - sh, pe := client.HelpConfigKV(globalContext, "subnet", "license", false) + if minioConfigSupportsSubnet(client) { + sh, pe := client.HelpConfigKV(globalContext, "subnet", "", false) fatalIf(probe.NewError(pe), "Unable to get config keys for SUBNET") buf, e := client.GetConfigKV(globalContext, "subnet") @@ -215,12 +217,34 @@ func getSubnetLicenseFromConfig(alias string) string { tgt, e := madmin.ParseSubSysTarget(buf, sh) fatalIf(probe.NewError(e), "Unable to parse sub-system target 'subnet'") - lic := tgt.KVS.Get("license") - if len(lic) > 0 { - return lic + for _, kv := range tgt.KVS { + if kv.Key == key { + return true, kv.Value + } } } + return false, "" +} +func getSubnetAPIKeyFromConfig(alias string) string { + // get the subnet api_key config from MinIO if available + supported, apiKey := getSubnetKeyFromMinIOConfig(alias, "api_key") + if supported { + return apiKey + } + + // otherwise get it from mc config + return mcConfig().Aliases[alias].APIKey +} + +func getSubnetLicenseFromConfig(alias string) string { + // get the subnet license config from MinIO if available + supported, lic := getSubnetKeyFromMinIOConfig(alias, "license") + if supported { + return lic + } + + // otherwise get it from mc config return mcConfig().Aliases[alias].License } @@ -231,7 +255,7 @@ func mcConfig() *configV10 { return config } -func minioConfigSupportsLicense(client *madmin.AdminClient) bool { +func minioConfigSupportsSubnet(client *madmin.AdminClient) bool { help, e := client.HelpConfigKV(globalContext, "", "", false) fatalIf(probe.NewError(e), "Unable to get minio config keys") @@ -244,20 +268,21 @@ func minioConfigSupportsLicense(client *madmin.AdminClient) bool { return false } -func setSubnetLicenseConfig(alias string, lic string) { - // Create a new MinIO Admin Client - client, err := newAdminClient(alias) - fatalIf(err, "Unable to initialize admin connection.") +func setSubnetAPIKeyConfig(alias string, apiKey string) { + supported, _ := getSubnetKeyFromMinIOConfig(alias, "api_key") + if supported { + // Create a new MinIO Admin Client + client, err := newAdminClient(alias) + fatalIf(err, "Unable to initialize admin connection.") - if minioConfigSupportsLicense(client) { - configStr := "subnet license=" + lic + configStr := "subnet license= api_key=" + apiKey _, e := client.SetConfigKV(globalContext, configStr) - fatalIf(probe.NewError(e), "Unable to set SUBNET license config on minio") + fatalIf(probe.NewError(e), "Unable to set SUBNET API key config on minio") return } mcCfg := mcConfig() aliasCfg := mcCfg.Aliases[alias] - aliasCfg.License = lic + aliasCfg.APIKey = apiKey setAlias(alias, aliasCfg) } @@ -315,7 +340,7 @@ func subnetLogin() (string, error) { reader := bufio.NewReader(os.Stdin) fmt.Print("SUBNET username: ") username, _ := reader.ReadString('\n') - username = strings.Trim(username, "\n") + username = strings.TrimSpace(username) if len(username) == 0 { return "", errors.New("Username cannot be empty. If you don't have one, please create one from here: " + minioSubscriptionURL) @@ -386,9 +411,14 @@ func getSubnetAccID(headers map[string]string) (string, error) { // registerClusterOnSubnet - Registers the given cluster on SUBNET func registerClusterOnSubnet(alias string, clusterRegInfo ClusterRegistrationInfo) (string, error) { - lic := getSubnetLicenseFromConfig(alias) + apiKey := getSubnetAPIKeyFromConfig(alias) - regURL, headers, e := subnetURLWithAuth(subnetRegisterURL(), lic) + lic := "" + if len(apiKey) == 0 { + lic = getSubnetLicenseFromConfig(alias) + } + + regURL, headers, e := subnetURLWithAuth(subnetRegisterURL(), apiKey, lic) if e != nil { return "", e } @@ -402,25 +432,10 @@ func registerClusterOnSubnet(alias string, clusterRegInfo ClusterRegistrationInf return subnetPostReq(regURL, reqPayload, headers) } -func verifySubnetLicense(lic string) error { - var pemBytes []byte - if globalDevMode { - pemBytes = []byte(`-----BEGIN PUBLIC KEY----- -MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEbo+e1wpBY4tBq9AONKww3Kq7m6QP/TBQ -mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU -9NKlTdQFGF3+aO6jbQ4hX/S5qPyF+a3z ------END PUBLIC KEY-----`) - } else { - pemBytes = []byte(`-----BEGIN PUBLIC KEY----- -MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEaK31xujr6/rZ7ZfXZh3SlwovjC+X8wGq -qkltaKyTLRENd4w3IRktYYCRgzpDLPn/nrf7snV/ERO5qcI7fkEES34IVEr+2Uff -JkO2PfyyAYEO/5dBlPh1Undu9WQl6J7B ------END PUBLIC KEY-----`) +// extractAndSaveAPIKey - extract api key from response and set it in minio config +func extractAndSaveAPIKey(alias string, resp string) { + subnetAPIKey := gjson.Parse(resp).Get("api_key").String() + if len(subnetAPIKey) > 0 { + setSubnetAPIKeyConfig(alias, subnetAPIKey) } - lv, e := licverifier.NewLicenseVerifier(pemBytes) - if e != nil { - return e - } - _, e = lv.Verify(lic) - return e } diff --git a/go.mod b/go.mod index bf9f8123..40a58c14 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/go-ole/go-ole v1.2.6 // indirect github.com/goccy/go-json v0.7.9 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.3.0 github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.0