Search in sources :

Example 1 with ValidationException

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

the class UserPermissionMigrationPeriodical method doRun.

@Override
public void doRun() {
    final List<User> users = userService.loadAll();
    final String adminRoleId = roleService.getAdminRoleObjectId();
    final String readerRoleId = roleService.getReaderRoleObjectId();
    for (User user : users) {
        if (user.isLocalAdmin()) {
            log.debug("Skipping local admin user.");
            continue;
        }
        final Set<String> fixedPermissions = Sets.newHashSet();
        final Set<String> fixedRoleIds = Sets.newHashSet(user.getRoleIds());
        final Set<String> permissionSet = Sets.newHashSet(user.getPermissions());
        boolean hasWildcardPermission = permissionSet.contains("*");
        if (hasWildcardPermission && !user.getRoleIds().contains(adminRoleId)) {
            // need to add the admin role to this user
            fixedRoleIds.add(adminRoleId);
        }
        final Set<String> basePermissions = permissions.readerPermissions(user.getName());
        final boolean hasCompleteReaderSet = permissionSet.containsAll(basePermissions);
        //   - it has the wildcard permissions
        if (!user.getRoleIds().isEmpty() && hasCompleteReaderSet && hasWildcardPermission) {
            log.debug("Not migrating user {}, it has already been migrated.", user.getName());
            continue;
        }
        if (hasCompleteReaderSet && !user.getRoleIds().contains(readerRoleId)) {
            // need to add the reader role to this user
            fixedRoleIds.add(readerRoleId);
        }
        // filter out the individual permissions to dashboards and streams
        final List<String> dashboardStreamPermissions = Lists.newArrayList(Sets.filter(permissionSet, permission -> !basePermissions.contains(permission) && !"*".equals(permission)));
        // add the minimal permission set back to the user
        fixedPermissions.addAll(permissions.userSelfEditPermissions(user.getName()));
        fixedPermissions.addAll(dashboardStreamPermissions);
        log.info("Migrating permissions to roles for user {} from permissions {} and roles {} to new permissions {} and roles {}", user.getName(), permissionSet, user.getRoleIds(), fixedPermissions, fixedRoleIds);
        user.setRoleIds(fixedRoleIds);
        user.setPermissions(Lists.newArrayList(fixedPermissions));
        try {
            userService.save(user);
        } catch (ValidationException e) {
            log.error("Unable to migrate user permissions for user " + user.getName(), e);
        }
    }
    log.info("Marking user permission migration as done.");
    clusterConfigService.write(UserPermissionMigrationState.create(true));
}
Also used : Logger(org.slf4j.Logger) RoleService(org.graylog2.users.RoleService) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Sets(com.google.common.collect.Sets) Inject(javax.inject.Inject) Periodical(org.graylog2.plugin.periodical.Periodical) List(java.util.List) Lists(com.google.common.collect.Lists) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) UserService(org.graylog2.shared.users.UserService) Predicate(com.google.common.base.Predicate) ValidationException(org.graylog2.plugin.database.ValidationException) UserPermissionMigrationState(org.graylog2.cluster.UserPermissionMigrationState) User(org.graylog2.plugin.database.users.User) Permissions(org.graylog2.shared.security.Permissions) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 2 with ValidationException

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

the class PersistedInputsImpl method add.

@Override
public boolean add(MessageInput input) {
    try {
        final Input mongoInput = getInput(input);
        // Persist input.
        String id = inputService.save(mongoInput);
        input.setPersistId(id);
        return true;
    } catch (ValidationException e) {
        return false;
    }
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 3 with ValidationException

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

the class PersistedInputsImpl method update.

@Override
public boolean update(String id, MessageInput newInput) {
    try {
        final Input oldInput = inputService.find(id);
        newInput.setPersistId(id);
        final Input mongoInput = getInput(newInput);
        final List<Extractor> extractors = inputService.getExtractors(oldInput);
        final Map<String, String> staticFields = oldInput.getStaticFields();
        inputService.save(mongoInput);
        for (Map.Entry<String, String> entry : staticFields.entrySet()) inputService.addStaticField(mongoInput, entry.getKey(), entry.getValue());
        for (Extractor extractor : extractors) inputService.addExtractor(mongoInput, extractor);
        return true;
    } catch (NotFoundException | ValidationException e) {
        return false;
    }
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) Extractor(org.graylog2.plugin.inputs.Extractor) Map(java.util.Map)

Example 4 with ValidationException

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

the class PersistedInputsImpl method getInput.

private Input getInput(MessageInput input) throws ValidationException {
    // Build MongoDB data
    final Map<String, Object> inputData = input.asMap();
    // ... and check if it would pass validation. We don't need to go on if it doesn't.
    final Input mongoInput;
    if (input.getId() != null)
        mongoInput = inputService.create(input.getId(), inputData);
    else
        mongoInput = inputService.create(inputData);
    return mongoInput;
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput)

Example 5 with ValidationException

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

the class StreamResource method create.

@POST
@Timed
@ApiOperation(value = "Create a stream")
@RequiresPermissions(RestPermissions.STREAMS_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) final CreateStreamRequest cr) throws ValidationException {
    // Create stream.
    final Stream stream = streamService.create(cr, getCurrentUser().getName());
    stream.setDisabled(true);
    if (!stream.getIndexSet().getConfig().isWritable()) {
        throw new BadRequestException("Assigned index set must be writable!");
    }
    final String id = streamService.save(stream);
    final List<CreateStreamRuleRequest> rules = firstNonNull(cr.rules(), Collections.<CreateStreamRuleRequest>emptyList());
    for (CreateStreamRuleRequest request : rules) {
        StreamRule streamRule = streamRuleService.create(id, request);
        streamRuleService.save(streamRule);
    }
    clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
    final Map<String, String> result = ImmutableMap.of("stream_id", id);
    final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(id);
    return Response.created(streamUri).entity(result).build();
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) CreateStreamRuleRequest(org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

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