public static EnvironmentVariableConfig fromJSON(JsonReader jsonReader) { String name = jsonReader.getString("name"); Boolean secure = jsonReader.optBoolean("secure").orElse(false); String value = secure ? jsonReader.optString("value").orElse(null) : jsonReader.getString("value"); String encryptedValue = jsonReader.optString("encrypted_value").orElse(null); try { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(); environmentVariableConfig.deserialize(name, value, secure, encryptedValue); return environmentVariableConfig; } catch (CryptoException e) { throw new InvalidGoCipherTextRuntimeException(e.getMessage(), e); } }
public static EnvironmentVariableConfig fromJSON(JsonReader jsonReader) { String name = jsonReader.getString("name"); Boolean secure = jsonReader.optBoolean("secure").orElse(false); String value = secure ? jsonReader.optString("value").orElse(null) : jsonReader.getString("value"); String encryptedValue = jsonReader.optString("encrypted_value").orElse(null); try { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(); environmentVariableConfig.deserialize(name, value, secure, encryptedValue); return environmentVariableConfig; } catch (CryptoException e) { throw new InvalidGoCipherTextRuntimeException(e.getMessage(), e); } }
@Test public void shouldDeserializeWithErrorFlagIfAnEncryptedVarialeHasBothClearTextAndCipherText() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", "clearText", true, "c!ph3rt3xt"); assertThat(variable.errors().getAllOn("value"), is(Arrays.asList("You may only specify `value` or `encrypted_value`, not both!"))); assertThat(variable.errors().getAllOn("encryptedValue"), is(Arrays.asList("You may only specify `value` or `encrypted_value`, not both!"))); }
@Test public void shouldErrorOutForEncryptedValueBeingSetWhenSecureIsFalse() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", null, false, "cipherText"); variable.validateTree(null); assertThat(variable.errors().getAllOn("encryptedValue"), is(Arrays.asList("You may specify encrypted value only when option 'secure' is true."))); } }
@Test public void shouldDeserializeWithNoErrorFlagIfAnEncryptedVariableHasCipherTextSetWithSecureTrue() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", null, true, "cipherText"); assertTrue(variable.errors().isEmpty()); }
@Test public void shouldDeserializeWithNoErrorFlagIfAnEncryptedVarialeHasClearTextWithSecureTrue() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", "clearText", true, null); assertTrue(variable.errors().isEmpty()); }
@Test public void shouldDeserializeWithNoErrorFlagIfAnEncryptedVarialeHasEitherClearTextWithSecureFalse() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", "clearText", false, null); assertTrue(variable.errors().isEmpty()); }