public EnvironmentVariableConfig getVariable(String variableName) { for (EnvironmentVariableConfig variableConfig : this) { if (variableConfig.getName().equals(variableName)) { return variableConfig; } } return null; }
public void removeIfExists(String name) { EnvironmentVariableConfig configToRemove = null; for (EnvironmentVariableConfig config : this) { if (config.getName().equals(name)) { configToRemove = config; break; } } if (configToRemove != null) { this.remove(configToRemove); } }
private boolean hasUsedUnconfiguredVariable(String pipelineName, EnvironmentVariablesConfig environmentVariables, OperationResult result) { for (EnvironmentVariableConfig variable : environmentVariables) { if (!goConfigService.hasVariableInScope(pipelineName, variable.getName())) { String variableUnconfiguredMessage = String.format("Variable '%s' has not been configured for pipeline '%s'", variable.getName(), pipelineName); result.notFound(variableUnconfiguredMessage, variableUnconfiguredMessage, HealthStateType.general(HealthStateScope.forPipeline(pipelineName))); return true; } } return false; }
public String getValue() { if (isSecure) { try { return goCipher.decrypt(encryptedValue.getValue()); } catch (CryptoException e) { throw new RuntimeException(format("Could not decrypt secure environment variable value for name %s", getName()), e); } } else { return value.getValue(); } }
@Override public void build(ScheduleOptions scheduleOptions, HttpOperationResult result, String pipelineName, PipelineScheduleOptions pipelineScheduleOptions, HealthStateType healthStateType) { for (EnvironmentVariableConfig environmentVariable : pipelineScheduleOptions.getPlainTextEnvironmentVariables()) { if (!goConfigService.hasVariableInScope(pipelineName, environmentVariable.getName())) { String variableUnconfiguredMessage = String.format("Variable '%s' has not been configured for pipeline '%s'", environmentVariable.getName(), pipelineName); result.unprocessibleEntity("Request to schedule pipeline rejected", variableUnconfiguredMessage, healthStateType); return; } scheduleOptions.getVariables().add(environmentVariable); } } }
CREnvironmentVariable environmentVariableConfigToCREnvironmentVariable(EnvironmentVariableConfig environmentVariableConfig) { if (environmentVariableConfig.isSecure()) { return new CREnvironmentVariable(environmentVariableConfig.getName(), null, environmentVariableConfig.getEncryptedValue()); } else { String value = environmentVariableConfig.getValue(); if(StringUtils.isBlank(value)) value = ""; return new CREnvironmentVariable(environmentVariableConfig.getName(), value); } }
public EnvironmentVariable(EnvironmentVariableConfig environmentVariableConfig) { this(environmentVariableConfig.getName(), environmentVariableConfig.getValue(), environmentVariableConfig.isSecure()); }
public void validate(ValidationContext validationContext) { try { getValue(); } catch (Exception e) { errors().add(VALUE, String.format("Encrypted value for variable named '%s' is invalid. This usually happens when the cipher text is modified to have an invalid value.", getName())); } }
private void validateDuplicateEnvironmentVariables(ValidationContext validationContext) { EnvironmentVariablesConfig allVariables = new EnvironmentVariablesConfig(); for (EnvironmentConfig part : this) { for (EnvironmentVariableConfig partVariable : part.getVariables()) { if (!allVariables.hasVariable(partVariable.getName())) { allVariables.add(partVariable); } else { //then it must be equal if (!allVariables.contains(partVariable)) configErrors.add(CONSISTENT_KV, String.format( "Environment variable '%s' is defined more than once with different values", partVariable.getName())); } } } }
public EnvironmentVariablesConfig overrideWith(EnvironmentVariablesConfig environmentVariablesConfig) { EnvironmentVariablesConfig variablesConfig = new EnvironmentVariablesConfig(); variablesConfig.addAll(this); for (EnvironmentVariableConfig environmentVariableConfig : environmentVariablesConfig) { variablesConfig.removeIfExists(environmentVariableConfig.getName()); variablesConfig.add(environmentVariableConfig); } return variablesConfig; }
public static void toJSON(OutputWriter outputWriter, EnvironmentVariableConfig environmentVariableConfig) { outputWriter .add("secure", environmentVariableConfig.isSecure()) .add("name", environmentVariableConfig.getName()); addValue(outputWriter, environmentVariableConfig); }
@Override public void build(ScheduleOptions scheduleOptions, HttpOperationResult result, String pipelineName, PipelineScheduleOptions pipelineScheduleOptions, HealthStateType healthStateType) { for (EnvironmentVariableConfig environmentVariable : pipelineScheduleOptions.getSecureEnvironmentVariables()) { if (!goConfigService.hasVariableInScope(pipelineName, environmentVariable.getName())) { String variableUnconfiguredMessage = String.format("Variable '%s' has not been configured for pipeline '%s'", environmentVariable.getName(), pipelineName); result.unprocessibleEntity("Request to schedule pipeline rejected", variableUnconfiguredMessage, healthStateType); return; } environmentVariable.validate(null); if (!environmentVariable.errors().isEmpty()) { result.unprocessibleEntity("Request to schedule pipeline rejected", environmentVariable.errors().asString(), healthStateType); return; } scheduleOptions.getVariables().add(environmentVariable); } } }
@Test public void shouldAssignNameAndValueForAVanillaEnvironmentVariable() { EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig((GoCipher) null); HashMap attrs = new HashMap(); attrs.put(EnvironmentVariableConfig.NAME, "foo"); attrs.put(EnvironmentVariableConfig.VALUE, "password"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getValue(), is("password")); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(false)); }
@Test public void shouldReturnEncryptedValueForSecureVariables() throws CryptoException { when(goCipher.encrypt("bar")).thenReturn("encrypted"); when(goCipher.decrypt("encrypted")).thenReturn("bar"); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher, "foo", "bar", true); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.getValue(), is("bar")); assertThat(environmentVariableConfig.getValueForDisplay(), is(environmentVariableConfig.getEncryptedValue())); }
@Test public void shouldEncryptValueWhenConstructedAsSecure() throws CryptoException { GoCipher goCipher = mock(GoCipher.class); String encryptedText = "encrypted"; when(goCipher.encrypt("password")).thenReturn(encryptedText); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher); HashMap attrs = getAttributeMap("password", "true", "true"); environmentVariableConfig.setConfigAttributes(attrs); assertThat(environmentVariableConfig.getEncryptedValue(), is(encryptedText)); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(true)); }
@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")); }
@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())); }
public static void toJSON(OutputWriter jsonWriter, EnvironmentVariableConfig environmentVariableConfig) { if (!environmentVariableConfig.errors().isEmpty()) { jsonWriter.addChild("errors", errorWriter -> { HashMap<String, String> mapping = new HashMap<>(); mapping.put("encryptedValue", "encrypted_value"); new ErrorGetter(mapping).toJSON(jsonWriter, environmentVariableConfig); }); } jsonWriter.add("secure", environmentVariableConfig.isSecure()); jsonWriter.add("name", environmentVariableConfig.getName()); if (environmentVariableConfig.isPlain()) { jsonWriter.addIfNotNull("value", environmentVariableConfig.getValueForDisplay()); } if (environmentVariableConfig.isSecure()) { jsonWriter.addIfNotNull("encrypted_value", environmentVariableConfig.getValueForDisplay()); } }
public static void toJSON(OutputWriter jsonWriter, EnvironmentVariableConfig environmentVariableConfig) { if (!environmentVariableConfig.errors().isEmpty()) { jsonWriter.addChild("errors", errorWriter -> { HashMap<String, String> mapping = new HashMap<>(); mapping.put("encryptedValue", "encrypted_value"); new ErrorGetter(mapping).toJSON(jsonWriter, environmentVariableConfig); }); } jsonWriter.add("secure", environmentVariableConfig.isSecure()); jsonWriter.add("name", environmentVariableConfig.getName()); if (environmentVariableConfig.isPlain()) { jsonWriter.addIfNotNull("value", environmentVariableConfig.getValueForDisplay()); } if (environmentVariableConfig.isSecure()) { jsonWriter.addIfNotNull("encrypted_value", environmentVariableConfig.getValueForDisplay()); } }
public static void toJSON(OutputWriter writer, TriggerOptions triggerOptions) { PipelineInstanceModel pipelineInstanceModel = triggerOptions.getPipelineInstanceModel(); writer .addLinks(outputLinkWriter -> outputLinkWriter.addAbsoluteLink("doc", Routes.Pipeline.DOC_TRIGGER_OPTIONS) .addLink("self", Routes.Pipeline.triggerOptions(pipelineInstanceModel.getName())) .addLink("schedule", Routes.Pipeline.schedule(pipelineInstanceModel.getName()))) .addChildList("variables", outputListWriter -> triggerOptions.getVariables().forEach(env -> outputListWriter.addChild(envWriter -> { envWriter .add("name", env.getName()) .add("secure", env.isSecure()); if (!env.isSecure()) { envWriter.add("value", env.getValue()); } }) )) .addChildList("materials", outputListWriter -> pipelineInstanceModel.getMaterials() .forEach(material -> outputListWriter.addChild(material(material, pipelineInstanceModel.findCurrentMaterialRevisionForUI(material))))); }