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

support empty password for mysql #899

Merged
merged 5 commits into from
Oct 15, 2018

Conversation

chungngoops
Copy link
Contributor

related issue: #896

added MYSQL_ALLOW_EMPTY_PASSWORD=yes option when the passwrod is empty.

  • verify all tests are passing.

added MYSQL_ALLOW_EMPTY_PASSWORD=yes option when the passwrod is empty.
@kiview
Copy link
Member

kiview commented Oct 4, 2018

Looks good, can you please add a test case for using an empty password?

@chungngoops
Copy link
Contributor Author

@kiview: sure!

Add unit test for empty password with non root user.
Copy link
Member

@kiview kiview left a comment

Choose a reason for hiding this comment

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

Can you please add a test for the exception condition? This needs to be done without @Rule, but instead by starting the container manually.

Please also make the sure the file is formatted correctly (see the .editorconfig file in the repository root).

addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", password);
} else {
if("root".equalsIgnoreCase(username)){
Copy link
Member

Choose a reason for hiding this comment

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

Please use username as the object for the method invocation.

if(password != null && !password.isEmpty()){
addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", password);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

We can use an else if here and lose one level of nesting.

.withPassword("")
.withEnv("MYSQL_ROOT_HOST", "%");

container.start();
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 do a try-catch here, catching ContainerException.

@testcontainers testcontainers deleted a comment Oct 10, 2018
@testcontainers testcontainers deleted a comment Oct 10, 2018
@testcontainers testcontainers deleted a comment Oct 10, 2018
@testcontainers testcontainers deleted a comment from kiview Oct 10, 2018
@testcontainers testcontainers deleted a comment Oct 10, 2018
@testcontainers testcontainers deleted a comment Oct 10, 2018
@testcontainers testcontainers deleted a comment Oct 10, 2018
.withDatabaseName(DB_NAME)
.withUsername(USER)
.withPassword("")
.withEnv("MYSQL_ROOT_HOST", "%");
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering, if we'd always need to add this and so we could put it into configure() as well?

} else if (MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes");
} else {
throw new ContainerLaunchException("Empty password can be used only with the root user");
Copy link
Member

Choose a reason for hiding this comment

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

I just found out, that we catch all exceptions and throw ContainerLaunchException in GenericContainer's doStart() method, so it's really not that important which exception we throw here, but ContainerLaunchException makes sense of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kiview: should we check the cause of the exception to make sure that the ContainerLaunchException is thrown by this method instead of others

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking something like this at first also. But in the end, we just want to check the public API and the container behaves as expected if we receive this exception, so I'd be good with it.

@kiview
Copy link
Member

kiview commented Oct 11, 2018

LGTM
You want to give it a final look @rnorth?

Copy link
Member

@rnorth rnorth left a comment

Choose a reason for hiding this comment

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

Looks fine to me - thanks for the contribution @chungngoops !

@kiview kiview merged commit 3126c54 into testcontainers:master Oct 15, 2018
@kiview kiview added this to the next milestone Oct 15, 2018
@kiview
Copy link
Member

kiview commented Oct 15, 2018

Merged, thanks a lot @chungngoops.

@rui-ferreira
Copy link

Unfortunately, this doesn't work if you launch the container via JDBC URL scheme, for example, with jdbc:mysql:5.7.22://localhost:3306/databasename?user=root&password=.

The problem is that JdbcDatabaseContainerProvider gets the password from the connection URL and instantiates MySQLContainer with a default test password in case the value in the URL is empty.
This is the problematic line:
final String password = connectionUrl.getQueryParameters().getOrDefault(pwdParamName, "test");

Can you provide a fix for this case?

@rui-ferreira
Copy link

Never mind, just noticed this got fixed in #2207. :)

@aspodin
Copy link

aspodin commented Apr 16, 2021

@OverRide
protected void configure() {
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d",
"mysql-default-conf");
addEnv("MYSQL_DATABASE", databaseName);
if (!MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
addEnv("MYSQL_USER", username);
}
if (password != null && !password.isEmpty()) {
addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", password);
} else if (MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes");
} else {
throw new ContainerLaunchException("Empty password can be used only with the root user");
}
setStartupAttempts(3);
}


addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes"); will be done for such configuration:
container = new CustomMySqlContainer(IMAGE_VERSION)
.withUsername("root")
.withPassword("");

but with this configuration container does not start with error message:
MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user
Remove MYSQL_USER="root" and use one of the following to control the root user password:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD

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.

5 participants