Fix secret loading in tests

This commit is contained in:
Łukasz Magiera 2019-07-23 22:37:06 +02:00
parent 5be0ecadd1
commit b41763d8cf
2 changed files with 40 additions and 42 deletions

View File

@ -2,9 +2,6 @@ package node
import (
"context"
"crypto/rand"
"io"
"io/ioutil"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
@ -14,7 +11,6 @@ import (
"github.com/filecoin-project/go-lotus/miner"
"github.com/filecoin-project/go-lotus/node/client"
"github.com/filecoin-project/go-lotus/node/modules"
"github.com/filecoin-project/go-lotus/node/repo"
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs/go-cid"
@ -36,9 +32,7 @@ type API struct {
PubSub *pubsub.PubSub
Mpool *chain.MessagePool
Wallet *chain.Wallet
Keystore types.KeyStore
APISecret *modules.APIAlg
Repo repo.LockedRepo
}
type jwtPayload struct {
@ -55,44 +49,11 @@ func (a *API) AuthVerify(ctx context.Context, token string) ([]string, error) {
}
func (a *API) AuthNew(ctx context.Context, perms []string) ([]byte, error) {
key, err := a.Keystore.Get(modules.JWTSecretName)
if err != nil {
log.Warn("Generating new API secret")
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
if err != nil {
return nil, err
}
key = types.KeyInfo{
Type: "jwt-hmac-secret",
PrivateKey: sk,
}
if err := a.Keystore.Put(modules.JWTSecretName, key); err != nil {
return nil, xerrors.Errorf("writing API secret: %w", err)
}
// TODO: make this configurable
p := jwtPayload{
Allow: api.AllPermissions,
}
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
if err != nil {
return nil, err
}
if err := a.Repo.SetAPIToken(cliToken); err != nil {
return nil, err
}
}
p := jwtPayload{
Allow: perms, // TODO: consider checking validity
}
return jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret))
}
func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {

View File

@ -2,8 +2,12 @@ package modules
import (
"context"
"crypto/rand"
"github.com/filecoin-project/go-lotus/api"
"github.com/gbrlsnchs/jwt/v3"
"golang.org/x/xerrors"
"io"
"io/ioutil"
"path/filepath"
"github.com/ipfs/go-bitswap"
@ -76,11 +80,44 @@ const JWTSecretName = "auth-jwt-private"
type APIAlg jwt.HMACSHA
func APISecret(keystore types.KeyStore) (*APIAlg, error) {
type jwtPayload struct {
Allow []string
}
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) {
key, err := keystore.Get(JWTSecretName)
if err != nil {
return nil, xerrors.Errorf("couldn't get JWT secret: %w", err)
log.Warn("Generating new API secret")
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
if err != nil {
return nil, err
}
key = types.KeyInfo{
Type: "jwt-hmac-secret",
PrivateKey: sk,
}
if err := keystore.Put(JWTSecretName, key); err != nil {
return nil, xerrors.Errorf("writing API secret: %w", err)
}
// TODO: make this configurable
p := jwtPayload{
Allow: api.AllPermissions,
}
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
if err != nil {
return nil, err
}
if err := lr.SetAPIToken(cliToken); err != nil {
return nil, err
}
}
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
}