public boolean isPlain() { return !isSecure(); }
public String getDisplayValue() { if (isSecure()) return "****"; return getValue(); }
public EnvironmentVariablesConfig getSecureVariables() { EnvironmentVariablesConfig result = new EnvironmentVariablesConfig(); for (EnvironmentVariableConfig environmentVariableConfig : this) { if (environmentVariableConfig.isSecure()) { result.add(environmentVariableConfig); } } return result; }
void addTo(EnvironmentVariableContext context) { context.setProperty(name, getValue(), isSecure()); }
public EnvironmentVariable(EnvironmentVariableConfig environmentVariableConfig) { this(environmentVariableConfig.getName(), environmentVariableConfig.getValue(), environmentVariableConfig.isSecure()); }
private static void addValue(OutputWriter outputWriter, EnvironmentVariableConfig environmentVariableConfig) { if (environmentVariableConfig.isSecure()) { outputWriter.add("encrypted_value", environmentVariableConfig.getDisplayValue()); } else { outputWriter.add("value", environmentVariableConfig.getDisplayValue()); } } }
public static void toJSON(OutputWriter outputWriter, EnvironmentVariableConfig environmentVariableConfig) { outputWriter .add("secure", environmentVariableConfig.isSecure()) .add("name", environmentVariableConfig.getName()); addValue(outputWriter, environmentVariableConfig); }
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 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()); } }
@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 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 shouldRetainEncryptedVariableWhenNotEdited() 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); when(mockGoCipher.encrypt(cipherText)).thenReturn("SHOULD NOT DO THIS"); EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(mockGoCipher); HashMap firstSubmit = getAttributeMap(plainText, "true", "true"); environmentVariableConfig.setConfigAttributes(firstSubmit); HashMap secondSubmit = getAttributeMap(cipherText, "true", "false"); environmentVariableConfig.setConfigAttributes(secondSubmit); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); assertThat(environmentVariableConfig.getName(), is("foo")); assertThat(environmentVariableConfig.isSecure(), is(true)); verify(mockGoCipher, never()).encrypt(cipherText); }
@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 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))))); }
@Test public void shouldAllowAnEnvironmentVariableToBeMarkedAsSecure_WithEncryptedValueInItsOwnTag() throws Exception { String value = "abc"; String encryptedValue = new GoCipher().encrypt(value); String content = configWithPipeline(format( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" secure=\"true\"><encryptedValue>%s</encryptedValue></variable>\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", encryptedValue), CONFIG_SCHEMA_VERSION); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("some_pipeline")); EnvironmentVariablesConfig variables = pipelineConfig.getVariables(); assertThat(variables.size(), is(1)); EnvironmentVariableConfig environmentVariableConfig = variables.get(0); assertThat(environmentVariableConfig.getEncryptedValue(), is(encryptedValue)); assertThat(environmentVariableConfig.isSecure(), is(true)); }
@Test public void shouldAllowAnEnvironmentVariableToBeMarkedAsSecure_WithValueInItsOwnTag() throws Exception { String cipherText = new GoCipher().encrypt("plainText"); String content = configWithPipeline( "<pipeline name='some_pipeline'>" + "<environmentvariables>\n" + " <variable name=\"var_name\" secure=\"true\"><encryptedValue>" + cipherText + "</encryptedValue></variable>\n" + " </environmentvariables>" + " <materials>" + " <svn url='svnurl'/>" + " </materials>" + " <stage name='some_stage'>" + " <jobs>" + " <job name='some_job'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", CONFIG_SCHEMA_VERSION); CruiseConfig config = ConfigMigrator.loadWithMigration(content).config; PipelineConfig pipelineConfig = config.pipelineConfigByName(new CaseInsensitiveString("some_pipeline")); EnvironmentVariablesConfig variables = pipelineConfig.getVariables(); assertThat(variables.size(), is(1)); EnvironmentVariableConfig environmentVariableConfig = variables.get(0); assertThat(environmentVariableConfig.getEncryptedValue(), is(cipherText)); assertThat(environmentVariableConfig.isSecure(), is(true)); }