← 输入框|github.com|刷新
Skip to content

Add option to specify connection URL - #133

Merged
mrigger merged 1 commit into
sqlancer:masterfrom
nukoyluoglu:URL_option
Jul 28, 2020
Merged

mrigger merged 1 commit into
sqlancer:masterfrom
nukoyluoglu:URL_option

Conversation

@nukoyluoglu

Copy link
Copy Markdown
Collaborator

This PR adds the option for the user to specify the initial connection URL in the Postgres implementation. This allows more flexibility in connecting with different users, hosts, ports, and optional connection parameters. The logging also includes the full URL instead of only the database name. The provided URL is checked for correct format, and parsed. If a username and password is specified in the URL, these overwrite the username and password set by the MainOptions (with sqlancer as default). When calling DriverManager to get connection, the username and password are removed from the URL string to avoid confusion.

@nukoyluoglu
nukoyluoglu marked this pull request as draft July 24, 2020 14:45
@nukoyluoglu
nukoyluoglu force-pushed the URL_option branch 2 times, most recently from 25aead8 to 9c450c1 Compare July 24, 2020 17:17
@nukoyluoglu
nukoyluoglu marked this pull request as ready for review July 24, 2020 17:46
@nukoyluoglu
nukoyluoglu marked this pull request as draft July 24, 2020 18:00

@mrigger mrigger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR! It would be great to implement something like this for all the DBMS that we support (in the future, perhaps in a more general way). Also see my thoughts in #95.

password = userInfo[1];
int userInfoIndex = entryURL.indexOf(userInfoURI);
entryURL = entryURL.substring(0, userInfoIndex)
+ entryURL.substring(userInfoIndex + userInfoURI.length() + 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be more readable to concatenate username and password, rather than doing entryURL.substring(userInfoIndex + userInfoURI.length() + 1) (if that's correct). Efficiency is not really a concern here, and I think doing so is less error prone.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Actually, I am removing username and password here instead. Otherwise, if username and password are specified both in the DriverManager call and in the url passed to the Driver Manager, the conflict causes errors. So the concatenation I am doing is [urlBeforeUserInfo]+[urlAfterUserInfo].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, thanks for the explanation! If you think that additional variable names would not convey additional information, we can also leave it like it is.

username = userInfo[0];
password = userInfo[1];
int userInfoIndex = entryURL.indexOf(userInfoURI);
entryURL = entryURL.substring(0, userInfoIndex)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think extracting entryURL.substring(0, userInfoIndex) to a variable would improve readability. Is it the host name?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is everything that comes before the userInfo, so "postgresql:".

con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + databaseName,
globalState.getOptions().getUserName(), globalState.getOptions().getPassword());
int pathIndex = entryURL.indexOf(entryPath);
String testURL = entryURL.substring(0, pathIndex + 1) + databaseName

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here: I think it would be cleaner to define the variables once, and then use these, rather than using the substring method.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The only issue is that these are a combination of many possible things that the user can decide to include in the URL. This makes it hard to define variables, but allows more flexibility in setting optional parameters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, I see. Thanks for the explanation! Maybe as a future PR, we could consider adding test cases to check if connection strings are correctly parsed (although it doesn't have a high priority at the moment).

String databaseName = globalState.getDatabaseName();
Connection con = DriverManager.getConnection(url, globalState.getOptions().getUserName(),
globalState.getOptions().getPassword());
globalState.getState().logStatement("\\c test;");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to remove this? Logging a \c test is useful when reducing a test case manually, since changing to the default database test makes it possible to drop the current database of the test case.

globalState.getOptions().getPassword());
globalState.getState().logStatement("\\c test;");
Connection con = DriverManager.getConnection("jdbc:" + entryURL, username, password);
globalState.getState().logStatement(entryURL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the entry URL here? If we log it without appending a -- first, is the log still executable as a SQL script?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is the connection string. Does logging involve executing SQL statements? I thought they were for printing, more clarification on that would be great in case I missed something!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Things written to the log should typically either be SQL statements or comments (prefixed by --), so that the log file can basically be directly used as an input to a DBMS. I should update the documentation about this.

@mrigger

mrigger commented Jul 24, 2020

Copy link
Copy Markdown
Contributor

Thanks! Let me know when the PR is ready to be merged. Please squash the two commits.

@thanodnl thanodnl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Left a small comment on the handling of the user password.

Comment on lines +237 to +239
String[] userInfo = userInfoURI.split(":", 2);
username = userInfo[0];
password = userInfo[1];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What happens when only the username is provided and no password?
I think we will get an out of bound exception on the last line here.

connection url to test: postgres://foo@localhost:5432/test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added a check to see if a : separator exists and parse it accordingly. DriverManager.getConnection() seems to safely accept null as a valid password, so I set the password to null if there is no password provided

@nukoyluoglu
nukoyluoglu marked this pull request as ready for review July 27, 2020 11:55
@nukoyluoglu

Copy link
Copy Markdown
Collaborator Author

Thanks! Let me know when the PR is ready to be merged. Please squash the two commits.

The PR is ready to be merged, thank you!

@mrigger
mrigger merged commit 5bac2fe into sqlancer:master Jul 28, 2020
@mrigger

mrigger commented Jul 28, 2020

Copy link
Copy Markdown
Contributor

Thanks! Sorry for the delay with merging, I was on holidays until including yesterday.

@nukoyluoglu
nukoyluoglu deleted the URL_option branch July 28, 2020 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants