2019-09-18 17:53:48 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-20 04:49:10 +00:00
|
|
|
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-05 22:59:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-09-18 17:53:48 +00:00
|
|
|
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2021-03-25 14:09:50 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-03-25 20:38:57 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-09-18 17:53:48 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 23:25:49 +00:00
|
|
|
var AuthCmd = &cli.Command{
|
2019-09-18 17:53:48 +00:00
|
|
|
Name: "auth",
|
|
|
|
Usage: "Manage RPC permissions",
|
|
|
|
Subcommands: []*cli.Command{
|
2021-03-23 23:25:49 +00:00
|
|
|
AuthCreateAdminToken,
|
|
|
|
AuthApiInfoToken,
|
2019-09-18 17:53:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:25:49 +00:00
|
|
|
var AuthCreateAdminToken = &cli.Command{
|
2019-11-14 20:19:52 +00:00
|
|
|
Name: "create-token",
|
|
|
|
Usage: "Create token",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "perm",
|
|
|
|
Usage: "permission to assign to the token, one of: read, write, sign, admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-09-18 17:53:48 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-12-18 00:52:17 +00:00
|
|
|
napi, closer, err := GetAPI(cctx)
|
2019-09-18 17:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
|
|
|
|
2019-09-18 17:53:48 +00:00
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-11-14 20:19:52 +00:00
|
|
|
if !cctx.IsSet("perm") {
|
2020-03-25 20:38:57 +00:00
|
|
|
return xerrors.New("--perm flag not set")
|
2019-11-14 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
perm := cctx.String("perm")
|
2019-11-18 16:48:58 +00:00
|
|
|
idx := 0
|
2021-03-25 14:09:50 +00:00
|
|
|
for i, p := range api.AllPermissions {
|
2020-05-20 18:23:51 +00:00
|
|
|
if auth.Permission(perm) == p {
|
2019-11-18 16:48:58 +00:00
|
|
|
idx = i + 1
|
2019-11-14 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 16:48:58 +00:00
|
|
|
if idx == 0 {
|
2021-03-25 14:09:50 +00:00
|
|
|
return fmt.Errorf("--perm flag has to be one of: %s", api.AllPermissions)
|
2019-11-14 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 20:37:11 +00:00
|
|
|
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
|
2021-03-25 14:09:50 +00:00
|
|
|
token, err := napi.AuthNew(ctx, api.AllPermissions[:idx])
|
2019-09-18 17:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Log in audit log when it is implemented
|
|
|
|
|
|
|
|
fmt.Println(string(token))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-03-25 20:38:57 +00:00
|
|
|
|
2021-03-23 23:25:49 +00:00
|
|
|
var AuthApiInfoToken = &cli.Command{
|
2020-03-25 20:38:57 +00:00
|
|
|
Name: "api-info",
|
|
|
|
Usage: "Get token with API info required to connect to this node",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "perm",
|
|
|
|
Usage: "permission to assign to the token, one of: read, write, sign, admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if !cctx.IsSet("perm") {
|
2021-03-15 12:42:57 +00:00
|
|
|
return xerrors.New("--perm flag not set, use with one of: read, write, sign, admin")
|
2020-03-25 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
perm := cctx.String("perm")
|
|
|
|
idx := 0
|
2021-03-25 14:09:50 +00:00
|
|
|
for i, p := range api.AllPermissions {
|
2020-05-20 18:23:51 +00:00
|
|
|
if auth.Permission(perm) == p {
|
2020-03-25 20:38:57 +00:00
|
|
|
idx = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx == 0 {
|
2021-03-25 14:09:50 +00:00
|
|
|
return fmt.Errorf("--perm flag has to be one of: %s", api.AllPermissions)
|
2020-03-25 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
|
2021-03-25 14:09:50 +00:00
|
|
|
token, err := napi.AuthNew(ctx, api.AllPermissions[:idx])
|
2020-03-25 20:38:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ti, ok := cctx.App.Metadata["repoType"]
|
|
|
|
if !ok {
|
2021-07-29 11:37:29 +00:00
|
|
|
log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?")
|
2022-02-16 11:26:41 +00:00
|
|
|
ti = repo.FullNode
|
2020-03-25 20:38:57 +00:00
|
|
|
}
|
|
|
|
t, ok := ti.(repo.RepoType)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("repoType type does not match the type of repo.RepoType")
|
|
|
|
}
|
|
|
|
|
2020-03-25 21:15:10 +00:00
|
|
|
ainfo, err := GetAPIInfo(cctx, t)
|
2020-03-25 20:38:57 +00:00
|
|
|
if err != nil {
|
2021-08-16 11:33:08 +00:00
|
|
|
return xerrors.Errorf("could not get API info for %s: %w", t, err)
|
2020-03-25 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Log in audit log when it is implemented
|
|
|
|
|
2022-03-03 15:45:11 +00:00
|
|
|
currentEnv, _, _ := t.APIInfoEnvVars()
|
2021-07-29 09:55:37 +00:00
|
|
|
fmt.Printf("%s=%s:%s\n", currentEnv, string(token), ainfo.Addr)
|
2020-03-25 20:38:57 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|