use of org.graylog2.migrations.Migration 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.migrations.Migration in project graylog2-server by Graylog2.
the class V20190127111728_MigrateWidgetFormatSettings method upgrade.
@Override
public void upgrade() {
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already completed.");
return;
}
final Set<String> viewIds = new HashSet<>();
final FindIterable<Document> documents = viewsCollection.find();
boolean viewMigrated;
for (final Document view : documents) {
viewMigrated = false;
final Document states = view.get("state", Document.class);
for (Map.Entry<String, Object> obj : states.entrySet()) {
final Document state = (Document) obj.getValue();
if (state.get("widgets") instanceof List) {
@SuppressWarnings("unchecked") final List<Document> widgets = (List) state.get("widgets");
for (final Document widget : widgets) {
final String type = widget.getString("type");
if (type.equals("aggregation")) {
final Document config = widget.get("config", Document.class);
final Document formatSettings = config.get("formatting_settings", Document.class);
if (formatSettings == null) {
continue;
}
final Object charColorsObj = formatSettings.get("chart_colors");
if (charColorsObj == null) {
continue;
}
viewMigrated = true;
@SuppressWarnings({ "unchecked", "rawtypes" }) final Map<String, String> chartColors = (Map) charColorsObj;
List<Document> chartColorSettings = chartColors.entrySet().stream().map(entry -> {
final Document chartColorFieldSetting = new Document();
chartColorFieldSetting.put("field_name", entry.getKey());
chartColorFieldSetting.put("chart_color", entry.getValue());
return chartColorFieldSetting;
}).collect(Collectors.toList());
formatSettings.put("chart_colors", chartColorSettings);
config.put("formatting_settings", formatSettings);
widget.put("config", config);
}
}
if (viewMigrated) {
state.put("widgets", widgets);
}
}
}
if (viewMigrated) {
viewsCollection.updateOne(new BasicDBObject("_id", view.getObjectId("_id")), new Document("$set", view));
final String viewId = view.getObjectId("_id").toString();
viewIds.add(viewId);
}
}
LOG.info("Migration completed. {} views where migrated.", viewIds.size());
clusterConfigService.write(V20190127111728_MigrateWidgetFormatSettings.MigrationCompleted.create(viewIds.size(), viewIds));
}
use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class V20191129134600_CreateInitialUrlWhitelist method upgrade.
@Override
public void upgrade() {
final MigrationCompleted migrationCompleted = configService.get(MigrationCompleted.class);
if (migrationCompleted != null) {
log.debug("Migration already completed.");
return;
}
UrlWhitelist whitelist = createWhitelist();
whitelistService.saveWhitelist(whitelist);
configService.write(MigrationCompleted.create(whitelist.toString()));
}
use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class V20161116172100_DefaultIndexSetMigration method upgrade.
@Override
public void upgrade() {
// Do not run again if the migration marker can be found in the database.
if (clusterConfigService.get(DefaultIndexSetCreated.class) != null) {
return;
}
final IndexManagementConfig indexManagementConfig = clusterConfigService.get(IndexManagementConfig.class);
checkState(indexManagementConfig != null, "Couldn't find index management configuration");
final IndexSetConfig config = IndexSetConfig.builder().title("Default index set").description("The Graylog default index set").isRegular(true).indexPrefix(elasticsearchConfiguration.getIndexPrefix()).shards(elasticsearchConfiguration.getShards()).replicas(elasticsearchConfiguration.getReplicas()).rotationStrategy(getRotationStrategyConfig(indexManagementConfig)).retentionStrategy(getRetentionStrategyConfig(indexManagementConfig)).creationDate(ZonedDateTime.now(ZoneOffset.UTC)).indexAnalyzer(elasticsearchConfiguration.getAnalyzer()).indexTemplateName(elasticsearchConfiguration.getTemplateName()).indexOptimizationMaxNumSegments(elasticsearchConfiguration.getIndexOptimizationMaxNumSegments()).indexOptimizationDisabled(elasticsearchConfiguration.isDisableIndexOptimization()).build();
final IndexSetConfig savedConfig = indexSetService.save(config);
clusterConfigService.write(DefaultIndexSetConfig.create(savedConfig.id()));
clusterConfigService.write(DefaultIndexSetCreated.create());
LOG.debug("Successfully created default index set: {}", savedConfig);
}
use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.
the class V20161122174500_AssignIndexSetsToStreamsMigration method upgrade.
@Override
public void upgrade() {
// Only run this migration once.
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already completed.");
return;
}
final IndexSetConfig indexSetConfig = findDefaultIndexSet();
final ImmutableSet.Builder<String> completedStreamIds = ImmutableSet.builder();
final ImmutableSet.Builder<String> failedStreamIds = ImmutableSet.builder();
// index sets, so the only one that exists is the "default" one created by an earlier migration.
for (Stream stream : streamService.loadAll()) {
if (isNullOrEmpty(stream.getIndexSetId())) {
LOG.info("Assigning index set <{}> ({}) to stream <{}> ({})", indexSetConfig.id(), indexSetConfig.title(), stream.getId(), stream.getTitle());
stream.setIndexSetId(indexSetConfig.id());
try {
streamService.save(stream);
completedStreamIds.add(stream.getId());
} catch (ValidationException e) {
LOG.error("Unable to save stream <{}>", stream.getId(), e);
failedStreamIds.add(stream.getId());
}
}
}
// Mark this migration as done.
clusterConfigService.write(MigrationCompleted.create(indexSetConfig.id(), completedStreamIds.build(), failedStreamIds.build()));
}
Aggregations