Skip to content

Commit d0e0c56

Browse files
Ensure cached subscriptions are cleared on reconfiguration via RC
1 parent ae1aa30 commit d0e0c56

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/AppSecSystem.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class AppSecSystem {
3939
private static final Map<AppSecModule, String> STARTED_MODULES_INFO = new HashMap<>();
4040
private static AppSecConfigServiceImpl APP_SEC_CONFIG_SERVICE;
4141
private static ReplaceableEventProducerService REPLACEABLE_EVENT_PRODUCER; // testing
42+
private static Runnable STOP_SUBSCRIPTION_SERVICE;
4243
private static Runnable RESET_SUBSCRIPTION_SERVICE;
4344

4445
public static void start(SubscriptionService gw, SharedCommunicationObjects sco) {
@@ -90,7 +91,8 @@ private static void doStart(SubscriptionService gw, SharedCommunicationObjects s
9091
loadModules(eventDispatcher, sco.monitoring);
9192

9293
gatewayBridge.init();
93-
RESET_SUBSCRIPTION_SERVICE = gatewayBridge::stop;
94+
STOP_SUBSCRIPTION_SERVICE = gatewayBridge::stop;
95+
RESET_SUBSCRIPTION_SERVICE = gatewayBridge::reset;
9496

9597
setActive(appSecEnabledConfig == ProductActivation.FULLY_ENABLED);
9698

@@ -127,7 +129,8 @@ public static void stop() {
127129
return;
128130
}
129131
REPLACEABLE_EVENT_PRODUCER = null;
130-
RESET_SUBSCRIPTION_SERVICE.run();
132+
STOP_SUBSCRIPTION_SERVICE.run();
133+
STOP_SUBSCRIPTION_SERVICE = null;
131134
RESET_SUBSCRIPTION_SERVICE = null;
132135
Blocking.setBlockingService(BlockingService.NOOP);
133136

@@ -176,6 +179,10 @@ private static void reloadSubscriptions(
176179
newEd.subscribeDataAvailable(dataSubscriptionSet);
177180

178181
replaceableEventProducerService.replaceEventProducerService(newEd);
182+
183+
if (RESET_SUBSCRIPTION_SERVICE != null) {
184+
RESET_SUBSCRIPTION_SERVICE.run();
185+
}
179186
}
180187

181188
public static boolean isStarted() {

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ public void init() {
173173
}
174174
}
175175

176+
/**
177+
* This method clears all the cached subscriptions, should be used everytime the configuration
178+
* changes and new addresses might appear or disappear from the config.
179+
*/
180+
public void reset() {
181+
initialReqDataSubInfo = null;
182+
rawRequestBodySubInfo = null;
183+
requestBodySubInfo = null;
184+
pathParamsSubInfo = null;
185+
respDataSubInfo = null;
186+
grpcServerMethodSubInfo = null;
187+
grpcServerRequestMsgSubInfo = null;
188+
graphqlServerRequestMsgSubInfo = null;
189+
requestEndSubInfo = null;
190+
dbSqlQuerySubInfo = null;
191+
ioNetUrlSubInfo = null;
192+
ioFileSubInfo = null;
193+
sessionIdSubInfo = null;
194+
userIdSubInfo = null;
195+
loginEventSubInfo.clear();
196+
execCmdSubInfo = null;
197+
shellCmdSubInfo = null;
198+
}
199+
176200
private Flow<Void> onUser(
177201
final RequestContext ctx_, final UserIdCollectionMode mode, final String originalUser) {
178202
if (mode == DISABLED) {

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/gateway/GatewayBridgeSpecification.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class GatewayBridgeSpecification extends DDSpecification {
7373
i
7474
}()
7575

76+
EventProducerService.DataSubscriberInfo emptyDsInfo = Stub() {
77+
isEmpty() >> true
78+
}
79+
7680
TraceSegmentPostProcessor pp = Mock()
7781
GatewayBridge bridge = new GatewayBridge(ig, eventDispatcher, null, [pp])
7882

@@ -1280,4 +1284,28 @@ class GatewayBridgeSpecification extends DDSpecification {
12801284
12811285
0 * eventDispatcher.publishDataEvent
12821286
}
1287+
1288+
void 'test configuration updates should reset cached subscriptions'() {
1289+
when:
1290+
requestSessionCB.apply(ctx, UUID.randomUUID().toString())
1291+
1292+
then:
1293+
1 * eventDispatcher.getDataSubscribers(KnownAddresses.SESSION_ID) >> emptyDsInfo
1294+
0 * eventDispatcher.publishDataEvent
1295+
1296+
when:
1297+
requestSessionCB.apply(ctx, UUID.randomUUID().toString())
1298+
1299+
then:
1300+
0 * eventDispatcher.getDataSubscribers
1301+
0 * eventDispatcher.publishDataEvent
1302+
1303+
when:
1304+
bridge.reset()
1305+
requestSessionCB.apply(ctx, UUID.randomUUID().toString())
1306+
1307+
then:
1308+
1 * eventDispatcher.getDataSubscribers(KnownAddresses.SESSION_ID) >> nonEmptyDsInfo
1309+
1 * eventDispatcher.publishDataEvent(_, _, _, _)
1310+
}
12831311
}

0 commit comments

Comments
 (0)