Search in sources :

Example 6 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.

the class ContentPackResource method deleteContentPackInstallationById.

@DELETE
@Path("{contentPackId}/installations/{installationId}")
@Timed
@ApiOperation(value = "Uninstall a content pack installation")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_UNINSTALL)
@JsonView(ContentPackView.HttpView.class)
public Response deleteContentPackInstallationById(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId contentPackId, @ApiParam(name = "installationId", value = "Installation ID", required = true) @PathParam("installationId") String installationId) {
    checkPermission(RestPermissions.CONTENT_PACK_UNINSTALL, contentPackId.toString());
    final ContentPackInstallation installation = contentPackInstallationPersistenceService.findById(new ObjectId(installationId)).orElseThrow(() -> new NotFoundException("Couldn't find installation " + installationId));
    final ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(installation.contentPackId(), installation.contentPackRevision()).orElseThrow(() -> new NotFoundException("Couldn't find content pack " + installation.contentPackId() + " rev " + installation.contentPackRevision()));
    final ContentPackUninstallation removedInstallation = contentPackService.uninstallContentPack(contentPack, installation);
    return Response.ok(ImmutableMap.of("content_pack", contentPack, "uninstalled", removedInstallation)).build();
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ObjectId(org.bson.types.ObjectId) ContentPackUninstallation(org.graylog2.contentpacks.model.ContentPackUninstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.

the class ContentPackResource method createContentPack.

@POST
@Timed
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Upload a content pack")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Missing or invalid content pack"), @ApiResponse(code = 500, message = "Error while saving content pack") })
@AuditEvent(type = AuditEventTypes.CONTENT_PACK_CREATE)
@JsonView(ContentPackView.HttpView.class)
public Response createContentPack(@ApiParam(name = "Request body", value = "Content pack", required = true) @NotNull @Valid final ContentPack contentPack) {
    checkPermission(RestPermissions.CONTENT_PACK_CREATE);
    final ContentPack pack = contentPackPersistenceService.filterMissingResourcesAndInsert(contentPack).orElseThrow(() -> new BadRequestException("Content pack " + contentPack.id() + " with this revision " + contentPack.revision() + " already found!"));
    final URI packUri = getUriBuilderToSelf().path(ContentPackResource.class).path("{contentPackId}").build(pack.id());
    return Response.created(packUri).build();
}
Also used : ContentPack(org.graylog2.contentpacks.model.ContentPack) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.

the class ContentPackResource method getContentPackRevisions.

@GET
@Path("{contentPackId}/{revision}")
@Timed
@ApiOperation(value = "Get a revision of a content pack")
@ApiResponses(value = { @ApiResponse(code = 500, message = "Error loading content packs") })
@JsonView(ContentPackView.HttpView.class)
public ContentPackResponse getContentPackRevisions(@ApiParam(name = "contentPackId", value = "Content pack ID", required = true) @PathParam("contentPackId") ModelId id, @ApiParam(name = "revision", value = "Content pack revision", required = true) @PathParam("revision") int revision) {
    checkPermission(RestPermissions.CONTENT_PACK_READ);
    ContentPack contentPack = contentPackPersistenceService.findByIdAndRevision(id, revision).orElseThrow(() -> new NotFoundException("Content pack " + id + " with revision " + revision + " not found!"));
    Set<ConstraintCheckResult> constraints = contentPackService.checkConstraints(contentPack);
    return ContentPackResponse.create(contentPack, constraints);
}
Also used : ConstraintCheckResult(org.graylog2.contentpacks.model.constraints.ConstraintCheckResult) ContentPack(org.graylog2.contentpacks.model.ContentPack) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) ApiResponses(io.swagger.annotations.ApiResponses)

Example 9 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.

the class V20191219090834_AddSourcesPageTest method upgradeFailsBecauseSourcePageContentPackExists.

@Test
public void upgradeFailsBecauseSourcePageContentPackExists() throws IOException {
    final URL contentPackURL = V20191219090834_AddSourcesPage.class.getResource("V20191219090834_AddSourcesPage_Content_Pack.json");
    final ContentPack contentPack = ContentPackV1.builder().id(ModelId.of("04fcf179-49e0-4e8f-9c02-0ff13062efbe")).summary("summary").revision(1).name("Sources Page").description("description").vendor("").url(URI.create("http://graylog.com")).entities(ImmutableSet.of()).build();
    final ContentPackInstallation contentPackInstallation = ContentPackInstallation.builder().contentPackId(contentPack.id()).contentPackRevision(1).parameters(ImmutableMap.of()).entities(ImmutableSet.of()).comment("Comment").createdAt(Instant.now()).createdBy("admin").build();
    when(configService.get(V20191219090834_AddSourcesPage.MigrationCompleted.class)).thenReturn(null);
    when(objectMapper.readValue(contentPackURL, ContentPack.class)).thenReturn(contentPack);
    when(contentPackPersistenceService.insert(contentPack)).thenReturn(Optional.empty());
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> migration.upgrade()).withMessage("Could not install Source Page Content Pack.");
    verify(configService).write(V20191219090834_AddSourcesPage.MigrationCompleted.create("04fcf179-49e0-4e8f-9c02-0ff13062efbe"));
    verifyZeroInteractions(contentPackService);
}
Also used : ContentPackInstallation(org.graylog2.contentpacks.model.ContentPackInstallation) ContentPack(org.graylog2.contentpacks.model.ContentPack) URL(java.net.URL) Test(org.junit.Test)

Example 10 with ContentPack

use of org.graylog2.contentpacks.model.ContentPack in project graylog2-server by Graylog2.

the class ContentPackPersistenceServiceTest method insertDuplicate.

@Test
public void insertDuplicate() {
    final ContentPackV1 contentPack = ContentPackV1.builder().id(ModelId.of("id")).revision(1).name("name").description("description").summary("summary").vendor("vendor").url(URI.create("https://www.graylog.org/")).entities(ImmutableSet.of()).build();
    contentPackPersistenceService.insert(contentPack);
    final Optional<ContentPack> savedContentPack2 = contentPackPersistenceService.insert(contentPack);
    assertThat(savedContentPack2).isEmpty();
}
Also used : ContentPackV1(org.graylog2.contentpacks.model.ContentPackV1) ContentPack(org.graylog2.contentpacks.model.ContentPack) Test(org.junit.Test)

Aggregations

ContentPack (org.graylog2.contentpacks.model.ContentPack)21 Test (org.junit.Test)11 ContentPackInstallation (org.graylog2.contentpacks.model.ContentPackInstallation)10 HashMap (java.util.HashMap)7 ContentPackV1 (org.graylog2.contentpacks.model.ContentPackV1)7 ModelId (org.graylog2.contentpacks.model.ModelId)7 Timed (com.codahale.metrics.annotation.Timed)6 JsonView (com.fasterxml.jackson.annotation.JsonView)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 URL (java.net.URL)5 Map (java.util.Map)5 NotFoundException (javax.ws.rs.NotFoundException)5 Path (javax.ws.rs.Path)5 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 ObjectId (org.bson.types.ObjectId)4 EntityWithExcerptFacade (org.graylog2.contentpacks.facades.EntityWithExcerptFacade)4 ContentPackUninstallation (org.graylog2.contentpacks.model.ContentPackUninstallation)4