Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: store enterprise license into the setting table (#1405)
ecmadao538ed2d56a8d
diff --git a/api/setting.go b/api/setting.goindex b94e108e4c..184687b2f5 100644--- a/api/setting.go+++ b/api/setting.go@@ -14,6 +14,8 @@ const (SettingBrandingLogo SettingName = "bb.branding.logo"// SettingWorkspaceID is the setting name for workspace identifier.SettingWorkspaceID SettingName = "bb.workspace.id"+ // SettingEnterpriseLicense is the setting name for enterprise license.+ SettingEnterpriseLicense SettingName = "bb.enterprise.license")// Setting is the API message for a setting.diff --git a/enterprise/api/license.go b/enterprise/api/license.goindex 3948b33489..19f89c7774 100644--- a/enterprise/api/license.go+++ b/enterprise/api/license.go@@ -53,7 +53,7 @@ func (l *License) validPlanType() error {// LicenseService is the service for enterprise license.type LicenseService interface {// StoreLicense will store license into file.- StoreLicense(tokenString string) error+ StoreLicense(patch *SubscriptionPatch) error// LoadLicense will load license from file and validate it.LoadLicense() (*License, error)}diff --git a/enterprise/api/subscription.go b/enterprise/api/subscription.goindex 5b631365d9..9f6caa5c41 100644--- a/enterprise/api/subscription.go+++ b/enterprise/api/subscription.go@@ -4,7 +4,8 @@ import "github.com/bytebase/bytebase/api"// SubscriptionPatch is the API message for update the subscription.type SubscriptionPatch struct {- License string `jsonapi:"attr,license"`+ UpdaterID int+ License string `jsonapi:"attr,license"`}// Subscription is the API message for subscription.diff --git a/enterprise/config/config.go b/enterprise/config/config.goindex dfbf33c4fc..97067c1cbd 100644--- a/enterprise/config/config.go+++ b/enterprise/config/config.go@@ -24,8 +24,6 @@ type Config struct {Audience string// MinimumInstance is the minimum instance count in each plan.MinimumInstance int- // StorePath is the file path to store license.- StorePath string}const (@@ -40,7 +38,7 @@ const ()// NewConfig will create a new enterprise config instance.-func NewConfig(l *zap.Logger, dataDir string, mode common.ReleaseMode) (*Config, error) {+func NewConfig(l *zap.Logger, mode common.ReleaseMode) (*Config, error) {l.Info("get project env", zap.String("env", string(mode)))filename := fmt.Sprintf("keys/%s.pub.pem", mode)@@ -50,17 +48,11 @@ func NewConfig(l *zap.Logger, dataDir string, mode common.ReleaseMode) (*Config,}l.Info("load public pem", zap.String("file", filename))- storefile := "license"- if mode != common.ReleaseModeProd {- storefile = fmt.Sprintf("license_%s", mode)- }-return &Config{PublicKey: string(licensePubKey),Version: keyID,Issuer: issuer,Audience: audience,MinimumInstance: minimumInstance,- StorePath: fmt.Sprintf("%s/%s", dataDir, storefile),}, nil}diff --git a/enterprise/service/license.go b/enterprise/service/license.goindex 1ab97678be..8fe5370899 100644--- a/enterprise/service/license.go+++ b/enterprise/service/license.go@@ -1,13 +1,14 @@package serviceimport (+ "context""fmt"- "io/ioutil""github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common"enterpriseAPI "github.com/bytebase/bytebase/enterprise/api""github.com/bytebase/bytebase/enterprise/config"+ "github.com/bytebase/bytebase/store""github.com/golang-jwt/jwt/v4""go.uber.org/zap")@@ -16,6 +17,7 @@ import (type LicenseService struct {l *zap.Loggerconfig *config.Config+ store *store.Store}// Claims creates a struct that will be encoded to a JWT.@@ -28,26 +30,27 @@ type Claims struct {}// NewLicenseService will create a new enterprise license service.-func NewLicenseService(l *zap.Logger, dataDir string, mode common.ReleaseMode) (*LicenseService, error) {- config, err := config.NewConfig(l, dataDir, mode)+func NewLicenseService(l *zap.Logger, mode common.ReleaseMode, store *store.Store) (*LicenseService, error) {+ config, err := config.NewConfig(l, mode)if err != nil {return nil, err}return &LicenseService{+ store: store,config: config,l: l,}, nil}// StoreLicense will store license into file.-func (s *LicenseService) StoreLicense(tokenString string) error {- if tokenString != "" {- if _, err := s.parseLicense(tokenString); err != nil {+func (s *LicenseService) StoreLicense(patch *enterpriseAPI.SubscriptionPatch) error {+ if patch.License != "" {+ if _, err := s.parseLicense(patch.License); err != nil {return nil}}- return s.writeLicense(tokenString)+ return s.writeLicense(patch)}// LoadLicense will load license from file and validate it.@@ -137,19 +140,30 @@ func (s *LicenseService) parseClaims(claims *Claims) (*enterpriseAPI.License, er}func (s *LicenseService) readLicense() (string, error) {- token, err := ioutil.ReadFile(s.config.StorePath)- if err != nil {+ settingName := api.SettingEnterpriseLicense+ ctx := context.Background()+ settings, err := s.store.FindSetting(ctx, &api.SettingFind{+ Name: &settingName,+ })++ if len(settings) == 0 {return "", common.Errorf(common.NotFound,- fmt.Errorf("cannot read license from %s, error %w", s.config.StorePath, err),+ fmt.Errorf("cannot find license with error %w", err),)}- return string(token), nil+ return settings[0].Value, nil}-func (s *LicenseService) writeLicense(token string) error {- return ioutil.WriteFile(s.config.StorePath, []byte(token), 0644)+func (s *LicenseService) writeLicense(patch *enterpriseAPI.SubscriptionPatch) error {+ ctx := context.Background()+ _, err := s.store.PatchSetting(ctx, &api.SettingPatch{+ UpdaterID: patch.UpdaterID,+ Name: api.SettingEnterpriseLicense,+ Value: patch.License,+ })+ return err}func convertPlanType(candidate string) (api.PlanType, error) {diff --git a/server/server.go b/server/server.goindex 7d25d94fc0..9d0a5fbeb3 100644--- a/server/server.go+++ b/server/server.go@@ -306,7 +306,7 @@ func NewServer(ctx context.Context, prof Profile, logger *zap.Logger, loggerLeve}s.ActivityManager = NewActivityManager(s, storeInstance)- s.LicenseService, err = enterpriseService.NewLicenseService(logger, prof.DataDir, prof.Mode)+ s.LicenseService, err = enterpriseService.NewLicenseService(logger, prof.Mode, s.store)if err != nil {return nil, fmt.Errorf("failed to create license service, error: %w", err)}@@ -376,6 +376,16 @@ func (server *Server) initSetting(ctx context.Context, store *store.Store) (*con}conf.workspaceID = workspaceSetting.Value+ // initial license+ if _, err = store.CreateSettingIfNotExist(ctx, &api.SettingCreate{+ CreatorID: api.SystemBotID,+ Name: api.SettingEnterpriseLicense,+ Value: "",+ Description: "Enterprise license",+ }); err != nil {+ return nil, err+ }+return conf, nil}diff --git a/server/subscription.go b/server/subscription.goindex 22a0061c31..ac56cc5d45 100644--- a/server/subscription.go+++ b/server/subscription.go@@ -27,8 +27,9 @@ func (s *Server) registerSubscriptionRoutes(g *echo.Group) {if err := jsonapi.UnmarshalPayload(c.Request().Body, patch); err != nil {return echo.NewHTTPError(http.StatusBadRequest, "Malformed create subscription request").SetInternal(err)}+ patch.UpdaterID = c.Get(getPrincipalIDContextKey()).(int)- if err := s.LicenseService.StoreLicense(patch.License); err != nil {+ if err := s.LicenseService.StoreLicense(patch); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to store license").SetInternal(err)}