lotus/cli/auth.go

135 lines
2.8 KiB
Go
Raw Permalink Normal View History

2019-09-18 17:53:48 +00:00
package cli
import (
"fmt"
"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{
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)
if !cctx.IsSet("perm") {
2020-03-25 20:38:57 +00:00
return xerrors.New("--perm flag not set")
}
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-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)
}
// 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") {
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 {
log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?")
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
currentEnv, _, _ := t.APIInfoEnvVars()
fmt.Printf("%s=%s:%s\n", currentEnv, string(token), ainfo.Addr)
2020-03-25 20:38:57 +00:00
return nil
},
}