Skip to content

Commit 3e61ace

Browse files
committed
config: add super macaroon startup flags and path validation
Introduce configuration flags to enable baking a super macaroon on startup. Add --bake-super-macaroon (none, read-only, read-write) and --super-macaroon-path. Also add validation logic to ensure that the configured super macaroon path ends with the expected '.macaroon' suffix, rejecting startup early otherwise.
1 parent be8a061 commit 3e61ace

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

config.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/lightninglabs/lightning-terminal/db/sqlc"
2828
"github.com/lightninglabs/lightning-terminal/firewall"
2929
"github.com/lightninglabs/lightning-terminal/firewalldb"
30+
"github.com/lightninglabs/lightning-terminal/macaroons"
3031
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
3132
"github.com/lightninglabs/lightning-terminal/session"
3233
"github.com/lightninglabs/lightning-terminal/subservers"
@@ -96,6 +97,22 @@ const (
9697
// autogenerated lit macaroon.
9798
DefaultMacaroonFilename = "lit.macaroon"
9899

100+
// DefaultSuperMacaroonFilename is the default file name for the
101+
// autogenerated super macaroon.
102+
DefaultSuperMacaroonFilename = "super.macaroon"
103+
104+
// noneChoice is the none choice for the bake-super-macaroon
105+
// configuration option.
106+
noneChoice = "none"
107+
108+
// readOnlyChoice is the read-only choice for the bake-super-macaroon
109+
// configuration option.
110+
readOnlyChoice = "read-only"
111+
112+
// defaultBakeSuperMacaroon is the default value for the
113+
// bake-super-macaroon configuration option.
114+
defaultBakeSuperMacaroon = noneChoice
115+
99116
defaultFirstLNCConnTimeout = 10 * time.Minute
100117

101118
// DatabaseBackendSqlite is the name of the SQLite database backend.
@@ -169,6 +186,12 @@ var (
169186
defaultSqliteDatabasePath = filepath.Join(
170187
DefaultLitDir, DefaultNetwork, defaultSqliteDatabaseFileName,
171188
)
189+
190+
// DefaultSuperMacaroonPath is the default full path of the super
191+
// macaroon.
192+
DefaultSuperMacaroonPath = filepath.Join(
193+
DefaultLitDir, DefaultNetwork, DefaultSuperMacaroonFilename,
194+
)
172195
)
173196

174197
// Config is the main configuration struct of lightning-terminal. It contains
@@ -203,6 +226,9 @@ type Config struct {
203226

204227
MacaroonPath string `long:"macaroonpath" description:"Path to write the macaroon for litd's RPC and REST services if it doesn't exist."`
205228

229+
BakeSuperMacaroon string `long:"bake-super-macaroon" description:"Bake a super macaroon on startup if it doesn't exist." choice:"none" choice:"read-only" choice:"read-write"`
230+
SuperMacaroonPath string `long:"super-macaroon-path" description:"Path to write the auto-baked super macaroon. This must include both the directory and the name of the macaroon file itself (which must end with the .macaroon suffix)."`
231+
206232
FirstLNCConnDeadline time.Duration `long:"firstlncconndeadline" description:"The duration after a new LNC session will be revoked if no connection is made with it. This only applies for the first connection which is made using the pairing phrase. "`
207233

208234
// DatabaseBackend is the database backend we will use for storing all
@@ -543,6 +569,8 @@ func defaultConfig() *Config {
543569
LetsEncryptListen: defaultLetsEncryptListen,
544570
LetsEncryptDir: defaultLetsEncryptDir,
545571
MacaroonPath: DefaultMacaroonPath,
572+
SuperMacaroonPath: DefaultSuperMacaroonPath,
573+
BakeSuperMacaroon: defaultBakeSuperMacaroon,
546574
DatabaseBackend: DatabaseBackendSqlite,
547575
Sqlite: &db.SqliteConfig{
548576
DatabaseFileName: defaultSqliteDatabasePath,
@@ -736,6 +764,34 @@ func loadAndValidateConfig(ctx context.Context,
736764
return nil, err
737765
}
738766

767+
if cfg.SuperMacaroonPath == DefaultSuperMacaroonPath {
768+
cfg.SuperMacaroonPath = filepath.Join(
769+
litDir, cfg.Network, DefaultSuperMacaroonFilename,
770+
)
771+
}
772+
773+
err = macaroons.HasMacaroonSuffix(
774+
cfg.SuperMacaroonPath,
775+
)
776+
if err != nil {
777+
return nil, err
778+
}
779+
780+
// Clean and expand the super macaroon path if configured, and ensure
781+
// the parent directory exists.
782+
if cfg.BakeSuperMacaroon != defaultBakeSuperMacaroon &&
783+
cfg.SuperMacaroonPath != "" {
784+
785+
cfg.SuperMacaroonPath = lncfg.CleanAndExpandPath(
786+
cfg.SuperMacaroonPath,
787+
)
788+
dir := filepath.Dir(cfg.SuperMacaroonPath)
789+
if err := makeDirectories(dir); err != nil {
790+
return nil, fmt.Errorf("unable to create super "+
791+
"macaroon directory %v: %w", dir, err)
792+
}
793+
}
794+
739795
err = cfg.DevConfig.Validate()
740796
if err != nil {
741797
return nil, err

0 commit comments

Comments
 (0)