Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openmetadata.schema.api.data.CreateGlossary;
import org.openmetadata.schema.api.policies.CreatePolicy;
import org.openmetadata.schema.api.teams.CreateRole;
import org.openmetadata.schema.api.teams.CreateTeam;
import org.openmetadata.schema.api.teams.CreateUser;
import org.openmetadata.schema.attachments.Asset;
import org.openmetadata.schema.entity.data.Glossary;
import org.openmetadata.schema.entity.policies.Policy;
import org.openmetadata.schema.entity.policies.accessControl.Rule;
import org.openmetadata.schema.entity.teams.Role;
import org.openmetadata.schema.entity.teams.Team;
import org.openmetadata.schema.type.MetadataOperation;
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.test.util.RestClient;
import org.openmetadata.sdk.test.util.SdkClients;
import org.openmetadata.sdk.test.util.TestNamespace;
Expand Down Expand Up @@ -269,4 +279,84 @@ void testListAttachmentsNoSortReturnsAll(TestNamespace ns) {
assertNotNull(a.getId());
assertNotNull(b.getId());
}

@Test
void testAttachmentReadsFollowParentEntityVisibility(TestNamespace ns) {
Glossary glossary = createGlossary(RestClient.admin(), ns, "attach-parent-visibility");
Asset asset = uploadAttachment(entityLink(glossary), "visibility.txt", "v");

RestClient restricted = userWithoutGlossaryView("att_deny");
assertEquals(403, readStatus(restricted, ATTACHMENTS_PATH + "/" + asset.getId()));
assertEquals(403, readStatus(restricted, externalListPath(glossary)));
}

@Test
void testAttachmentReadsAllowedWhenParentIsVisible(TestNamespace ns) {
Glossary glossary = createGlossary(RestClient.admin(), ns, "attach-parent-visible");
Asset asset = uploadAttachment(entityLink(glossary), "visible.txt", "v");

RestClient viewer = plainUser("att_view");
assertEquals(200, readStatus(viewer, ATTACHMENTS_PATH + "/" + asset.getId()));
assertEquals(200, readStatus(viewer, externalListPath(glossary)));
}

private static String externalListPath(Glossary glossary) {
return ATTACHMENTS_PATH + "/fqn/" + glossary.getFullyQualifiedName() + "/External";
}

private static int readStatus(RestClient client, String path) {
try (Response response = client.rawGet(path)) {
return response.getStatus();
}
}

private static String shortName(String label) {
return label + "_" + UUID.randomUUID().toString().substring(0, 8);
}

private static RestClient plainUser(String label) {
String userName = shortName(label);
String email = userName + "@test.openmetadata.org";
SdkClients.adminClient().users().create(new CreateUser().withName(userName).withEmail(email));
return RestClient.forUser(email, new String[] {});
}

private static RestClient userWithoutGlossaryView(String label) {
OpenMetadataClient admin = SdkClients.adminClient();
String prefix = shortName(label);
Rule denyView =
new Rule()
.withName(prefix + "_rule")
.withResources(List.of("glossary"))
.withOperations(List.of(MetadataOperation.VIEW_ALL, MetadataOperation.VIEW_BASIC))
.withEffect(Rule.Effect.DENY);
Policy policy =
admin
.policies()
.create(new CreatePolicy().withName(prefix + "_policy").withRules(List.of(denyView)));
Role role =
admin
.roles()
.create(
new CreateRole()
.withName(prefix + "_role")
.withPolicies(List.of(policy.getFullyQualifiedName())));
Team team =
admin
.teams()
.create(
new CreateTeam()
.withName(prefix + "_team")
.withTeamType(CreateTeam.TeamType.GROUP)
.withDefaultRoles(List.of(role.getId())));
String email = prefix + "u@test.openmetadata.org";
admin
.users()
.create(
new CreateUser()
.withName(prefix + "u")
.withEmail(email)
.withTeams(List.of(team.getId())));
return RestClient.forUser(email, new String[] {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public Response getAssetById(
if (asset == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
authorizeViewOfAttachedEntity(securityContext, asset.getEntityLink());
return Response.ok(asset).build();
}

Expand Down Expand Up @@ -174,13 +175,7 @@ public Response downloadAsset(
return Response.status(Response.Status.NOT_FOUND).build();
}

// Authorization check
MessageParser.EntityLink parsedLink = MessageParser.EntityLink.parse(asset.getEntityLink());
ResourceContextInterface resourceContext =
new ResourceContext<>(parsedLink.getEntityType(), null, parsedLink.getEntityFQN());
OperationContext operationContext =
new OperationContext(parsedLink.getEntityType(), MetadataOperation.VIEW_BASIC);
authorizer.authorize(securityContext, operationContext, resourceContext);
authorizeViewOfAttachedEntity(securityContext, asset.getEntityLink());

boolean isImage = asset.getContentType() != null && asset.getContentType().startsWith("image/");
boolean useCdn = cdnUrl != null && !cdnUrl.isEmpty();
Expand Down Expand Up @@ -297,15 +292,36 @@ public Response listAttachmentsByFqn(
@QueryParam("sortBy") String sortBy,
@QueryParam("sortOrder") String sortOrder,
@QueryParam("limit") Integer limit,
@QueryParam("offset") @DefaultValue("0") int offset) {
@QueryParam("offset") @DefaultValue("0") int offset,
@Context SecurityContext securityContext) {
List<Asset> assets = assetRepository.getByFQN(fqn, assetType);
if (assets == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
// Assets are looked up by FQN and asset type, neither of which pins the parent entity type,
// so one page can span more than one parent. Authorize each distinct parent rather than
// assuming the first one speaks for the rest.
assets.stream()
.map(Asset::getEntityLink)
.distinct()
.forEach(entityLink -> authorizeViewOfAttachedEntity(securityContext, entityLink));
List<Asset> result = applySortAndPaginate(assets, sortBy, sortOrder, limit, offset);
return Response.ok(result).build();
}

/**
* Attachments inherit the visibility of the entity they hang off, so every endpoint that returns
* attachment data resolves the parent entity and checks {@code VIEW_BASIC} against it.
*/
private void authorizeViewOfAttachedEntity(SecurityContext securityContext, String entityLink) {
MessageParser.EntityLink parsedLink = MessageParser.EntityLink.parse(entityLink);
ResourceContextInterface resourceContext =
new ResourceContext<>(parsedLink.getEntityType(), null, parsedLink.getEntityFQN());
OperationContext operationContext =
new OperationContext(parsedLink.getEntityType(), MetadataOperation.VIEW_BASIC);
authorizer.authorize(securityContext, operationContext, resourceContext);
}

private static List<Asset> applySortAndPaginate(
List<Asset> assets, String sortBy, String sortOrder, Integer limit, int offset) {
if (offset < 0) {
Expand Down
Loading