Search in sources :

Example 1 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class RulesService method validateSecretConfigReferences.

public void validateSecretConfigReferences(PackageMaterial packageMaterial) {
    PackageRepository pkgRepository = packageMaterial.getPackageDefinition().getRepository();
    Map<CaseInsensitiveString, StringBuilder> errors = validate(packageMaterial.getSecretParams(), pkgRepository.getClass(), pkgRepository.getName(), "Package Material");
    if (!errors.isEmpty()) {
        throw new RulesViolationException(errorString(errors));
    }
}
Also used : RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) PackageRepository(com.thoughtworks.go.domain.packagerepository.PackageRepository)

Example 2 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class BuildAssignmentServiceTest method shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration.

@Test
void shouldFailTheJobWhenRulesViolationErrorOccursForElasticConfiguration() throws IOException, IllegalArtifactLocationException {
    PipelineConfig pipelineWithElasticJob = PipelineConfigMother.pipelineWithElasticJob(elasticProfileId1);
    JobPlan jobPlan = new InstanceFactory().createJobPlan(pipelineWithElasticJob.first().getJobs().first(), schedulingContext);
    jobPlans.add(jobPlan);
    JobInstance jobInstance = mock(JobInstance.class);
    doThrow(new RulesViolationException("some rules related violation message")).when(elasticAgentPluginService).shouldAssignWork(elasticAgentInstance.elasticAgentMetadata(), null, jobPlan.getElasticProfile(), jobPlan.getClusterProfile(), jobPlan.getIdentifier());
    when(jobInstance.getState()).thenReturn(JobState.Scheduled);
    when(jobInstanceService.buildById(anyLong())).thenReturn(jobInstance);
    buildAssignmentService.onTimer();
    assertThatCode(() -> {
        JobPlan matchingJob = buildAssignmentService.findMatchingJob(elasticAgentInstance);
        assertThat(matchingJob).isNull();
        assertThat(buildAssignmentService.jobPlans()).containsExactly(jobPlan);
    }).doesNotThrowAnyException();
    InOrder inOrder = inOrder(jobInstanceService, scheduleService, consoleService, jobStatusTopic);
    inOrder.verify(jobInstanceService).buildById(jobPlan.getJobId());
    inOrder.verify(consoleService).appendToConsoleLog(jobPlan.getIdentifier(), "\nThis job was failed by GoCD. This job is configured to run on an elastic agent, there were errors while resolving secrets for the the associated elastic configurations.\nReasons: some rules related violation message");
    inOrder.verify(scheduleService).failJob(jobInstance);
    inOrder.verify(jobStatusTopic).post(new JobStatusMessage(jobPlan.getIdentifier(), JobState.Scheduled, elasticAgentInstance.getUuid()));
}
Also used : InOrder(org.mockito.InOrder) JobStatusMessage(com.thoughtworks.go.server.messaging.JobStatusMessage) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Example 3 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class PackageDefinitionService method checkConnection.

public void checkConnection(final PackageDefinition packageDefinition, final LocalizedOperationResult result) {
    try {
        String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId();
        secretParamResolver.resolve(packageDefinition);
        Result checkConnectionResult = packageRepositoryExtension.checkConnectionToPackage(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository()));
        String messages = checkConnectionResult.getMessagesForDisplay();
        if (!checkConnectionResult.isSuccessful()) {
            result.connectionError("Package check Failed. Reason(s): " + messages);
            return;
        }
        result.setMessage("OK. " + messages);
    } catch (Exception e) {
        if (e instanceof RulesViolationException || e instanceof SecretResolutionFailureException) {
            result.unprocessableEntity("Package check Failed. Reason(s): " + e.getMessage());
        } else {
            result.internalServerError("Package check Failed. Reason(s): " + e.getMessage());
        }
    }
}
Also used : RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) Result(com.thoughtworks.go.plugin.api.response.Result) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) LocalizedOperationResult(com.thoughtworks.go.server.service.result.LocalizedOperationResult) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)

Example 4 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class ElasticAgentPluginService method jobCompleted.

public void jobCompleted(JobInstance job) {
    AgentInstance agentInstance = agentService.findAgent(job.getAgentUuid());
    if (!agentInstance.isElastic()) {
        LOGGER.debug("Agent {} is not elastic. Skipping further execution.", agentInstance.getUuid());
        return;
    }
    if (job.isAssignedToAgent()) {
        jobCreationTimeMap.remove(job.getId());
    }
    String pluginId = agentInstance.elasticAgentMetadata().elasticPluginId();
    String elasticAgentId = agentInstance.elasticAgentMetadata().elasticAgentId();
    JobIdentifier jobIdentifier = job.getIdentifier();
    ElasticProfile elasticProfile = job.getPlan().getElasticProfile();
    ClusterProfile clusterProfile = job.getPlan().getClusterProfile();
    try {
        secretParamResolver.resolve(elasticProfile);
        Map<String, String> elasticProfileConfiguration = elasticProfile.getConfigurationAsMap(true, true);
        Map<String, String> clusterProfileConfiguration = emptyMap();
        if (clusterProfile != null) {
            secretParamResolver.resolve(clusterProfile);
            clusterProfileConfiguration = clusterProfile.getConfigurationAsMap(true, true);
        }
        elasticAgentPluginRegistry.reportJobCompletion(pluginId, elasticAgentId, jobIdentifier, elasticProfileConfiguration, clusterProfileConfiguration);
    } catch (RulesViolationException | SecretResolutionFailureException e) {
        String description = format("The job completion call to the plugin for the job identifier [%s] failed for secrets resolution: %s ", jobIdentifier.toString(), e.getMessage());
        ServerHealthState healthState = error("Failed to notify plugin", description, general(scopeForJob(jobIdentifier)));
        healthState.setTimeout(Timeout.FIVE_MINUTES);
        serverHealthService.update(healthState);
        LOGGER.error(description);
    }
}
Also used : AgentInstance(com.thoughtworks.go.domain.AgentInstance) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 5 with RulesViolationException

use of com.thoughtworks.go.server.exceptions.RulesViolationException in project gocd by gocd.

the class PackageRepositoryServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection.

@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection() {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    doThrow(new RulesViolationException("some rule violation message")).when(secretParamResolver).resolve(any(PackageRepository.class));
    service.checkConnection(packageRepository("some-plugin"), result);
    assertThat(result.isSuccessful()).isFalse();
    assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
    assertThat(result.message()).isEqualTo("Could not connect to package repository. Reason(s): some rule violation message");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) PackageRepository(com.thoughtworks.go.domain.packagerepository.PackageRepository) Test(org.junit.jupiter.api.Test)

Aggregations

RulesViolationException (com.thoughtworks.go.server.exceptions.RulesViolationException)14 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)8 Test (org.junit.jupiter.api.Test)7 SecretResolutionFailureException (com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException)5 Username (com.thoughtworks.go.server.domain.Username)4 EntityConfigUpdateCommand (com.thoughtworks.go.config.commands.EntityConfigUpdateCommand)2 ClusterProfile (com.thoughtworks.go.config.elastic.ClusterProfile)2 ElasticProfile (com.thoughtworks.go.config.elastic.ElasticProfile)2 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)2 Configuration (com.thoughtworks.go.domain.config.Configuration)2 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)2 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)2 Result (com.thoughtworks.go.plugin.api.response.Result)2 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)2 PackageMaterialTestHelper.assertPackageConfiguration (com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration)2 LocalizedOperationResult (com.thoughtworks.go.server.service.result.LocalizedOperationResult)2 HashMap (java.util.HashMap)2 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 ScmMaterialConfig (com.thoughtworks.go.config.materials.ScmMaterialConfig)1 AgentInstance (com.thoughtworks.go.domain.AgentInstance)1