Last active
February 2, 2017 09:48
-
-
Save calvernaz/67ce5d96175b1ddd8a2c2f795ca35efa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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