Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subnet package in pkg for license validation #3156

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/admin_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func GetSubnetInfoResponse(session *models.Principal, params subnetApi.SubnetInf
return nil, ErrorWithContext(ctx, ErrSubnetLicenseNotFound)
}

licenseInfo, err := subnet.ParseLicense(client, seededLicense)
licenseInfo, err := getLicenseInfo(*client.Client, seededLicense)
if err != nil {
return nil, ErrorWithContext(ctx, err)
}
Expand Down
16 changes: 14 additions & 2 deletions api/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package api

import (
"net/http"
"os"

"github.com/minio/console/pkg/subnet"
"github.com/minio/pkg/v2/licverifier"
"github.com/minio/pkg/v2/subnet"
)

type SubnetPlan int
Expand All @@ -43,8 +45,18 @@ func (sp SubnetPlan) String() string {

var InstanceLicensePlan = PlanAGPL

func getLicenseInfo(client http.Client, license string) (*licverifier.LicenseInfo, error) {
lv := subnet.LicenseValidator{
Client: client,
ExpiryGracePeriod: 0,
}
lv.Init(getConsoleDevMode())
return lv.ParseLicense(license)
}

func fetchLicensePlan() {
licenseInfo, err := subnet.ParseLicense(GetConsoleHTTPClient("", "127.0.0.1"), os.Getenv(EnvSubnetLicense))
client := GetConsoleHTTPClient("", "127.0.0.1")
licenseInfo, err := getLicenseInfo(*client, os.Getenv(EnvSubnetLicense))
if err != nil {
return
}
Expand Down
44 changes: 0 additions & 44 deletions pkg/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
package subnet

import (
"bytes"
"encoding/json"
"errors"
"fmt"

"github.com/minio/console/pkg/http"

"github.com/minio/pkg/v2/licverifier"

"github.com/minio/console/models"
"github.com/minio/madmin-go/v3"
mc "github.com/minio/mc/cmd"
Expand Down Expand Up @@ -124,46 +120,6 @@ func Register(client http.ClientI, admInfo madmin.InfoMessage, apiKey, token, ac
return nil, errors.New("subnet api key not found")
}

const publicKey = "/downloads/license-pubkey.pem"

// downloadSubnetPublicKey will download the current subnet public key.
func downloadSubnetPublicKey(client http.ClientI) (string, error) {
// Get the public key directly from Subnet
url := fmt.Sprintf("%s%s", subnetBaseURL(), publicKey)
resp, err := client.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return "", err
}
return buf.String(), err
}

// ParseLicense parses the license with the bundle public key and return it's information
func ParseLicense(client http.ClientI, license string) (*licverifier.LicenseInfo, error) {
var publicKeys []string

subnetPubKey, err := downloadSubnetPublicKey(client)
if err != nil {
// there was an issue getting the subnet public key
// use hardcoded public keys instead
publicKeys = OfflinePublicKeys
} else {
publicKeys = append(publicKeys, subnetPubKey)
}

licenseInfo, err := GetLicenseInfoFromJWT(license, publicKeys)
if err != nil {
return nil, err
}

return licenseInfo, nil
}

func GetAPIKey(client http.ClientI, token string) (string, error) {
resp, err := subnetGetReq(client, subnetAPIKeyURL(), subnetAuthHeaders(token))
if err != nil {
Expand Down
Loading