From 74bf18e8cee9f04dbc7ce445f35516022de46ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 2 Feb 2024 18:21:50 +0100 Subject: [PATCH] harmony api compat: support AuthNew --- node/modules/storageminer_svc.go | 20 +++++----- provider/lpmarket/fakelm/lmimpl.go | 59 ++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/node/modules/storageminer_svc.go b/node/modules/storageminer_svc.go index 1a14de0f9..eace506be 100644 --- a/node/modules/storageminer_svc.go +++ b/node/modules/storageminer_svc.go @@ -33,19 +33,19 @@ var _ sectorblocks.SectorBuilder = *new(MinerSealingService) func harmonyApiInfoToConf(apiInfo string) (config.HarmonyDB, error) { hc := config.HarmonyDB{} - // apiInfo - harmony:maddr:user:pass:dbname:host:port + // apiInfo - harmony:layer:maddr:user:pass:dbname:host:port parts := strings.Split(apiInfo, ":") - if len(parts) != 7 { + if len(parts) != 8 { return config.HarmonyDB{}, xerrors.Errorf("invalid harmonydb info '%s'", apiInfo) } - hc.Username = parts[2] - hc.Password = parts[3] - hc.Database = parts[4] - hc.Hosts = []string{parts[5]} - hc.Port = parts[6] + hc.Username = parts[3] + hc.Password = parts[4] + hc.Database = parts[5] + hc.Hosts = []string{parts[6]} + hc.Port = parts[7] return hc, nil } @@ -64,7 +64,7 @@ func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx } parts := strings.Split(apiInfo, ":") - maddr, err := address.NewFromString(parts[1]) + maddr, err := address.NewFromString(parts[2]) if err != nil { return nil, xerrors.Errorf("parsing miner address: %w", err) } @@ -83,10 +83,12 @@ func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx return nil, xerrors.Errorf("getting miner info: %w", err) } - lp := fakelm.NewLMRPCProvider(si, maddr, abi.ActorID(mid), mi.SectorSize, pin) + lp := fakelm.NewLMRPCProvider(si, maddr, abi.ActorID(mid), mi.SectorSize, pin, db, parts[1]) ast := api.StorageMinerStruct{} + ast.CommonStruct.Internal.AuthNew = lp.AuthNew + ast.Internal.ActorAddress = lp.ActorAddress ast.Internal.WorkerJobs = lp.WorkerJobs ast.Internal.SectorsStatus = lp.SectorsStatus diff --git a/provider/lpmarket/fakelm/lmimpl.go b/provider/lpmarket/fakelm/lmimpl.go index b5c9370eb..3a93dbd00 100644 --- a/provider/lpmarket/fakelm/lmimpl.go +++ b/provider/lpmarket/fakelm/lmimpl.go @@ -2,6 +2,12 @@ package fakelm import ( "context" + "encoding/base64" + "github.com/BurntSushi/toml" + "github.com/filecoin-project/go-jsonrpc/auth" + "github.com/filecoin-project/lotus/lib/harmony/harmonydb" + "github.com/filecoin-project/lotus/node/config" + "github.com/gbrlsnchs/jwt/v3" "net/http" "net/url" @@ -26,16 +32,20 @@ type LMRPCProvider struct { ssize abi.SectorSize - pi lpmarket.Ingester + pi lpmarket.Ingester + db *harmonydb.DB + confLayer string } -func NewLMRPCProvider(si paths.SectorIndex, maddr address.Address, minerID abi.ActorID, ssize abi.SectorSize, pi lpmarket.Ingester) *LMRPCProvider { +func NewLMRPCProvider(si paths.SectorIndex, maddr address.Address, minerID abi.ActorID, ssize abi.SectorSize, pi lpmarket.Ingester, db *harmonydb.DB, confLayer string) *LMRPCProvider { return &LMRPCProvider{ - si: si, - maddr: maddr, - minerID: minerID, - ssize: ssize, - pi: pi, + si: si, + maddr: maddr, + minerID: minerID, + ssize: ssize, + pi: pi, + db: db, + confLayer: confLayer, } } @@ -275,4 +285,39 @@ func (l *LMRPCProvider) AllocatePieceToSector(ctx context.Context, maddr address return l.pi.AllocatePieceToSector(ctx, maddr, piece, rawSize, source, header) } +func (l *LMRPCProvider) AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) { + var cs []struct { + Config string + } + + err := l.db.Select(ctx, &cs, "select config from harmony_config where title = $1", l.confLayer) + if err != nil { + return nil, err + } + + if len(cs) == 0 { + return nil, xerrors.Errorf("no harmony config found") + } + + lp := config.DefaultLotusProvider() + if _, err := toml.Decode(cs[0].Config, lp); err != nil { + return nil, xerrors.Errorf("decode harmony config: %w", err) + } + + type jwtPayload struct { + Allow []auth.Permission + } + + p := jwtPayload{ + Allow: perms, + } + + sk, err := base64.StdEncoding.DecodeString(lp.Apis.StorageRPCSecret) + if err != nil { + return nil, xerrors.Errorf("decode secret: %w", err) + } + + return jwt.Sign(&p, jwt.NewHS256(sk)) +} + var _ MinimalLMApi = &LMRPCProvider{}