Search in sources :

Example 21 with Migration

use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.

the class RolesToGrantsMigrationTest method setUp.

@BeforeEach
void setUp(MongoDBTestService mongodb, MongoJackObjectMapperProvider mongoJackObjectMapperProvider, GRNRegistry grnRegistry, TestUserService userService) {
    when(permissions.readerBasePermissions()).thenReturn(ImmutableSet.of());
    when(validator.validate(any())).thenReturn(ImmutableSet.of());
    this.grnRegistry = grnRegistry;
    roleService = new RoleServiceImpl(mongodb.mongoConnection(), mongoJackObjectMapperProvider, permissions, validator);
    dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
    this.userService = userService;
    DBGrantService dbGrantService = new DBGrantService(mongodb.mongoConnection(), mongoJackObjectMapperProvider, grnRegistry);
    migration = new RolesToGrantsMigration(roleService, userService, dbGrantService, grnRegistry, "admin");
}
Also used : DBGrantService(org.graylog.security.DBGrantService) RoleServiceImpl(org.graylog2.users.RoleServiceImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with Migration

use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.

the class V20191121145100_FixDefaultGrokPatterns method migratePattern.

private void migratePattern(PatternToMigrate patternToMigrate) throws ValidationException {
    final Optional<GrokPattern> currentPattern = grokPatternService.loadByName(patternToMigrate.name());
    if (!currentPattern.isPresent()) {
        log.debug("Couldn't find default pattern '{}'. Skipping migration.", patternToMigrate.name());
        return;
    }
    final GrokPattern pattern = currentPattern.get();
    if (!patternToMigrate.migrateFrom().equals(pattern.pattern())) {
        log.info("Skipping migration of modified default Grok Pattern '{}'.", pattern.name());
    } else {
        log.info("Migrating default Grok Pattern '{}'.", pattern.name());
        final GrokPattern migratedPattern = pattern.toBuilder().pattern(patternToMigrate.migrateTo()).build();
        grokPatternService.update(migratedPattern);
    }
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern)

Example 23 with Migration

use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.

the class V20191219090834_AddSourcesPage method upgrade.

@Override
public void upgrade() {
    if (configService.get(V20191219090834_AddSourcesPage.MigrationCompleted.class) != null) {
        LOG.debug("Migration already completed.");
        return;
    }
    try {
        final URL contentPackURL = V20191219090834_AddSourcesPage.class.getResource("V20191219090834_AddSourcesPage_Content_Pack.json");
        final ContentPack contentPack = this.objectMapper.readValue(contentPackURL, ContentPack.class);
        final ContentPack pack = this.contentPackPersistenceService.insert(contentPack).orElseThrow(() -> {
            configService.write(V20191219090834_AddSourcesPage.MigrationCompleted.create(contentPack.id().toString()));
            return new ContentPackException("Content pack " + contentPack.id() + " with this revision " + contentPack.revision() + " already found!");
        });
        contentPackService.installContentPack(pack, Collections.emptyMap(), "Add Sources Page", "admin");
        configService.write(V20191219090834_AddSourcesPage.MigrationCompleted.create(pack.id().toString()));
    } catch (Exception e) {
        throw new RuntimeException("Could not install Source Page Content Pack.", e);
    }
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) ContentPack(org.graylog2.contentpacks.model.ContentPack) URL(java.net.URL) IOException(java.io.IOException) ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException)

Example 24 with Migration

use of org.graylog2.migrations.Migration in project graylog2-server by Graylog2.

the class V20161215163900_MoveIndexSetDefaultConfig method upgrade.

@Override
public void upgrade() {
    if (clusterConfigService.get(MigrationCompleted.class) != null) {
        LOG.debug("Migration already done.");
        return;
    }
    // Do not overwrite an existing default index config
    boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null;
    final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE));
    for (final Document document : documents) {
        final ObjectId id = document.getObjectId(FIELD_ID);
        final String idString = id.toHexString();
        final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false);
        if (!defaultDone && isDefault) {
            defaultDone = true;
            clusterConfigService.write(DefaultIndexSetConfig.create(idString));
        }
        final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount();
        if (modifiedCount > 0) {
            LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
            builder.add(idString);
        } else {
            LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString);
        }
    }
    clusterConfigService.write(MigrationCompleted.create(builder.build()));
}
Also used : DefaultIndexSetConfig(org.graylog2.indexer.indexset.DefaultIndexSetConfig) ImmutableSet(com.google.common.collect.ImmutableSet) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document)

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