diff --git a/service/config/sample.yml b/service/config/sample.yml index 612b91f6..a90f3409 100644 --- a/service/config/sample.yml +++ b/service/config/sample.yml @@ -79,8 +79,6 @@ dynamoDbTables: tableName: Example_RemoteConfig reportMessage: tableName: Example_ReportMessage - reservedUsernames: - tableName: Example_ReservedUsernames subscriptions: tableName: Example_Subscriptions verificationSessions: diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 01317e52..175790ca 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -229,7 +229,6 @@ import org.whispersystems.textsecuregcm.workers.AssignUsernameCommand; import org.whispersystems.textsecuregcm.workers.CertificateCommand; import org.whispersystems.textsecuregcm.workers.CheckDynamicConfigurationCommand; import org.whispersystems.textsecuregcm.workers.DeleteUserCommand; -import org.whispersystems.textsecuregcm.workers.ReserveUsernameCommand; import org.whispersystems.textsecuregcm.workers.ServerVersionCommand; import org.whispersystems.textsecuregcm.workers.SetCrawlerAccelerationTask; import org.whispersystems.textsecuregcm.workers.SetRequestLoggingEnabledTask; @@ -259,7 +258,6 @@ public class WhisperServerService extends Application patternCache = CacheBuilder.newBuilder() - .maximumSize(1_000) - .build(new CacheLoader<>() { - @Override - public Pattern load(final String s) { - return Pattern.compile(s, Pattern.CASE_INSENSITIVE); - } - }); - - @VisibleForTesting - static final String KEY_PATTERN = "P"; - private static final String ATTR_RESERVED_FOR_UUID = "U"; - - private static final Timer IS_PROHIBITED_TIMER = Metrics.timer(name(ProhibitedUsernames.class, "isProhibited")); - - private static final Logger log = LoggerFactory.getLogger(ProhibitedUsernames.class); - - public ProhibitedUsernames(final DynamoDbClient dynamoDbClient, final String tableName) { - this.dynamoDbClient = dynamoDbClient; - this.tableName = tableName; - } - - public boolean isProhibited(final String nickname, final UUID accountIdentifier) { - return IS_PROHIBITED_TIMER.record(() -> { - final ScanIterable scanIterable = dynamoDbClient.scanPaginator(ScanRequest.builder() - .tableName(tableName) - .build()); - - for (final ScanResponse scanResponse : scanIterable) { - if (scanResponse.hasItems()) { - for (final Map item : scanResponse.items()) { - try { - final Pattern pattern = patternCache.get(item.get(KEY_PATTERN).s()); - final UUID reservedFor = AttributeValues.getUUID(item, ATTR_RESERVED_FOR_UUID, null); - - if (pattern.matcher(nickname).matches() && !accountIdentifier.equals(reservedFor)) { - return true; - } - } catch (final Exception e) { - log.error("Failed to load pattern from item: {}", item, e); - } - } - } - } - - return false; - }); - } - - /** - * Prohibits username except for all accounts except `reservedFor` - * - * @param pattern pattern to prohibit - * @param reservedFor an account that is allowed to use names in the pattern - */ - public void prohibitUsername(final String pattern, final UUID reservedFor) { - dynamoDbClient.putItem(PutItemRequest.builder() - .tableName(tableName) - .item(Map.of( - KEY_PATTERN, AttributeValues.fromString(pattern), - ATTR_RESERVED_FOR_UUID, AttributeValues.fromUUID(reservedFor))) - .build()); - } -} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/AssignUsernameCommand.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/AssignUsernameCommand.java index 7863a01e..4e867782 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/AssignUsernameCommand.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/AssignUsernameCommand.java @@ -50,7 +50,6 @@ import org.whispersystems.textsecuregcm.storage.MessagesManager; import org.whispersystems.textsecuregcm.storage.PhoneNumberIdentifiers; import org.whispersystems.textsecuregcm.storage.Profiles; import org.whispersystems.textsecuregcm.storage.ProfilesManager; -import org.whispersystems.textsecuregcm.storage.ProhibitedUsernames; import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswords; import org.whispersystems.textsecuregcm.storage.RegistrationRecoveryPasswordsManager; import org.whispersystems.textsecuregcm.storage.ReportMessageDynamoDb; @@ -176,8 +175,6 @@ public class AssignUsernameCommand extends EnvironmentCommand { - - public ReserveUsernameCommand() { - super("reserve-username", "Reserve a username pattern for a specific account identifier"); - } - - @Override - public void configure(final Subparser subparser) { - super.configure(subparser); - - subparser.addArgument("-p", "--pattern") - .dest("pattern") - .type(String.class) - .required(true); - - subparser.addArgument("-u", "--uuid") - .dest("uuid") - .type(String.class) - .required(true); - } - - @Override - protected void run(final Bootstrap bootstrap, final Namespace namespace, - final WhisperServerConfiguration config) throws Exception { - - final DynamoDbClient dynamoDbClient = DynamoDbFromConfig.client(config.getDynamoDbClientConfiguration(), - software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider.create()); - - final ProhibitedUsernames prohibitedUsernames = new ProhibitedUsernames(dynamoDbClient, - config.getDynamoDbTables().getReservedUsernames().getTableName()); - - final String pattern = namespace.getString("pattern").trim(); - - try { - Pattern.compile(pattern); - } catch (final Exception e) { - throw new IllegalArgumentException("Bad pattern: " + pattern, e); - } - - final UUID aci = UUID.fromString(namespace.getString("uuid").trim()); - - prohibitedUsernames.prohibitUsername(pattern, aci); - - System.out.format("Reserved %s for account %s\n", pattern, aci); - } -}