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