litcli: prepare for a client connection without a macaroon

In preparation for an upcoming commit where litcli commands for the
Status server are added, the `getClientConn` method is updated with a
`noMac` parameter. If true, them the connection will be made without a
macaroon.
This commit is contained in:
Elle Mouton 2023-05-21 19:01:35 +02:00
parent 6310eedbfc
commit 143876d1ec
No known key found for this signature in database
GPG key ID: D7D916376026F177
7 changed files with 33 additions and 29 deletions

View file

@ -78,7 +78,7 @@ var createAccountCommand = cli.Command{
func createAccount(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -185,7 +185,7 @@ var updateAccountCommand = cli.Command{
func updateAccount(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -253,7 +253,7 @@ var listAccountsCommand = cli.Command{
func listAccounts(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -293,7 +293,7 @@ var accountInfoCommand = cli.Command{
func accountInfo(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -341,7 +341,7 @@ var removeAccountCommand = cli.Command{
func removeAccount(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}

View file

@ -99,7 +99,7 @@ var listActionsCommand = cli.Command{
func listActions(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}

View file

@ -116,7 +116,7 @@ var listAutopilotSessionsCmd = cli.Command{
func revokeAutopilotSession(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -144,7 +144,7 @@ func revokeAutopilotSession(ctx *cli.Context) error {
func listAutopilotSessions(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -165,7 +165,7 @@ func listAutopilotSessions(ctx *cli.Context) error {
func listFeatures(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -189,7 +189,7 @@ func initAutopilotSession(ctx *cli.Context) error {
sessionExpiry := time.Now().Add(sessionLength).Unix()
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}

View file

@ -89,13 +89,15 @@ func fatal(err error) {
os.Exit(1)
}
func connectClient(ctx *cli.Context) (grpc.ClientConnInterface, func(), error) {
func connectClient(ctx *cli.Context, noMac bool) (grpc.ClientConnInterface,
func(), error) {
rpcServer := ctx.GlobalString("rpcserver")
tlsCertPath, macPath, err := extractPathArgs(ctx)
if err != nil {
return nil, nil, err
}
conn, err := getClientConn(rpcServer, tlsCertPath, macPath)
conn, err := getClientConn(rpcServer, tlsCertPath, macPath, noMac)
if err != nil {
return nil, nil, err
}
@ -104,18 +106,20 @@ func connectClient(ctx *cli.Context) (grpc.ClientConnInterface, func(), error) {
return conn, cleanup, nil
}
func getClientConn(address, tlsCertPath, macaroonPath string) (*grpc.ClientConn,
error) {
// We always need to send a macaroon.
macOption, err := readMacaroon(macaroonPath)
if err != nil {
return nil, err
}
func getClientConn(address, tlsCertPath, macaroonPath string, noMac bool) (
*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithDefaultCallOptions(maxMsgRecvSize),
macOption,
}
if !noMac {
macOption, err := readMacaroon(macaroonPath)
if err != nil {
return nil, err
}
opts = append(opts, macOption)
}
// TLS cannot be disabled, we'll always have a cert file to read.

View file

@ -58,7 +58,7 @@ var privacyMapConvertStrCommand = cli.Command{
func privacyMapConvertStr(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -112,7 +112,7 @@ var privacyMapConvertUint64Command = cli.Command{
func privacyMapConvertUint64(ctx *cli.Context) error {
ctxb := context.Background()
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}

View file

@ -53,7 +53,7 @@ var litCommands = []cli.Command{
}
func getInfo(ctx *cli.Context) error {
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -72,7 +72,7 @@ func getInfo(ctx *cli.Context) error {
}
func shutdownLit(ctx *cli.Context) error {
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -109,7 +109,7 @@ func bakeSuperMacaroon(ctx *cli.Context) error {
}
suffix := binary.BigEndian.Uint32(suffixBytes[:])
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}

View file

@ -96,7 +96,7 @@ var addSessionCommand = cli.Command{
}
func addSession(ctx *cli.Context) error {
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -229,7 +229,7 @@ var sessionStateMap = map[litrpc.SessionState]sessionFilter{
func listSessions(filter sessionFilter) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error {
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}
@ -279,7 +279,7 @@ var revokeSessionCommand = cli.Command{
}
func revokeSession(ctx *cli.Context) error {
clientConn, cleanup, err := connectClient(ctx)
clientConn, cleanup, err := connectClient(ctx, false)
if err != nil {
return err
}