2022-08-25 08:06:29 -04:00
|
|
|
//go:build enterprise
|
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-25 08:06:29 -04:00
|
|
|
package license
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-12 07:38:50 -04:00
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2022-08-25 08:06:29 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2022-08-25 08:06:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Checker struct {
|
|
|
|
quotaChecker QuotaChecker
|
|
|
|
fileHandler file.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChecker(quotaChecker QuotaChecker, fileHandler file.Handler) *Checker {
|
|
|
|
return &Checker{
|
|
|
|
quotaChecker: quotaChecker,
|
|
|
|
fileHandler: fileHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckLicense tries to read the license file and contact license server
|
|
|
|
// to fetch quota information.
|
|
|
|
// If no license file is found, community license is assumed.
|
2022-09-08 05:02:04 -04:00
|
|
|
func (c *Checker) CheckLicense(ctx context.Context, provider cloudprovider.Provider, providerCfg config.ProviderConfig, printer func(string, ...any)) error {
|
2022-08-25 08:06:29 -04:00
|
|
|
licenseID, err := FromFile(c.fileHandler, constants.LicenseFilename)
|
2022-09-12 07:38:50 -04:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2022-09-25 14:31:09 -04:00
|
|
|
printer("Using community license.\n")
|
2022-08-25 08:06:29 -04:00
|
|
|
licenseID = CommunityLicense
|
2022-09-12 07:38:50 -04:00
|
|
|
} else if err != nil {
|
|
|
|
printer("Error: %v\nContinuing with community license.\n", err)
|
|
|
|
licenseID = CommunityLicense
|
2022-08-25 08:06:29 -04:00
|
|
|
} else {
|
|
|
|
printer("Constellation license found!\n")
|
|
|
|
}
|
2022-09-08 05:02:04 -04:00
|
|
|
providerStr := provider.String()
|
|
|
|
if provider == cloudprovider.Azure {
|
|
|
|
if *providerCfg.Azure.ConfidentialVM {
|
|
|
|
providerStr = "azure-cvm"
|
|
|
|
} else {
|
|
|
|
providerStr = "azure-tl"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:06:29 -04:00
|
|
|
quotaResp, err := c.quotaChecker.QuotaCheck(ctx, QuotaCheckRequest{
|
2022-09-08 05:02:04 -04:00
|
|
|
License: licenseID,
|
|
|
|
Action: Init,
|
|
|
|
Provider: providerStr,
|
2022-08-25 08:06:29 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
printer("Unable to contact license server.\n")
|
|
|
|
printer("Please keep your vCPU quota in mind.\n")
|
2022-09-12 07:38:50 -04:00
|
|
|
} else if licenseID == CommunityLicense {
|
|
|
|
printer("For details, see https://docs.edgeless.systems/constellation/overview/license\n")
|
2022-08-25 08:06:29 -04:00
|
|
|
} else {
|
|
|
|
printer("Please keep your vCPU quota (%d) in mind.\n", quotaResp.Quota)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|