use of org.graylog2.Configuration in project graylog2-server by Graylog2.
the class MessageOutputFactoryTest method testNonExistentOutputType.
@Test(expected = IllegalArgumentException.class)
public void testNonExistentOutputType() throws MessageOutputConfigurationException {
final String outputType = "non.existent";
final Output output = mock(Output.class);
when(output.getType()).thenReturn(outputType);
final Stream stream = mock(Stream.class);
final Configuration configuration = mock(Configuration.class);
messageOutputFactory.fromStreamOutput(output, stream, configuration);
}
use of org.graylog2.Configuration in project graylog2-server by Graylog2.
the class StreamServiceImpl method updateCallbackConfiguration.
// I tried to be sorry, really. https://www.youtube.com/watch?v=3KVyRqloGmk
private void updateCallbackConfiguration(String action, String type, String entity, List<AlarmCallbackConfiguration> streamCallbacks) {
final AtomicBoolean ran = new AtomicBoolean(false);
streamCallbacks.stream().filter(callback -> callback.getType().equals(EmailAlarmCallback.class.getCanonicalName())).forEach(callback -> {
ran.set(true);
final Map<String, Object> configuration = callback.getConfiguration();
String key;
if ("users".equals(type)) {
key = EmailAlarmCallback.CK_USER_RECEIVERS;
} else {
key = EmailAlarmCallback.CK_EMAIL_RECEIVERS;
}
@SuppressWarnings("unchecked") final List<String> recipients = (List<String>) configuration.get(key);
if ("add".equals(action)) {
if (!recipients.contains(entity)) {
recipients.add(entity);
}
} else {
if (recipients.contains(entity)) {
recipients.remove(entity);
}
}
configuration.put(key, recipients);
final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callback).toBuilder().setConfiguration(configuration).build();
try {
alarmCallbackConfigurationService.save(updatedConfig);
} catch (ValidationException e) {
throw new BadRequestException("Unable to save alarm callback configuration", e);
}
});
if (!ran.get()) {
throw new BadRequestException("Unable to " + action + " receiver: Stream has no email alarm callback.");
}
}
use of org.graylog2.Configuration in project graylog2-server by Graylog2.
the class LdapUserAuthenticator method updateFromLdap.
private void updateFromLdap(User user, LdapEntry userEntry, LdapSettings ldapSettings, String username) {
final String displayNameAttribute = ldapSettings.getDisplayNameAttribute();
final String fullName = firstNonNull(userEntry.get(displayNameAttribute), username);
user.setName(username);
user.setFullName(fullName);
user.setExternal(true);
if (user.getTimeZone() == null) {
user.setTimeZone(rootTimeZone);
}
final String email = userEntry.getEmail();
if (isNullOrEmpty(email)) {
LOG.debug("No email address found for user {} in LDAP. Using {}@localhost", username, username);
user.setEmail(username + "@localhost");
} else {
user.setEmail(email);
}
// TODO This is a crude hack until we have a proper way to distinguish LDAP users from normal users
if (isNullOrEmpty(user.getHashedPassword())) {
((UserImpl) user).setHashedPassword("User synced from LDAP.");
}
// map ldap groups to user roles, if the mapping is present
final Set<String> translatedRoleIds = Sets.newHashSet(Sets.union(Sets.newHashSet(ldapSettings.getDefaultGroupId()), ldapSettings.getAdditionalDefaultGroupIds()));
if (!userEntry.getGroups().isEmpty()) {
// ldap search returned groups, these always override the ones set on the user
try {
final Map<String, Role> roleNameToRole = roleService.loadAllLowercaseNameMap();
for (String ldapGroupName : userEntry.getGroups()) {
final String roleName = ldapSettings.getGroupMapping().get(ldapGroupName);
if (roleName == null) {
LOG.debug("User {}: No group mapping for ldap group <{}>", username, ldapGroupName);
continue;
}
final Role role = roleNameToRole.get(roleName.toLowerCase(Locale.ENGLISH));
if (role != null) {
LOG.debug("User {}: Mapping ldap group <{}> to role <{}>", username, ldapGroupName, role.getName());
translatedRoleIds.add(role.getId());
} else {
LOG.warn("User {}: No role found for ldap group <{}>", username, ldapGroupName);
}
}
} catch (NotFoundException e) {
LOG.error("Unable to load user roles", e);
}
} else if (ldapSettings.getGroupMapping().isEmpty() || ldapSettings.getGroupSearchBase().isEmpty() || ldapSettings.getGroupSearchPattern().isEmpty() || ldapSettings.getGroupIdAttribute().isEmpty()) {
// no group mapping or configuration set, we'll leave the previously set groups alone on sync
// when first creating the user these will be empty
translatedRoleIds.addAll(user.getRoleIds());
}
user.setRoleIds(translatedRoleIds);
// preserve the raw permissions (the ones without the synthetic self-edit permissions or the "*" admin one)
user.setPermissions(user.getPermissions());
}
use of org.graylog2.Configuration in project graylog2-server by Graylog2.
the class TimeBasedRotationStrategy method shouldRotate.
@Nullable
@Override
protected Result shouldRotate(String index, IndexSet indexSet) {
final IndexSetConfig indexSetConfig = requireNonNull(indexSet.getConfig(), "Index set configuration must not be null");
final String indexSetId = indexSetConfig.id();
checkState(!isNullOrEmpty(index), "Index name must not be null or empty");
checkState(!isNullOrEmpty(indexSetId), "Index set ID must not be null or empty");
checkState(indexSetConfig.rotationStrategy() instanceof TimeBasedRotationStrategyConfig, "Invalid rotation strategy config <" + indexSetConfig.rotationStrategy().getClass().getCanonicalName() + "> for index set <" + indexSetId + ">");
final TimeBasedRotationStrategyConfig config = (TimeBasedRotationStrategyConfig) indexSetConfig.rotationStrategy();
final Period rotationPeriod = config.rotationPeriod().normalizedStandard();
final DateTime now = Tools.nowUTC();
// when first started, we might not know the last rotation time, look up the creation time of the index instead.
if (!lastRotation.containsKey(indexSetId)) {
final DateTime creationDate = indices.indexCreationDate(index);
if (creationDate != null) {
final DateTime currentAnchor = determineRotationPeriodAnchor(creationDate, rotationPeriod);
anchor.put(indexSetId, currentAnchor);
lastRotation.put(indexSetId, creationDate);
}
// still not able to figure out the last rotation time, we'll rotate forcibly
if (!lastRotation.containsKey(indexSetId)) {
return new SimpleResult(true, "No known previous rotation time, forcing index rotation now.");
}
}
final DateTime currentAnchor = anchor.get(indexSetId);
final DateTime nextRotation = currentAnchor.plus(rotationPeriod);
if (nextRotation.isAfter(now)) {
final String message = new MessageFormat("Next rotation at {0}", Locale.ENGLISH).format(new Object[] { nextRotation });
return new SimpleResult(false, message);
}
// determine new anchor (push it to within less then one period before now) in case we missed one or more periods
DateTime tmpAnchor;
int multiplicator = 0;
do {
tmpAnchor = currentAnchor.withPeriodAdded(rotationPeriod, ++multiplicator);
} while (tmpAnchor.isBefore(now));
final DateTime nextAnchor = currentAnchor.withPeriodAdded(rotationPeriod, multiplicator - 1);
anchor.put(indexSetId, nextAnchor);
lastRotation.put(indexSetId, now);
final String message = new MessageFormat("Rotation period {0} elapsed, next rotation at {1}", Locale.ENGLISH).format(new Object[] { now, nextAnchor });
return new SimpleResult(true, message);
}
use of org.graylog2.Configuration in project graylog2-server by Graylog2.
the class AbstractIndexCountBasedRetentionStrategy method retain.
@Override
public void retain(IndexSet indexSet) {
final Map<String, Set<String>> deflectorIndices = indexSet.getAllIndexAliases();
final int indexCount = (int) deflectorIndices.keySet().stream().filter(indexName -> !indices.isReopened(indexName)).count();
final Optional<Integer> maxIndices = getMaxNumberOfIndices(indexSet);
if (!maxIndices.isPresent()) {
LOG.warn("No retention strategy configuration found, not running index retention!");
return;
}
// Do we have more indices than the configured maximum?
if (indexCount <= maxIndices.get()) {
LOG.debug("Number of indices ({}) lower than limit ({}). Not performing any retention actions.", indexCount, maxIndices.get());
return;
}
// We have more indices than the configured maximum! Remove as many as needed.
final int removeCount = indexCount - maxIndices.get();
final String msg = "Number of indices (" + indexCount + ") higher than limit (" + maxIndices.get() + "). " + "Running retention for " + removeCount + " indices.";
LOG.info(msg);
activityWriter.write(new Activity(msg, IndexRetentionThread.class));
runRetention(indexSet, deflectorIndices, removeCount);
}
Aggregations