remove appconfig in favor of S3ObjectMonitor
This commit is contained in:
parent
63021e0ca3
commit
0018e0bec6
|
@ -324,10 +324,12 @@ callingZkConfig:
|
||||||
backupsZkConfig:
|
backupsZkConfig:
|
||||||
serverSecret: secret://backupsZkConfig.serverSecret
|
serverSecret: secret://backupsZkConfig.serverSecret
|
||||||
|
|
||||||
appConfig:
|
dynamicConfig:
|
||||||
application: example
|
s3Region: a-region
|
||||||
environment: example
|
s3Bucket: a-bucket
|
||||||
configuration: example
|
objectKey: dynamic-config.yaml
|
||||||
|
maxSize: 100000
|
||||||
|
refreshInterval: PT10S
|
||||||
|
|
||||||
remoteConfig:
|
remoteConfig:
|
||||||
globalConfig: # keys and values that are given to clients on GET /v1/config
|
globalConfig: # keys and values that are given to clients on GET /v1/config
|
||||||
|
|
|
@ -364,14 +364,6 @@
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
<artifactId>dynamodb</artifactId>
|
<artifactId>dynamodb</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
|
||||||
<artifactId>appconfig</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
|
||||||
<artifactId>appconfigdata</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.amazonaws</groupId>
|
<groupId>com.amazonaws</groupId>
|
||||||
<artifactId>dynamodb-lock-client</artifactId>
|
<artifactId>dynamodb-lock-client</artifactId>
|
||||||
|
|
|
@ -27,7 +27,6 @@ import org.whispersystems.textsecuregcm.configuration.DatadogConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DefaultAwsCredentialsFactory;
|
import org.whispersystems.textsecuregcm.configuration.DefaultAwsCredentialsFactory;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DirectoryV2Configuration;
|
import org.whispersystems.textsecuregcm.configuration.DirectoryV2Configuration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DogstatsdConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.DogstatsdConfiguration;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DynamicConfigurationManagerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory;
|
import org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory;
|
||||||
import org.whispersystems.textsecuregcm.configuration.DynamoDbTables;
|
import org.whispersystems.textsecuregcm.configuration.DynamoDbTables;
|
||||||
import org.whispersystems.textsecuregcm.configuration.ExternalRequestFilterConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.ExternalRequestFilterConfiguration;
|
||||||
|
@ -244,7 +243,7 @@ public class WhisperServerConfiguration extends Configuration {
|
||||||
@Valid
|
@Valid
|
||||||
@NotNull
|
@NotNull
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private DynamicConfigurationManagerFactory appConfig;
|
private S3ObjectMonitorFactory dynamicConfig;
|
||||||
|
|
||||||
@Valid
|
@Valid
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -487,8 +486,8 @@ public class WhisperServerConfiguration extends Configuration {
|
||||||
return remoteConfig;
|
return remoteConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DynamicConfigurationManagerFactory getAppConfig() {
|
public S3ObjectMonitorFactory getDynamicConfig() {
|
||||||
return appConfig;
|
return dynamicConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BadgesConfiguration getBadges() {
|
public BadgesConfiguration getBadges() {
|
||||||
|
|
|
@ -351,8 +351,9 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||||
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
|
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
|
||||||
.scheduledExecutorService(name(getClass(), "dynamicConfiguration-%d")).threads(1).build();
|
.scheduledExecutorService(name(getClass(), "dynamicConfiguration-%d")).threads(1).build();
|
||||||
|
|
||||||
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager = config.getAppConfig()
|
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
|
||||||
.build(DynamicConfiguration.class, dynamicConfigurationExecutor, awsCredentialsProvider);
|
new DynamicConfigurationManager<>(
|
||||||
|
config.getDynamicConfig().build(awsCredentialsProvider, dynamicConfigurationExecutor), DynamicConfiguration.class);
|
||||||
dynamicConfigurationManager.start();
|
dynamicConfigurationManager.start();
|
||||||
|
|
||||||
MetricsUtil.configureRegistries(config, environment, dynamicConfigurationManager);
|
MetricsUtil.configureRegistries(config, environment, dynamicConfigurationManager);
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
package org.whispersystems.textsecuregcm.configuration;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
|
|
||||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
||||||
|
|
||||||
@JsonTypeName("default")
|
|
||||||
public class AppConfigConfiguration implements DynamicConfigurationManagerFactory {
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String application;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String environment;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String configuration;
|
|
||||||
|
|
||||||
public String getApplication() {
|
|
||||||
return application;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEnvironment() {
|
|
||||||
return environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getConfigurationName() {
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> DynamicConfigurationManager<T> build(Class<T> klazz, ScheduledExecutorService scheduledExecutorService,
|
|
||||||
AwsCredentialsProvider awsCredentialsProvider) {
|
|
||||||
return new DynamicConfigurationManager<>(application, environment, configuration, awsCredentialsProvider, klazz,
|
|
||||||
scheduledExecutorService);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2024 Signal Messenger, LLC
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.configuration;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
||||||
import io.dropwizard.jackson.Discoverable;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
|
|
||||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = AppConfigConfiguration.class)
|
|
||||||
public interface DynamicConfigurationManagerFactory extends Discoverable {
|
|
||||||
|
|
||||||
<T> DynamicConfigurationManager<T> build(Class<T> configurationClass,
|
|
||||||
ScheduledExecutorService scheduledExecutorService, AwsCredentialsProvider awsCredentialsProvider);
|
|
||||||
}
|
|
|
@ -7,44 +7,31 @@ package org.whispersystems.textsecuregcm.storage;
|
||||||
|
|
||||||
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import io.micrometer.core.instrument.Metrics;
|
import io.micrometer.core.instrument.Metrics;
|
||||||
import java.time.Duration;
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import javax.validation.ConstraintViolation;
|
import javax.validation.ConstraintViolation;
|
||||||
import javax.validation.Validation;
|
import javax.validation.Validation;
|
||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.whispersystems.textsecuregcm.s3.S3ObjectMonitor;
|
||||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
|
||||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
||||||
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionResponse;
|
|
||||||
|
|
||||||
public class DynamicConfigurationManager<T> {
|
public class DynamicConfigurationManager<T> {
|
||||||
|
|
||||||
private final String application;
|
private final S3ObjectMonitor configMonitor;
|
||||||
private final String environment;
|
|
||||||
private final String configurationName;
|
|
||||||
private final AppConfigDataClient appConfigClient;
|
|
||||||
private final Class<T> configurationClass;
|
private final Class<T> configurationClass;
|
||||||
|
|
||||||
// Set on initial config fetch
|
// Set on initial config fetch
|
||||||
private final AtomicReference<T> configuration = new AtomicReference<>();
|
private final AtomicReference<T> configuration = new AtomicReference<>();
|
||||||
private final CountDownLatch initialized = new CountDownLatch(1);
|
private final CountDownLatch initialized = new CountDownLatch(1);
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
|
||||||
private String configurationToken = null;
|
|
||||||
|
|
||||||
private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
|
private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
|
||||||
|
@ -54,28 +41,9 @@ public class DynamicConfigurationManager<T> {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DynamicConfigurationManager.class);
|
private static final Logger logger = LoggerFactory.getLogger(DynamicConfigurationManager.class);
|
||||||
|
|
||||||
public DynamicConfigurationManager(String application, String environment, String configurationName,
|
public DynamicConfigurationManager(final S3ObjectMonitor configMonitor, final Class<T> configurationClass) {
|
||||||
AwsCredentialsProvider awsCredentialsProvider, Class<T> configurationClass,
|
this.configMonitor = configMonitor;
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
this(AppConfigDataClient
|
|
||||||
.builder()
|
|
||||||
.credentialsProvider(awsCredentialsProvider)
|
|
||||||
.overrideConfiguration(ClientOverrideConfiguration.builder()
|
|
||||||
.apiCallTimeout(Duration.ofSeconds(10))
|
|
||||||
.apiCallAttemptTimeout(Duration.ofSeconds(10)).build())
|
|
||||||
.build(),
|
|
||||||
application, environment, configurationName, configurationClass, scheduledExecutorService);
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
DynamicConfigurationManager(AppConfigDataClient appConfigClient, String application, String environment,
|
|
||||||
String configurationName, Class<T> configurationClass, ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
this.appConfigClient = appConfigClient;
|
|
||||||
this.application = application;
|
|
||||||
this.environment = environment;
|
|
||||||
this.configurationName = configurationName;
|
|
||||||
this.configurationClass = configurationClass;
|
this.configurationClass = configurationClass;
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getConfiguration() {
|
public T getConfiguration() {
|
||||||
|
@ -88,92 +56,63 @@ public class DynamicConfigurationManager<T> {
|
||||||
return configuration.get();
|
return configuration.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public synchronized void start() {
|
||||||
if (initialized.getCount() == 0) {
|
if (initialized.getCount() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
configuration.set(retrieveInitialDynamicConfiguration());
|
|
||||||
|
this.configMonitor.start(this::receiveConfiguration);
|
||||||
|
|
||||||
|
// Starting an S3ObjectMonitor immediately does a blocking retrieve of the data, but it might
|
||||||
|
// fail to parse, in which case we wait for an update (which will happen on another thread) to
|
||||||
|
// give us a valid configuration before marking ourselves ready
|
||||||
|
while (configuration.get() == null) {
|
||||||
|
logger.warn("Failed to retrieve or parse initial dynamic configuration");
|
||||||
|
try {
|
||||||
|
this.wait();
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
initialized.countDown();
|
initialized.countDown();
|
||||||
|
|
||||||
scheduledExecutorService.scheduleWithFixedDelay(() -> {
|
|
||||||
try {
|
|
||||||
retrieveDynamicConfiguration().ifPresent(configuration::set);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.warn("Error retrieving dynamic configuration", e);
|
|
||||||
}
|
|
||||||
}, 0, 5, TimeUnit.SECONDS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<T> retrieveDynamicConfiguration() throws JsonProcessingException {
|
private synchronized void receiveConfiguration(InputStream configDataStream) {
|
||||||
if (configurationToken == null) {
|
final String configData;
|
||||||
logger.error("Invalid configuration token, will not be able to fetch configuration updates");
|
|
||||||
}
|
|
||||||
GetLatestConfigurationResponse latestConfiguration;
|
|
||||||
try {
|
try {
|
||||||
latestConfiguration = appConfigClient.getLatestConfiguration(GetLatestConfigurationRequest.builder()
|
configData = new String(configDataStream.readAllBytes());
|
||||||
.configurationToken(configurationToken)
|
} catch (IOException e) {
|
||||||
.build());
|
|
||||||
// token to use in the next fetch
|
|
||||||
configurationToken = latestConfiguration.nextPollConfigurationToken();
|
|
||||||
logger.debug("next token: {}", configurationToken);
|
|
||||||
} catch (final RuntimeException e) {
|
|
||||||
Metrics.counter(ERROR_COUNTER_NAME, ERROR_TYPE_TAG_NAME, "fetch").increment();
|
Metrics.counter(ERROR_COUNTER_NAME, ERROR_TYPE_TAG_NAME, "fetch").increment();
|
||||||
throw e;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!latestConfiguration.configuration().asByteBuffer().hasRemaining()) {
|
logger.info("Received new dynamic configuration of length {}", configData.length());
|
||||||
// empty configuration means nothing has changed
|
parseConfiguration(configData, configurationClass).ifPresent(configuration::set);
|
||||||
return Optional.empty();
|
this.notify();
|
||||||
}
|
|
||||||
logger.info("Received new config of length {}, next configuration token: {}",
|
|
||||||
latestConfiguration.configuration().asByteBuffer().remaining(),
|
|
||||||
configurationToken);
|
|
||||||
|
|
||||||
try {
|
|
||||||
return parseConfiguration(latestConfiguration.configuration().asUtf8String(), configurationClass);
|
|
||||||
} catch (final JsonProcessingException e) {
|
|
||||||
Metrics.counter(ERROR_COUNTER_NAME,
|
|
||||||
ERROR_TYPE_TAG_NAME, "parse",
|
|
||||||
CONFIG_CLASS_TAG_NAME, configurationClass.getName()).increment();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public static <T> Optional<T> parseConfiguration(final String configurationYaml, final Class<T> configurationClass)
|
public static <T> Optional<T> parseConfiguration(final String configurationYaml, final Class<T> configurationClass) {
|
||||||
throws JsonProcessingException {
|
final T configuration;
|
||||||
final T configuration = SystemMapper.yamlMapper().readValue(configurationYaml, configurationClass);
|
try {
|
||||||
|
configuration = SystemMapper.yamlMapper().readValue(configurationYaml, configurationClass);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
logger.warn("Failed to parse dynamic configuration", e);
|
||||||
|
Metrics.counter(ERROR_COUNTER_NAME,
|
||||||
|
ERROR_TYPE_TAG_NAME, "parse",
|
||||||
|
CONFIG_CLASS_TAG_NAME, configurationClass.getName()).increment();
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
final Set<ConstraintViolation<T>> violations = VALIDATOR.validate(configuration);
|
final Set<ConstraintViolation<T>> violations = VALIDATOR.validate(configuration);
|
||||||
|
|
||||||
final Optional<T> maybeDynamicConfiguration;
|
if (!violations.isEmpty()) {
|
||||||
|
|
||||||
if (violations.isEmpty()) {
|
|
||||||
maybeDynamicConfiguration = Optional.of(configuration);
|
|
||||||
} else {
|
|
||||||
logger.warn("Failed to validate configuration: {}", violations);
|
logger.warn("Failed to validate configuration: {}", violations);
|
||||||
maybeDynamicConfiguration = Optional.empty();
|
Metrics.counter(ERROR_COUNTER_NAME,
|
||||||
|
ERROR_TYPE_TAG_NAME, "validate",
|
||||||
|
CONFIG_CLASS_TAG_NAME, configurationClass.getName()).increment();
|
||||||
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return maybeDynamicConfiguration;
|
return Optional.of(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private T retrieveInitialDynamicConfiguration() {
|
|
||||||
for (;;) {
|
|
||||||
try {
|
|
||||||
if (configurationToken == null) {
|
|
||||||
// first time around, start the configuration session
|
|
||||||
final StartConfigurationSessionResponse startResponse = appConfigClient
|
|
||||||
.startConfigurationSession(StartConfigurationSessionRequest.builder()
|
|
||||||
.applicationIdentifier(application)
|
|
||||||
.environmentIdentifier(environment)
|
|
||||||
.configurationProfileIdentifier(configurationName).build());
|
|
||||||
configurationToken = startResponse.initialConfigurationToken();
|
|
||||||
}
|
|
||||||
return retrieveDynamicConfiguration().orElseThrow(() -> new IllegalStateException("No initial configuration available"));
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.warn("Error retrieving initial dynamic configuration", e);
|
|
||||||
Util.sleep(1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.workers;
|
package org.whispersystems.textsecuregcm.workers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import io.dropwizard.core.cli.Command;
|
import io.dropwizard.core.cli.Command;
|
||||||
import io.dropwizard.core.setup.Bootstrap;
|
import io.dropwizard.core.setup.Bootstrap;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -37,12 +36,7 @@ public class CheckDynamicConfigurationCommand extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValid(final Class<?> configurationClass, final String yamlConfig) {
|
private boolean isValid(final Class<?> configurationClass, final String yamlConfig) {
|
||||||
try {
|
|
||||||
return DynamicConfigurationManager.parseConfiguration(yamlConfig, configurationClass).isPresent();
|
return DynamicConfigurationManager.parseConfiguration(yamlConfig, configurationClass).isPresent();
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
System.err.println(e.getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -103,8 +103,9 @@ record CommandDependencies(
|
||||||
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
|
ScheduledExecutorService dynamicConfigurationExecutor = environment.lifecycle()
|
||||||
.scheduledExecutorService(name(name, "dynamicConfiguration-%d")).threads(1).build();
|
.scheduledExecutorService(name(name, "dynamicConfiguration-%d")).threads(1).build();
|
||||||
|
|
||||||
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager = configuration.getAppConfig().build(
|
DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
|
||||||
DynamicConfiguration.class, dynamicConfigurationExecutor, awsCredentialsProvider);
|
new DynamicConfigurationManager<>(
|
||||||
|
configuration.getDynamicConfig().build(awsCredentialsProvider, dynamicConfigurationExecutor), DynamicConfiguration.class);
|
||||||
dynamicConfigurationManager.start();
|
dynamicConfigurationManager.start();
|
||||||
|
|
||||||
final ClientResources.Builder redisClientResourcesBuilder = ClientResources.builder();
|
final ClientResources.Builder redisClientResourcesBuilder = ClientResources.builder();
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
org.whispersystems.textsecuregcm.configuration.AwsCredentialsProviderFactory
|
org.whispersystems.textsecuregcm.configuration.AwsCredentialsProviderFactory
|
||||||
org.whispersystems.textsecuregcm.configuration.DatadogConfiguration
|
org.whispersystems.textsecuregcm.configuration.DatadogConfiguration
|
||||||
org.whispersystems.textsecuregcm.configuration.DynamicConfigurationManagerFactory
|
|
||||||
org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory
|
org.whispersystems.textsecuregcm.configuration.DynamoDbClientFactory
|
||||||
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClusterFactory
|
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClusterFactory
|
||||||
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClientFactory
|
org.whispersystems.textsecuregcm.configuration.FaultTolerantRedisClientFactory
|
||||||
|
|
|
@ -1,108 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2024 Signal Messenger, LLC
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.configuration;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import javax.validation.constraints.NotBlank;
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
|
|
||||||
|
|
||||||
import io.dropwizard.util.Resources;
|
|
||||||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
|
|
||||||
|
|
||||||
@JsonTypeName("local")
|
|
||||||
public class LocalDynamicConfigurationManagerFactory implements DynamicConfigurationManagerFactory {
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String application;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String environment;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotEmpty
|
|
||||||
private String configuration;
|
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
@NotBlank
|
|
||||||
private String configPath;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> DynamicConfigurationManager<T> build(final Class<T> klazz,
|
|
||||||
final ScheduledExecutorService scheduledExecutorService, final AwsCredentialsProvider awsCredentialsProvider) {
|
|
||||||
|
|
||||||
return new LocalDynamicConfigurationManager<>(configPath, application, environment, configuration,
|
|
||||||
awsCredentialsProvider, klazz, scheduledExecutorService);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class LocalDynamicConfigurationManager<T> extends DynamicConfigurationManager<T> {
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DynamicConfigurationManager.class);
|
|
||||||
|
|
||||||
private final Path configPath;
|
|
||||||
private final Class<T> configurationClass;
|
|
||||||
private T cachedConfig;
|
|
||||||
private final Instant lastConfigLoadedTime;
|
|
||||||
|
|
||||||
public LocalDynamicConfigurationManager(final String configPath, final String application, final String environment,
|
|
||||||
final String configurationName, final AwsCredentialsProvider awsCredentialsProvider,
|
|
||||||
final Class<T> configurationClass, final ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
|
|
||||||
super(application, environment, configurationName, awsCredentialsProvider, configurationClass,
|
|
||||||
scheduledExecutorService);
|
|
||||||
|
|
||||||
this.configPath = Path.of(Resources.getResource("config").getPath()).resolve(configPath);
|
|
||||||
this.configurationClass = configurationClass;
|
|
||||||
this.cachedConfig = null;
|
|
||||||
this.lastConfigLoadedTime = null;
|
|
||||||
maybeUpdateConfig();
|
|
||||||
if (cachedConfig == null) {
|
|
||||||
throw new IllegalArgumentException("failed to load initial config");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getConfiguration() {
|
|
||||||
maybeUpdateConfig();
|
|
||||||
return cachedConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized void maybeUpdateConfig() {
|
|
||||||
try {
|
|
||||||
if (lastConfigLoadedTime != null &&
|
|
||||||
!lastConfigLoadedTime.isBefore(Files.readAttributes(configPath, BasicFileAttributes.class).lastModifiedTime().toInstant())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String configContents = Files.readString(configPath);
|
|
||||||
parseConfiguration(configContents, configurationClass).ifPresent(config -> cachedConfig = config);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.warn("Failed to update configuration", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,7 +18,7 @@ import java.util.function.Consumer;
|
||||||
public class StaticS3ObjectMonitorFactory implements S3ObjectMonitorFactory {
|
public class StaticS3ObjectMonitorFactory implements S3ObjectMonitorFactory {
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private byte[] object = new byte[0];
|
private String object = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public S3ObjectMonitor build(final AwsCredentialsProvider awsCredentialsProvider,
|
public S3ObjectMonitor build(final AwsCredentialsProvider awsCredentialsProvider,
|
||||||
|
@ -28,9 +28,9 @@ public class StaticS3ObjectMonitorFactory implements S3ObjectMonitorFactory {
|
||||||
|
|
||||||
private static class StaticS3ObjectMonitor extends S3ObjectMonitor {
|
private static class StaticS3ObjectMonitor extends S3ObjectMonitor {
|
||||||
|
|
||||||
private final byte[] object;
|
private final String object;
|
||||||
|
|
||||||
public StaticS3ObjectMonitor(final byte[] object, final AwsCredentialsProvider awsCredentialsProvider) {
|
public StaticS3ObjectMonitor(final String object, final AwsCredentialsProvider awsCredentialsProvider) {
|
||||||
super(awsCredentialsProvider, "local-test-region", "test-bucket", null, 0L, null, null);
|
super(awsCredentialsProvider, "local-test-region", "test-bucket", null, 0L, null, null);
|
||||||
|
|
||||||
this.object = object;
|
this.object = object;
|
||||||
|
@ -38,7 +38,7 @@ public class StaticS3ObjectMonitorFactory implements S3ObjectMonitorFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void start(final Consumer<InputStream> changeListener) {
|
public synchronized void start(final Consumer<InputStream> changeListener) {
|
||||||
changeListener.accept(new ByteArrayInputStream(object));
|
changeListener.accept(new ByteArrayInputStream(object.getBytes()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,75 +2,51 @@ package org.whispersystems.textsecuregcm.storage;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||||
|
import static org.mockito.Mockito.any;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.BrokenBarrierException;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import java.util.concurrent.CyclicBarrier;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.AdditionalAnswers;
|
||||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||||
import software.amazon.awssdk.core.SdkBytes;
|
import org.whispersystems.textsecuregcm.s3.S3ObjectMonitor;
|
||||||
import software.amazon.awssdk.services.appconfigdata.AppConfigDataClient;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
|
|
||||||
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionResponse;
|
|
||||||
|
|
||||||
class DynamicConfigurationManagerTest {
|
class DynamicConfigurationManagerTest {
|
||||||
|
|
||||||
private static final SdkBytes VALID_CONFIG = SdkBytes.fromUtf8String("""
|
private static final byte[] VALID_CONFIG = """
|
||||||
test: true
|
test: true
|
||||||
captcha:
|
captcha:
|
||||||
scoreFloor: 1.0
|
scoreFloor: 1.0
|
||||||
""");
|
""".getBytes();
|
||||||
|
private static final ExecutorService BACKGROUND_THREAD = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
private DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager;
|
private DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager;
|
||||||
private AppConfigDataClient appConfig;
|
private S3ObjectMonitor configMonitor;
|
||||||
private StartConfigurationSessionRequest startConfigurationSession;
|
|
||||||
private final ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
this.appConfig = mock(AppConfigDataClient.class);
|
this.configMonitor = mock(S3ObjectMonitor.class);
|
||||||
this.dynamicConfigurationManager = new DynamicConfigurationManager<>(
|
this.dynamicConfigurationManager = new DynamicConfigurationManager<>(configMonitor, DynamicConfiguration.class);
|
||||||
appConfig, "foo", "bar", "baz", DynamicConfiguration.class, scheduledExecutorService);
|
|
||||||
this.startConfigurationSession = StartConfigurationSessionRequest.builder()
|
|
||||||
.applicationIdentifier("foo")
|
|
||||||
.environmentIdentifier("bar")
|
|
||||||
.configurationProfileIdentifier("baz")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void teardown() {
|
|
||||||
scheduledExecutorService.shutdown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetInitialConfig() {
|
void testGetInitialConfig() {
|
||||||
when(appConfig.startConfigurationSession(startConfigurationSession))
|
// supply real config on start, then never send updates
|
||||||
.thenReturn(StartConfigurationSessionResponse.builder()
|
doAnswer(AdditionalAnswers.<Consumer<InputStream>>answerVoid(cb -> cb.accept(new ByteArrayInputStream(VALID_CONFIG))))
|
||||||
.initialConfigurationToken("initial")
|
.when(configMonitor).start(any());
|
||||||
.build());
|
|
||||||
|
|
||||||
// call with initial token will return a real config
|
assertTimeoutPreemptively(Duration.ofSeconds(1), () -> {
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder()
|
|
||||||
.configurationToken("initial").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(VALID_CONFIG)
|
|
||||||
.nextPollConfigurationToken("next").build());
|
|
||||||
|
|
||||||
// subsequent config calls will return empty (no update)
|
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
|
||||||
configurationToken("next").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(SdkBytes.fromUtf8String(""))
|
|
||||||
.nextPollConfigurationToken("next").build());
|
|
||||||
|
|
||||||
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
|
|
||||||
dynamicConfigurationManager.start();
|
dynamicConfigurationManager.start();
|
||||||
assertThat(dynamicConfigurationManager.getConfiguration()).isNotNull();
|
assertThat(dynamicConfigurationManager.getConfiguration()).isNotNull();
|
||||||
});
|
});
|
||||||
|
@ -78,33 +54,13 @@ class DynamicConfigurationManagerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBadConfig() {
|
void testBadConfig() {
|
||||||
when(appConfig.startConfigurationSession(startConfigurationSession))
|
// supply a bad config, then wait for the test to signal, then supply a good config
|
||||||
.thenReturn(StartConfigurationSessionResponse.builder()
|
doAnswer(AdditionalAnswers.<Consumer<InputStream>>answerVoid(cb -> {
|
||||||
.initialConfigurationToken("initial")
|
cb.accept(new ByteArrayInputStream("zzz".getBytes()));
|
||||||
.build());
|
BACKGROUND_THREAD.submit(() -> cb.accept(new ByteArrayInputStream(VALID_CONFIG)));
|
||||||
|
})).when(configMonitor).start(any());
|
||||||
|
|
||||||
// call with initial token will return a bad config
|
assertTimeoutPreemptively(Duration.ofSeconds(1), () -> {
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder()
|
|
||||||
.configurationToken("initial").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(SdkBytes.fromUtf8String("zzz"))
|
|
||||||
.nextPollConfigurationToken("goodconfig").build());
|
|
||||||
|
|
||||||
// next config call will return a good config
|
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
|
||||||
configurationToken("goodconfig").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(VALID_CONFIG)
|
|
||||||
.nextPollConfigurationToken("next").build());
|
|
||||||
|
|
||||||
// all subsequent config calls will return an empty config (no update)
|
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
|
||||||
configurationToken("next").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(SdkBytes.fromUtf8String(""))
|
|
||||||
.nextPollConfigurationToken("next").build());
|
|
||||||
|
|
||||||
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
|
|
||||||
dynamicConfigurationManager.start();
|
dynamicConfigurationManager.start();
|
||||||
assertThat(dynamicConfigurationManager.getConfiguration()).isNotNull();
|
assertThat(dynamicConfigurationManager.getConfiguration()).isNotNull();
|
||||||
});
|
});
|
||||||
|
@ -112,50 +68,41 @@ class DynamicConfigurationManagerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetConfigMultiple() {
|
void testGetConfigMultiple() {
|
||||||
when(appConfig.startConfigurationSession(startConfigurationSession))
|
final CyclicBarrier barrier = new CyclicBarrier(2);
|
||||||
.thenReturn(StartConfigurationSessionResponse.builder()
|
// supply an initial config, wait for the test to signal, then supply a distinct good config
|
||||||
.initialConfigurationToken("0")
|
doAnswer(AdditionalAnswers.<Consumer<InputStream>>answerVoid(cb -> {
|
||||||
.build());
|
cb.accept(new ByteArrayInputStream(VALID_CONFIG));
|
||||||
|
BACKGROUND_THREAD.submit(() -> {
|
||||||
// initial config
|
try {
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
barrier.await(); // wait for initial config to be consumed
|
||||||
configurationToken("0").build()))
|
cb.accept(
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
new ByteArrayInputStream("""
|
||||||
.configuration(VALID_CONFIG)
|
|
||||||
.nextPollConfigurationToken("1").build());
|
|
||||||
|
|
||||||
// config update with a real config
|
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
|
||||||
configurationToken("1").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(SdkBytes.fromUtf8String("""
|
|
||||||
experiments:
|
experiments:
|
||||||
test:
|
test:
|
||||||
enrollmentPercentage: 50
|
enrollmentPercentage: 50
|
||||||
captcha:
|
captcha:
|
||||||
scoreFloor: 1.0
|
scoreFloor: 1.0
|
||||||
"""))
|
""".getBytes()));
|
||||||
.nextPollConfigurationToken("2").build());
|
barrier.await(); // signal availability of new config
|
||||||
|
} catch (InterruptedException | BrokenBarrierException e) {}
|
||||||
// all subsequent are no update
|
});
|
||||||
when(appConfig.getLatestConfiguration(GetLatestConfigurationRequest.builder().
|
})).when(configMonitor).start(any());
|
||||||
configurationToken("2").build()))
|
|
||||||
.thenReturn(GetLatestConfigurationResponse.builder()
|
|
||||||
.configuration(SdkBytes.fromUtf8String(""))
|
|
||||||
.nextPollConfigurationToken("2").build());
|
|
||||||
|
|
||||||
// the internal waiting done by dynamic configuration manager catches the InterruptedException used
|
// the internal waiting done by dynamic configuration manager catches the InterruptedException used
|
||||||
// by JUnit’s @Timeout, so we use assertTimeoutPreemptively
|
// by JUnit’s @Timeout, so we use assertTimeoutPreemptively
|
||||||
assertTimeoutPreemptively(Duration.ofSeconds(5), () -> {
|
assertTimeoutPreemptively(Duration.ofSeconds(1), () -> {
|
||||||
// we should eventually get the updated config (or the test will timeout)
|
|
||||||
dynamicConfigurationManager.start();
|
dynamicConfigurationManager.start();
|
||||||
while (dynamicConfigurationManager.getConfiguration().getExperimentEnrollmentConfiguration("test").isEmpty()) {
|
DynamicConfiguration config = dynamicConfigurationManager.getConfiguration();
|
||||||
Thread.sleep(100);
|
assertThat(config).isNotNull();
|
||||||
}
|
assertThat(config.getExperimentEnrollmentConfiguration("test")).isEmpty();
|
||||||
assertThat(
|
barrier.await(); // signal consumption of initial config
|
||||||
dynamicConfigurationManager.getConfiguration().getExperimentEnrollmentConfiguration("test").get()
|
barrier.await(); // wait for availability of new config
|
||||||
.getEnrollmentPercentage()).isEqualTo(50);
|
config = dynamicConfigurationManager.getConfiguration();
|
||||||
|
assertThat(config).isNotNull();
|
||||||
|
assertThat(config.getExperimentEnrollmentConfiguration("test")).isNotEmpty();
|
||||||
|
assertThat(config.getExperimentEnrollmentConfiguration("test").get().getEnrollmentPercentage())
|
||||||
|
.isEqualTo(50);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
org.whispersystems.textsecuregcm.configuration.LocalDynamicConfigurationManagerFactory
|
|
|
@ -1,2 +0,0 @@
|
||||||
captcha:
|
|
||||||
scoreFloor: 1.0
|
|
|
@ -321,12 +321,11 @@ callingZkConfig:
|
||||||
backupsZkConfig:
|
backupsZkConfig:
|
||||||
serverSecret: secret://backupsZkConfig.serverSecret
|
serverSecret: secret://backupsZkConfig.serverSecret
|
||||||
|
|
||||||
appConfig:
|
dynamicConfig:
|
||||||
type: local
|
type: static
|
||||||
application: test
|
object: |
|
||||||
environment: test
|
captcha:
|
||||||
configuration: test
|
scoreFloor: 1.0
|
||||||
configPath: test-dynamic.yml
|
|
||||||
|
|
||||||
remoteConfig:
|
remoteConfig:
|
||||||
globalConfig: # keys and values that are given to clients on GET /v1/config
|
globalConfig: # keys and values that are given to clients on GET /v1/config
|
||||||
|
|
Loading…
Reference in New Issue