cmd: warn of dangerous flags for both config and CLI

This commit is contained in:
Péter Szilágyi 2017-01-11 14:49:01 +02:00
parent 67e3f40940
commit 062bf4d74b
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
2 changed files with 46 additions and 39 deletions

View file

@ -152,6 +152,7 @@ func init() {
app.Before = func(ctx *cli.Context) error { app.Before = func(ctx *cli.Context) error {
// Override default configs if specified in the config file // Override default configs if specified in the config file
utils.OverrideDefaults(ctx) utils.OverrideDefaults(ctx)
utils.WarnDangerousFlags(ctx)
// Ensure we can utilize all cores and configure any profilers // Ensure we can utilize all cores and configure any profilers
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View file

@ -419,14 +419,6 @@ var (
} }
) )
// dangerousFlags is a set of flags considered dangerous that the user is warned
// about if they are contained within the config file.
var dangerousFlags = map[string]string{
RPCListenAddrFlag.Name: "May allow ousiders to access your node",
WSListenAddrFlag.Name: "May allow ousiders to access your node",
IPCPathFlag.Name: "May allow other users/programs to access your node",
}
// OverrideDefaults parses a config .ini file if one was specified as a CLI flag // OverrideDefaults parses a config .ini file if one was specified as a CLI flag
// and overrides any non-manually set config values. // and overrides any non-manually set config values.
func OverrideDefaults(ctx *cli.Context) { func OverrideDefaults(ctx *cli.Context) {
@ -461,28 +453,19 @@ func OverrideDefaults(ctx *cli.Context) {
Fatalf("Failed to load initial configurations: %v", err) Fatalf("Failed to load initial configurations: %v", err)
} }
overrides := make(map[string]string) overrides := make(map[string]string)
dangerous := make(map[string]string)
for _, sec := range cfg.Sections() { for _, sec := range cfg.Sections() {
for _, key := range sec.Keys() { for _, key := range sec.Keys() {
name := key.Name() name := key.Name()
if _, ok := flags[name]; !ok { if _, ok := flags[name]; !ok {
Fatalf("Unknown configuration entry: %v", name) Fatalf("Unknown configuration entry: %s", name)
} }
if _, duplicate := overrides[name]; duplicate || ctx.GlobalIsSet(name) { if _, duplicate := overrides[name]; duplicate || ctx.GlobalIsSet(name) {
overrides[name] = key.Value() overrides[name] = key.Value()
continue continue
} }
if dangerousFlags[name] != "" {
dangerous[name] = key.Value()
}
ctx.GlobalSet(name, key.Value()) ctx.GlobalSet(name, key.Value())
} }
} }
// Warn the user in case of some weird configuration combos
if len(overrides) > 0 || len(dangerous) > 0 {
fmt.Println("-----------------------------------------------------------------")
// If config file values were overridden, warn the user // If config file values were overridden, warn the user
if len(overrides) > 0 { if len(overrides) > 0 {
fmt.Println("Config file values overridden via the CLI:") fmt.Println("Config file values overridden via the CLI:")
@ -493,8 +476,33 @@ func OverrideDefaults(ctx *cli.Context) {
} }
fmt.Println() fmt.Println()
} }
}
// dangerousFlags is a set of flags considered dangerous that the user is warned
// about if they are contained within the config file.
var dangerousFlags = map[string]string{
RPCListenAddrFlag.Name: "May allow ousiders to access your node",
WSListenAddrFlag.Name: "May allow ousiders to access your node",
IPCPathFlag.Name: "May allow other users/programs to access your node",
}
// WarnDangerousFlags checks whether the user specified dangerous flags either
// in a config file or manually on the CLI, and display a warning in such cases.
func WarnDangerousFlags(ctx *cli.Context) {
// Gather any potentially dangerous flags
dangerous := make(map[string]string)
for _, flag := range ctx.GlobalFlagNames() {
fmt.Println(flag, ctx.GlobalIsSet(flag))
if ctx.GlobalIsSet(flag) && dangerousFlags[flag] != "" {
dangerous[flag] = ctx.GlobalString(flag)
}
}
if len(dangerous) == 0 {
return
}
// Dangerous flags set, warn the user
// If config file contains dangerous flags, warn the user // If config file contains dangerous flags, warn the user
if len(dangerous) > 0 { fmt.Println("-----------------------------------------------------------------")
fmt.Println("Config file contains dangerous flags:") fmt.Println("Config file contains dangerous flags:")
for key, val := range dangerous { for key, val := range dangerous {
fmt.Printf(" - %s:\n", key) fmt.Printf(" - %s:\n", key)
@ -503,10 +511,8 @@ func OverrideDefaults(ctx *cli.Context) {
} }
fmt.Println() fmt.Println()
fmt.Println("If you don't know what the above flags do, terminate immediately!") fmt.Println("If you don't know what the above flags do, terminate immediately!")
}
fmt.Println("-----------------------------------------------------------------") fmt.Println("-----------------------------------------------------------------")
} }
}
// MakeDataDir retrieves the currently requested data directory, terminating // MakeDataDir retrieves the currently requested data directory, terminating
// if none (or the empty string) is specified. If the node is starting a testnet, // if none (or the empty string) is specified. If the node is starting a testnet,