Search in sources :

Example 6 with RulesViolationException

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

the class PackageDefinitionServiceTest method shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection.

@Test
void shouldSetResultAsUnprocessableEntityIfRulesViolationForCheckConnection() {
    PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", new Configuration(), packageRepository);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    doThrow(new RulesViolationException("some rule violation message")).when(secretParamResolver).resolve(packageDefinition);
    service.checkConnection(packageDefinition, result);
    assertThat(result.isSuccessful()).isFalse();
    assertThat(result.httpCode()).isEqualTo(HttpStatus.SC_UNPROCESSABLE_ENTITY);
    assertThat(result.message()).isEqualTo("Package check Failed. Reason(s): some rule violation message");
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Configuration(com.thoughtworks.go.domain.config.Configuration) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) Test(org.junit.jupiter.api.Test)

Example 7 with RulesViolationException

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

the class StatusReportsController method agentStatusReport.

public ModelAndView agentStatusReport(Request request, Response response) throws Exception {
    String pluginId = request.params("plugin_id");
    String elasticAgentId = parseElasticAgentId(request);
    String jobIdString = request.queryParams("job_id");
    long jobId;
    try {
        jobId = Long.parseLong(jobIdString);
    } catch (NumberFormatException e) {
        return errorPage(response, HttpStatus.UNPROCESSABLE_ENTITY.value(), "Agent Status Report", "Please provide a valid job_id for Agent Status Report.");
    }
    try {
        JobInstance jobInstance = jobInstanceService.buildById(jobId);
        String agentStatusReport = elasticAgentPluginService.getAgentStatusReport(pluginId, jobInstance.getIdentifier(), elasticAgentId);
        Map<Object, Object> object = new HashMap<>();
        object.put("viewTitle", "Agent Status Report");
        object.put("viewFromPlugin", agentStatusReport);
        return new ModelAndView(object, "status_reports/index.ftlh");
    } catch (RecordNotFoundException e) {
        return errorPage(response, 404, "Agent Status Report", e.getMessage());
    } catch (DataRetrievalFailureException | UnsupportedOperationException e) {
        String message = String.format("Status Report for plugin with id: '%s' for agent '%s' is not found.", pluginId, elasticAgentId);
        return errorPage(response, 404, "Agent Status Report", message);
    } catch (RulesViolationException | SecretResolutionFailureException e) {
        LOGGER.error(e.getMessage(), e);
        return errorPage(response, 500, "Agent Status Report", e.getMessage());
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return errorPage(response, 500, "Agent Status Report", UNKNOWN_ERROR_MESSAGE);
    }
}
Also used : JobInstance(com.thoughtworks.go.domain.JobInstance) HashMap(java.util.HashMap) ModelAndView(spark.ModelAndView) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException)

Example 8 with RulesViolationException

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

the class ClusterProfilesChangedPluginNotifier method onEntityConfigChange.

@Override
public void onEntityConfigChange(ClusterProfile updatedClusterProfile) {
    try {
        LOGGER.debug("Resolving secrets for updated cluster profile: {}", updatedClusterProfile);
        secretParamResolver.resolve(updatedClusterProfile);
    } catch (RulesViolationException | SecretResolutionFailureException e) {
        logAndRaiseServerHealthMessage(updatedClusterProfile, e.getMessage());
        return;
    }
    Map<String, String> updatedClusterConfigMap = updatedClusterProfile.getConfigurationAsMap(true, true);
    if (goConfigService.getElasticConfig().getClusterProfiles().find(updatedClusterProfile.getId()) == null) {
        registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.DELETED, updatedClusterConfigMap, null);
        updateClusterProfilesCopy();
        return;
    }
    ClusterProfile oldClusterProfile = existingClusterProfiles.find(updatedClusterProfile.getId());
    if (oldClusterProfile == null) {
        registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.CREATED, null, updatedClusterConfigMap);
        updateClusterProfilesCopy();
        return;
    }
    try {
        LOGGER.debug("Resolving secrets for older cluster profile: {}", oldClusterProfile);
        secretParamResolver.resolve(oldClusterProfile);
    } catch (RulesViolationException | SecretResolutionFailureException e) {
        logAndRaiseServerHealthMessage(oldClusterProfile, e.getMessage());
        return;
    }
    // cluster profile has been updated without changing plugin id
    Map<String, String> olderClusterConfigMap = oldClusterProfile.getConfigurationAsMap(true, true);
    if (oldClusterProfile.getPluginId().equals(updatedClusterProfile.getPluginId())) {
        registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.UPDATED, olderClusterConfigMap, updatedClusterConfigMap);
        updateClusterProfilesCopy();
    } else {
        // cluster profile has been updated including changing plugin id.
        // this internally results in deletion of a profile belonging to old plugin id and creation of the profile belonging to new plugin id
        registry.notifyPluginAboutClusterProfileChanged(updatedClusterProfile.getPluginId(), ClusterProfilesChangedStatus.CREATED, null, updatedClusterConfigMap);
        registry.notifyPluginAboutClusterProfileChanged(oldClusterProfile.getPluginId(), ClusterProfilesChangedStatus.DELETED, olderClusterConfigMap, null);
        updateClusterProfilesCopy();
    }
}
Also used : RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) SecretResolutionFailureException(com.thoughtworks.go.plugin.access.exceptions.SecretResolutionFailureException) ClusterProfile(com.thoughtworks.go.config.elastic.ClusterProfile)

Example 9 with RulesViolationException

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

the class PackageRepositoryService method checkConnection.

public void checkConnection(final PackageRepository packageRepository, final LocalizedOperationResult result) {
    try {
        secretParamResolver.resolve(packageRepository);
        Result checkConnectionResult = packageRepositoryExtension.checkConnectionToRepository(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
        String messages = checkConnectionResult.getMessagesForDisplay();
        if (!checkConnectionResult.isSuccessful()) {
            result.connectionError("Could not connect to package repository. Reason(s): " + messages);
            return;
        }
        result.setMessage("Connection OK. " + messages);
        return;
    } catch (Exception e) {
        if (e instanceof RulesViolationException || e instanceof SecretResolutionFailureException) {
            result.unprocessableEntity("Could not connect to package repository. Reason(s): " + e.getMessage());
        } else {
            result.internalServerError("Could not connect to package repository. 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 10 with RulesViolationException

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

the class ElasticProfileServiceTest method shouldReturn422WhenExceptionRelatedToRulesOccur.

@Test
void shouldReturn422WhenExceptionRelatedToRulesOccur() {
    ElasticProfile elasticProfile = new ElasticProfile("ldap", clusterProfileId, create("key", false, "{{SECRET:[id][key]}}"));
    Username username = new Username("username");
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    doThrow(new RulesViolationException("some rules violation message")).when(secretParamResolver).resolve(elasticProfile);
    elasticProfileService.create(username, elasticProfile, result);
    assertThat(result.httpCode()).isEqualTo(422);
    assertThat(result.message()).isEqualTo("Validations failed for agentProfile 'ldap'. Error(s): [some rules violation message]. Please correct and resubmit.");
    verifyNoInteractions(validator);
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) RulesViolationException(com.thoughtworks.go.server.exceptions.RulesViolationException) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) 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