Search in sources :

Example 1 with Migration

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));
}
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 Migration

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));
}
Also used : Document(org.bson.Document) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Logger(org.slf4j.Logger) MongoCollection(com.mongodb.client.MongoCollection) MapEntry(com.google.protobuf.MapEntry) Inject(com.google.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) BasicDBObject(com.mongodb.BasicDBObject) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) WithBeanGetter(org.graylog.autovalue.WithBeanGetter) JsonAutoDetect(com.fasterxml.jackson.annotation.JsonAutoDetect) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) AutoValue(com.google.auto.value.AutoValue) Migration(org.graylog2.migrations.Migration) MongoConnection(org.graylog2.database.MongoConnection) Document(org.bson.Document) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with Migration

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()));
}
Also used : UrlWhitelist(org.graylog2.system.urlwhitelist.UrlWhitelist)

Example 4 with Migration

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);
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) DefaultIndexSetConfig(org.graylog2.indexer.indexset.DefaultIndexSetConfig) DefaultIndexSetCreated(org.graylog2.indexer.indexset.DefaultIndexSetCreated) IndexManagementConfig(org.graylog2.indexer.management.IndexManagementConfig)

Example 5 with Migration

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()));
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ImmutableSet(com.google.common.collect.ImmutableSet) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Stream(org.graylog2.plugin.streams.Stream)

Aggregations

Set (java.util.Set)4 Document (org.bson.Document)4 NotFoundException (org.graylog2.database.NotFoundException)4 DefaultIndexSetConfig (org.graylog2.indexer.indexset.DefaultIndexSetConfig)4 IndexSetConfig (org.graylog2.indexer.indexset.IndexSetConfig)4 ValidationException (org.graylog2.plugin.database.ValidationException)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 ObjectId (org.bson.types.ObjectId)3 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 ContentPackException (org.graylog2.contentpacks.exceptions.ContentPackException)3 Before (org.junit.Before)3 Test (org.junit.Test)3 BasicDBObject (com.mongodb.BasicDBObject)2 Map (java.util.Map)2