Search in sources :

Example 86 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class V20190705071400_AddEventIndexSetsMigration method createEventsStream.

private void createEventsStream(String streamId, String streamTitle, String streamDescription, IndexSet indexSet) {
    final ObjectId id = new ObjectId(streamId);
    final Map<String, Object> fields = ImmutableMap.<String, Object>builder().put(StreamImpl.FIELD_TITLE, streamTitle).put(StreamImpl.FIELD_DESCRIPTION, streamDescription).put(StreamImpl.FIELD_DISABLED, false).put(StreamImpl.FIELD_CREATED_AT, DateTime.now(DateTimeZone.UTC)).put(StreamImpl.FIELD_CREATOR_USER_ID, "admin").put(StreamImpl.FIELD_MATCHING_TYPE, StreamImpl.MatchingType.DEFAULT.name()).put(StreamImpl.FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, true).put(StreamImpl.FIELD_INDEX_SET_ID, requireNonNull(indexSet.getConfig().id(), "index set ID cannot be null")).put(StreamImpl.FIELD_DEFAULT_STREAM, false).build();
    final Stream stream = new StreamImpl(id, fields, Collections.emptyList(), Collections.emptySet(), indexSet);
    try {
        streamService.save(stream);
        LOG.info("Successfully created events stream <{}/{}>", stream.getId(), stream.getTitle());
    } catch (ValidationException e) {
        LOG.error("Couldn't create events stream <{}/{}>! This is a bug!", streamId, streamTitle, e);
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) StreamImpl(org.graylog2.streams.StreamImpl) Stream(org.graylog2.plugin.streams.Stream)

Example 87 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class MigrationHelpers method ensureUser.

@Nullable
public String ensureUser(String userName, String password, String firstName, String lastName, String email, Set<String> expectedRoles) {
    User previousUser = null;
    try {
        previousUser = userService.load(userName);
        if (previousUser == null || !previousUser.getRoleIds().containsAll(expectedRoles)) {
            final String msg = "Invalid user '" + userName + "', fixing it.";
            LOG.error(msg);
            throw new IllegalArgumentException(msg);
        }
    } catch (IllegalArgumentException ignored) {
        LOG.info("{} user is missing or invalid, re-adding it as a built-in user.", userName);
        final User fixedUser;
        if (previousUser != null) {
            fixedUser = previousUser;
            fixedUser.setRoleIds(expectedRoles);
        } else {
            fixedUser = userService.create();
            fixedUser.setName(userName);
            fixedUser.setFirstLastFullNames(firstName, lastName);
            fixedUser.setPassword(password);
            fixedUser.setEmail(email);
            fixedUser.setPermissions(Collections.emptyList());
            fixedUser.setRoleIds(expectedRoles);
            fixedUser.setTimeZone(DateTimeZone.UTC);
        }
        try {
            return userService.save(fixedUser);
        } catch (ValidationException e) {
            LOG.error("Unable to save fixed '" + userName + "' user, please restart Graylog to fix this.", e);
        }
    }
    if (previousUser == null) {
        LOG.error("Unable to access fixed '" + userName + "' user, please restart Graylog to fix this.");
        return null;
    }
    return previousUser.getId();
}
Also used : User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) Nullable(javax.annotation.Nullable)

Example 88 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class MigrationHelpers method ensureBuiltinRole.

@Nullable
public String ensureBuiltinRole(String roleName, String description, Set<String> expectedPermissions) {
    Role previousRole = null;
    try {
        previousRole = roleService.load(roleName);
        if (!previousRole.isReadOnly() || !expectedPermissions.equals(previousRole.getPermissions())) {
            final String msg = "Invalid role '" + roleName + "', fixing it.";
            LOG.error(msg);
            // jump to fix code
            throw new IllegalArgumentException(msg);
        }
    } catch (NotFoundException | IllegalArgumentException | NoSuchElementException ignored) {
        LOG.info("{} role is missing or invalid, re-adding it as a built-in role.", roleName);
        final RoleImpl fixedRole = new RoleImpl();
        // copy the mongodb id over, in order to update the role instead of readding it
        if (previousRole != null) {
            fixedRole._id = previousRole.getId();
        }
        fixedRole.setReadOnly(true);
        fixedRole.setName(roleName);
        fixedRole.setDescription(description);
        fixedRole.setPermissions(expectedPermissions);
        try {
            final Role savedRole = roleService.save(fixedRole);
            return savedRole.getId();
        } catch (DuplicateKeyException | ValidationException e) {
            LOG.error("Unable to save fixed '" + roleName + "' role, please restart Graylog to fix this.", e);
        }
    }
    if (previousRole == null) {
        LOG.error("Unable to access fixed '" + roleName + "' role, please restart Graylog to fix this.");
        return null;
    }
    return previousRole.getId();
}
Also used : Role(org.graylog2.shared.users.Role) RoleImpl(org.graylog2.users.RoleImpl) NotFoundException(org.graylog2.database.NotFoundException) NoSuchElementException(java.util.NoSuchElementException) Nullable(javax.annotation.Nullable)

Example 89 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class V20161116172200_CreateDefaultStreamMigration method createDefaultStream.

private void createDefaultStream() {
    final IndexSet indexSet = indexSetRegistry.getDefault();
    final ObjectId id = new ObjectId(Stream.DEFAULT_STREAM_ID);
    final Map<String, Object> fields = ImmutableMap.<String, Object>builder().put(StreamImpl.FIELD_TITLE, "All messages").put(StreamImpl.FIELD_DESCRIPTION, "Stream containing all messages").put(StreamImpl.FIELD_DISABLED, false).put(StreamImpl.FIELD_CREATED_AT, DateTime.now(DateTimeZone.UTC)).put(StreamImpl.FIELD_CREATOR_USER_ID, "local:admin").put(StreamImpl.FIELD_MATCHING_TYPE, StreamImpl.MatchingType.DEFAULT.name()).put(StreamImpl.FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, false).put(StreamImpl.FIELD_DEFAULT_STREAM, true).put(StreamImpl.FIELD_INDEX_SET_ID, indexSet.getConfig().id()).build();
    final Stream stream = new StreamImpl(id, fields, Collections.emptyList(), Collections.emptySet(), indexSet);
    try {
        streamService.save(stream);
        LOG.info("Successfully created default stream: {}", stream.getTitle());
    } catch (ValidationException e) {
        LOG.error("Couldn't create default stream! This is a bug!");
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) StreamImpl(org.graylog2.streams.StreamImpl) Stream(org.graylog2.plugin.streams.Stream) IndexSet(org.graylog2.indexer.IndexSet)

Example 90 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class V20161125142400_EmailAlarmCallbackMigration method migrateStream.

private Optional<String> migrateStream(Stream stream) {
    final Map<String, Object> defaultConfig = this.getDefaultEmailAlarmCallbackConfig();
    LOG.debug("Creating email alarm callback for stream <" + stream.getId() + ">");
    final AlarmCallbackConfiguration alarmCallbackConfiguration = alarmCallbackService.create(stream.getId(), CreateAlarmCallbackRequest.create(EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", defaultConfig), "local:admin");
    try {
        final String callbackId = this.alarmCallbackService.save(alarmCallbackConfiguration);
        LOG.debug("Successfully created email alarm callback <" + callbackId + "> for stream <" + stream.getId() + ">.");
        return Optional.of(callbackId);
    } catch (ValidationException e) {
        LOG.error("Unable to create email alarm callback for stream <" + stream.getId() + ">: ", e);
    }
    return Optional.empty();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)52 AuditEvent (org.graylog2.audit.jersey.AuditEvent)52 ValidationException (org.graylog2.plugin.database.ValidationException)52 Timed (com.codahale.metrics.annotation.Timed)39 Path (javax.ws.rs.Path)32 ApiResponses (io.swagger.annotations.ApiResponses)30 BadRequestException (javax.ws.rs.BadRequestException)28 PUT (javax.ws.rs.PUT)27 Consumes (javax.ws.rs.Consumes)25 POST (javax.ws.rs.POST)24 Produces (javax.ws.rs.Produces)24 User (org.graylog2.plugin.database.users.User)22 URI (java.net.URI)20 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)15 NotFoundException (org.graylog2.database.NotFoundException)15 Test (org.junit.Test)15 Stream (org.graylog2.plugin.streams.Stream)14 ObjectId (org.bson.types.ObjectId)13 GrokPattern (org.graylog2.grok.GrokPattern)13 MessageInput (org.graylog2.plugin.inputs.MessageInput)13