Skip to content

Commit d58eb09

Browse files
committed
find participants on old sdks
1 parent 645ba1c commit d58eb09

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed
Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
package com.uid2.operator.vertx;
22

3+
import com.uid2.operator.util.Tuple;
34
import com.uid2.shared.Const;
5+
import com.uid2.shared.auth.IAuthorizable;
6+
import com.uid2.shared.auth.IAuthorizableProvider;
7+
import com.uid2.shared.middleware.AuthMiddleware;
48
import io.micrometer.core.instrument.Counter;
59
import io.micrometer.core.instrument.Metrics;
610
import io.vertx.core.Handler;
711
import io.vertx.ext.web.RoutingContext;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
814

915
import java.io.IOException;
1016
import java.nio.file.DirectoryStream;
1117
import java.nio.file.Files;
1218
import java.nio.file.Path;
1319
import java.nio.file.Paths;
1420
import java.util.HashMap;
21+
import java.util.HashSet;
1522
import java.util.Map;
23+
import java.util.Set;
1624

1725
public class ClientVersionCapturingHandler implements Handler<RoutingContext> {
18-
private final Map<String, Counter> _clientVersionCounters = new HashMap<>();
26+
private static final Logger LOGGER = LoggerFactory.getLogger(ClientVersionCapturingHandler.class);
27+
private static final String BEARER_TOKEN_PREFIX = "bearer ";
28+
private final Map<Tuple.Tuple2<String, String>, Counter> _clientVersionCounters = new HashMap<>();
29+
private IAuthorizableProvider authKeyStore;
30+
private final Set<String> versions = new HashSet<>();
1931

20-
public ClientVersionCapturingHandler(String dir, String whitelistGlob) throws IOException {
32+
public ClientVersionCapturingHandler(String dir, String whitelistGlob, IAuthorizableProvider authKeyStore) throws IOException {
33+
this.authKeyStore = authKeyStore;
2134
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(Paths.get(dir), whitelistGlob)) {
2235
dirStream.forEach(path -> {
2336
final String version = getFileNameWithoutExtension(path);
24-
final Counter counter = Counter
25-
.builder("uid2.client_sdk_versions")
26-
.description("counter for how many http requests are processed per each client sdk version")
27-
.tags("client_version", version)
28-
.register(Metrics.globalRegistry);
29-
_clientVersionCounters.put(version, counter);
37+
versions.add(version);
3038
});
3139
}
3240
}
@@ -36,11 +44,22 @@ public void handle(RoutingContext context) {
3644
if (clientVersion == null) {
3745
clientVersion = !context.queryParam("client").isEmpty() ? context.queryParam("client").get(0) : null;
3846
}
39-
if (clientVersion != null) {
40-
final Counter counter = _clientVersionCounters.get(clientVersion);
41-
if (counter != null) {
42-
counter.increment();
43-
}
47+
String apiContact;
48+
try {
49+
final String authHeaderValue = context.request().getHeader("Authorization");
50+
final String authKey = extractBearerToken(authHeaderValue);
51+
final IAuthorizable profile = this.authKeyStore.get(authKey);
52+
apiContact = profile.getContact();
53+
apiContact = apiContact == null ? "unknown" : apiContact;
54+
} catch (Exception ex) {
55+
apiContact = "unknown";
56+
}
57+
if (clientVersion != null && versions.contains(clientVersion)) {
58+
_clientVersionCounters.computeIfAbsent(new Tuple.Tuple2<>(apiContact, clientVersion), tuple -> Counter
59+
.builder("uid2.client_sdk_versions")
60+
.description("counter for how many http requests are processed per each client sdk version")
61+
.tags("api_contact", tuple.getItem1(), "client_version", tuple.getItem2())
62+
.register(Metrics.globalRegistry)).increment();;
4463
}
4564
context.next();
4665
}
@@ -49,4 +68,22 @@ private static String getFileNameWithoutExtension(Path path) {
4968
final String fileName = path.getFileName().toString();
5069
return fileName.indexOf(".") > 0 ? fileName.substring(0, fileName.lastIndexOf(".")) : fileName;
5170
}
71+
72+
private static String extractBearerToken(final String headerValue) {
73+
if (headerValue == null) {
74+
return null;
75+
}
76+
77+
final String v = headerValue.trim();
78+
if (v.length() < BEARER_TOKEN_PREFIX.length()) {
79+
return null;
80+
}
81+
82+
final String givenPrefix = v.substring(0, BEARER_TOKEN_PREFIX.length());
83+
84+
if (!BEARER_TOKEN_PREFIX.equals(givenPrefix.toLowerCase())) {
85+
return null;
86+
}
87+
return v.substring(BEARER_TOKEN_PREFIX.length());
88+
}
5289
}

src/main/java/com/uid2/operator/vertx/UIDOperatorVerticle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private Router createRoutesSetup() throws IOException {
221221

222222
router.allowForward(AllowForwardHeaders.X_FORWARD);
223223
router.route().handler(new RequestCapturingHandler());
224-
router.route().handler(new ClientVersionCapturingHandler("static/js", "*.js"));
224+
router.route().handler(new ClientVersionCapturingHandler("static/js", "*.js", clientKeyProvider));
225225
router.route().handler(CorsHandler.create()
226226
.addRelativeOrigin(".*.")
227227
.allowedMethod(io.vertx.core.http.HttpMethod.GET)

0 commit comments

Comments
 (0)