2024-09-08 15:28:22: merge [master:33b4e2d9651dc592eae18e258984b11b35cc0a5e];
This commit is contained in:
1 parent
6f45b1861f
commit
02df1a6546
36 files changed
+653
-3262
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package baseline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/entity"
|
||||
)
|
||||
|
||||
type Transaction interface {
|
||||
CreateDomain(ctx context.Context, name string, desc string, systemId int) (*entity.Domain, error)
|
||||
CreateService(ctx context.Context, name string, desc string, domainId int) (*entity.Service, error)
|
||||
CreateApplication(ctx context.Context, name string, desc string, serviceId int, appType string) (*entity.Application, error)
|
||||
UpsertAccessList(ctx context.Context, e entity.AccessList) (int, error)
|
||||
SaveToken(ctx context.Context, token string, appId int, expireTime int) (*entity.Token, error)
|
||||
GetTokenById(ctx context.Context, token string) (*entity.Token, error)
|
||||
TryLock(ctx context.Context, key string) (bool, error)
|
||||
}
|
||||
|
||||
type TxRunner interface {
|
||||
BaselineTx(ctx context.Context, tx func(ctx context.Context, tx Transaction) error) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg conf.Baseline
|
||||
txRunner TxRunner
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func NewService(cfg conf.Baseline, txRunner TxRunner, logger log.Logger) Service {
|
||||
return Service{
|
||||
cfg: cfg,
|
||||
txRunner: txRunner,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Service) Do(ctx context.Context) error {
|
||||
ctx = log.ToContext(ctx, log.String("worker", "baseline"))
|
||||
if s.cfg.InitialAdminUiToken == "" {
|
||||
s.logger.Info(ctx, "initial admin ui token is empty, skip baseline")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := s.txRunner.BaselineTx(ctx, s.transaction)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "run baseline transaction")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Service) transaction(ctx context.Context, tx Transaction) error {
|
||||
locked, err := tx.TryLock(ctx, "isp-system-service.baseline")
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "try lock isp-system-service.baseline")
|
||||
}
|
||||
if !locked {
|
||||
s.logger.Info(ctx, "baseline is locked, skip baseline")
|
||||
return nil
|
||||
}
|
||||
|
||||
token, err := tx.GetTokenById(ctx, s.cfg.InitialAdminUiToken)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get token by id")
|
||||
}
|
||||
if token != nil {
|
||||
s.logger.Info(ctx, "initial admin ui token is exist, skip baseline")
|
||||
return nil
|
||||
}
|
||||
|
||||
s.logger.Info(ctx, "initial admin ui token is empty, run baseline")
|
||||
|
||||
domain, err := tx.CreateDomain(ctx, "root", "", 1)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "create domain")
|
||||
}
|
||||
|
||||
service, err := tx.CreateService(ctx, "rootService", "", domain.Id)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "create service")
|
||||
}
|
||||
|
||||
app, err := tx.CreateApplication(ctx, "admin", "", service.Id, "SYSTEM")
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "create application")
|
||||
}
|
||||
|
||||
_, err = tx.SaveToken(ctx, s.cfg.InitialAdminUiToken, app.Id, -1)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "save token")
|
||||
}
|
||||
|
||||
_, err = tx.UpsertAccessList(ctx, entity.AccessList{
|
||||
AppId: app.Id,
|
||||
Method: "admin/auth/login",
|
||||
Value: true,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "upsert access list")
|
||||
}
|
||||
|
||||
s.logger.Info(ctx, "baseline done")
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -15,7 +15,7 @@ func NewTokenSource() TokenSource {
|
||||
}
|
||||
|
||||
func (s TokenSource) CreateApplicationToken() (string, error) {
|
||||
cryptoRand := make([]byte, 128) //nolint:gomnd
|
||||
cryptoRand := make([]byte, 128) //nolint:mnd
|
||||
_, err := rand.Read(cryptoRand)
|
||||
if err != nil {
|
||||
return "", errors.WithMessage(err, "crypto/rand read")
|
||||
|
||||
Reference in new issue
Block a user