Skip to content

Commit 7a10627

Browse files
authored
Allow admins to perform operations when OBAC is enabled (#6273)
1 parent 9cbefda commit 7a10627

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

app/src/main/java/io/apicurio/registry/auth/AuthorizedInterceptor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ public Object authorizeMethod(InvocationContext context) throws Exception {
167167
}
168168

169169
// If Owner-only is enabled, apply ownership rules
170-
if (authConfig.ownerOnlyAuthorizationEnabled.get() && !obac.isAuthorized(context)) {
171-
log.warn("OBAC enabled and operation not permitted due to wrong owner.");
172-
throw new ForbiddenException("User " + securityIdentity.getPrincipal().getName() + " is not authorized to perform the requested operation.");
170+
if (authConfig.ownerOnlyAuthorizationEnabled.get()) {
171+
if (authConfig.roleBasedAuthorizationEnabled && rbac.isAdmin()) {
172+
// User is admin, that's good enough.
173+
} else if (!obac.isAuthorized(context)) {
174+
log.warn("OBAC enabled and operation not permitted due to wrong owner.");
175+
throw new ForbiddenException("User " + securityIdentity.getPrincipal().getName()
176+
+ " is not authorized to perform the requested operation.");
177+
}
173178
}
174179

175180
return context.proceed();

app/src/main/java/io/apicurio/registry/rest/v2/GroupsResourceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ public void deleteArtifactVersionMetaData(String groupId, String artifactId, Str
671671
*/
672672
@Override
673673
@Audited(extractParameters = {"0", KEY_GROUP_ID, "1", KEY_ARTIFACT_ID, "2", KEY_VERSION})
674-
@Authorized(style = AuthorizedStyle.GroupAndArtifact, level = AuthorizedLevel.Write)
674+
@Authorized(style = AuthorizedStyle.GroupOnly, level = AuthorizedLevel.Write)
675675
public Comment addArtifactVersionComment(String groupId, String artifactId, String version, NewComment data) {
676676
requireParameter("groupId", groupId);
677677
requireParameter("artifactId", artifactId);
@@ -686,7 +686,7 @@ public Comment addArtifactVersionComment(String groupId, String artifactId, Stri
686686
*/
687687
@Override
688688
@Audited(extractParameters = {"0", KEY_GROUP_ID, "1", KEY_ARTIFACT_ID, "2", KEY_VERSION, "3", "comment_id"})
689-
@Authorized(style = AuthorizedStyle.GroupAndArtifact, level = AuthorizedLevel.Write)
689+
@Authorized(style = AuthorizedStyle.GroupOnly, level = AuthorizedLevel.Write)
690690
public void deleteArtifactVersionComment(String groupId, String artifactId, String version, String commentId) {
691691
requireParameter("groupId", groupId);
692692
requireParameter("artifactId", artifactId);
@@ -716,7 +716,7 @@ public List<Comment> getArtifactVersionComments(String groupId, String artifactI
716716
*/
717717
@Override
718718
@Audited(extractParameters = {"0", KEY_GROUP_ID, "1", KEY_ARTIFACT_ID, "2", KEY_VERSION, "3", "comment_id"})
719-
@Authorized(style = AuthorizedStyle.GroupAndArtifact, level = AuthorizedLevel.Write)
719+
@Authorized(style = AuthorizedStyle.GroupOnly, level = AuthorizedLevel.Write)
720720
public void updateArtifactVersionComment(String groupId, String artifactId, String version, String commentId, NewComment data) {
721721
requireParameter("groupId", groupId);
722722
requireParameter("artifactId", artifactId);

app/src/test/java/io/apicurio/registry/auth/SimpleAuthTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ public void testOwnerOnlyAuthorization() throws Exception {
242242
Auth authDev = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_CLIENT_ID, "test1");
243243
RegistryClient clientDev = createClient(authDev);
244244

245+
Auth authDev2 = new OidcAuth(httpClient, JWKSMockServer.DEVELOPER_2_CLIENT_ID, "test1");
246+
RegistryClient clientDev2 = createClient(authDev2);
247+
245248
Auth authAdmin = new OidcAuth(httpClient, JWKSMockServer.ADMIN_CLIENT_ID, "test1");
246249
RegistryClient clientAdmin = createClient(authAdmin);
247250

@@ -274,6 +277,25 @@ public void testOwnerOnlyAuthorization() throws Exception {
274277
rule.setType(RuleType.COMPATIBILITY);
275278
rule.setConfig(CompatibilityLevel.BACKWARD.name());
276279
clientAdmin.createArtifactRule(groupId, artifactId2, rule);
280+
281+
// Dev User will create an artifact
282+
String artifactId3 = TestUtils.generateArtifactId();
283+
clientDev.createArtifact(groupId, artifactId3, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
284+
285+
// Admin user can delete the artifact (because they are an admin)
286+
clientAdmin.deleteArtifact(groupId, artifactId3);
287+
288+
// Dev User will create another artifact
289+
String artifactId4 = TestUtils.generateArtifactId();
290+
clientDev.createArtifact(groupId, artifactId4, ArtifactType.JSON, new ByteArrayInputStream("{}".getBytes()));
291+
292+
// Dev2 User cannot delete the artifact (is **NOT** the owner)
293+
Assertions.assertThrows(ForbiddenException.class, () -> {
294+
clientDev2.deleteArtifact(groupId, artifactId4);
295+
});
296+
297+
// Dev User CAN delete the artifact (is the owner)
298+
clientDev.deleteArtifact(groupId, artifactId4);
277299
}
278300

279301
@Test

0 commit comments

Comments
 (0)