Skip to content

Instantly share code, notes, and snippets.

@calvernaz
Last active February 2, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvernaz/67ce5d96175b1ddd8a2c2f795ca35efa to your computer and use it in GitHub Desktop.
Save calvernaz/67ce5d96175b1ddd8a2c2f795ca35efa to your computer and use it in GitHub Desktop.
package com.hikari.datasource;
import com.zaxxer.hikari.HikariConfig;
public class EncryptedHikariConfig extends HikariConfig {
private String encryptionKey;
public String getEncryptionKey() {
return encryptionKey;
}
public void setEncryptionKey(String encryptionKey) {
this.encryptionKey = encryptionKey;
}
/**
* Copies all the properties of this config and returns a new config with the password decrypted. If no encryption
* key is set then there is no copy or decryption and the config is returned without change.
*
* @return
*/
public HikariConfig copyAndDecryptPassword() {
if (!hasEncryptedKey()) return this;
EncryptedHikariConfig other = copyConfig();
other.decryptPassword();
return other;
}
private void decryptPassword() {
setPassword(decryptedPassword());
}
private EncryptedHikariConfig copyConfig() {
EncryptedHikariConfig other = new EncryptedHikariConfig();
other.encryptionKey = encryptionKey;
copyState(other);
return other;
}
private boolean hasEncryptedKey() {
return (null != getEncryptionKey()) && ("" != getEncryptionKey());
}
private String decryptedPassword() {
return PasswordDecrypter.decrypt(getPassword(), getEncryptionKey());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment