Skip to content

Commit 71c3c07

Browse files
committed
terminal: auto-bake super macaroon on startup
Automatically bake a super macaroon on startup if it doesn't already exist on disk and the `bake-super-macaroon` option is configured. On startup, the node verifies if the super macaroon file exists. If it does, it parses the macaroon, extracts and verifies the version and root key ID, and asserts that the macaroon permissions exactly match the expected active permissions. If there is a mismatch, the macaroon is regenerated and overwritten on disk. If the file does not exist, a new super macaroon is baked and written directly to disk. Also validate that the `bake-super-macaroon` option is not enabled when LND is running in stateless initialization mode, failing startup early if they are used together.
1 parent 3e61ace commit 71c3c07

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

terminal.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ func (g *LightningTerminal) start(ctx context.Context) error {
802802
return fmt.Errorf("could not start litd sub-servers: %v", err)
803803
}
804804

805+
// Bake the super macaroon on startup if configured, now that all local
806+
// and remote sub-servers have been started and active permissions are
807+
// fully known.
808+
if err := g.setupSuperMacaroon(ctx); err != nil {
809+
return fmt.Errorf("could not setup super macaroon: %w", err)
810+
}
811+
805812
// We can now set the status of LiT as running.
806813
g.statusMgr.SetRunning(subservers.LIT)
807814

@@ -2172,3 +2179,64 @@ func randId(n int) string {
21722179

21732180
return string(b)
21742181
}
2182+
2183+
// setupSuperMacaroon bakes a super macaroon and writes it to disk if needed.
2184+
func (g *LightningTerminal) setupSuperMacaroon(ctx context.Context) error {
2185+
// If the bake-super-macaroon option is set to none, we don't bake a
2186+
// macaroon.
2187+
if g.cfg.BakeSuperMacaroon == noneChoice {
2188+
return nil
2189+
}
2190+
2191+
// If the super macaroon baking option is enabled, we cannot run in
2192+
// stateless initialization mode as it won't write any macaroons to the
2193+
// filesystem.
2194+
if g.cfg.statelessInitMode {
2195+
return fmt.Errorf("cannot use bake-super-macaroon " +
2196+
"with stateless-init mode")
2197+
}
2198+
2199+
path := g.cfg.SuperMacaroonPath
2200+
2201+
readOnly := g.cfg.BakeSuperMacaroon == readOnlyChoice
2202+
activePerms := g.permsMgr.ActivePermissions(readOnly)
2203+
2204+
if litmac.SuperMacaroonExists(path) {
2205+
matches, err := litmac.MacaroonMatchesPermissions(
2206+
path, activePerms,
2207+
)
2208+
2209+
if err == nil && matches {
2210+
log.Debugf("Super macaroon already exists at "+
2211+
"%v and matches configuration, "+
2212+
"skipping bake", path)
2213+
2214+
return nil
2215+
}
2216+
if err != nil {
2217+
return fmt.Errorf(
2218+
"unable to verify super macaroon "+
2219+
"permissions at %v, please delete "+
2220+
"it if the issue persists: %w",
2221+
path, err,
2222+
)
2223+
}
2224+
2225+
log.Infof("Super macaroon permissions " +
2226+
"differ from configuration, " +
2227+
"regenerating...")
2228+
}
2229+
2230+
log.Infof("Baking super macaroon on startup...")
2231+
2232+
// Bake the super macaroon and write it to disk.
2233+
if err := litmac.BakeAndWriteSuperMacaroon(
2234+
ctx, g.basicClient, path, activePerms,
2235+
); err != nil {
2236+
return err
2237+
}
2238+
2239+
log.Infof("Successfully baked and wrote super macaroon to %v", path)
2240+
2241+
return nil
2242+
}

0 commit comments

Comments
 (0)