Search in sources :

Example 1 with SPINNAKER_PREFIX

use of com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX in project front50 by spinnaker.

the class PipelineController method validatePipeline.

/**
 * Ensure basic validity of the pipeline. Invalid pipelines will raise runtime exceptions.
 *
 * @param pipeline The Pipeline to validate
 */
private void validatePipeline(final Pipeline pipeline, Boolean staleCheck) {
    // Pipelines must have an application and a name
    if (StringUtils.isAnyBlank(pipeline.getApplication(), pipeline.getName())) {
        throw new InvalidEntityException("A pipeline requires name and application fields");
    }
    // Check if pipeline type is templated
    if (TYPE_TEMPLATED.equals(pipeline.getType())) {
        PipelineTemplateDAO templateDAO = getTemplateDAO();
        // Check templated pipelines to ensure template is valid
        String source;
        switch(pipeline.getSchema()) {
            case "v2":
                V2TemplateConfiguration v2Config = objectMapper.convertValue(pipeline, V2TemplateConfiguration.class);
                source = v2Config.getTemplate().getReference();
                break;
            default:
                TemplateConfiguration v1Config = objectMapper.convertValue(pipeline.getConfig(), TemplateConfiguration.class);
                source = v1Config.getPipeline().getTemplate().getSource();
                break;
        }
        // Check if template id which is after :// is in the store
        if (source.startsWith(SPINNAKER_PREFIX)) {
            String templateId = source.substring(SPINNAKER_PREFIX.length());
            try {
                templateDAO.findById(templateId);
            } catch (NotFoundException notFoundEx) {
                throw new BadRequestException("Configured pipeline template not found", notFoundEx);
            }
        }
    }
    checkForDuplicatePipeline(pipeline.getApplication(), pipeline.getName().trim(), pipeline.getId());
    final ValidatorErrors errors = new ValidatorErrors();
    pipelineValidators.forEach(it -> it.validate(pipeline, errors));
    if (staleCheck && !Strings.isNullOrEmpty(pipeline.getId()) && pipeline.getLastModified() != null) {
        checkForStalePipeline(pipeline, errors);
    }
    if (errors.hasErrors()) {
        String message = errors.getAllErrorsMessage();
        throw new ValidationException(message, errors.getAllErrors());
    }
}
Also used : ValidationException(com.netflix.spinnaker.kork.web.exceptions.ValidationException) PipelineTemplateDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) V2TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) ValidatorErrors(com.netflix.spinnaker.front50.api.validator.ValidatorErrors) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) V2TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration)

Example 2 with SPINNAKER_PREFIX

use of com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX in project front50 by spinnaker.

the class V2PipelineTemplateController method getDependentConfigs.

@VisibleForTesting
List<String> getDependentConfigs(String templateId) {
    List<String> dependentConfigIds = new ArrayList<>();
    String prefixedId = SPINNAKER_PREFIX + templateId;
    pipelineDAO.all().stream().filter(pipeline -> pipeline.getType() != null && pipeline.getType().equals(TYPE_TEMPLATED)).forEach(templatedPipeline -> {
        String source;
        try {
            TemplateConfiguration config = objectMapper.convertValue(templatedPipeline.getConfig(), TemplateConfiguration.class);
            source = config.getPipeline().getTemplate().getSource();
        } catch (Exception e) {
            return;
        }
        if (source != null && source.equalsIgnoreCase(prefixedId)) {
            dependentConfigIds.add(templatedPipeline.getId());
        }
    });
    return dependentConfigIds;
}
Also used : PipelineTemplate(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate) PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) InvalidRequestException(com.netflix.spinnaker.front50.exceptions.InvalidRequestException) PipelineDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineDAO) PipelineTemplateDAO(com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO) MessageDigest(java.security.MessageDigest) Pipeline(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Hex(org.apache.commons.codec.binary.Hex) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) Map(java.util.Map) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) TYPE_TEMPLATED(com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED) DuplicateEntityException(com.netflix.spinnaker.front50.exceptions.DuplicateEntityException) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TreeMap(java.util.TreeMap) SPINNAKER_PREFIX(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) ArrayList(java.util.ArrayList) TemplateConfiguration(com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration) InvalidRequestException(com.netflix.spinnaker.front50.exceptions.InvalidRequestException) BadRequestException(com.netflix.spinnaker.front50.exception.BadRequestException) InvalidEntityException(com.netflix.spinnaker.front50.exceptions.InvalidEntityException) DuplicateEntityException(com.netflix.spinnaker.front50.exceptions.DuplicateEntityException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NotFoundException(com.netflix.spinnaker.kork.web.exceptions.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

BadRequestException (com.netflix.spinnaker.front50.exception.BadRequestException)2 InvalidEntityException (com.netflix.spinnaker.front50.exceptions.InvalidEntityException)2 PipelineTemplateDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO)2 TemplateConfiguration (com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration)2 NotFoundException (com.netflix.spinnaker.kork.web.exceptions.NotFoundException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Pipeline (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline)1 TYPE_TEMPLATED (com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED)1 ValidatorErrors (com.netflix.spinnaker.front50.api.validator.ValidatorErrors)1 DuplicateEntityException (com.netflix.spinnaker.front50.exceptions.DuplicateEntityException)1 InvalidRequestException (com.netflix.spinnaker.front50.exceptions.InvalidRequestException)1 PipelineDAO (com.netflix.spinnaker.front50.model.pipeline.PipelineDAO)1 PipelineTemplate (com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate)1 SPINNAKER_PREFIX (com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX)1 V2TemplateConfiguration (com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration)1 ValidationException (com.netflix.spinnaker.kork.web.exceptions.ValidationException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 MessageDigest (java.security.MessageDigest)1