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));
}
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;
}
}
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;
}
}
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;
}
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();
}
Aggregations