Renew license online if license file not passed (#4429)

This commit is contained in:
Shireesh Anjal
2023-01-09 01:19:23 -08:00
committed by GitHub
parent e6e2b57022
commit 7c6499b8a8
2 changed files with 89 additions and 33 deletions
+39 -22
View File
@@ -20,7 +20,6 @@ package cmd
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
@@ -35,7 +34,7 @@ var licenseUpdateCmd = cli.Command{
OnUsageError: onUsageError,
Action: mainLicenseUpdate,
Before: setGlobalsFromContext,
Flags: append(supportGlobalFlags, subnetCommonFlags...),
Flags: supportGlobalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
@@ -49,6 +48,8 @@ FLAGS:
EXAMPLES:
1. Update license for cluster with alias 'play' from the file license.key
{{.Prompt}} {{.HelpName}} play license.key
2. Update (renew) license for already registered cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play
`,
}
@@ -73,21 +74,51 @@ func (li licUpdateMessage) JSON() string {
}
func mainLicenseUpdate(ctx *cli.Context) error {
if len(ctx.Args()) != 2 {
args := ctx.Args()
argsLen := len(args)
if argsLen > 2 || argsLen < 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
console.SetColor(licUpdateMsgTag, color.New(color.FgGreen, color.Bold))
aliasedURL := ctx.Args().Get(0)
alias, _ := initSubnetConnectivity(ctx, aliasedURL, false)
aliasedURL := args.Get(0)
alias, _ := url2Alias(aliasedURL)
licFile := ctx.Args().Get(1)
if argsLen == 2 {
licFile := args.Get(1)
printMsg(performLicenseUpdate(licFile, alias))
return nil
}
printMsg(performLicenseUpdate(licFile, alias))
// renew the license
printMsg(performLicenseRenew(alias))
return nil
}
func performLicenseRenew(alias string) licUpdateMessage {
apiKey, _, e := getSubnetCreds(alias)
fatalIf(probe.NewError(e), "Error getting subnet creds")
if len(apiKey) == 0 {
errMsg := fmt.Sprintf("Please register the cluster first by running 'mc license register %s'", alias)
fatal(errDummy().Trace(), errMsg)
}
renewURL := subnetLicenseRenewURL()
headers := subnetAPIKeyAuthHeaders(apiKey)
headers.addDeploymentIDHeader(alias)
resp, e := subnetPostReq(renewURL, nil, headers)
fatalIf(probe.NewError(e), "Error renewing license for %s", alias)
extractAndSaveSubnetCreds(alias, resp)
return licUpdateMessage{
Alias: alias,
Status: "success",
}
}
func performLicenseUpdate(licFile string, alias string) licUpdateMessage {
lum := licUpdateMessage{
Alias: alias,
@@ -98,21 +129,7 @@ func performLicenseUpdate(licFile string, alias string) licUpdateMessage {
fatalIf(probe.NewError(e), fmt.Sprintf("Unable to read license file %s", licFile))
lic := string(licBytes)
li, e := parseLicense(lic)
fatalIf(probe.NewError(e), fmt.Sprintf("Error parsing license from %s", licFile))
if li.ExpiresAt.Before(time.Now()) {
fatalIf(errDummy().Trace(), fmt.Sprintf("License has expired on %s", li.ExpiresAt))
}
if li.DeploymentID != getAdminInfo(alias).DeploymentID {
fatalIf(errDummy().Trace(), fmt.Sprintf("License is invalid for the deployment %s", alias))
}
setSubnetLicense(alias, lic)
if len(li.APIKey) > 0 {
setSubnetAPIKey(alias, li.APIKey)
}
validateAndSaveLic(lic, alias, true)
return lum
}
+50 -11
View File
@@ -43,9 +43,10 @@ import (
)
const (
subnetRespBodyLimit = 1 << 20 // 1 MiB
minioSubscriptionURL = "https://min.io/subscription"
subnetPublicKeyPath = "/downloads/license-pubkey.pem"
subnetRespBodyLimit = 1 << 20 // 1 MiB
minioSubscriptionURL = "https://min.io/subscription"
subnetPublicKeyPath = "/downloads/license-pubkey.pem"
minioDeploymentIDHeader = "x-minio-deployment-id"
)
var (
@@ -102,6 +103,10 @@ func subnetUnregisterURL(depID string) string {
return subnetBaseURL() + "/api/cluster/unregister?deploymentId=" + depID
}
func subnetLicenseRenewURL() string {
return subnetBaseURL() + "/api/cluster/renew-license"
}
func subnetOfflineRegisterURL(regToken string) string {
return subnetBaseURL() + "/cluster/register?token=" + regToken
}
@@ -151,6 +156,12 @@ func subnetURLWithAuth(reqURL string, apiKey string) (string, map[string]string,
return reqURL, subnetAPIKeyAuthHeaders(apiKey), nil
}
type subnetHeaders map[string]string
func (h subnetHeaders) addDeploymentIDHeader(alias string) {
h[minioDeploymentIDHeader] = getAdminInfo(alias).DeploymentID
}
func subnetTokenAuthHeaders(authToken string) map[string]string {
return map[string]string{"Authorization": "Bearer " + authToken}
}
@@ -159,7 +170,7 @@ func subnetLicenseAuthHeaders(lic string) map[string]string {
return map[string]string{"x-subnet-license": lic}
}
func subnetAPIKeyAuthHeaders(apiKey string) map[string]string {
func subnetAPIKeyAuthHeaders(apiKey string) subnetHeaders {
return map[string]string{"x-subnet-api-key": apiKey}
}
@@ -580,9 +591,44 @@ func unregisterClusterFromSubnet(alias string, depID string, apiKey string) erro
return nil
}
// validateAndSaveLic - validates the given license in minio config
// If the license contains api key and the saveApiKey arg is true,
// api key is also saved in the minio config
func validateAndSaveLic(lic string, alias string, saveAPIKey bool) string {
li, e := parseLicense(lic)
fatalIf(probe.NewError(e), "Error parsing license")
if li.ExpiresAt.Before(time.Now()) {
fatalIf(errDummy().Trace(), fmt.Sprintf("License has expired on %s", li.ExpiresAt))
}
if li.DeploymentID != getAdminInfo(alias).DeploymentID {
fatalIf(errDummy().Trace(), fmt.Sprintf("License is invalid for the deployment %s", alias))
}
setSubnetLicense(alias, lic)
if len(li.APIKey) > 0 && saveAPIKey {
setSubnetAPIKey(alias, li.APIKey)
}
return li.APIKey
}
// extractAndSaveSubnetCreds - extract license from response and set it in minio config
func extractAndSaveSubnetCreds(alias string, resp string) (string, string, error) {
parsedResp := gjson.Parse(resp)
lic, e := extractSubnetCred("license", parsedResp)
if e != nil {
return "", "", e
}
if len(lic) > 0 {
apiKey := validateAndSaveLic(lic, alias, true)
if len(apiKey) > 0 {
return apiKey, lic, nil
}
}
apiKey, e := extractSubnetCred("api_key", parsedResp)
if e != nil {
return "", "", e
@@ -591,13 +637,6 @@ func extractAndSaveSubnetCreds(alias string, resp string) (string, string, error
setSubnetAPIKey(alias, apiKey)
}
lic, e := extractSubnetCred("license", parsedResp)
if e != nil {
return "", "", e
}
if len(lic) > 0 {
setSubnetLicense(alias, lic)
}
return apiKey, lic, nil
}