harmony api compat: support AuthNew

This commit is contained in:
Łukasz Magiera 2024-02-02 18:21:50 +01:00
parent 75c908dd0b
commit 74bf18e8ce
2 changed files with 63 additions and 16 deletions

View File

@ -33,19 +33,19 @@ var _ sectorblocks.SectorBuilder = *new(MinerSealingService)
func harmonyApiInfoToConf(apiInfo string) (config.HarmonyDB, error) { func harmonyApiInfoToConf(apiInfo string) (config.HarmonyDB, error) {
hc := config.HarmonyDB{} hc := config.HarmonyDB{}
// apiInfo - harmony:maddr:user:pass:dbname:host:port // apiInfo - harmony:layer:maddr:user:pass:dbname:host:port
parts := strings.Split(apiInfo, ":") parts := strings.Split(apiInfo, ":")
if len(parts) != 7 { if len(parts) != 8 {
return config.HarmonyDB{}, xerrors.Errorf("invalid harmonydb info '%s'", apiInfo) return config.HarmonyDB{}, xerrors.Errorf("invalid harmonydb info '%s'", apiInfo)
} }
hc.Username = parts[2] hc.Username = parts[3]
hc.Password = parts[3] hc.Password = parts[4]
hc.Database = parts[4] hc.Database = parts[5]
hc.Hosts = []string{parts[5]} hc.Hosts = []string{parts[6]}
hc.Port = parts[6] hc.Port = parts[7]
return hc, nil return hc, nil
} }
@ -64,7 +64,7 @@ func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx
} }
parts := strings.Split(apiInfo, ":") parts := strings.Split(apiInfo, ":")
maddr, err := address.NewFromString(parts[1]) maddr, err := address.NewFromString(parts[2])
if err != nil { if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err) 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) 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 := api.StorageMinerStruct{}
ast.CommonStruct.Internal.AuthNew = lp.AuthNew
ast.Internal.ActorAddress = lp.ActorAddress ast.Internal.ActorAddress = lp.ActorAddress
ast.Internal.WorkerJobs = lp.WorkerJobs ast.Internal.WorkerJobs = lp.WorkerJobs
ast.Internal.SectorsStatus = lp.SectorsStatus ast.Internal.SectorsStatus = lp.SectorsStatus

View File

@ -2,6 +2,12 @@ package fakelm
import ( import (
"context" "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/http"
"net/url" "net/url"
@ -27,15 +33,19 @@ type LMRPCProvider struct {
ssize abi.SectorSize 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{ return &LMRPCProvider{
si: si, si: si,
maddr: maddr, maddr: maddr,
minerID: minerID, minerID: minerID,
ssize: ssize, ssize: ssize,
pi: pi, 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) 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{} var _ MinimalLMApi = &LMRPCProvider{}