Search in sources :

Example 1 with IllegalArtifactLocationException

use of com.thoughtworks.go.domain.exception.IllegalArtifactLocationException in project gocd by gocd.

the class ArtifactsServiceTest method shouldLogAndIgnoreExceptionsWhenDeletingStageArtifacts.

@Test
void shouldLogAndIgnoreExceptionsWhenDeletingStageArtifacts() throws IllegalArtifactLocationException {
    ArtifactsService artifactsService = new ArtifactsService(resolverService, stageService, artifactsDirHolder, zipUtil);
    Stage stage = StageMother.createPassedStage("pipeline", 10, "stage", 20, "job", new Date());
    ArtifactDirectoryChooser chooser = mock(ArtifactDirectoryChooser.class);
    ReflectionUtil.setField(artifactsService, "chooser", chooser);
    when(chooser.findArtifact(any(LocatableEntity.class), eq(""))).thenThrow(new IllegalArtifactLocationException("holy cow!"));
    try (LogFixture logFixture = logFixtureFor(ArtifactsService.class, Level.DEBUG)) {
        artifactsService.purgeArtifactsForStage(stage);
        assertThat(logFixture.contains(Level.ERROR, "Error occurred while clearing artifacts for 'pipeline/10/stage/20'. Error: 'holy cow!'")).isTrue();
    }
    verify(stageService).markArtifactsDeletedFor(stage);
}
Also used : LogFixture(com.thoughtworks.go.util.LogFixture) LocatableEntity(com.thoughtworks.go.domain.LocatableEntity) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) Stage(com.thoughtworks.go.domain.Stage) Date(java.util.Date) ArtifactDirectoryChooser(com.thoughtworks.go.server.view.artifacts.ArtifactDirectoryChooser) Test(org.junit.jupiter.api.Test)

Example 2 with IllegalArtifactLocationException

use of com.thoughtworks.go.domain.exception.IllegalArtifactLocationException in project gocd by gocd.

the class ArtifactsController method postArtifact.

@RequestMapping(value = "/repository/restful/artifact/POST/*", method = RequestMethod.POST)
public ModelAndView postArtifact(@RequestParam("pipelineName") String pipelineName, @RequestParam("pipelineCounter") String pipelineCounter, @RequestParam("stageName") String stageName, @RequestParam(value = "stageCounter", required = false) String stageCounter, @RequestParam("buildName") String buildName, @RequestParam(value = "buildId", required = false) Long buildId, @RequestParam("filePath") String filePath, @RequestParam(value = "attempt", required = false) Integer attempt, MultipartHttpServletRequest request) throws Exception {
    JobIdentifier jobIdentifier;
    if (!headerConstraint.isSatisfied(request)) {
        return ResponseCodeView.create(HttpServletResponse.SC_BAD_REQUEST, "Missing required header 'Confirm'");
    }
    if (!isValidStageCounter(stageCounter)) {
        return buildNotFound(pipelineName, pipelineCounter, stageName, stageCounter, buildName);
    }
    try {
        jobIdentifier = restfulService.findJob(pipelineName, pipelineCounter, stageName, stageCounter, buildName, buildId);
    } catch (Exception e) {
        return buildNotFound(pipelineName, pipelineCounter, stageName, stageCounter, buildName);
    }
    int convertedAttempt = attempt == null ? 1 : attempt;
    try {
        File artifact = artifactsService.findArtifact(jobIdentifier, filePath);
        if (artifact.exists() && artifact.isFile()) {
            return FileModelAndView.fileAlreadyExists(filePath);
        }
        MultipartFile multipartFile = multipartFile(request);
        if (multipartFile == null) {
            return FileModelAndView.invalidUploadRequest();
        }
        boolean success = saveFile(convertedAttempt, artifact, multipartFile, shouldUnzipStream(multipartFile));
        if (!success) {
            return FileModelAndView.errorSavingFile(filePath);
        }
        success = updateChecksumFile(request, jobIdentifier, filePath);
        if (!success) {
            return FileModelAndView.errorSavingChecksumFile(filePath);
        }
        return FileModelAndView.fileCreated(filePath);
    } catch (IllegalArtifactLocationException e) {
        return FileModelAndView.forbiddenUrl(filePath);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) HeaderConstraint(com.thoughtworks.go.server.security.HeaderConstraint) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with IllegalArtifactLocationException

use of com.thoughtworks.go.domain.exception.IllegalArtifactLocationException in project gocd by gocd.

the class ArtifactsService method findArtifactRoot.

@Override
public String findArtifactRoot(JobIdentifier identifier) throws IllegalArtifactLocationException {
    JobIdentifier id = jobResolverService.actualJobIdentifier(identifier);
    try {
        String fullArtifactPath = chooser.findArtifact(id, "").getCanonicalPath();
        String artifactRoot = artifactsDirHolder.getArtifactsDir().getCanonicalPath();
        String relativePath = fullArtifactPath.replace(artifactRoot, "");
        if (relativePath.startsWith(File.separator)) {
            relativePath = relativePath.replaceFirst("\\" + File.separator, "");
        }
        return relativePath;
    } catch (IOException e) {
        throw new IllegalArtifactLocationException("No artifact found.", e);
    }
}
Also used : IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) JobIdentifier(com.thoughtworks.go.domain.JobIdentifier) IOException(java.io.IOException)

Example 4 with IllegalArtifactLocationException

use of com.thoughtworks.go.domain.exception.IllegalArtifactLocationException in project gocd by gocd.

the class ArtifactDirectoryChooser method findArtifact.

public File findArtifact(LocatableEntity locatableEntity, String path) throws IllegalArtifactLocationException {
    try {
        File root = chooseExistingRoot(locatableEntity);
        if (root == null) {
            root = preferredRoot(locatableEntity);
        }
        File file = new File(root, path);
        if (!FileUtil.isSubdirectoryOf(root, file)) {
            throw new IllegalArtifactLocationException("Artifact path [" + path + "] is illegal." + " Path must be inside the artifact directory.");
        }
        return file;
    } catch (IOException e) {
        throw new IllegalArtifactLocationException("Artifact path [" + path + "] is illegal." + e.getMessage(), e);
    }
}
Also used : IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) IOException(java.io.IOException) File(java.io.File)

Example 5 with IllegalArtifactLocationException

use of com.thoughtworks.go.domain.exception.IllegalArtifactLocationException in project gocd by gocd.

the class ScheduledPipelineLoaderIntegrationTest method shouldSetAServerHealthMessageWhenMaterialForPipelineWithBuildCauseIsNotFound.

@Test
public void shouldSetAServerHealthMessageWhenMaterialForPipelineWithBuildCauseIsNotFound() throws IllegalArtifactLocationException, IOException {
    JobConfig jobConfig = new JobConfig("job-one");
    jobConfig.addTask(new AntTask());
    PipelineConfig pipelineConfig = PipelineConfigMother.pipelineConfig("last", new StageConfig(new CaseInsensitiveString("stage"), new JobConfigs(jobConfig)));
    pipelineConfig.materialConfigs().clear();
    SvnMaterialConfig onDirOne = MaterialConfigsMother.svnMaterialConfig("google.com", "dirOne", "loser", "boozer", false, "**/*.html");
    final P4MaterialConfig onDirTwo = MaterialConfigsMother.p4MaterialConfig("host:987654321", "zoozer", "secret", "through-the-window", true);
    onDirTwo.setConfigAttributes(Collections.singletonMap(ScmMaterialConfig.FOLDER, "dirTwo"));
    pipelineConfig.addMaterialConfig(onDirOne);
    pipelineConfig.addMaterialConfig(onDirTwo);
    configHelper.addPipeline(pipelineConfig);
    Pipeline building = PipelineMother.building(pipelineConfig);
    final Pipeline pipeline = dbHelper.savePipelineWithMaterials(building);
    CruiseConfig cruiseConfig = configHelper.currentConfig();
    PipelineConfig cfg = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("last"));
    cfg.removeMaterialConfig(cfg.materialConfigs().get(1));
    configHelper.writeConfigFile(cruiseConfig);
    assertThat(serverHealthService.filterByScope(HealthStateScope.forPipeline("last")).size(), is(0));
    final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId();
    Date currentTime = new Date(System.currentTimeMillis() - 1);
    Pipeline loadedPipeline = null;
    try {
        loadedPipeline = loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId);
        fail("should not have loaded pipeline with build-cause as one of the necessary materials was not found");
    } catch (Exception e) {
        assertThat(e, is(instanceOf(StaleMaterialsOnBuildCause.class)));
        assertThat(e.getMessage(), is("Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config."));
    }
    assertThat(loadedPipeline, is(nullValue()));
    JobInstance reloadedJobInstance = jobInstanceService.buildById(jobId);
    assertThat(reloadedJobInstance.getState(), is(JobState.Completed));
    assertThat(reloadedJobInstance.getResult(), is(JobResult.Failed));
    HealthStateScope scope = HealthStateScope.forJob("last", "stage", "job-one");
    assertThat(serverHealthService.filterByScope(scope).size(), is(1));
    ServerHealthState error = serverHealthService.filterByScope(HealthStateScope.forJob("last", "stage", "job-one")).get(0);
    assertThat(error, is(ServerHealthState.error("Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config.", "Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.", HealthStateType.general(HealthStateScope.forJob("last", "stage", "job-one")))));
    DateTime expiryTime = (DateTime) ReflectionUtil.getField(error, "expiryTime");
    assertThat(expiryTime.toDate().after(currentTime), is(true));
    assertThat(expiryTime.toDate().before(new Date(System.currentTimeMillis() + 5 * 60 * 1000 + 1)), is(true));
    String logText = FileUtils.readFileToString(consoleService.consoleLogArtifact(reloadedJobInstance.getIdentifier()), UTF_8);
    assertThat(logText, containsString("Cannot load job 'last/" + pipeline.getCounter() + "/stage/1/job-one' because material " + onDirTwo + " was not found in config."));
    assertThat(logText, containsString("Job for pipeline 'last/" + pipeline.getCounter() + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed."));
}
Also used : HealthStateScope(com.thoughtworks.go.serverhealth.HealthStateScope) StaleMaterialsOnBuildCause(com.thoughtworks.go.server.materials.StaleMaterialsOnBuildCause) Date(java.util.Date) IllegalArtifactLocationException(com.thoughtworks.go.domain.exception.IllegalArtifactLocationException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) P4MaterialConfig(com.thoughtworks.go.config.materials.perforce.P4MaterialConfig) ServerHealthState(com.thoughtworks.go.serverhealth.ServerHealthState) SvnMaterialConfig(com.thoughtworks.go.config.materials.svn.SvnMaterialConfig) Test(org.junit.jupiter.api.Test)

Aggregations

IllegalArtifactLocationException (com.thoughtworks.go.domain.exception.IllegalArtifactLocationException)5 IOException (java.io.IOException)4 JobIdentifier (com.thoughtworks.go.domain.JobIdentifier)2 File (java.io.File)2 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 P4MaterialConfig (com.thoughtworks.go.config.materials.perforce.P4MaterialConfig)1 SvnMaterialConfig (com.thoughtworks.go.config.materials.svn.SvnMaterialConfig)1 LocatableEntity (com.thoughtworks.go.domain.LocatableEntity)1 Stage (com.thoughtworks.go.domain.Stage)1 StaleMaterialsOnBuildCause (com.thoughtworks.go.server.materials.StaleMaterialsOnBuildCause)1 HeaderConstraint (com.thoughtworks.go.server.security.HeaderConstraint)1 ArtifactDirectoryChooser (com.thoughtworks.go.server.view.artifacts.ArtifactDirectoryChooser)1 HealthStateScope (com.thoughtworks.go.serverhealth.HealthStateScope)1 ServerHealthState (com.thoughtworks.go.serverhealth.ServerHealthState)1 LogFixture (com.thoughtworks.go.util.LogFixture)1 DateTime (org.joda.time.DateTime)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1