From edd633ff2f782b3dc033910d4959e9887806ecd4 Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:14:23 +0530 Subject: [PATCH] Save api key passed as argument in config (#4157) When the account api key is passed to `mc license register` via the `--api-key` flag and after the registration is successful, save it to minio config, so that it can be used automatically in subsequent calls. Also stop extracting and storing api key from the response of SUBNET register api. --- cmd/license-register.go | 6 +++- cmd/license-update.go | 2 +- cmd/subnet-utils.go | 69 +++++++++++++++++++++++++---------------- go.sum | 3 ++ 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/cmd/license-register.go b/cmd/license-register.go index 5c820fcd..7db03755 100644 --- a/cmd/license-register.go +++ b/cmd/license-register.go @@ -220,13 +220,17 @@ func registerOnline(clusterRegInfo ClusterRegistrationInfo, alias string, accAPI if len(accAPIKey) > 0 { resp, e = registerClusterWithSubnetCreds(clusterRegInfo, accAPIKey, "") + if e == nil { + // save the api key in config + setSubnetAPIKey(alias, accAPIKey) + } } else { resp, e = registerClusterOnSubnet(alias, clusterRegInfo) } fatalIf(probe.NewError(e), "Could not register cluster with SUBNET:") - extractAndSaveSubnetCreds(alias, resp) + extractAndSaveLicense(alias, resp) } func getAdminInfo(aliasedURL string) madmin.InfoMessage { diff --git a/cmd/license-update.go b/cmd/license-update.go index 2d2b930c..9ff0a2cf 100644 --- a/cmd/license-update.go +++ b/cmd/license-update.go @@ -113,6 +113,6 @@ func performLicenseUpdate(licFile string, alias string, airgap bool) licUpdateMe fatalIf(errDummy().Trace(), fmt.Sprintf("License is invalid for the deployment %s", alias)) } - setSubnetCreds(alias, "", lic) + setSubnetLicense(alias, lic) return lum } diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index f192263d..1b0be5f0 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -320,42 +320,58 @@ func minioConfigSupportsSubSys(client *madmin.AdminClient, subSys string) bool { return false } -func setSubnetCredsInMcConfig(alias string, apiKey string, lic string) { - mcCfg := mcConfig() - aliasCfg := mcCfg.Aliases[alias] +func setSubnetAPIKeyInMcConfig(alias string, apiKey string) { + aliasCfg := mcConfig().Aliases[alias] if len(apiKey) > 0 { aliasCfg.APIKey = apiKey } + + setAlias(alias, aliasCfg) +} + +func setSubnetLicenseInMcConfig(alias string, lic string) { + aliasCfg := mcConfig().Aliases[alias] 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 - } - +func setSubnetConfig(alias string, subKey string, cfgVal string) { client, err := newAdminClient(alias) fatalIf(err, "Unable to initialize admin connection.") - configStr := "subnet" - if apiKeySupported && len(apiKey) > 0 { - configStr += " api_key=" + apiKey + cfgKey := "subnet " + subKey + _, e := client.SetConfigKV(globalContext, cfgKey+"="+cfgVal) + fatalIf(probe.NewError(e), "Unable to set "+cfgKey+" config on MinIO") +} + +func setSubnetAPIKey(alias string, apiKey string) { + if len(apiKey) == 0 { + fatal(errDummy().Trace(), "API Key must not be empty.") } - if licSupported && len(lic) > 0 { - configStr += " license=" + lic + + _, apiKeySupported := getKeyFromMinIOConfig(alias, "subnet", "api_key") + if !apiKeySupported { + setSubnetAPIKeyInMcConfig(alias, apiKey) + return } - _, e := client.SetConfigKV(globalContext, configStr) - fatalIf(probe.NewError(e), "Unable to set SUBNET license/api_key config on MinIO") + + setSubnetConfig(alias, "api_key", apiKey) +} + +func setSubnetLicense(alias string, lic string) { + if len(lic) == 0 { + fatal(errDummy().Trace(), "License must not be empty.") + } + + _, licSupported := getKeyFromMinIOConfig(alias, "subnet", "license") + if !licSupported { + setSubnetLicenseInMcConfig(alias, lic) + return + } + + setSubnetConfig(alias, "license", lic) } func getClusterRegInfo(admInfo madmin.InfoMessage, clusterName string) ClusterRegistrationInfo { @@ -523,12 +539,11 @@ func getSubnetCreds(alias string) (string, string, error) { return apiKey, lic, nil } -// 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() +// extractAndSaveLicense - extract license from response and set it in minio config +func extractAndSaveLicense(alias string, resp string) { subnetLic := gjson.Parse(resp).Get("license").String() - if len(subnetAPIKey) > 0 || len(subnetLic) > 0 { - setSubnetCreds(alias, subnetAPIKey, subnetLic) + if len(subnetLic) > 0 { + setSubnetLicense(alias, subnetLic) } } diff --git a/go.sum b/go.sum index aa98ddff..912cbc3d 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= @@ -271,6 +272,7 @@ github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s= github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4= github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= @@ -340,6 +342,7 @@ github.com/minio/colorjson v1.0.2/go.mod h1:JWxcL2n8T8JVf+NY6awl6kn5nK49aAzHOeQE github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY= github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw= github.com/minio/madmin-go v1.3.5/go.mod h1:vGKGboQgGIWx4DuDUaXixjlIEZOCIp6ivJkQoiVaACc= +github.com/minio/madmin-go v1.3.11/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98= github.com/minio/madmin-go v1.4.9 h1:kFJhzWlDomaK1l2RT6yMRVDTYBUTcfn7sT5c8PCM/3Q= github.com/minio/madmin-go v1.4.9/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=