Skip to content

Commit

Permalink
AppSecConfig: fix new config not being saved
Browse files Browse the repository at this point in the history
  • Loading branch information
cataphract committed Jul 27, 2021
1 parent 8c46f04 commit 6a444a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ private class AgentConfigPollingRunnable implements Runnable {
private int consecutiveFailures;
private OkHttpClient okHttpClient;
private HttpUrl httpUrl;
private MessageDigest digest;

AgentConfigPollingRunnable() {
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new UndeclaredThrowableException(e);
}
}

@Override
public void run() {
Expand Down Expand Up @@ -136,13 +145,8 @@ private boolean fetchConfig(FleetSubscriptionImpl sub) {
log.warn("IOException when reading fleet service response");
return false;
}
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new UndeclaredThrowableException(e);
}

digest.reset();
byte[] hash = digest.digest(body);
if (Arrays.equals(hash, sub.lastHash)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private void subscribeFleetService(FleetService fleetService) {
Map<String, Object> stringObjectMap =
ADAPTER.fromJson(Okio.buffer(Okio.source(is)));
distributeSubConfigurations(stringObjectMap);
this.lastConfig.set(stringObjectMap);
} catch (IOException e) {
log.error("Error deserializing appsec config", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,35 @@ class AppSecConfigServiceImplSpecification extends Specification {
appSecConfigService.addSubConfigListener("waf2", listener) == Optional.empty()
}

void 'provides update configuration to subscription'() {
void 'provides updated configuration to subscription'() {
AppSecConfigService.SubconfigListener subconfigListener = Mock()
FleetService.ConfigurationListener savedConfigurationListener
def initialWafConfig

when:
appSecConfigService.addSubConfigListener("waf", subconfigListener)
initialWafConfig = appSecConfigService.addSubConfigListener("waf", subconfigListener)
appSecConfigService.init()

then:
1 * fleetService.subscribe(FleetService.Product.APPSEC, _) >> {
savedConfigurationListener = it[1]
Mock(FleetService.FleetSubscription)
}
initialWafConfig.get() != null

when:
savedConfigurationListener.onNewConfiguration(new ByteArrayInputStream('{"waf": "my config"}'.bytes))
savedConfigurationListener.onNewConfiguration(
new ByteArrayInputStream(
'{"waf": "my config", "foo": "another config"}'.bytes))

then:
1 * subconfigListener.onNewSubconfig('my config')

when:
def fooInitialConfig = appSecConfigService.addSubConfigListener('foo', Mock(AppSecConfigService.SubconfigListener))

then:
fooInitialConfig.get() == 'another config'
}

void 'error in one listener does not prevent others from running'() {
Expand Down
4 changes: 1 addition & 3 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ final class CachedData {
// transitive dependency. Since okhttp is declared here and moshi is not, this lead to an incompatible version
"com.squareup.okio:okio:${versions.okio}",
"com.datadoghq:java-dogstatsd-client:${versions.dogstatsd}",
"com.github.jnr:jnr-unixsocket:${versions.jnr_unixsocket}"
// TODO: not adding new stuff here because it results in redundant dependencies
// moshi, :communication and :utils:container-utils are also part of shared jar
"com.github.jnr:jnr-unixsocket:${versions.jnr_unixsocket}",
],

// Inverse of "shared". These exclude directives are part of shadowJar's DSL
Expand Down

0 comments on commit 6a444a9

Please sign in to comment.