Skip to content

Commit

Permalink
get valid paths from route definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Nara committed Jul 18, 2024
1 parent f94574e commit 988ea51
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/main/java/com/uid2/operator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Main {

private final JsonObject config;
private final Vertx vertx;
private final Set<String> validPaths;
private final ApplicationVersion appVersion;
private final ICloudStorage fsLocal;
private final ICloudStorage fsOptOut;
Expand Down Expand Up @@ -180,6 +181,9 @@ public Main(Vertx vertx, JsonObject config) throws Exception {
}
}
metrics = new OperatorMetrics(getKeyManager(), saltProvider);

UIDOperatorVerticle verticle = new UIDOperatorVerticle(config, this.clientSideTokenGenerate, siteProvider, clientKeyProvider, clientSideKeypairProvider, getKeyManager(), saltProvider, optOutStore, Clock.systemUTC(), _statsCollectorQueue, new SecureLinkValidatorService(this.serviceLinkProvider, this.serviceProvider), this.shutdownHandler::handleSaltRetrievalResponse, true);
this.validPaths = verticle.getVerticleRoutes();
}

private KeyManager getKeyManager() {
Expand Down Expand Up @@ -264,6 +268,8 @@ private ICloudStorage wrapCloudStorageForOptOut(ICloudStorage cloudStorage) {
}

private void run() throws Exception {
setupMetrics(this.validPaths);

Supplier<Verticle> operatorVerticleSupplier = () -> {
UIDOperatorVerticle verticle = new UIDOperatorVerticle(config, this.clientSideTokenGenerate, siteProvider, clientKeyProvider, clientSideKeypairProvider, getKeyManager(), saltProvider, optOutStore, Clock.systemUTC(), _statsCollectorQueue, new SecureLinkValidatorService(this.serviceLinkProvider, this.serviceProvider), this.shutdownHandler::handleSaltRetrievalResponse);
return verticle;
Expand Down Expand Up @@ -364,7 +370,7 @@ private Future<String> createAndDeployCloudSyncStoreVerticle(String name, ICloud

private Future<String> createAndDeployStatsCollector() {
Promise<String> promise = Promise.promise();
StatsCollectorVerticle statsCollectorVerticle = new StatsCollectorVerticle(60000, config.getInteger(Const.Config.MaxInvalidPaths, 50));
StatsCollectorVerticle statsCollectorVerticle = new StatsCollectorVerticle(60000, config.getInteger(Const.Config.MaxInvalidPaths, 50), this.validPaths);
vertx.deployVerticle(statsCollectorVerticle, promise);
_statsCollectorQueue = statsCollectorVerticle;
return promise.future();
Expand Down Expand Up @@ -399,7 +405,7 @@ private static Vertx createVertx() {
.setLabels(EnumSet.of(Label.HTTP_METHOD, Label.HTTP_CODE, Label.HTTP_PATH))
.setJvmMetricsEnabled(true)
.setEnabled(true);
setupMetrics(metricOptions);
BackendRegistries.setupBackend(metricOptions);

final int threadBlockedCheckInterval = Utils.isProductionEnvironment()
? 60 * 1000
Expand All @@ -412,9 +418,7 @@ private static Vertx createVertx() {
return Vertx.vertx(vertxOptions);
}

private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
BackendRegistries.setupBackend(metricOptions);

private static void setupMetrics(Set<String> validPaths) {
MeterRegistry backendRegistry = BackendRegistries.getDefaultNow();
if (backendRegistry instanceof PrometheusMeterRegistry) {
// prometheus specific configuration
Expand All @@ -427,7 +431,7 @@ private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
.meterFilter(MeterFilter.replaceTagValues(Label.HTTP_PATH.toString(), actualPath -> {
try {
String normalized = HttpUtils.normalizePath(actualPath).split("\\?")[0];
return Endpoints.pathSet().contains(normalized) ? normalized : "/unknown";
return validPaths.contains(normalized) ? normalized : "/unknown";
} catch (IllegalArgumentException e) {
return actualPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public class StatsCollectorVerticle extends AbstractVerticle implements IStatsCollectorQueue {
private static final Logger LOGGER = LoggerFactory.getLogger(StatsCollectorVerticle.class);
private final Set<String> validPaths;
private HashMap<String, EndpointStat> pathMap;

private static final int MAX_AVAILABLE = 1000;
Expand All @@ -41,7 +42,8 @@ public class StatsCollectorVerticle extends AbstractVerticle implements IStatsCo
private final ObjectMapper mapper;
private final Counter queueFullCounter;

public StatsCollectorVerticle(long jsonIntervalMS, int maxInvalidPaths) {
public StatsCollectorVerticle(long jsonIntervalMS, int maxInvalidPaths, Set<String> validPaths) {
this.validPaths = validPaths;
pathMap = new HashMap<>();

_statsCollectorCount = new AtomicInteger();
Expand Down Expand Up @@ -116,8 +118,7 @@ public void handleMessage(Message message) {

EndpointStat endpointStat = new EndpointStat(endpoint, siteId, apiVersion, domain);

Set<String> endpoints = Endpoints.pathSet();
if(endpoints.contains(path) || pathMap.containsKey(path) || (pathMap.size() < this.maxInvalidPaths + endpoints.size() && messageItem.getApiContact() != null)) {
if(validPaths.contains(path) || pathMap.containsKey(path) || (pathMap.size() < this.maxInvalidPaths + validPaths.size() && messageItem.getApiContact() != null)) {
pathMap.merge(path, endpointStat, this::mergeEndpoint);
}

Expand All @@ -130,7 +131,7 @@ public void handleMessage(Message message) {
logCycleSkipperCounter.increment();
} else {
_runningSerializer = true;
if(pathMap.size() == this.maxInvalidPaths + endpoints.size()) {
if(pathMap.size() == this.maxInvalidPaths + validPaths.size()) {
LOGGER.error("max invalid paths reached; a large number of invalid paths have been requested from authenticated participants");
}
Object[] stats = pathMap.values().toArray();
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/uid2/operator/vertx/UIDOperatorVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.AllowForwardHeaders;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
Expand Down Expand Up @@ -130,6 +131,8 @@ public class UIDOperatorVerticle extends AbstractVerticle {
private final static List<String> SUPPORTED_IN_APP = Arrays.asList("Android", "ios", "tvos");
public final static String ORIGIN_HEADER = "Origin";

private final Set<String> validPaths;

public UIDOperatorVerticle(JsonObject config,
boolean clientSideTokenGenerate,
ISiteStore siteProvider,
Expand All @@ -142,6 +145,22 @@ public UIDOperatorVerticle(JsonObject config,
IStatsCollectorQueue statsCollectorQueue,
SecureLinkValidatorService secureLinkValidatorService,
Handler<Boolean> saltRetrievalResponseHandler) {
this(config, clientSideTokenGenerate, siteProvider, clientKeyProvider, clientSideKeypairProvider, keyManager, saltProvider, optOutStore, clock, statsCollectorQueue, secureLinkValidatorService, saltRetrievalResponseHandler, false);
}

public UIDOperatorVerticle(JsonObject config,
boolean clientSideTokenGenerate,
ISiteStore siteProvider,
IClientKeyProvider clientKeyProvider,
IClientSideKeypairStore clientSideKeypairProvider,
KeyManager keyManager,
ISaltProvider saltProvider,
IOptOutStore optOutStore,
Clock clock,
IStatsCollectorQueue statsCollectorQueue,
SecureLinkValidatorService secureLinkValidatorService,
Handler<Boolean> saltRetrievalResponseHandler,
boolean setHealthy) {
this.keyManager = keyManager;
this.secureLinkValidatorService = secureLinkValidatorService;
try {
Expand All @@ -151,7 +170,11 @@ public UIDOperatorVerticle(JsonObject config,
}
this.config = config;
this.clientSideTokenGenerate = clientSideTokenGenerate;
this.healthComponent.setHealthStatus(false, "not started");
if(setHealthy) {
this.healthComponent.setHealthStatus(true);
} else {
this.healthComponent.setHealthStatus(false, "not started");
}
this.auth = new AuthMiddleware(clientKeyProvider);
this.encoder = new EncryptedTokenEncoder(keyManager);
this.siteProvider = siteProvider;
Expand Down Expand Up @@ -179,6 +202,11 @@ public UIDOperatorVerticle(JsonObject config,
this.saltRetrievalResponseHandler = saltRetrievalResponseHandler;
this.optOutStatusApiEnabled = config.getBoolean(Const.Config.OptOutStatusApiEnabled, false);
this.optOutStatusMaxRequestSize = config.getInteger(Const.Config.OptOutStatusMaxRequestSize, 5000);
try {
this.validPaths = createRoutesSetup().getRoutes().stream().map(Route::getPath).collect(Collectors.toSet());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down Expand Up @@ -2104,4 +2132,8 @@ public enum UserConsentStatus {
INSUFFICIENT,
INVALID,
}

public Set<String> getVerticleRoutes() {
return this.validPaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class StatsCollectorVerticleTest {
private StatsCollectorVerticle verticle;

@BeforeEach
void deployVerticle(Vertx vertx, VertxTestContext testContext) throws Throwable {
verticle = new StatsCollectorVerticle(1000, MAX_INVALID_PATHS);
void deployVerticle(Vertx vertx, VertxTestContext testContext) {
verticle = new StatsCollectorVerticle(1000, MAX_INVALID_PATHS, Endpoints.pathSet());
vertx.deployVerticle(verticle, testContext.succeeding(id -> testContext.completeNow()));
}

Expand Down

0 comments on commit 988ea51

Please sign in to comment.