-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lnconfig: Support utilizing Environment Variables in lnd.conf
for rpcuser
and rpcpass
fields.
#8310
lnconfig: Support utilizing Environment Variables in lnd.conf
for rpcuser
and rpcpass
fields.
#8310
Conversation
40d78af
to
4f42f8b
Compare
config.go
Outdated
// 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please break everything at 80 characters (while configuring your editor to use 8 spaces for tab characters).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for letting me know! I've checked the code_formatting_rules.md
and configured my Visual Studio code settings.json
to reflect the Recommended settings for your editor
rules so that it would be reflected automatically in the next PRs:
"editor.insertSpaces": false,
"editor.tabSize": 8,
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 80,
"go.formatTool": "gofmt",
"go.useLanguageServer": true,
"editor.formatOnSave": true,
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
],
"[markdown]": {
"files.eol": "\n",
"files.insertFinalNewline": true,
"editor.wordWrap": "bounded",
"editor.wordWrapColumn": 80
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 🙏🏼
The main feedback I have is that the regex you put together has a slight inaccuracy to it. Other than that, there are a couple opportunities for simplification.
As @guggero points out, you should consult the style guide to ensure all those guidelines are followed. You can ensure these checks pass locally by running make fmt-check
and make lint
.
Finally, please add to the latest release notes a short description of this change.
config.go
Outdated
// - 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_]*))$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I plug this into a diagram tool, I notice that the start of line and end of line characters are broken up across the branches. I don't believe you intended this. I would suggest making it so that both branches are anchored at the beginning and end of line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, this makes sense, and thanks a lot for mentioning this diagram tool the regular expressions make more sense now :)
Here is after updating the regular expression in the diagram tool so that both branches are anchored at the beginning and end of the line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wasn't going to eyeball that regex and be like "LGTM" 😅
config.go
Outdated
matches := re.FindStringSubmatch(value) | ||
|
||
if len(matches) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd combine these lines to make it such that the matches variable is only valid in the block scope.
75b8252
to
187c4df
Compare
Thanks for the great feedback @guggero @ProofOfKeags I've addressed your suggestions! |
6f8b242
to
e489af2
Compare
Before I'm going to review in detail again, just one comment: It feels weird to me that we would want to support different formats and even fall back values within the parsing code in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good. Gonna defer to @guggero as far as "should we do this". But assuming we want to, the change itself seems good to go.
@mohamedawnallah, remember to re-request review from reviewers when ready |
I'm going to unsubscribe from this since I have approved. Please ping me if my input is needed again. |
@guggero I'd like to discuss this issue again anytime you're available. I agree with your thoughts That the changes that this PR proposes add much complexity compared to the value it added. I made the fallback value since the main issue #8295 mentioned that in the demo
cc: @ProofOfKeags |
Okay, I guess it could be useful to have a fall back value. Although I think that can also lead to hard-to-find errors if you're not expecting that. So I guess my main ask here is to simplify the regular expression, so it's easier to read and understand. |
This looks great! I'm gonna submit a patch and will let you know. Thanks for sharing! |
e489af2
to
ddb70d3
Compare
Important Auto Review SkippedAuto reviews are limited to the following labels: llm-review. Please add one of these labels to enable auto reviews. Please check the settings in the CodeRabbit UI or the To trigger a single review, invoke the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good now, thanks for the changes!
Just a couple of nits, then we're good to merge 💯
config.go
Outdated
`^\$\{([a-zA-Z_][a-zA-Z0-9_]*):-([\S]+)\}$`, | ||
) | ||
|
||
// Match against supported formats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing full stop at end of sentence.
config.go
Outdated
rpcUser := supplyEnvValue(string(userSubmatches[1])) | ||
rpcPass := supplyEnvValue(string(passSubmatches[1])) | ||
|
||
return rpcUser, rpcPass, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can fit more on one line now (maybe even all values).
config_test.go
Outdated
@@ -52,3 +53,67 @@ func TestConfigToFlatMap(t *testing.T) { | |||
require.Equal(t, redactedPassword, result["db.etcd.pass"]) | |||
require.Equal(t, redactedPassword, result["db.postgres.dsn"]) | |||
} | |||
|
|||
func TestSupplyEnvValue(t *testing.T) { | |||
// Mock environment variables for testing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing full stop.
config_test.go
Outdated
func TestSupplyEnvValue(t *testing.T) { | ||
// Mock environment variables for testing | ||
os.Setenv("EXISTING_VAR", "existing_value") | ||
os.Setenv("EMPTY_VAR", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use t.Setenv()
instead, so they're cleaned up automatically at the end of the test.
config_test.go
Outdated
for _, test := range tests { | ||
t.Run(test.description, func(t *testing.T) { | ||
result := supplyEnvValue(test.input) | ||
if result != test.expected { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should use require.Equal()
for new code.
ddb70d3
to
fb2a634
Compare
In this commit, we support utilizing the usage of enviroment variables in `lnd.conf` file for `rpcuser` and `rpcpass` fields by adding `supplyEnvValue` function in `config.go` that returns the value of the specified environment variable, the fall-back value if provided, or the original input string if no matching environment variables are found or set.
fb2a634
to
0add4fc
Compare
Thanks a lot, @guggero for your suggestion. It is now much simpler. I've addressed all of your feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🫡
Change Description
supplyEnvValue
function inconfig.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.supplyEnvValue
function inparseRPCParams
function inconfig.go
file whererpcuser
andrpcpass
fields are being parsed.Fixes #8295
Steps to Test
Added the test cases for
supplyEnvVale
in theconfig_test.go
file covering the happy and sad paths.Pull Request Checklist
Testing
Code Style and Documentation
[skip ci]
in the commit message for small changes.📝 Please see our Contribution Guidelines for further guidance.