From 706de8e2f17c85b0f118a20d06c5fa773d6f091d Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Mon, 29 Nov 2021 15:07:43 -0500 Subject: [PATCH] Add a command to migrate remote configuration entries from Postgres to DynamoDB --- .../textsecuregcm/WhisperServerService.java | 2 + .../workers/MigrateRemoteConfigsCommand.java | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 service/src/main/java/org/whispersystems/textsecuregcm/workers/MigrateRemoteConfigsCommand.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 9320f6c3d..90aa6c6fc 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -215,6 +215,7 @@ import org.whispersystems.textsecuregcm.websocket.WebSocketAccountAuthenticator; import org.whispersystems.textsecuregcm.workers.CertificateCommand; import org.whispersystems.textsecuregcm.workers.CheckDynamicConfigurationCommand; import org.whispersystems.textsecuregcm.workers.DeleteUserCommand; +import org.whispersystems.textsecuregcm.workers.MigrateRemoteConfigsCommand; import org.whispersystems.textsecuregcm.workers.ReserveUsernameCommand; import org.whispersystems.textsecuregcm.workers.ServerVersionCommand; import org.whispersystems.textsecuregcm.workers.SetCrawlerAccelerationTask; @@ -242,6 +243,7 @@ public class WhisperServerService extends Application("accountdb", "accountsdb.xml") { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/MigrateRemoteConfigsCommand.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/MigrateRemoteConfigsCommand.java new file mode 100644 index 000000000..1e93472ea --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/MigrateRemoteConfigsCommand.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2021 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.workers; + +import io.dropwizard.Application; +import io.dropwizard.cli.EnvironmentCommand; +import io.dropwizard.jdbi3.JdbiFactory; +import io.dropwizard.setup.Environment; +import net.sourceforge.argparse4j.inf.Namespace; +import org.jdbi.v3.core.Jdbi; +import org.whispersystems.textsecuregcm.WhisperServerConfiguration; +import org.whispersystems.textsecuregcm.storage.FaultTolerantDatabase; +import org.whispersystems.textsecuregcm.storage.RemoteConfigs; +import org.whispersystems.textsecuregcm.storage.RemoteConfigsDynamoDb; +import org.whispersystems.textsecuregcm.util.DynamoDbFromConfig; +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; + +public class MigrateRemoteConfigsCommand extends EnvironmentCommand { + + public MigrateRemoteConfigsCommand() { + super(new Application<>() { + @Override + public void run(WhisperServerConfiguration configuration, Environment environment) { + } + }, "migrate-remote-config", "Migrate remote configuration from Postgres to DynamoDB"); + } + + @Override + protected void run(final Environment environment, final Namespace namespace, + final WhisperServerConfiguration configuration) throws Exception { + + JdbiFactory jdbiFactory = new JdbiFactory(); + Jdbi accountJdbi = jdbiFactory.build(environment, configuration.getAccountsDatabaseConfiguration(), "accountdb"); + FaultTolerantDatabase accountDatabase = new FaultTolerantDatabase("account_database_delete_user", accountJdbi, + configuration.getAccountsDatabaseConfiguration().getCircuitBreakerConfiguration()); + + DynamoDbClient dynamoDbClient = DynamoDbFromConfig.client( + configuration.getDynamoDbClientConfiguration(), + software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider.create()); + + RemoteConfigs remoteConfigs = new RemoteConfigs(accountDatabase); + RemoteConfigsDynamoDb remoteConfigsDynamoDb = + new RemoteConfigsDynamoDb(dynamoDbClient, configuration.getDynamoDbTables().getRemoteConfig().getTableName()); + + remoteConfigs.getAll().forEach(remoteConfigsDynamoDb::set); + } +}