-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix: Fix Domain based permission issues #15296
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
Open
yonglingsong
wants to merge
2
commits into
datahub-project:master
Choose a base branch
from
yonglingsong:domainFix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.linkedin.metadata.aspect.validation; | ||
|
|
||
| import static com.linkedin.metadata.Constants.APP_SOURCE; | ||
| import static com.linkedin.metadata.Constants.DOMAINS_ASPECT_NAME; | ||
| import static com.linkedin.metadata.Constants.EDITABLE_SCHEMA_METADATA_ASPECT_NAME; | ||
| import static com.linkedin.metadata.Constants.GLOBAL_TAGS_ASPECT_NAME; | ||
| import static com.linkedin.metadata.Constants.SCHEMA_METADATA_ASPECT_NAME; | ||
|
|
@@ -15,6 +16,7 @@ | |
| import com.linkedin.common.TagAssociationArray; | ||
| import com.linkedin.common.urn.Urn; | ||
| import com.linkedin.data.template.GetMode; | ||
| import com.linkedin.domain.Domains; | ||
| import com.linkedin.entity.Aspect; | ||
| import com.linkedin.events.metadata.ChangeType; | ||
| import com.linkedin.metadata.aspect.AspectRetriever; | ||
|
|
@@ -35,6 +37,7 @@ | |
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
@@ -54,22 +57,26 @@ | |
| @Accessors(chain = true) | ||
| public class PrivilegeConstraintsValidator extends AspectPayloadValidator { | ||
| @Nonnull private AspectPluginConfig config; | ||
| private boolean domainBasedAuthorizationEnabled = false; | ||
|
|
||
| @Override | ||
| protected Stream<AspectValidationException> validateProposedAspectsWithAuth( | ||
| @Nonnull Collection<? extends BatchItem> mcpItems, | ||
| protected Stream<AspectValidationException> validateProposedAspectsWithOriginalBatch( | ||
| @Nonnull Collection<? extends BatchItem> filteredBatch, | ||
| @Nonnull Collection<? extends BatchItem> originalBatch, | ||
| @Nonnull RetrieverContext retrieverContext, | ||
| @Nullable AuthorizationSession session) { | ||
| ValidationExceptionCollection exceptions = ValidationExceptionCollection.newCollection(); | ||
|
|
||
| if (session == null) { | ||
| exceptions.addException( | ||
| mcpItems.stream().findFirst().orElseThrow(IllegalStateException::new), | ||
| filteredBatch.stream().findFirst().orElseThrow(IllegalStateException::new), | ||
| "No authentication details found, cannot authorize change."); | ||
| return exceptions.streamAllExceptions(); | ||
| } | ||
|
|
||
| for (BatchItem item : mcpItems) { | ||
| // Process only the filtered batch (aspects this validator should handle) | ||
| // Use originalBatch for cross-aspect lookups (e.g., finding domains) | ||
| for (BatchItem item : filteredBatch) { | ||
| if (item.getSystemMetadata() != null | ||
| && item.getSystemMetadata().getProperties() != null | ||
| && UI_SOURCE.equals(item.getSystemMetadata().getProperties().get(APP_SOURCE))) { | ||
|
|
@@ -82,6 +89,7 @@ protected Stream<AspectValidationException> validateProposedAspectsWithAuth( | |
| validateGlobalTags( | ||
| session, | ||
| item, | ||
| originalBatch, // Use originalBatch for domain lookups | ||
| aspectRetriever, | ||
| aspectRetriever.getLatestAspectObject(item.getUrn(), GLOBAL_TAGS_ASPECT_NAME)) | ||
| .forEach(exceptions::addException); | ||
|
|
@@ -90,6 +98,7 @@ protected Stream<AspectValidationException> validateProposedAspectsWithAuth( | |
| validateSchemaMetadata( | ||
| session, | ||
| item, | ||
| originalBatch, // Use originalBatch for domain lookups | ||
| aspectRetriever, | ||
| aspectRetriever.getLatestAspectObject(item.getUrn(), SCHEMA_METADATA_ASPECT_NAME)) | ||
| .forEach(exceptions::addException); | ||
|
|
@@ -98,6 +107,7 @@ protected Stream<AspectValidationException> validateProposedAspectsWithAuth( | |
| validateEditableSchemaMetadata( | ||
| session, | ||
| item, | ||
| originalBatch, // Use originalBatch for domain lookups | ||
| aspectRetriever, | ||
| aspectRetriever.getLatestAspectObject( | ||
| item.getUrn(), EDITABLE_SCHEMA_METADATA_ASPECT_NAME)) | ||
|
|
@@ -114,6 +124,7 @@ protected Stream<AspectValidationException> validateProposedAspectsWithAuth( | |
| private List<AspectValidationException> validateGlobalTags( | ||
| AuthorizationSession session, | ||
| BatchItem item, | ||
| Collection<? extends BatchItem> allBatchItems, | ||
| AspectRetriever aspectRetriever, | ||
| @Nullable Aspect currentTagsAspect) { | ||
| GlobalTags newTags; | ||
|
|
@@ -136,11 +147,22 @@ private List<AspectValidationException> validateGlobalTags( | |
| } | ||
| if (newTags != null) { | ||
| Set<Urn> tagDifference = extractTagDifference(newTags, currentTags); | ||
|
|
||
| // Combine tags + domains (if enabled) as subResources for authorization check | ||
| Set<Urn> subResources = new HashSet<>(tagDifference); | ||
|
|
||
| // Only collect domain information if domain-based authorization is enabled | ||
| if (domainBasedAuthorizationEnabled) { | ||
| Set<Urn> domainUrns = | ||
| getEntityDomainsFromBatchOrDB(item.getUrn(), allBatchItems, aspectRetriever); | ||
| subResources.addAll(domainUrns); | ||
| } | ||
|
|
||
| if (!AuthUtil.isAPIAuthorizedEntityUrnsWithSubResources( | ||
| session, | ||
| ApiOperation.fromChangeType(item.getChangeType()), | ||
| List.of(item.getUrn()), | ||
| tagDifference)) { | ||
| subResources)) { | ||
| return List.of( | ||
| AspectValidationException.forItem( | ||
| item, "Unauthorized to modify one or more tag Urns: " + tagDifference)); | ||
|
|
@@ -181,6 +203,7 @@ private Set<Urn> extractTagDifference(GlobalTags newTags, @Nullable GlobalTags c | |
| private List<AspectValidationException> validateSchemaMetadata( | ||
| AuthorizationSession session, | ||
| BatchItem item, | ||
| Collection<? extends BatchItem> allBatchItems, | ||
| AspectRetriever aspectRetriever, | ||
| @Nullable Aspect currentSchemaAspect) { | ||
| SchemaMetadata schemaMetadata; | ||
|
|
@@ -222,11 +245,22 @@ private List<AspectValidationException> validateSchemaMetadata( | |
| existingTagsMap.get(schemaField.getFieldPath()))) | ||
| .flatMap(Set::stream) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| // Combine tags + domains (if enabled) as subResources for authorization check | ||
| Set<Urn> subResources = new HashSet<>(tagDifference); | ||
|
|
||
| // Only collect domain information if domain-based authorization is enabled | ||
| if (domainBasedAuthorizationEnabled) { | ||
| Set<Urn> domainUrns = | ||
| getEntityDomainsFromBatchOrDB(item.getUrn(), allBatchItems, aspectRetriever); | ||
| subResources.addAll(domainUrns); | ||
| } | ||
|
|
||
| if (!AuthUtil.isAPIAuthorizedEntityUrnsWithSubResources( | ||
| session, | ||
| ApiOperation.fromChangeType(item.getChangeType()), | ||
| List.of(item.getUrn()), | ||
| tagDifference)) { | ||
| subResources)) { | ||
| return List.of( | ||
| AspectValidationException.forItem( | ||
| item, "Unauthorized to modify one or more tag Urns: " + tagDifference)); | ||
|
|
@@ -238,6 +272,7 @@ private List<AspectValidationException> validateSchemaMetadata( | |
| private List<AspectValidationException> validateEditableSchemaMetadata( | ||
| AuthorizationSession session, | ||
| BatchItem item, | ||
| Collection<? extends BatchItem> allBatchItems, | ||
| AspectRetriever aspectRetriever, | ||
| @Nullable Aspect currentSchemaAspect) { | ||
| EditableSchemaMetadata editableSchemaMetadata; | ||
|
|
@@ -261,9 +296,10 @@ private List<AspectValidationException> validateEditableSchemaMetadata( | |
| } else { | ||
| editableSchemaMetadata = item.getAspect(EditableSchemaMetadata.class); | ||
| } | ||
| if (editableSchemaMetadata != null) { | ||
| if (editableSchemaMetadata != null | ||
| && editableSchemaMetadata.getEditableSchemaFieldInfo() != null) { | ||
| final Map<String, GlobalTags> existingTagsMap = new HashMap<>(); | ||
| if (currentSchema != null) { | ||
| if (currentSchema != null && currentSchema.getEditableSchemaFieldInfo() != null) { | ||
| existingTagsMap.putAll( | ||
| currentSchema.getEditableSchemaFieldInfo().stream() | ||
| .collect( | ||
|
|
@@ -282,19 +318,90 @@ private List<AspectValidationException> validateEditableSchemaMetadata( | |
| existingTagsMap.get(schemaField.getFieldPath()))) | ||
| .flatMap(Set::stream) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| // Combine tags + domains (if enabled) as subResources for authorization check | ||
| Set<Urn> subResources = new HashSet<>(tagDifference); | ||
|
|
||
| // Only collect domain information if domain-based authorization is enabled | ||
| if (domainBasedAuthorizationEnabled) { | ||
| Set<Urn> domainUrns = | ||
| getEntityDomainsFromBatchOrDB(item.getUrn(), allBatchItems, aspectRetriever); | ||
| subResources.addAll(domainUrns); | ||
| } | ||
|
|
||
| if (!AuthUtil.isAPIAuthorizedEntityUrnsWithSubResources( | ||
| session, | ||
| ApiOperation.fromChangeType(item.getChangeType()), | ||
| List.of(item.getUrn()), | ||
| tagDifference)) { | ||
| subResources)) { | ||
| return List.of( | ||
| AspectValidationException.forItem( | ||
| item, "Unauthorized to modify one or more tag Urns: " + tagDifference)); | ||
| item, | ||
| "Unauthorized to modify editable schema field tags on entity: " + item.getUrn())); | ||
| } | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the domain URNs for an entity to include as subResources in authorization checks. This | ||
| * allows policies to filter based on both entity domains AND other subResources like tags. | ||
| * | ||
| * <p>The PolicyEngine is designed to handle heterogeneous subResources and has special logic to | ||
| * extract domain information from the entire subResource collection when evaluating DOMAIN field | ||
| * criteria. | ||
| */ | ||
| private Set<Urn> getEntityDomains(Urn entityUrn, AspectRetriever aspectRetriever) { | ||
| try { | ||
| Aspect domainsAspect = aspectRetriever.getLatestAspectObject(entityUrn, DOMAINS_ASPECT_NAME); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the read which is not protected by a transaction and subject to caching. |
||
| if (domainsAspect != null) { | ||
| Domains domains = RecordUtils.toRecordTemplate(Domains.class, domainsAspect.data()); | ||
| return new HashSet<>(domains.getDomains()); | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn("Failed to retrieve domains for entity {}: {}", entityUrn, e.getMessage()); | ||
| } | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| /** | ||
| * Get domain URNs for an entity using UNION strategy. | ||
| * | ||
| * <p>UNION-BASED AUTHORIZATION: we must check permissions against ALL domains: - Existing domains | ||
| * (from entity) and current domains - New domains (from batch) | ||
| * | ||
| * @param entityUrn the entity URN to get domains for | ||
| * @param allBatchItems all items in the current batch | ||
| * @param aspectRetriever for DB lookup of existing domains | ||
| * @return set of domain URNs (union of existing + batch domains) | ||
| */ | ||
| private Set<Urn> getEntityDomainsFromBatchOrDB( | ||
| Urn entityUrn, | ||
| Collection<? extends BatchItem> allBatchItems, | ||
| AspectRetriever aspectRetriever) { | ||
|
|
||
| Set<Urn> domainUnion = new HashSet<>(getEntityDomains(entityUrn, aspectRetriever)); | ||
|
|
||
| // Add domains from batch if entity is being updated | ||
| allBatchItems.stream() | ||
| .filter( | ||
| item -> | ||
| entityUrn.equals(item.getUrn()) && DOMAINS_ASPECT_NAME.equals(item.getAspectName())) | ||
| .forEach( | ||
| item -> { | ||
| try { | ||
| Domains domains = item.getAspect(Domains.class); | ||
| if (domains != null && domains.getDomains() != null) { | ||
| domainUnion.addAll(domains.getDomains()); | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn("Failed to extract domains from batch item: {}", e.getMessage()); | ||
| } | ||
| }); | ||
|
|
||
| return domainUnion; | ||
| } | ||
|
|
||
| @Override | ||
| protected Stream<AspectValidationException> validateProposedAspects( | ||
| @Nonnull Collection<? extends BatchItem> changeMCPs, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach is not secure because the validateProposed method is executed outside of a transaction and the aspect lookup is subject to caching. The validation of a non-existent domain can only safely be done within a database transaction, otherwise it is subject to race conditions which could lead to privilege escalation.