Skip to content

Commit a7e85b6

Browse files
Merge pull request #313 from IABTechLab/sch-UID2-2555-role-validation-for-service-links-in-operator
sch-UID2-2555-role-validation-for-service-links-in-operator
2 parents 792bebb + cb12ec3 commit a7e85b6

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/main/java/com/uid2/operator/service/SecureLinkValidatorService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.uid2.shared.auth.ClientKey;
44
import com.uid2.shared.auth.IAuthorizable;
5+
import com.uid2.shared.auth.Role;
56
import com.uid2.shared.middleware.AuthMiddleware;
67
import com.uid2.shared.model.Service;
78
import com.uid2.shared.model.ServiceLink;
@@ -25,7 +26,7 @@ public SecureLinkValidatorService(RotatingServiceLinkStore rotatingServiceLinkSt
2526
this.rotatingServiceStore = rotatingServiceStore;
2627
}
2728

28-
public boolean validateRequest(RoutingContext rc, JsonObject requestJsonObject) {
29+
public boolean validateRequest(RoutingContext rc, JsonObject requestJsonObject, Role role) {
2930
boolean result = true;
3031
final IAuthorizable profile = AuthMiddleware.getAuthClient(rc);
3132
if (profile instanceof ClientKey) {
@@ -34,15 +35,20 @@ public boolean validateRequest(RoutingContext rc, JsonObject requestJsonObject)
3435
// service_id is set in the request, so need to check if the given link_id is linked to this service
3536
if (this.rotatingServiceLinkStore == null) {
3637
// this is an invalid configuration. This operator is not set to validate service links, but has a service Id set.
37-
LOGGER.warn("Invalid configuration. Operator not set to validate service links (validate_service_links=false in config), but the calling client has a ServiceId set. ");
38+
LOGGER.warn("Path: {} , Invalid configuration. Operator not set to validate service links (validate_service_links=false in config), but the calling client has a ServiceId set. ", rc.normalizedPath());
3839
return false;
3940
}
4041

4142
if (requestJsonObject.containsKey(LINK_ID)) {
4243
String linkId = requestJsonObject.getString(LINK_ID);
4344
ServiceLink serviceLink = this.rotatingServiceLinkStore.getServiceLink(clientKey.getServiceId(), linkId);
4445
if (serviceLink == null) {
45-
LOGGER.warn("ClientKey has ServiceId set, but LinkId in request was not authorized. ServiceId: {}, LinkId in request: {}", clientKey.getServiceId(), linkId);
46+
LOGGER.warn("Path: {} , ClientKey has ServiceId set, but LinkId in request was not authorized. ServiceId: {}, LinkId in request: {}", rc.normalizedPath(), clientKey.getServiceId(), linkId);
47+
return false;
48+
}
49+
if (!serviceLink.getRoles().contains(role)) {
50+
LOGGER.warn("Path: {} , ServiceLink {} does not have role {}", rc.normalizedPath(), linkId, role);
51+
4652
return false;
4753
}
4854
Service service = rotatingServiceStore.getService(clientKey.getServiceId());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ private void handleIdentityMapV2(RoutingContext rc) {
13191319
}
13201320

13211321
JsonObject requestJsonObject = (JsonObject) rc.data().get(REQUEST);
1322-
if (!this.secureLinkValidatorService.validateRequest(rc, requestJsonObject)) {
1322+
if (!this.secureLinkValidatorService.validateRequest(rc, requestJsonObject, Role.MAPPER)) {
13231323
ResponseUtil.Error(ResponseStatus.Unauthorized, HttpStatus.SC_UNAUTHORIZED, rc, "Invalid link_id");
13241324
return;
13251325
}

src/test/java/com/uid2/operator/UIDOperatorVerticleTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void deployVerticle(Vertx vertx, VertxTestContext testContext, TestInfo t
111111
mocks = MockitoAnnotations.openMocks(this);
112112
when(saltProvider.getSnapshot(any())).thenReturn(saltProviderSnapshot);
113113
when(clock.instant()).thenAnswer(i -> now);
114-
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class))).thenReturn(true);
114+
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class), any(Role.class))).thenReturn(true);
115115

116116

117117
JsonObject config = new JsonObject();
@@ -4117,7 +4117,7 @@ void keySharingRotatingKeysets_IDREADER(String testRun, Vertx vertx, VertxTestCo
41174117
@Test
41184118
void secureLinkValidationPassesReturnsIdentity(Vertx vertx, VertxTestContext testContext) {
41194119
JsonObject req = setupIdentityMapServiceLinkTest();
4120-
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class))).thenReturn(true);
4120+
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class), any(Role.class))).thenReturn(true);
41214121

41224122
send("v2", vertx, "v2" + "/identity/map", false, null, req, 200, json -> {
41234123
checkIdentityMapResponse(json, "[email protected]", "[email protected]");
@@ -4128,7 +4128,7 @@ void secureLinkValidationPassesReturnsIdentity(Vertx vertx, VertxTestContext tes
41284128
@Test
41294129
void secureLinkValidationFailsReturnsIdentityError(Vertx vertx, VertxTestContext testContext) {
41304130
JsonObject req = setupIdentityMapServiceLinkTest();
4131-
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class))).thenReturn(false);
4131+
when(this.secureLinkValidatorService.validateRequest(any(RoutingContext.class), any(JsonObject.class), any(Role.class))).thenReturn(false);
41324132

41334133
send("v2", vertx, "v2" + "/identity/map", false, null, req, 401, json -> {
41344134
assertEquals("unauthorized", json.getString("status"));

src/test/java/com/uid2/operator/service/SecureLinkValidatorServiceTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ void validateRequest_serviceIdNotSet_returnsTrue() {
4040
this.setClientKey(0);
4141

4242
SecureLinkValidatorService service = new SecureLinkValidatorService(this.rotatingServiceLinkStore, this.rotatingServiceStore);
43-
assertTrue(service.validateRequest(this.routingContext, null));
43+
assertTrue(service.validateRequest(this.routingContext, null, Role.MAPPER));
4444
}
4545

4646
@Test
47-
void validateRequest_linkIdFound_returnsTrue() {
47+
void validateRequest_linkIdFoundAndRoleAllowed_returnsTrue() {
4848
this.setClientKey(10);
4949
JsonObject requestJsonObject = new JsonObject();
5050
requestJsonObject.put(SecureLinkValidatorService.LINK_ID, "999");
5151

52-
when(this.rotatingServiceLinkStore.getServiceLink(10, "999")).thenReturn(new ServiceLink("999", 10, 100, "testServiceLink", null));
52+
when(this.rotatingServiceLinkStore.getServiceLink(10, "999")).thenReturn(new ServiceLink("999", 10, 100, "testServiceLink", Set.of(Role.MAPPER)));
5353

5454
SecureLinkValidatorService service = new SecureLinkValidatorService(this.rotatingServiceLinkStore, this.rotatingServiceStore);
55-
assertTrue(service.validateRequest(this.routingContext, requestJsonObject));
55+
assertTrue(service.validateRequest(this.routingContext, requestJsonObject, Role.MAPPER));
5656
}
5757

5858
@Test
@@ -64,7 +64,19 @@ void validateRequest_linkIdNotFound_returnsFalse() {
6464
when(this.rotatingServiceLinkStore.getServiceLink(10, "999")).thenReturn(null);
6565

6666
SecureLinkValidatorService service = new SecureLinkValidatorService(this.rotatingServiceLinkStore, this.rotatingServiceStore);
67-
assertFalse(service.validateRequest(this.routingContext, requestJsonObject));
67+
assertFalse(service.validateRequest(this.routingContext, requestJsonObject, Role.MAPPER));
68+
}
69+
70+
@Test
71+
void validateRequest_roleNotInServiceLink_returnsFalse() {
72+
this.setClientKey(10);
73+
JsonObject requestJsonObject = new JsonObject();
74+
requestJsonObject.put(SecureLinkValidatorService.LINK_ID, "999");
75+
76+
when(this.rotatingServiceLinkStore.getServiceLink(10, "999")).thenReturn(new ServiceLink("999", 10, 100, "testServiceLink", Set.of(Role.SHARER, Role.CLIENTKEY_ISSUER)));
77+
78+
SecureLinkValidatorService service = new SecureLinkValidatorService(this.rotatingServiceLinkStore, this.rotatingServiceStore);
79+
assertFalse(service.validateRequest(this.routingContext, requestJsonObject, Role.MAPPER));
6880
}
6981

7082
private void setClientKey(int serviceId) {

0 commit comments

Comments
 (0)