Make HikariConfig able to retrieve passwords dynamically from a password supplier (#1196)

Adds a passwordSupplier property with setter and getter
Refactors the password property to use a built-in passwordSupplier
If setting the password directly, the passwordSupplier property returns NULL
pull/1442/head
Alejandro Abdelnur 6 years ago
parent 086bf18389
commit f51af54e82

@ -39,6 +39,7 @@ import java.util.TreeSet;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
import static com.zaxxer.hikari.util.UtilityElf.getNullIfEmpty; import static com.zaxxer.hikari.util.UtilityElf.getNullIfEmpty;
import static com.zaxxer.hikari.util.UtilityElf.safeIsAssignableFrom; import static com.zaxxer.hikari.util.UtilityElf.safeIsAssignableFrom;
@ -48,6 +49,19 @@ import static java.util.concurrent.TimeUnit.SECONDS;
@SuppressWarnings({"SameParameterValue", "unused"}) @SuppressWarnings({"SameParameterValue", "unused"})
public class HikariConfig implements HikariConfigMXBean public class HikariConfig implements HikariConfigMXBean
{ {
// Password supplier to use when the password is set directly to the configuration instance.
private static class StringPasswordSupplier implements Supplier<String> {
private final String password;
public StringPasswordSupplier(String password) {
this.password = password;
}
@Override public String get() {
return password;
}
}
private static final Logger LOGGER = LoggerFactory.getLogger(HikariConfig.class); private static final Logger LOGGER = LoggerFactory.getLogger(HikariConfig.class);
private static final char[] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); private static final char[] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
@ -70,7 +84,7 @@ public class HikariConfig implements HikariConfigMXBean
private volatile int maxPoolSize; private volatile int maxPoolSize;
private volatile int minIdle; private volatile int minIdle;
private volatile String username; private volatile String username;
private volatile String password; private volatile Supplier<String> passwordSupplier = new StringPasswordSupplier(null);
// Properties NOT changeable at runtime // Properties NOT changeable at runtime
// //
@ -268,13 +282,34 @@ public class HikariConfig implements HikariConfigMXBean
this.minIdle = minIdle; this.minIdle = minIdle;
} }
/**
* Get the password supplier to use to provide the password for DataSource.getConnection(user, password) calls.
* @return the password supplier or null if there is no supplier or the password has been set directly.
*/
public Supplier<String> getPasswordSupplier()
{
return (passwordSupplier instanceof StringPasswordSupplier) ? null : passwordSupplier;
}
/**
* Set the password supplier to use for DataSource.getConnection(username, password) calls.
* @param passwordSupplier the password supplier
*/
public void setPasswordSupplier(Supplier<String> passwordSupplier)
{
if (passwordSupplier == null) {
throw new IllegalArgumentException("passwordSupplier cannot be null");
}
this.passwordSupplier = passwordSupplier;
}
/** /**
* Get the default password to use for DataSource.getConnection(username, password) calls. * Get the default password to use for DataSource.getConnection(username, password) calls.
* @return the password * @return the password
*/ */
public String getPassword() public String getPassword()
{ {
return password; return passwordSupplier.get();
} }
/** /**
@ -284,7 +319,7 @@ public class HikariConfig implements HikariConfigMXBean
@Override @Override
public void setPassword(String password) public void setPassword(String password)
{ {
this.password = password; setPasswordSupplier(new StringPasswordSupplier(password));
} }
/** /**

@ -0,0 +1,56 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari;
import org.junit.Assert;
import org.junit.Test;
import java.util.function.Supplier;
public class TestHikariConfig {
@Test
public void testPasswordAsString()
{
// default password NULL
HikariConfig config = new HikariConfig();
Assert.assertNull(config.getPassword());
Assert.assertNull(config.getPasswordSupplier());
// set password NULL
config = new HikariConfig();
config.setPassword(null);
Assert.assertNull(config.getPassword());
Assert.assertNull(config.getPasswordSupplier());
// set password value
config = new HikariConfig();
config.setPassword("password");
Assert.assertEquals("password", config.getPassword());
Assert.assertNull(config.getPasswordSupplier());
}
@Test
public void testPasswordAsSupplier()
{
HikariConfig config = new HikariConfig();
Supplier<String> supplier = () -> "password";
config.setPasswordSupplier(supplier);
Assert.assertEquals("password", config.getPassword());
Assert.assertEquals(supplier, config.getPasswordSupplier());
}
}
Loading…
Cancel
Save