Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unbounded path label #132

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/java/com/uid2/optout/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.uid2.optout;

import com.uid2.optout.vertx.OptOutLogProducer;
import com.uid2.optout.vertx.OptOutServiceVerticle;
import com.uid2.optout.vertx.PartnerConfigMonitor;
import com.uid2.optout.vertx.PartnerConfigMonitorV2;
import com.uid2.optout.vertx.*;
import com.uid2.shared.ApplicationVersion;
import com.uid2.shared.Utils;
import com.uid2.shared.attest.AttestationResponseHandler;
Expand Down Expand Up @@ -223,7 +220,8 @@ private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
.meterFilter(new PrometheusRenameFilter())
.meterFilter(MeterFilter.replaceTagValues(Label.HTTP_PATH.toString(), actualPath -> {
try {
return HttpUtils.normalizePath(actualPath).split("\\?")[0];
String normalized = HttpUtils.normalizePath(actualPath).split("\\?")[0];
return Endpoints.pathSet().contains(normalized) ? normalized : "/unknown";
} catch (IllegalArgumentException e) {
return actualPath;
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/uid2/optout/vertx/Endpoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.uid2.optout.vertx;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum Endpoints {
OPS_HEALTHCHECK("/ops/healthcheck"),
OPTOUT_REFRESH("/optout/refresh"),
OPTOUT_WRITE("/optout/write"),
OPTOUT_REPLICATE("/optout/replicate"),
OPTOUT_PARTNER_MOCK("/optout/partner_mock");
private final String path;

Endpoints(final String path) {
this.path = path;
}

public static Set<String> pathSet() {
return Stream.of(Endpoints.values()).map(Endpoints::toString).collect(Collectors.toSet());
}

@Override
public String toString() {
return path;
}
}
17 changes: 7 additions & 10 deletions src/main/java/com/uid2/optout/vertx/OptOutServiceVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import static com.uid2.optout.vertx.Endpoints.*;

public class OptOutServiceVerticle extends AbstractVerticle {
public static final String IDENTITY_HASH = "identity_hash";
public static final String ADVERTISING_ID = "advertising_id";
public static final String REFRESH_METHOD = "/optout/refresh";
public static final String WRITE_METHOD = "/optout/write";
public static final String REPLICATE_METHOD = "/optout/replicate";
public static final String HEALTHCHECK_METHOD = "/ops/healthcheck";
public static final String OPTOUT_PARTNER_MOCK_METHOD = "/optout/partner_mock";

private static final Logger LOGGER = LoggerFactory.getLogger(OptOutServiceVerticle.class);
private final HealthComponent healthComponent = HealthManager.instance.registerComponent("http-server");
Expand Down Expand Up @@ -168,18 +165,18 @@ private Router createRouter() {
.allowedHeader("Access-Control-Allow-Headers")
.allowedHeader("Content-Type"));

router.route(WRITE_METHOD)
router.route(Endpoints.OPTOUT_WRITE.toString())
.handler(internalAuth.handle(this::handleWrite));
router.route(REPLICATE_METHOD)
router.route(Endpoints.OPTOUT_REPLICATE.toString())
.handler(auth.handle(this::handleReplicate, Role.OPTOUT));
router.route(REFRESH_METHOD)
router.route(Endpoints.OPTOUT_REFRESH.toString())
.handler(auth.handle(attest.handle(this::handleRefresh, Role.OPERATOR), Role.OPERATOR));
router.get(HEALTHCHECK_METHOD)
router.get(Endpoints.OPS_HEALTHCHECK.toString())
.handler(this::handleHealthCheck);

if (this.enableOptOutPartnerMock) {
final OperatorKey loopbackClient = new OperatorKey("", "", "loopback", "loopback", "loopback", 0, false, "");
router.route(OPTOUT_PARTNER_MOCK_METHOD).handler(auth.loopbackOnly(this::handleOptOutPartnerMock, loopbackClient));
router.route(Endpoints.OPTOUT_PARTNER_MOCK.toString()).handler(auth.loopbackOnly(this::handleOptOutPartnerMock, loopbackClient));
}

//// if enabled, this would add handler for exposing prometheus metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.ArrayList;
import java.util.List;

import static com.uid2.optout.vertx.Endpoints.OPTOUT_WRITE;

@RunWith(VertxUnitRunner.class)
public class OptOutServiceVerticleTest {
private static final String INTERNAL_TEST_KEY = "test-operator-key";
Expand Down Expand Up @@ -92,7 +94,7 @@ public void writeId_expect200(TestContext context) {

@Test
public void getHealthCheck_expectOK(TestContext context) {
verifyStatus(context, OptOutServiceVerticle.HEALTHCHECK_METHOD, 200);
verifyStatus(context, Endpoints.OPS_HEALTHCHECK.toString(), 200);
}

@Test
Expand Down Expand Up @@ -139,7 +141,7 @@ public void replicate_expect200(TestContext context) {
public void testQuorumClient_expectSuccess(TestContext context) {
String[] uris = new String[3];
for (int i = 0; i < 3; ++i) {
uris[i] = String.format("http://127.0.0.1:%d%s", Const.Port.ServicePortForOptOut, OptOutServiceVerticle.WRITE_METHOD);
uris[i] = String.format("http://127.0.0.1:%d%s", Const.Port.ServicePortForOptOut, Endpoints.OPTOUT_WRITE);
}

QuorumWebClient quorumClient = new QuorumWebClient(vertx, uris);
Expand All @@ -155,7 +157,7 @@ public void testQuorumClient_expectSuccess(TestContext context) {
public void testQuorumClient1Failure_expectSuccess(TestContext context) {
String[] uris = new String[3];
for (int i = 0; i < 2; ++i) {
uris[i] = String.format("http://127.0.0.1:%d%s", Const.Port.ServicePortForOptOut, OptOutServiceVerticle.WRITE_METHOD);
uris[i] = String.format("http://127.0.0.1:%d%s", Const.Port.ServicePortForOptOut, Endpoints.OPTOUT_WRITE);
}
uris[2] = "http://httpstat.us/404";

Expand Down Expand Up @@ -201,7 +203,7 @@ private String writeQuery(byte[] identityHash, byte[] advertisingId) {
}

private String writeQuery(String identityHashB64, String advertisingIdB64) {
return String.format("%s?%s=%s&%s=%s", OptOutServiceVerticle.WRITE_METHOD,
return String.format("%s?%s=%s&%s=%s", Endpoints.OPTOUT_WRITE,
OptOutServiceVerticle.IDENTITY_HASH,
identityHashB64,
OptOutServiceVerticle.ADVERTISING_ID,
Expand All @@ -214,7 +216,7 @@ private String replicateQuery(long id) {
}

private String replicateQuery(String identityHashB64, String advertisingIdB64) {
return String.format("%s?%s=%s&%s=%s", OptOutServiceVerticle.REPLICATE_METHOD,
return String.format("%s?%s=%s&%s=%s", Endpoints.OPTOUT_REPLICATE,
OptOutServiceVerticle.IDENTITY_HASH,
identityHashB64,
OptOutServiceVerticle.ADVERTISING_ID,
Expand Down