lotus/cli/auth.go

136 lines
2.8 KiB
Go
Raw Normal View History

2019-09-18 17:53:48 +00:00
package cli
import (
"fmt"
2020-03-25 20:38:57 +00:00
"golang.org/x/xerrors"
"github.com/urfave/cli/v2"
2019-09-18 17:53:48 +00:00
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/go-jsonrpc/auth"
2019-12-09 17:08:32 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
2020-03-25 20:38:57 +00:00
"github.com/filecoin-project/lotus/node/repo"
2019-09-18 17:53:48 +00:00
)
var authCmd = &cli.Command{
Name: "auth",
Usage: "Manage RPC permissions",
Subcommands: []*cli.Command{
authCreateAdminToken,
2020-03-25 20:38:57 +00:00
authApiInfoToken,
2019-09-18 17:53:48 +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
2019-12-09 17:08:32 +00:00
for i, p := range apistruct.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 {
2019-12-09 17:08:32 +00:00
return fmt.Errorf("--perm flag has to be one of: %s", apistruct.AllPermissions)
}
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
2019-12-09 17:08:32 +00:00
token, err := napi.AuthNew(ctx, apistruct.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
var authApiInfoToken = &cli.Command{
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")
}
perm := cctx.String("perm")
idx := 0
for i, p := range apistruct.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 {
return fmt.Errorf("--perm flag has to be one of: %s", apistruct.AllPermissions)
}
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
token, err := napi.AuthNew(ctx, apistruct.AllPermissions[:idx])
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 GetAPI?")
ti = repo.FullNode
}
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 {
2020-03-25 21:15:10 +00:00
return xerrors.Errorf("could not get API info: %w", err)
2020-03-25 20:38:57 +00:00
}
envVar := envForRepo(t)
// TODO: Log in audit log when it is implemented
2020-03-25 21:15:10 +00:00
fmt.Printf("%s=%s:%s\n", envVar, string(token), ainfo.Addr)
2020-03-25 20:38:57 +00:00
return nil
},
}