public static EnvironmentVariableConfig fromJSON(JsonObject jsonReader) { String name = jsonReader.get("name").getAsString(); String value = jsonReader.get("value").getAsString(); boolean secure = jsonReader.has("secure") && jsonReader.get("secure").getAsBoolean(); EnvironmentVariableConfig environmentConfig = new EnvironmentVariableConfig(new GoCipher(), name, value, secure); return environmentConfig; }
public EnvironmentVariableConfig toEnvironmentVariableConfig(CREnvironmentVariable crEnvironmentVariable) { if (crEnvironmentVariable.hasEncryptedValue()) { return new EnvironmentVariableConfig(cipher, crEnvironmentVariable.getName(), crEnvironmentVariable.getEncryptedValue()); } else { String value = crEnvironmentVariable.getValue(); if(StringUtils.isBlank(value)) value = ""; return new EnvironmentVariableConfig(crEnvironmentVariable.getName(), value); } }
private SchedulingContext setEnvironmentVariables(SchedulingContext context, JobConfig config, String jobName, int counter) { EnvironmentVariablesConfig environmentVariablesConfig = new EnvironmentVariablesConfig(); int index = context.isRerun() ? getOldJobIndex(jobName) : counter; environmentVariablesConfig.add(new EnvironmentVariableConfig("GO_JOB_RUN_INDEX", Integer.toString(index))); environmentVariablesConfig.add(new EnvironmentVariableConfig("GO_JOB_RUN_COUNT", Integer.toString(config.getRunInstanceCountValue()))); context = context.overrideEnvironmentVariables(environmentVariablesConfig); return context; }
@Test public void shouldNotConsiderErrorsForEqualsCheck() { EnvironmentVariableConfig config1 = new EnvironmentVariableConfig("name", "value"); EnvironmentVariableConfig config2 = new EnvironmentVariableConfig("name", "value"); config2.addError("name", "errrr"); assertThat(config1.equals(config2), is(true)); }
@Test public void shouldGetPlainTextValue() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap attrs = getAttributeMap(plainText, "false", "1"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getValue(), is(plainText)); verify(mockGoCipher, never()).decrypt(anyString()); verify(mockGoCipher, never()).encrypt(anyString()); }
@Test public void shouldNotMaskValueIfNotSecure() { EnvironmentVariableConfig secureEnvironmentVariable = new EnvironmentVariableConfig(goCipher, "plain_key", "plain_value", false); Assert.assertThat(secureEnvironmentVariable.getDisplayValue(), is("plain_value")); }
@Test public void shouldEncryptValueWhenChanged() throws CryptoException { GoCipher mockGoCipher = mock(GoCipher.class); String plainText = "password"; String cipherText = "encrypted"; when(mockGoCipher.encrypt(plainText)).thenReturn(cipherText); when(mockGoCipher.decrypt(cipherText)).thenReturn(plainText); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap firstSubmit = getAttributeMap(plainText, "true", "true"); environmentVariableConfig.setConfigAttributes(firstSubmit); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); }
@Test public void shouldGetOnlyPlainTextVariables() throws CryptoException { EnvironmentVariablesConfig environmentVariablesConfig = new EnvironmentVariablesConfig(); EnvironmentVariableConfig plainVar1 = new EnvironmentVariableConfig("var1", "var1_value"); EnvironmentVariableConfig plainVar2 = new EnvironmentVariableConfig("var2", "var2_value"); EnvironmentVariableConfig var1 = secureVariable(goCipher, "foo1", "bar1", "encryptedBar1"); EnvironmentVariableConfig var2 = secureVariable(goCipher, "foo2", "bar2", "encryptedBar2"); environmentVariablesConfig.addAll(Arrays.asList(var1, var2, plainVar1, plainVar2)); List<EnvironmentVariableConfig> variables = environmentVariablesConfig.getPlainTextVariables(); assertThat(variables.size(), is(2)); assertThat(variables, hasItems(plainVar1, plainVar2)); }
@Test public void shouldUpdateEnvironmentVariables() { environmentConfig.addEnvironmentVariable("hello", "world"); environmentConfig.setConfigAttributes(Collections.singletonMap(BasicEnvironmentConfig.VARIABLES_FIELD, Arrays.asList(envVar("foo", "bar"), envVar("baz", "quux")))); assertThat(environmentConfig.getVariables(), hasItem(new EnvironmentVariableConfig("foo", "bar"))); assertThat(environmentConfig.getVariables(), hasItem(new EnvironmentVariableConfig("baz", "quux"))); assertThat(environmentConfig.getVariables().size(), is(2)); }
@Test public void shouldCreateEnvironmentVariableFromEnvironmentVariableConfig() { final EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig("foo", "bar"); assertThat(new EnvironmentVariable(environmentVariableConfig), is(new EnvironmentVariable("foo", "bar"))); final EnvironmentVariableConfig secureEnvironmentVariableConfig = new EnvironmentVariableConfig(new GoCipher(), "foo", "bar", true); assertThat(new EnvironmentVariable(secureEnvironmentVariableConfig), is(new EnvironmentVariable("foo", "bar", true))); }
@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 shouldReturnValueForInSecureVariables() { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher, "foo", "bar", false); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.getValue(), is("bar")); assertThat(environmentVariableConfig.getValueForDisplay(), is("bar")); }
public static StageConfig stageConfigWithParams(String stageName, String paramName) { StageConfig stageConfig = StageConfigMother.stageConfig(stageName); ArrayList<EnvironmentVariableConfig> environmentVariableConfigs = new ArrayList<>(); environmentVariableConfigs.add(new EnvironmentVariableConfig("env1", "#{" + paramName + "}")); stageConfig.setVariables(new EnvironmentVariablesConfig(environmentVariableConfigs)); stageConfig.getJobs().add(JobConfigMother.jobConfig()); return stageConfig; }
@Test public void shouldCopyEnvironmentVariableConfig() { EnvironmentVariableConfig secureEnvironmentVariable = new EnvironmentVariableConfig(goCipher, "plain_key", "plain_value", true); EnvironmentVariableConfig copy = new EnvironmentVariableConfig(secureEnvironmentVariable); assertThat(copy.getName(), is(secureEnvironmentVariable.getName())); assertThat(copy.getValue(), is(secureEnvironmentVariable.getValue())); assertThat(copy.getEncryptedValue(), is(secureEnvironmentVariable.getEncryptedValue())); assertThat(copy.isSecure(), is(secureEnvironmentVariable.isSecure())); }
@Test public void shouldNotSetEnvironmentVariableFromConfigAttributesIfNameAndValueIsEmpty() { environmentConfig.setConfigAttributes(Collections.singletonMap(BasicEnvironmentConfig.VARIABLES_FIELD, Arrays.asList(envVar("", "anything"), envVar("", "")))); assertThat(environmentConfig.errors().isEmpty(), is(true)); assertThat(environmentConfig.getVariables(), hasItem(new EnvironmentVariableConfig("", "anything"))); assertThat(environmentConfig.getVariables().size(), is(1)); }
@Test public void shouldClearEnvironmentVariablesWhenTheMapIsNull() { environmentVariablesConfig = new EnvironmentVariablesConfig(); EnvironmentVariableConfig one = new EnvironmentVariableConfig("FOO", "BAR"); environmentVariablesConfig.add(one); environmentVariablesConfig.setConfigAttributes(null); assertThat(environmentVariablesConfig.size(), is(0)); }
@Test public void shouldDeserializeWithNoErrorFlagIfAnEncryptedVariableHasCipherTextSetWithSecureTrue() throws Exception { EnvironmentVariableConfig variable = new EnvironmentVariableConfig(); variable.deserialize("PASSWORD", null, true, "cipherText"); assertTrue(variable.errors().isEmpty()); }
@Test public void shouldPopulateErrorWhenVariableNameStartsWithSpace() { environmentVariablesConfig = new EnvironmentVariablesConfig(); EnvironmentVariableConfig one = new EnvironmentVariableConfig(" foo", "BAR"); environmentVariablesConfig.add(one); environmentVariablesConfig.validate(context); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().on(EnvironmentVariableConfig.NAME), contains("Environment Variable cannot start or end with spaces for pipeline 'some-pipeline'.")); }
@Test public void shouldPopulateErrorWhenVariableNameIsEmpty() { environmentVariablesConfig = new EnvironmentVariablesConfig(); EnvironmentVariableConfig one = new EnvironmentVariableConfig("", "BAR"); environmentVariablesConfig.add(one); environmentVariablesConfig.validate(context); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().on(EnvironmentVariableConfig.NAME), contains("Environment Variable cannot have an empty name for pipeline 'some-pipeline'.")); }
@Test public void shouldPopulateErrorWhenVariableNameContainsLeadingAndTrailingSpaces() { environmentVariablesConfig = new EnvironmentVariablesConfig(); EnvironmentVariableConfig one = new EnvironmentVariableConfig(" FOO ", "BAR"); environmentVariablesConfig.add(one); environmentVariablesConfig.validate(context); assertThat(one.errors().isEmpty(), is(false)); assertThat(one.errors().on(EnvironmentVariableConfig.NAME), contains("Environment Variable cannot start or end with spaces for pipeline 'some-pipeline'.")); }