@VisibleForTesting
Key loadSecretFileFromFile(@Nullable String path) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, InvalidKeyException {
if (StringUtils.isBlank(path)) {
- throw new IllegalStateException("Secret key not found. Please set the property " + CoreProperties.ENCRYPTION_SECRET_KEY_FILE);
+ throw new IllegalStateException("Secret key not found. Please set the property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH);
}
File file = new File(path);
if (!file.exists() || !file.isFile()) {
- throw new IllegalStateException("The property " + CoreProperties.ENCRYPTION_SECRET_KEY_FILE + " does not link to a valid file: " + path);
+ throw new IllegalStateException("The property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH + " does not link to a valid file: " + path);
}
String s = FileUtils.readFileToString(file);
if (StringUtils.isBlank(s)) {
@VisibleForTesting
String getPathToSecretKey() {
- String path = settings.getClearString(CoreProperties.ENCRYPTION_SECRET_KEY_FILE);
+ String path = settings.getClearString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH);
if (StringUtils.isBlank(path)) {
path = new File(FileUtils.getUserDirectoryPath(), ".sonar/sonar-secret.txt").getPath();
}
package org.sonar.api.config;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.sonar.api.CoreProperties;
@Test
public void encrypt() throws Exception {
Settings settings = new Settings();
- settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_FILE, pathToSecretKey());
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, pathToSecretKey());
AesCipher cipher = new AesCipher(settings);
String encryptedText = cipher.encrypt("this is a secret");
@Test
public void decrypt() throws Exception {
Settings settings = new Settings();
- settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_FILE, pathToSecretKey());
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, pathToSecretKey());
AesCipher cipher = new AesCipher(settings);
// the following value has been encrypted with the key /org/sonar/api/config/AesCipherTest/aes_secret_key.txt
public void decrypt_bad_key() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt");
Settings settings = new Settings();
- settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_FILE, new File(resource.toURI()).getCanonicalPath());
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, new File(resource.toURI()).getCanonicalPath());
AesCipher cipher = new AesCipher(settings);
try {
public void decrypt_other_key() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/other_secret_key.txt");
Settings settings = new Settings();
- settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_FILE, new File(resource.toURI()).getCanonicalPath());
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, new File(resource.toURI()).getCanonicalPath());
AesCipher cipher = new AesCipher(settings);
try {
@Test
public void encryptThenDecrypt() throws Exception {
Settings settings = new Settings();
- settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_FILE, pathToSecretKey());
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, pathToSecretKey());
AesCipher cipher = new AesCipher(settings);
assertThat(cipher.decrypt(cipher.encrypt("foo")), is("foo"));