private Optional<String> getInternal(String key) { Optional<String> value = Optional.ofNullable(properties.get(key)); if (!value.isPresent()) { // default values cannot be encrypted, so return value as-is. return Optional.ofNullable(definitions.getDefaultValue(key)); } if (encryption.isEncrypted(value.get())) { try { return Optional.of(encryption.decrypt(value.get())); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + key + ". Please check your secret key.", e); } } return value; }
public ScannerProperties(Map<String, String> properties) { encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); Map<String, String> decryptedProps = new HashMap<>(properties.size()); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = decryptedProps; }
/** * The effective value of the specified property. Can return * {@code null} if the property is not set and has no * defined default value. * <p> * If the property is encrypted with a secret key, * then the returned value is decrypted. * </p> * * @throws IllegalStateException if value is encrypted but fails to be decrypted. */ @CheckForNull public String getString(String key) { String effectiveKey = definitions.validKey(key); Optional<String> value = getRawString(effectiveKey); if (!value.isPresent()) { // default values cannot be encrypted, so return value as-is. return getDefaultValue(effectiveKey); } if (encryption.isEncrypted(value.get())) { try { return encryption.decrypt(value.get()); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + effectiveKey + ". Please check your secret key.", e); } } return value.get(); }
@Test public void decrypt_uncrypted_text() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("foo")).isEqualTo("foo"); } }
@Test public void decrypt() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("{b64}Zm9v")).isEqualTo("foo"); }
@Test public void decrypt_unknown_algorithm() { Encryption encryption = new Encryption(null); assertThat(encryption.decrypt("{xxx}Zm9v")).isEqualTo("{xxx}Zm9v"); }
@Test public void generate_valid_secret_key() throws IOException { GenerateSecretKeyWsResponse result = call(); String secretKey = result.getSecretKey(); File file = temporaryFolder.newFile(); FileUtils.writeStringToFile(file, secretKey); encryption.setPathToSecretKey(file.getAbsolutePath()); String encryptedValue = encryption.encrypt("my value"); String decryptedValue = encryption.decrypt(encryptedValue); assertThat(decryptedValue).isEqualTo("my value"); }
public UserProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { encryption = new Encryption(pathToSecretKey); Map<String, String> decryptedProps = Maps.newHashMap(); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = Maps.newHashMap(decryptedProps); }
public UserProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { encryption = new Encryption(pathToSecretKey); Map<String, String> decryptedProps = Maps.newHashMap(); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = Maps.newHashMap(decryptedProps); }
private Optional<String> getInternal(String key) { Optional<String> value = Optional.ofNullable(properties.get(key)); if (!value.isPresent()) { // default values cannot be encrypted, so return value as-is. return Optional.ofNullable(definitions.getDefaultValue(key)); } if (encryption.isEncrypted(value.get())) { try { return Optional.of(encryption.decrypt(value.get())); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + key + ". Please check your secret key.", e); } } return value; }
public ScannerProperties(Map<String, String> properties) { encryption = new Encryption(properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); Map<String, String> decryptedProps = new HashMap<>(properties.size()); for (Map.Entry<String, String> entry : properties.entrySet()) { String value = entry.getValue(); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); } } decryptedProps.put(entry.getKey(), value); } this.properties = decryptedProps; }
public String getString(String key) { String value = getClearString(key); if (value != null && encryption.isEncrypted(value)) { try { value = encryption.decrypt(value); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + key + ". Please check your secret key.", e); } } return value; }
/** * The effective value of the specified property. Can return * {@code null} if the property is not set and has no * defined default value. * <p> * If the property is encrypted with a secret key, * then the returned value is decrypted. * </p> * * @throws IllegalStateException if value is encrypted but fails to be decrypted. */ @CheckForNull public String getString(String key) { String effectiveKey = definitions.validKey(key); Optional<String> value = getRawString(effectiveKey); if (!value.isPresent()) { // default values cannot be encrypted, so return value as-is. return getDefaultValue(effectiveKey); } if (encryption.isEncrypted(value.get())) { try { return encryption.decrypt(value.get()); } catch (Exception e) { throw new IllegalStateException("Fail to decrypt the property " + effectiveKey + ". Please check your secret key.", e); } } return value.get(); }