Skip to content

Commit

Permalink
lnconfig: Support utilizing Environment Variables in lnd.conf for `…
Browse files Browse the repository at this point in the history
…rpcuser` and `rpcpass` fields.

- Added `extractEnvVariable` function in `config.go` that returns The value of the specified environment variable, or the default value if provided, or the original input string if no matching variable is found or set.- Called `extractEnvVariable` function in `parseRPCParams` function in `config.go` file where `rpcuser` and `rpcpass` fields are being parsed.
  • Loading branch information
mohamedawnallah committed Dec 23, 2023
1 parent 0df507e commit 40d78af
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,12 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{},
var daemonName, confDir, confFile, confFileBase string
switch conf := nodeConfig.(type) {
case *lncfg.Btcd:
// Resolves environment variable references in RPCUser and RPCPass fields.
conf.RPCUser = extractEnvVariable(conf.RPCUser)
conf.RPCPass = extractEnvVariable(conf.RPCPass)

fmt.Println("conf.RPCUser:", conf.RPCUser)

// If both RPCUser and RPCPass are set, we assume those
// credentials are good to use.
if conf.RPCUser != "" && conf.RPCPass != "" {
Expand Down Expand Up @@ -1822,6 +1828,12 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{},
confFile = conf.ConfigPath
confFileBase = BitcoinChainName

// Resolves environment variable references in RPCUser and RPCPass fields.
conf.RPCUser = extractEnvVariable(conf.RPCUser)
conf.RPCPass = extractEnvVariable(conf.RPCPass)

fmt.Println("conf.RPCUser:", conf.RPCUser)

// Check that cookie and credentials don't contradict each
// other.
if (conf.RPCUser != "" || conf.RPCPass != "") &&
Expand Down Expand Up @@ -1917,6 +1929,52 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{},
return nil
}

// extractEnvVariable extracts the value of an environment variable from a string.
// It supports the following formats:
// 1) $ENVIRONMENT_VARIABLE
// 2) ${ENVIRONMENT_VARIABLE}
// 3) ${ENVIRONMENT_VARIABLE:-DEFAULT_VALUE}
//
// Standard environment variable naming conventions:
// - ENVIRONMENT_VARIABLE contains letters, digits, and underscores, and does not start with a digit.
// - DEFAULT_VALUE follows the rule that it can contain any characters except whitespace.
//
// Parameters:
// - value: The input string containing references to environment variables.
//
// Returns:
// - string: The value of the specified environment variable, or the default value
// if provided, or the original input string if no matching variable is found or set.
func extractEnvVariable(value string) string {
re := regexp.MustCompile(`^(?:\${([a-zA-Z_][a-zA-Z0-9_]*)?(:-(\S+))?})|(?:\$([a-zA-Z_][a-zA-Z0-9_]*))$`)

matches := re.FindStringSubmatch(value)

if len(matches) > 0 {
// envVariable, Group 1 corresponding to this format: ${ENVIRONMENT_VARIABLE}
// or ${ENVIRONMENT_VARIABLE:-DEFAULT_VALUE}
envVariable := matches[1]
// envVariable, Group 4 corresponding to this format: $ENVIRONMENT_VARIABLE
if matches[4] != "" {
envVariable = matches[4]
}

// defaultValue, Group 3 corrosponding to this format: ${ENVIRONMENT_VARIABLE:-DEFAULT_VALUE}
defaultValue := matches[3]

envValue := os.Getenv(envVariable)

// Use the default value if the environment variable is not set
if envValue == "" && defaultValue != "" {
envValue = defaultValue
}

return envValue
}

return value
}

// extractBtcdRPCParams attempts to extract the RPC credentials for an existing
// btcd instance. The passed path is expected to be the location of btcd's
// application data directory on the target system.
Expand Down Expand Up @@ -1960,7 +2018,11 @@ func extractBtcdRPCParams(btcdConfigPath string) (string, string, error) {
return "", "", fmt.Errorf("unable to find rpcuser in config")
}

return string(userSubmatches[1]), string(passSubmatches[1]), nil
// Resolves environment variable references in RPCUser and RPCPass fields.
rpcUser := extractEnvVariable(string(userSubmatches[1]))
rpcPass := extractEnvVariable(string(passSubmatches[1]))

return rpcUser, rpcPass, nil
}

// extractBitcoindRPCParams attempts to extract the RPC credentials for an
Expand Down Expand Up @@ -2085,7 +2147,11 @@ func extractBitcoindRPCParams(networkName, bitcoindDataDir, bitcoindConfigPath,
"in config")
}

return string(userSubmatches[1]), string(passSubmatches[1]),
// Resolves environment variable references in RPCUser and RPCPass fields.
rpcUser := extractEnvVariable(string(userSubmatches[1]))
rpcPass := extractEnvVariable(string(passSubmatches[1]))

return rpcUser, rpcPass,
zmqBlockHost, zmqTxHost, nil
}

Expand Down

0 comments on commit 40d78af

Please sign in to comment.