private String errorKey = null;
- private static Result newError(String key) {
+ private static Result newError(@Nullable String key) {
return new Result(key);
}
+ @Nullable
private Result(String errorKey) {
this.errorKey = errorKey;
}
return StringUtils.isBlank(errorKey);
}
- public @Nullable String getErrorKey() {
+ @Nullable
+ public String getErrorKey() {
return errorKey;
}
}
}
public String[] getOptions() {
- return options;
+ return options.clone();
}
public String getDescription() {
import org.sonar.api.ServerComponent;
import org.sonar.api.utils.DateUtils;
+import javax.annotation.Nullable;
import java.util.*;
/**
return this;
}
- public final Settings setProperty(String key, String value) {
+ public final Settings setProperty(String key, @Nullable String value) {
if (!clearIfNullValue(key, value)) {
properties.put(key, StringUtils.trim(value));
}
return this;
}
- public final Settings setProperty(String key, Boolean value) {
+ public final Settings setProperty(String key, @Nullable Boolean value) {
if (!clearIfNullValue(key, value)) {
properties.put(key, String.valueOf(value));
}
return this;
}
- public final Settings setProperty(String key, Integer value) {
+ public final Settings setProperty(String key, @Nullable Integer value) {
if (!clearIfNullValue(key, value)) {
properties.put(key, String.valueOf(value));
}
return this;
}
- public final Settings setProperty(String key, Long value) {
+ public final Settings setProperty(String key, @Nullable Long value) {
if (!clearIfNullValue(key, value)) {
properties.put(key, String.valueOf(value));
}
return this;
}
- public final Settings setProperty(String key, Double value) {
+ public final Settings setProperty(String key, @Nullable Double value) {
if (!clearIfNullValue(key, value)) {
properties.put(key, String.valueOf(value));
}
return this;
}
- public final Settings setProperty(String key, Date date) {
+ public final Settings setProperty(String key, @Nullable Date date) {
return setProperty(key, date, false);
}
return addProperties(props);
}
- public final Settings setProperty(String key, Date date, boolean includeTime) {
+ public final Settings setProperty(String key, @Nullable Date date, boolean includeTime) {
if (!clearIfNullValue(key, date)) {
properties.put(key, includeTime ? DateUtils.formatDateTime(date) : DateUtils.formatDate(date));
}
return definitions;
}
- private boolean clearIfNullValue(String key, Object value) {
+ private boolean clearIfNullValue(String key, @Nullable Object value) {
if (value == null) {
properties.remove(key);
return true;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
+import org.hamcrest.Matchers;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.CoreProperties;
import javax.crypto.BadPaddingException;
public class AesCipherTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Test
public void generateRandomSecretKey() {
AesCipher cipher = new AesCipher(new Settings());
assertThat(Base64.isArrayByteBase64(encryptedText.getBytes()), is(true));
}
+ @Test
+ public void encrypt_bad_key() throws Exception {
+ thrown.expect(RuntimeException.class);
+ thrown.expectMessage("Invalid AES key");
+
+ URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/bad_secret_key.txt");
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, new File(resource.toURI()).getCanonicalPath());
+ AesCipher cipher = new AesCipher(settings);
+
+ cipher.encrypt("this is a secret");
+ }
+
@Test
public void decrypt() throws Exception {
Settings settings = new Settings();
assertThat(secretKey.getEncoded().length, greaterThan(10));
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void loadSecretKeyFromFile_file_does_not_exist() throws Exception {
+ thrown.expect(IllegalStateException.class);
+
AesCipher cipher = new AesCipher(new Settings());
cipher.loadSecretFileFromFile("/file/does/not/exist");
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void loadSecretKeyFromFile_no_property() throws Exception {
+ thrown.expect(IllegalStateException.class);
+
AesCipher cipher = new AesCipher(new Settings());
cipher.loadSecretFileFromFile(null);
}
+ @Test
+ public void hasSecretKey() throws Exception {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, pathToSecretKey());
+ AesCipher cipher = new AesCipher(settings);
+
+ assertThat(cipher.hasSecretKey(), Matchers.is(true));
+ }
+
+ @Test
+ public void doesNotHaveSecretKey() throws Exception {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.ENCRYPTION_SECRET_KEY_PATH, "/my/twitter/id/is/SimonBrandhof");
+ AesCipher cipher = new AesCipher(settings);
+
+ assertThat(cipher.hasSecretKey(), Matchers.is(false));
+ }
+
+
private String pathToSecretKey() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/aes_secret_key.txt");
return new File(resource.toURI()).getCanonicalPath();