Search in sources :

Example 1 with Rules

use of com.thoughtworks.go.config.rules.Rules in project gocd by gocd.

the class PartialConfigServiceIntegrationTest method onFailedPartialConfig_shouldRemoveLastValidPartialsFromConfigInCaseOfRuleViolations.

@Test
public void onFailedPartialConfig_shouldRemoveLastValidPartialsFromConfigInCaseOfRuleViolations() {
    partialConfigService.onSuccessPartialConfig(repoConfig1, PartialConfigMother.withPipeline("p1_repo1", new RepoConfigOrigin(repoConfig1, "1")));
    assertThat(goConfigDao.loadConfigHolder().config.getAllPipelineNames().contains(new CaseInsensitiveString("p1_repo1")), is(true));
    assertThat(serverHealthService.filterByScope(HealthStateScope.forPartialConfigRepo(repoConfig1.getRepo().getFingerprint())).isEmpty(), is(true));
    goConfigRepoConfigDataSource.onConfigRepoConfigChange(repoConfig1);
    ConfigRepoConfig repoConfig1Cloned = createConfigRepoWithDefaultRules(git("url1"), "plugin", "id-1");
    repoConfig1Cloned.setRules(new Rules());
    partialConfigService.onFailedPartialConfig(repoConfig1Cloned, null);
    assertThat(cachedGoPartials.getValid(repoConfig1Cloned.getRepo().getFingerprint()), is(nullValue()));
    assertThat(goConfigDao.loadConfigHolder().config.getAllPipelineNames().contains(new CaseInsensitiveString("p1_repo1")), is(false));
    cachedGoPartials.clear();
}
Also used : ConfigRepoConfig(com.thoughtworks.go.config.remote.ConfigRepoConfig) RepoConfigOrigin(com.thoughtworks.go.config.remote.RepoConfigOrigin) Rules(com.thoughtworks.go.config.rules.Rules) Test(org.junit.jupiter.api.Test)

Example 2 with Rules

use of com.thoughtworks.go.config.rules.Rules in project gocd by gocd.

the class PartialConfigServiceIntegrationTest method shouldRemovePipelinesWhenRulesAreUpdatedToSpecifyNoWhitelist.

// See Error #1 from https://github.com/gocd/gocd/issues/8368
@Test
public void shouldRemovePipelinesWhenRulesAreUpdatedToSpecifyNoWhitelist() {
    partialConfigService.onSuccessPartialConfig(repoConfig1, PartialConfigMother.withPipeline("p1_repo1", new RepoConfigOrigin(repoConfig1, "1")));
    assertThat(goConfigDao.loadConfigHolder().config.getAllPipelineNames().contains(new CaseInsensitiveString("p1_repo1")), is(true));
    assertThat(serverHealthService.filterByScope(HealthStateScope.forPartialConfigRepo(repoConfig1.getRepo().getFingerprint())).isEmpty(), is(true));
    goConfigRepoConfigDataSource.onConfigRepoConfigChange(repoConfig1);
    ConfigRepoConfig repoConfig1Cloned = createConfigRepoWithDefaultRules(git("url1"), "plugin", "id-1");
    repoConfig1Cloned.setRules(new Rules());
    partialConfigService.onSuccessPartialConfig(repoConfig1Cloned, PartialConfigMother.withPipeline("p1_repo1", new RepoConfigOrigin(repoConfig1Cloned, "1")));
    assertThat(goConfigDao.loadConfigHolder().config.getAllPipelineNames().contains(new CaseInsensitiveString("p1_repo1")), is(false));
    assertThat(serverHealthService.filterByScope(HealthStateScope.forPartialConfigRepo(repoConfig1)).isEmpty(), is(false));
    ServerHealthState healthStateForInvalidConfigMerge = serverHealthService.filterByScope(HealthStateScope.forPartialConfigRepo(repoConfig1)).get(0);
    assertThat(healthStateForInvalidConfigMerge.getMessage(), is("Invalid Merged Configuration"));
    assertThat(healthStateForInvalidConfigMerge.getDescription(), is("Number of errors: 1+\n" + "I. Rule Validation Errors: \n" + "\t1. Not allowed to refer to pipeline group 'group'. Check the 'Rules' of this config repository.;; \n" + "\n" + "II. Config Validation Errors: \n" + "- For Config Repo: url1 at 1"));
    assertThat(healthStateForInvalidConfigMerge.getLogLevel(), is(HealthStateLevel.ERROR));
    cachedGoPartials.clear();
}
Also used : ConfigRepoConfig(com.thoughtworks.go.config.remote.ConfigRepoConfig) RepoConfigOrigin(com.thoughtworks.go.config.remote.RepoConfigOrigin) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) Rules(com.thoughtworks.go.config.rules.Rules) Test(org.junit.jupiter.api.Test)

Example 3 with Rules

use of com.thoughtworks.go.config.rules.Rules in project gocd by gocd.

the class MagicalGoConfigXmlLoaderTest method shouldLoadSecretConfigs.

@Test
void shouldLoadSecretConfigs() {
    String content = config("<secretConfigs>" + "<secretConfig id=\"my_secret\" pluginId=\"gocd_file_based_plugin\">\n" + "    <description>All secrets for env1</description>" + "    <configuration>" + "       <property>\n" + "           <key>PasswordFilePath</key>\n" + "           <value>/godata/config/password.properties</value>\n" + "       </property>\n" + "    </configuration>" + "    <rules>\n" + "        <deny action=\"refer\" type=\"pipeline_group\">my_group</deny>\n" + "        <allow action=\"refer\" type=\"pipeline_group\">other_group</allow>  \n" + "    </rules>\n" + "</secretConfig>" + "</secretConfigs>", 116);
    CruiseConfig config = ConfigMigrator.load(content);
    SecretConfigs secretConfigs = config.getSecretConfigs();
    assertThat(secretConfigs.size()).isEqualTo(1);
    SecretConfig secretConfig = secretConfigs.first();
    assertThat(secretConfig.getId()).isEqualTo("my_secret");
    assertThat(secretConfig.getPluginId()).isEqualTo("gocd_file_based_plugin");
    assertThat(secretConfig.getDescription()).isEqualTo("All secrets for env1");
    Configuration configuration = secretConfig.getConfiguration();
    assertThat(configuration.size()).isEqualTo(1);
    assertThat(configuration.getProperty("PasswordFilePath").getValue()).isEqualTo("/godata/config/password.properties");
    Rules rules = secretConfig.getRules();
    assertThat(rules.size()).isEqualTo(2);
    assertThat(rules).containsExactly(new Deny("refer", "pipeline_group", "my_group"), new Allow("refer", "pipeline_group", "other_group"));
}
Also used : RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) PluginConfiguration(com.thoughtworks.go.plugin.domain.common.PluginConfiguration) PackageConfiguration(com.thoughtworks.go.plugin.access.packagematerial.PackageConfiguration) Deny(com.thoughtworks.go.config.rules.Deny) Rules(com.thoughtworks.go.config.rules.Rules) Allow(com.thoughtworks.go.config.rules.Allow) Test(org.junit.jupiter.api.Test)

Example 4 with Rules

use of com.thoughtworks.go.config.rules.Rules in project gocd by gocd.

the class PartialConfigServiceTest method keepsLastValidPartialOnFailureWhenRulesAllow.

@Test
void keepsLastValidPartialOnFailureWhenRulesAllow() {
    when(goConfigService.updateConfig(any(UpdateConfigCommand.class))).thenThrow(new RuntimeException("Nope"));
    when(partialConfigHelper.isEquivalent(any(PartialConfig.class), any(PartialConfig.class))).thenReturn(false);
    // an empty set guarantees violations
    final Rules rules = new Rules();
    rules.add(new Allow("refer", SupportedEntity.PIPELINE_GROUP.getType(), "two"));
    configRepoConfig.setRules(rules);
    final PartialConfig lastValid = withPipelineInGroup("p1", "two");
    lastValid.setOrigins(new RepoConfigOrigin(configRepoConfig, "1"));
    final PartialConfig incoming = withPipelineInGroup("p1", "one");
    incoming.setOrigins(new RepoConfigOrigin(configRepoConfig, "2"));
    cachedGoPartials.cacheAsLastKnown(configRepoConfig.getRepo().getFingerprint(), lastValid);
    cachedGoPartials.markAllKnownAsValid();
    // baseline
    assertEquals(1, cachedGoPartials.lastValidPartials().size());
    assertEquals(lastValid, cachedGoPartials.lastValidPartials().get(0));
    assertFalse(lastValid.hasErrors());
    assertFalse(incoming.hasErrors());
    service.onSuccessPartialConfig(configRepoConfig, incoming);
    final String violationMessage = "Not allowed to refer to pipeline group 'one'. Check the 'Rules' of this config repository.";
    assertTrue(incoming.hasErrors(), "should have rule violations");
    assertEquals(violationMessage, incoming.errors().on("pipeline_group"));
    assertFalse(lastValid.hasErrors(), "should not have rule violations");
    assertEquals(1, cachedGoPartials.lastValidPartials().size());
    assertEquals(lastValid, cachedGoPartials.lastValidPartials().get(0));
    verify(goConfigService).updateConfig(any(UpdateConfigCommand.class));
}
Also used : PartialConfig(com.thoughtworks.go.config.remote.PartialConfig) RepoConfigOrigin(com.thoughtworks.go.config.remote.RepoConfigOrigin) Rules(com.thoughtworks.go.config.rules.Rules) Allow(com.thoughtworks.go.config.rules.Allow) Test(org.junit.jupiter.api.Test)

Example 5 with Rules

use of com.thoughtworks.go.config.rules.Rules in project gocd by gocd.

the class PartialConfigServiceTest method clearsLastValidPartialOnFailureWhenFailsRuleValidations.

@Test
void clearsLastValidPartialOnFailureWhenFailsRuleValidations() {
    when(goConfigService.updateConfig(any(UpdateConfigCommand.class))).thenThrow(new RuntimeException("Nope")).thenReturn(ConfigSaveState.UPDATED);
    when(partialConfigHelper.isEquivalent(any(PartialConfig.class), any(PartialConfig.class))).thenReturn(false);
    // an empty set guarantees violations
    configRepoConfig.setRules(new Rules());
    final PartialConfig lastValid = withPipeline("p1", new RepoConfigOrigin(configRepoConfig, "1"));
    final PartialConfig incoming = withPipeline("p1", new RepoConfigOrigin(configRepoConfig, "2"));
    cachedGoPartials.cacheAsLastKnown(configRepoConfig.getRepo().getFingerprint(), lastValid);
    cachedGoPartials.markAllKnownAsValid();
    // baseline
    assertEquals(1, cachedGoPartials.lastValidPartials().size());
    assertEquals(lastValid, cachedGoPartials.lastValidPartials().get(0));
    assertFalse(lastValid.hasErrors());
    assertFalse(incoming.hasErrors());
    service.onSuccessPartialConfig(configRepoConfig, incoming);
    final String violationMessage = "Not allowed to refer to pipeline group 'group'. Check the 'Rules' of this config repository.";
    assertTrue(incoming.hasErrors(), "should have rule violations");
    assertEquals(violationMessage, incoming.errors().on("pipeline_group"));
    assertTrue(lastValid.hasErrors(), "should have rule violations");
    assertEquals(violationMessage, lastValid.errors().on("pipeline_group"));
    assertTrue(cachedGoPartials.lastValidPartials().isEmpty());
    verify(goConfigService, times(2)).updateConfig(any(UpdateConfigCommand.class));
}
Also used : PartialConfig(com.thoughtworks.go.config.remote.PartialConfig) RepoConfigOrigin(com.thoughtworks.go.config.remote.RepoConfigOrigin) Rules(com.thoughtworks.go.config.rules.Rules) Test(org.junit.jupiter.api.Test)

Aggregations

Rules (com.thoughtworks.go.config.rules.Rules)8 Test (org.junit.jupiter.api.Test)8 RepoConfigOrigin (com.thoughtworks.go.config.remote.RepoConfigOrigin)6 ConfigRepoConfig (com.thoughtworks.go.config.remote.ConfigRepoConfig)4 PartialConfig (com.thoughtworks.go.config.remote.PartialConfig)3 Allow (com.thoughtworks.go.config.rules.Allow)3 ServerHealthState (com.thoughtworks.go.serverhealth.ServerHealthState)3 Deny (com.thoughtworks.go.config.rules.Deny)1 GoConfigMother (com.thoughtworks.go.helper.GoConfigMother)1 PackageConfiguration (com.thoughtworks.go.plugin.access.packagematerial.PackageConfiguration)1 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)1 PluginConfiguration (com.thoughtworks.go.plugin.domain.common.PluginConfiguration)1