Skip to content
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

add user and password to uri if the uri does not contain user password #560

Merged
merged 12 commits into from
Sep 13, 2023

Conversation

naughtyGitCat
Copy link
Contributor


  • Tests passed.
  • Fix conflicts with target branch.
  • Update jira ticket description if needed.
  • Attach screenshots/console output to confirm new behavior to jira ticket, if applicable.

Once all checks pass and the code is ready for review, please add pmm-review-exporters team as the reviewer. That would assign people from the review team automatically. Report any issues on our Forum or Discord.

@naughtyGitCat naughtyGitCat requested a review from a team as a code owner September 13, 2022 06:47
@naughtyGitCat naughtyGitCat requested review from ShashankSinha252 and tshcherban and removed request for a team September 13, 2022 06:47
@it-percona-cla
Copy link

it-percona-cla commented Sep 13, 2022

CLA assistant check
All committers have signed the CLA.

@denisok denisok added this to the v0.35.0 milestone Sep 14, 2022
@denisok denisok linked an issue Sep 14, 2022 that may be closed by this pull request
main.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@naughtyGitCat naughtyGitCat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improve compatibility, make the change do impact less

@tshcherban
Copy link
Contributor

@ShashankSinha252 passing both CI (FeatureBuild) and metrics test

@naughtyGitCat
Copy link
Contributor Author

@ShashankSinha252 are there any more changes that should be made by me?

@Aleksa-del
Copy link

Hello, @naughtyGitCat! Thank you for your contribution! We would love to send you some gifts. Please, contact us at community-team@percona.com with your mailing address, phone number (for delivery only) and T-shirt size.

Copy link
Member

@BupycHuk BupycHuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @naughtyGitCat can I ask what you mean by exposing URL in the original issue? In case we expose the full URL in logs it's a problem, but the solution you made probably won't help, because they're still in the URL variable. So it would be good to understand how do you expose URL?

Comment on lines 176 to 182
e.opts.EnableDiagnosticData = false
e.opts.EnableDBStats = false
e.opts.EnableCollStats = false
e.opts.EnableTopMetrics = false
e.opts.EnableReplicasetStatus = false
e.opts.EnableIndexStats = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it related to the issue we are solving in this PR?
it would be good to write some logs here to show that we are disabling collectors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a separate issue.
if we run the exporter to monitoring arbiter role instance. the exporter will be hung to stuck.
It seems I should make a separate issue and pr. if possible, i will learn how to separate commits from pr before this week ends

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you

Copy link
Contributor Author

@naughtyGitCat naughtyGitCat Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @naughtyGitCat can I ask what you mean by exposing URL in the original issue? In case we expose the full URL in logs it's a problem, but the solution you made probably won't help, because they're still in the URL variable. So it would be good to understand how do you expose URL?

  • my purpose is to try to expose the monitored instance url, the benefit is easily identifying the exporter instance target DB instance. a host maybe runs multi exporter instances to monitor multi different DB instances. currently, the exporter can not easily identify which DB it belongs to.
  • by adding url, we can safely expose the url info, and hide the user pass info by the env value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to expose URL in mongodb_exporter logs/metrics or somehow else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to expose URL in mongodb_exporter logs/metrics or somehow else?

I want to expose the url in cmdline, which can be easily identified by ps command, the log or metric need more works to fetch url info. hard to manipulate exporter instance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, ok, it makes sense

main.go Outdated
log.Debugf("Prepending mongodb:// to the URI")
// IF user@pass not contained in uri AND custom user and pass supplied in arguments
// DO concat a new uri with user and pass arguments value
if !strings.Contains(opts.URI, "@") && opts.User != "" && opts.Password != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be two separate ifs, in case we have data source value like username:password@host:port without mongodb:// prefix this if will be skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in my modification, I removed possible exits mongodb:// prefix. and after the user password re parse logic, the prefix will be added back when this if ends.

and, in username:password@host:port without mongodb:// circumstances, this if(aka my modifcation) will not be triggered.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and it changes current behavior and breaks backward compatibility, because before your changes we were prepending mongodb://

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, U are right, I will change it to achieve backward compatibility

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have taken a look on changes carefully,
in this pr,
I add two new arguments user and password.

for backward compatibility, the two new user and password arguments will be an empty string when not supplied, this if clause will not be triggered.
only when using new user and password argument is supplied and there is no user and pass in origin URI. this if will be triggered.

besides, if the user and password are both supplied in the origin URI and arguments(env or --user --pass), the exporter will take the URI one. (if not triggered )
the user, pass value in URI will have more priority than arguments

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract whole this logic and write a test so it will be clear what's wrong

the case which was working and won't work. username:password@host:port should be converted to mongodb://username:password@host:port

Copy link
Contributor Author

@naughtyGitCat naughtyGitCat Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven extracted logic, and write a test. 1cdf1a6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the check is stucked by

ok  	github.com/percona/mongodb_exporter/internal/tu	0.097s
?   	github.com/percona/mongodb_exporter/internal/util	[no test files]
FAIL
make: *** [Makefile:104: test-race] Error 1

This error seems unrelated to the code I've submitted.

@naughtyGitCat naughtyGitCat reopened this Dec 12, 2022
@naughtyGitCat
Copy link
Contributor Author

I have split two arbiter-related commits to #607

and this pr will only contains user pass expose commits

@naughtyGitCat naughtyGitCat removed the request for review from ShashankSinha252 January 10, 2023 09:25
main_test.go Outdated
t.Logf("Origin: %s", originalBareURI)
t.Logf("Expect: %s", originalBareURI)
t.Logf("Result: %s", newUri)
if newUri != originalBareURI {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if newUri != originalBareURI {
if newUri != originalPrefixBareURI {

This is the expected behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have reorgnized the test code, 24987e4

could you review it again, thanks

main_test.go Outdated
origin: "127.0.0.1",
newUser: "",
newPassword: "",
expect: "127.0.0.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the expectation here is mongodb://127.0.0.1, that's how it used to work

Suggested change
expect: "127.0.0.1",
expect: "mongodb://127.0.0.1",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake, have correct it both in main.go and main_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does need a mongod:// prefix in all situation, according to https://www.mongodb.com/docs/manual/reference/connection-string/

main.go Outdated
Comment on lines 104 to 105
// add back mongodb://
uri = "mongodb://" + uri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove it, anyway we do it a few lines later

@BupycHuk
Copy link
Member

BupycHuk commented Sep 1, 2023

please fix linters

@naughtyGitCat
Copy link
Contributor Author

naughtyGitCat commented Sep 1, 2023

@BupycHuk seems weird, the below check

ok  	github.com/percona/mongodb_exporter/internal/tu	(cached)
?   	github.com/percona/mongodb_exporter/internal/util	

have no relation with my change, and some of the checks seem to randomly pass and failed

and the check function InspectContainer seems related to docker env only other than the main logic code

@BupycHuk
Copy link
Member

BupycHuk commented Sep 1, 2023

@BupycHuk
Copy link
Member

BupycHuk commented Sep 1, 2023

Yeah, tests are unstable lately and we are working on it

@naughtyGitCat
Copy link
Contributor Author

I meant this one https://github.com/percona/mongodb_exporter/actions/runs/6048290809/job/16413363823?pr=560

failed with:
diff --git a/main_test.go b/main_test.go
is caused by
git diff --exit-code
and this command only means to mark two commits changed or not.
I think it was related to the merging from percona main to forked main

@naughtyGitCat
Copy link
Contributor Author

ci and lint check rules look like teetering

main_test.go Outdated
@@ -16,6 +16,7 @@
package main

import (
"github.com/stretchr/testify/assert"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this import should be moved below as a separate block

Copy link
Contributor Author

@naughtyGitCat naughtyGitCat Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean code orders like this

import (
	"context"
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/prometheus/client_golang/prometheus/testutil"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"go.mongodb.org/mongo-driver/bson"

	"github.com/percona/mongodb_exporter/internal/tu"
)

changed, years ago I did the same order like this, but when the Goland and gofmt auto changed dependency order, I thought the auto reordered was more go-style.

@BupycHuk BupycHuk merged commit 9571d21 into percona:main Sep 13, 2023
2 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature]split user and password from uri
7 participants