private static PluggableArtifactConfig createPluggableArtifactConfig() { return new PluggableArtifactConfig("installer", "s3", create("src", false, "build/libs.*.zip")); }
@Test public void shouldCreatePluggableArtifact() { final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID", create("Foo", false, "Bar")); assertThat(artifactConfig.getId(), is("Artifact-ID")); assertThat(artifactConfig.getStoreId(), is("Store-ID")); assertThat(artifactConfig.getArtifactType(), is(ArtifactType.external)); assertThat(artifactConfig.getArtifactTypeValue(), is("Pluggable Artifact")); assertThat(artifactConfig.getConfiguration().get(0), is(create("Foo", false, "Bar"))); }
@Test public void addConfigurations_shouldLeaveUserEnteredValuesAsIsIfArtifactStoreIsNull() throws CryptoException { PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "non-existent-store-id"); ArrayList<ConfigurationProperty> configurationProperties = new ArrayList<>(); configurationProperties.add(ConfigurationPropertyMother.create("plain", false, "plain")); configurationProperties.add(ConfigurationPropertyMother.create("secure", true, new GoCipher().encrypt("password"))); pluggableArtifactConfig.addConfigurations(configurationProperties); assertThat(pluggableArtifactConfig.getConfiguration(), is(configurationProperties)); }
public String toJSON() { final HashMap<String, Object> artifactStoreAsHashMap = new HashMap<>(); artifactStoreAsHashMap.put("id", getId()); artifactStoreAsHashMap.put("storeId", getStoreId()); artifactStoreAsHashMap.put("configuration", this.getConfiguration().getConfigurationAsMap(true)); return new Gson().toJson(artifactStoreAsHashMap); }
private void encryptSecureConfigurations(ArtifactStore artifactStore) { if (artifactStore != null && hasPluginInfo(artifactStore)) { for (ConfigurationProperty configuration : getConfiguration()) { configuration.handleSecureValueConfiguration(isSecure(configuration.getConfigKeyName(), artifactStore)); } } }
@Override public void validateUniqueness(List<ArtifactConfig> existingArtifactConfigList) { for (ArtifactConfig existingArtifactConfig : existingArtifactConfigList) { if (existingArtifactConfig instanceof PluggableArtifactConfig) { final PluggableArtifactConfig pluggableArtifactConfig = (PluggableArtifactConfig) existingArtifactConfig; if (this.getId().equalsIgnoreCase(pluggableArtifactConfig.getId())) { this.addError("id", String.format("Duplicate pluggable artifacts with id `%s` defined.", getId())); existingArtifactConfig.addError("id", String.format("Duplicate pluggable artifacts with id `%s` defined.", getId())); } if (this.getStoreId().equalsIgnoreCase(pluggableArtifactConfig.getStoreId())) { if (configuration.size() == pluggableArtifactConfig.getConfiguration().size() && this.configuration.containsAll(pluggableArtifactConfig.getConfiguration())) { this.addError("id", "Duplicate pluggable artifacts configuration defined."); existingArtifactConfig.addError("id", "Duplicate pluggable artifacts configuration defined."); } } return; } } existingArtifactConfigList.add(this); }
@Test public void setConfigAttributes_shouldSetConfigurationAsIsIfPluginIdIsBlank() throws CryptoException { HashMap<Object, Object> imageMap = new HashMap<>(); imageMap.put("value", new GoCipher().encrypt("some-encrypted-value")); imageMap.put("isSecure", "true"); HashMap<Object, Object> tagMap = new HashMap<>(); tagMap.put("value", "18.6.0"); tagMap.put("isSecure", "false"); HashMap<Object, Object> configurationMap1 = new HashMap<>(); configurationMap1.put("Image", imageMap); configurationMap1.put("Tag", tagMap); HashMap<String, Object> artifactPlan1 = new HashMap<>(); artifactPlan1.put("artifactTypeValue", "Pluggable Artifact"); artifactPlan1.put("id", "artifactId"); artifactPlan1.put("storeId", "storeId"); artifactPlan1.put("pluginId", ""); artifactPlan1.put("configuration", configurationMap1); List<Map> artifactPlansList = new ArrayList<>(); artifactPlansList.add(artifactPlan1); ArtifactConfigs artifactConfigs = new ArtifactConfigs(); artifactConfigs.setConfigAttributes(artifactPlansList); assertThat(artifactConfigs.size(), is(1)); PluggableArtifactConfig artifactConfig = (PluggableArtifactConfig) artifactConfigs.get(0); assertThat(artifactConfig.getArtifactType(), is(ArtifactType.external)); assertThat(artifactConfig.getId(), is("artifactId")); assertThat(artifactConfig.getStoreId(), is("storeId")); assertThat(artifactConfig.getConfiguration().getProperty("Image").getValue(), is("some-encrypted-value")); assertThat(artifactConfig.getConfiguration().getProperty("Tag").getValue(), is("18.6.0")); }
@Test public void shouldHandleEncryptionOfConfigPropertiesIfStoreIdIsAValidParam() throws Exception { GoCipher goCipher = new GoCipher(); ArrayList<PluginConfiguration> pluginConfigurations = new ArrayList<>(); pluginConfigurations.add(new PluginConfiguration("key1", new Metadata(true, true))); pluginConfigurations.add(new PluginConfiguration("key2", new Metadata(true, false))); when(artifactPluginInfo.getArtifactConfigSettings()).thenReturn(new PluggableInstanceSettings(pluginConfigurations)); ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("id", "#{storeId}", secureProperty, nonSecureProperty); PluggableArtifactConfig preprocessedPluggableArtifactConfig = new PluggableArtifactConfig("id", "store-id", secureProperty, nonSecureProperty); BasicCruiseConfig cruiseConfig = GoConfigMother.defaultCruiseConfig(); cruiseConfig.getArtifactStores().add(new ArtifactStore("store-id", "cd.go.s3")); pluggableArtifactConfig.encryptSecureProperties(cruiseConfig, preprocessedPluggableArtifactConfig); assertThat(secureProperty.isSecure(), is(true)); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(nonSecureProperty.isSecure(), is(false)); assertThat(nonSecureProperty.getValue(), is("value2")); }
@Test public void validate_shouldValidateUniquenessOnId() { final PluggableArtifactConfig existingConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID"); final List<ArtifactConfig> artifactConfigConfigs = Arrays.asList(existingConfig); final PluggableArtifactConfig newConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID"); newConfig.validateUniqueness(artifactConfigConfigs); assertTrue(newConfig.hasErrors()); assertTrue(existingConfig.hasErrors()); assertThat(newConfig.errors().on("id"), is("Duplicate pluggable artifacts with id `Artifact-ID` defined.")); assertThat(existingConfig.errors().on("id"), is("Duplicate pluggable artifacts with id `Artifact-ID` defined.")); }
@Test public void validate_shouldValidateArtifactPropertiesConfigurationKeyUniqueness() { final ValidationContext validationContext = mock(ValidationContext.class); final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID", create("Foo", false, "Bar"), create("Foo", true, "Bar")); final ArtifactStores artifactStores = mock(ArtifactStores.class); assertFalse(artifactConfig.hasErrors()); when(validationContext.artifactStores()).thenReturn(artifactStores); when(artifactStores.find("Store-ID")).thenReturn(new ArtifactStore("Store-ID", "pluginId")); artifactConfig.validate(validationContext); Configuration configuration = artifactConfig.getConfiguration(); assertThat(configuration.get(0).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); assertThat(configuration.get(1).errors().getAllOn("configurationKey"), is(Arrays.asList("Duplicate key 'Foo' found for Pluggable Artifact"))); }
@Test public void validate_shouldAddAnErrorIfArtifactIdIsInvalid() { PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("asf@%", "f"); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("docker", "cd.go.docker")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); assertThat(artifactConfig.errors().getAllOn("id"), is(Arrays.asList("Invalid pluggable artifact id name 'asf@%'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters."))); }
@Test public void hasValidPluginAndStore_shouldReturnTrueIfPluginAndStoreExist() { PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig("dist", "s3"); assertTrue(pluggableArtifactConfig.hasValidPluginAndStore(new ArtifactStore("s3", "cd.go.s3"))); } }
String pluginId = (String) attrMap.get("pluginId"); Map<String, Object> userSpecifiedConfiguration = (Map<String, Object>) attrMap.get("configuration"); PluggableArtifactConfig pluggableArtifactConfig = new PluggableArtifactConfig(artifactId, storeId); this.add(pluggableArtifactConfig); if (userSpecifiedConfiguration == null) { Configuration configuration = pluggableArtifactConfig.getConfiguration(); pluggableArtifactConfig.getConfiguration().addNewConfigurationWithValue(configuration.getKey(), String.valueOf(configuration.getValue()), false);
@Test public void shouldSerializeToJson() { final PluggableArtifactConfig config = new PluggableArtifactConfig("id1", "Store-ID", create("Foo", false, "Bar")); final String actual = config.toJSON(); assertThat(actual, is("{\"configuration\":{\"Foo\":\"Bar\"},\"id\":\"id1\",\"storeId\":\"Store-ID\"}")); }
@Test public void validate_shouldValidatePluggableArtifactStoreId() { final ValidationContext validationContext = mock(ValidationContext.class); final PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig("Artifact-ID", "Store-ID"); final ArtifactStores artifactStores = mock(ArtifactStores.class); assertFalse(artifactConfig.hasErrors()); when(validationContext.artifactStores()).thenReturn(artifactStores); when(validationContext.isWithinPipelines()).thenReturn(true); when(validationContext.getPipeline()).thenReturn(PipelineConfigMother.pipelineConfig("pipe")); when(artifactStores.find("Store-ID")).thenReturn(null); artifactConfig.validate(validationContext); assertTrue(artifactConfig.hasErrors()); assertThat(artifactConfig.errors().getAll(), hasSize(1)); assertThat(artifactConfig.errors().getAllOn("storeId"), hasSize(1)); assertThat(artifactConfig.errors().on("storeId"), is("Artifact store with id `Store-ID` does not exist. Please correct the `storeId` attribute on pipeline `pipe`.")); }
public void validateExternalArtifactConfig(PluggableArtifactConfig preprocessedPluggableArtifactConfig, ArtifactStore artifactStore, boolean addPluginIdError) { if (preprocessedPluggableArtifactConfig.hasValidPluginAndStore(artifactStore)) { String pluginId = artifactStore.getPluginId(); try { ValidationResult validationResult = artifactExtension.validatePluggableArtifactConfig(pluginId, preprocessedPluggableArtifactConfig.getConfiguration().getConfigurationAsMap(true)); mapErrorsToConfiguration(validationResult, preprocessedPluggableArtifactConfig.getConfiguration(), preprocessedPluggableArtifactConfig); } catch (PluginNotFoundException e) { preprocessedPluggableArtifactConfig.addError("pluginId", String.format("Plugin with id `%s` is not found.", pluginId)); } } else { if (addPluginIdError) { preprocessedPluggableArtifactConfig.addError("pluginId", "Could not determine the plugin to perform the plugin validations. Possible reasons: artifact store does not exist or plugin is not installed."); } } }
public void addConfigurations(List<ConfigurationProperty> configurationProperties) { this.getConfiguration().addAll(configurationProperties); } }
public List<PluggableArtifactConfig> findByStoreId(String storeId) { final ArrayList<PluggableArtifactConfig> pluggableArtifactConfigs = new ArrayList<>(); for (PluggableArtifactConfig artifact : getPluggableArtifactConfigs()) { if (artifact.getStoreId().equals(storeId)) { pluggableArtifactConfigs.add(artifact); } } return pluggableArtifactConfigs; }
@Test public void validateTree_shouldValidateNullId() { PluggableArtifactConfig artifactConfig = new PluggableArtifactConfig(null, "s3"); final ArtifactStores artifactStores = new ArtifactStores(new ArtifactStore("s3", "cd.go.s3")); final boolean result = artifactConfig.validateTree(ValidationContextMother.validationContext(artifactStores)); assertFalse(result); }
public void encryptSecureProperties(CruiseConfig preprocessedCruiseConfig, PluggableArtifactConfig preprocessedPluggableArtifactConfig) { if (storeId != null) { ArtifactStore artifactStore = preprocessedCruiseConfig.getArtifactStores().find(preprocessedPluggableArtifactConfig.getStoreId()); encryptSecureConfigurations(artifactStore); } }