lncli: don't read macaroons when not using them

When we either don't use macaroons (because the global --no-macaroons
flag is set) or don't need them (for the wallet unlocker commands), we
don't try to read the file at all to avoid running into an error if the
file doesn't exist (which it doesn't in those two cases).
This commit is contained in:
Oliver Gugger 2020-09-25 15:38:40 +02:00
parent 44524fd2e5
commit b28bf29c5f
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
3 changed files with 37 additions and 23 deletions

@ -117,7 +117,7 @@ func profileAdd(ctx *cli.Context) error {
} }
// Create a profile struct from all the global options. // Create a profile struct from all the global options.
profile, err := profileFromContext(ctx, true) profile, err := profileFromContext(ctx, true, false)
if err != nil { if err != nil {
return fmt.Errorf("could not load global options: %v", err) return fmt.Errorf("could not load global options: %v", err)
} }

@ -70,7 +70,7 @@ func getClient(ctx *cli.Context) (lnrpc.LightningClient, func()) {
func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn { func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
// First, we'll get the selected stored profile or an ephemeral one // First, we'll get the selected stored profile or an ephemeral one
// created from the global options in the CLI context. // created from the global options in the CLI context.
profile, err := getGlobalOptions(ctx) profile, err := getGlobalOptions(ctx, skipMacaroons)
if err != nil { if err != nil {
fatal(fmt.Errorf("could not load global options: %v", err)) fatal(fmt.Errorf("could not load global options: %v", err))
} }

@ -52,7 +52,9 @@ func (e *profileEntry) cert() (*x509.CertPool, error) {
// exists, these global options might be read from a predefined profile. If no // exists, these global options might be read from a predefined profile. If no
// profile exists, the global options from the command line are returned as an // profile exists, the global options from the command line are returned as an
// ephemeral profile entry. // ephemeral profile entry.
func getGlobalOptions(ctx *cli.Context) (*profileEntry, error) { func getGlobalOptions(ctx *cli.Context, skipMacaroons bool) (*profileEntry,
error) {
var profileName string var profileName string
// Try to load the default profile file and depending on its existence // Try to load the default profile file and depending on its existence
@ -62,7 +64,7 @@ func getGlobalOptions(ctx *cli.Context) (*profileEntry, error) {
// The legacy case where no profile file exists and the user also didn't // The legacy case where no profile file exists and the user also didn't
// request to use one. We only consider the global options here. // request to use one. We only consider the global options here.
case err == errNoProfileFile && !ctx.GlobalIsSet("profile"): case err == errNoProfileFile && !ctx.GlobalIsSet("profile"):
return profileFromContext(ctx, false) return profileFromContext(ctx, false, skipMacaroons)
// The file doesn't exist but the user specified an explicit profile. // The file doesn't exist but the user specified an explicit profile.
case err == errNoProfileFile && ctx.GlobalIsSet("profile"): case err == errNoProfileFile && ctx.GlobalIsSet("profile"):
@ -78,13 +80,13 @@ func getGlobalOptions(ctx *cli.Context) (*profileEntry, error) {
// setting the flag to an empty string. We fall back to the default/old // setting the flag to an empty string. We fall back to the default/old
// behavior. // behavior.
case ctx.GlobalIsSet("profile") && ctx.GlobalString("profile") == "": case ctx.GlobalIsSet("profile") && ctx.GlobalString("profile") == "":
return profileFromContext(ctx, false) return profileFromContext(ctx, false, skipMacaroons)
// There is a file, but no default profile is specified. The user also // There is a file, but no default profile is specified. The user also
// didn't specify a profile to use so we fall back to the default/old // didn't specify a profile to use so we fall back to the default/old
// behavior. // behavior.
case !ctx.GlobalIsSet("profile") && len(f.Default) == 0: case !ctx.GlobalIsSet("profile") && len(f.Default) == 0:
return profileFromContext(ctx, false) return profileFromContext(ctx, false, skipMacaroons)
// The user didn't specify a profile but there is a default one defined. // The user didn't specify a profile but there is a default one defined.
case !ctx.GlobalIsSet("profile") && len(f.Default) > 0: case !ctx.GlobalIsSet("profile") && len(f.Default) > 0:
@ -109,7 +111,9 @@ func getGlobalOptions(ctx *cli.Context) (*profileEntry, error) {
// profileFromContext creates an ephemeral profile entry from the global options // profileFromContext creates an ephemeral profile entry from the global options
// set in the CLI context. // set in the CLI context.
func profileFromContext(ctx *cli.Context, store bool) (*profileEntry, error) { func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) (
*profileEntry, error) {
// Parse the paths of the cert and macaroon. This will validate the // Parse the paths of the cert and macaroon. This will validate the
// chain and network value as well. // chain and network value as well.
tlsCertPath, macPath, err := extractPathArgs(ctx) tlsCertPath, macPath, err := extractPathArgs(ctx)
@ -129,6 +133,22 @@ func profileFromContext(ctx *cli.Context, store bool) (*profileEntry, error) {
} }
} }
entry := &profileEntry{
RPCServer: ctx.GlobalString("rpcserver"),
LndDir: lncfg.CleanAndExpandPath(ctx.GlobalString("lnddir")),
Chain: ctx.GlobalString("chain"),
Network: ctx.GlobalString("network"),
NoMacaroons: ctx.GlobalBool("no-macaroons"),
TLSCert: string(tlsCert),
}
// If we aren't using macaroons in general (flag --no-macaroons) or
// don't need macaroons for this command (wallet unlocker), we can now
// return already.
if skipMacaroons || ctx.GlobalBool("no-macaroons") {
return entry, nil
}
// Now load and possibly encrypt the macaroon file. // Now load and possibly encrypt the macaroon file.
macBytes, err := ioutil.ReadFile(macPath) macBytes, err := ioutil.ReadFile(macPath)
if err != nil { if err != nil {
@ -166,22 +186,16 @@ func profileFromContext(ctx *cli.Context, store bool) (*profileEntry, error) {
macEntry.Name = strings.TrimSuffix(macEntry.Name, ".macaroon") macEntry.Name = strings.TrimSuffix(macEntry.Name, ".macaroon")
} }
// Now that we have the complicated arguments behind us, let's return // Now that we have the macaroon jar as well, let's return the entry
// the new entry with all the values populated. // with all the values populated.
return &profileEntry{ entry.Macaroons = &macaroonJar{
RPCServer: ctx.GlobalString("rpcserver"), Default: macEntry.Name,
LndDir: lncfg.CleanAndExpandPath(ctx.GlobalString("lnddir")), Timeout: ctx.GlobalInt64("macaroontimeout"),
Chain: ctx.GlobalString("chain"), IP: ctx.GlobalString("macaroonip"),
Network: ctx.GlobalString("network"), Jar: []*macaroonEntry{macEntry},
NoMacaroons: ctx.GlobalBool("no-macaroons"), }
TLSCert: string(tlsCert),
Macaroons: &macaroonJar{ return entry, nil
Default: macEntry.Name,
Timeout: ctx.GlobalInt64("macaroontimeout"),
IP: ctx.GlobalString("macaroonip"),
Jar: []*macaroonEntry{macEntry},
},
}, nil
} }
// loadProfileFile tries to load the file specified and JSON deserialize it into // loadProfileFile tries to load the file specified and JSON deserialize it into