Skip to content

Commit

Permalink
feat(auth): Add backwards compatible field resolver (#9096)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro93 authored Oct 25, 2023
1 parent b612545 commit 9cccd22
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
* List of entity field types to fetch for a given entity
*/
public enum EntityFieldType {

/**
* Type of the entity (e.g. dataset, chart)
* @deprecated
*/
@Deprecated
RESOURCE_URN,
/**
* Urn of the entity
* @deprecated
*/
@Deprecated
RESOURCE_TYPE,
/**
* Type of the entity (e.g. dataset, chart)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.datahub.authorization;

import com.datahub.authorization.fieldresolverprovider.DataPlatformInstanceFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.EntityTypeFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.OwnerFieldResolverProvider;
import com.datahub.authentication.Authentication;
import com.datahub.authorization.fieldresolverprovider.DataPlatformInstanceFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.DomainFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.EntityUrnFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.EntityFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.EntityTypeFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.EntityUrnFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.GroupMembershipFieldResolverProvider;
import com.datahub.authorization.fieldresolverprovider.OwnerFieldResolverProvider;
import com.google.common.collect.ImmutableList;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.util.Pair;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -34,7 +35,7 @@ public ResolvedEntitySpec resolve(EntitySpec entitySpec) {

private Map<EntityFieldType, FieldResolver> getFieldResolvers(EntitySpec entitySpec) {
return _entityFieldResolverProviders.stream()
.collect(Collectors.toMap(EntityFieldResolverProvider::getFieldType,
hydrator -> hydrator.getFieldResolver(entitySpec)));
.flatMap(resolver -> resolver.getFieldTypes().stream().map(fieldType -> Pair.of(fieldType, resolver)))
.collect(Collectors.toMap(Pair::getKey, pair -> pair.getValue().getFieldResolver(entitySpec)));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.datahub.authorization.fieldresolverprovider;

import static com.linkedin.metadata.Constants.DATA_PLATFORM_INSTANCE_ASPECT_NAME;
import static com.linkedin.metadata.Constants.DATA_PLATFORM_INSTANCE_ENTITY_NAME;

import com.datahub.authentication.Authentication;
import com.datahub.authorization.EntityFieldType;
import com.datahub.authorization.EntitySpec;
Expand All @@ -14,10 +11,13 @@
import com.linkedin.entity.EnvelopedAspect;
import com.linkedin.entity.client.EntityClient;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.linkedin.metadata.Constants.*;

/**
* Provides field resolver for domain given resourceSpec
*/
Expand All @@ -29,8 +29,8 @@ public class DataPlatformInstanceFieldResolverProvider implements EntityFieldRes
private final Authentication _systemAuthentication;

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.DATA_PLATFORM_INSTANCE;
public List<EntityFieldType> getFieldTypes() {
return Collections.singletonList(EntityFieldType.DATA_PLATFORM_INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -37,8 +38,8 @@ public class DomainFieldResolverProvider implements EntityFieldResolverProvider
private final Authentication _systemAuthentication;

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.DOMAIN;
public List<EntityFieldType> getFieldTypes() {
return Collections.singletonList(EntityFieldType.DOMAIN);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.datahub.authorization.FieldResolver;
import com.datahub.authorization.EntityFieldType;
import com.datahub.authorization.EntitySpec;
import java.util.List;


/**
Expand All @@ -11,9 +12,10 @@
public interface EntityFieldResolverProvider {

/**
* Field that this hydrator is hydrating
* List of fields that this hydrator is hydrating.
* @return
*/
EntityFieldType getFieldType();
List<EntityFieldType> getFieldTypes();

/**
* Return resolver for fetching the field values given the entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import com.datahub.authorization.FieldResolver;
import com.datahub.authorization.EntityFieldType;
import com.datahub.authorization.EntitySpec;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;


/**
* Provides field resolver for entity type given entitySpec
*/
public class EntityTypeFieldResolverProvider implements EntityFieldResolverProvider {

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.TYPE;
public List<EntityFieldType> getFieldTypes() {
return ImmutableList.of(EntityFieldType.TYPE, EntityFieldType.RESOURCE_TYPE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import com.datahub.authorization.FieldResolver;
import com.datahub.authorization.EntityFieldType;
import com.datahub.authorization.EntitySpec;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;


/**
* Provides field resolver for entity urn given entitySpec
*/
public class EntityUrnFieldResolverProvider implements EntityFieldResolverProvider {

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.URN;
public List<EntityFieldType> getFieldTypes() {
return ImmutableList.of(EntityFieldType.URN, EntityFieldType.RESOURCE_URN);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.linkedin.identity.NativeGroupMembership;
import com.linkedin.metadata.Constants;
import com.linkedin.identity.GroupMembership;
import java.util.Collections;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -35,8 +36,8 @@ public class GroupMembershipFieldResolverProvider implements EntityFieldResolver
private final Authentication _systemAuthentication;

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.GROUP_MEMBERSHIP;
public List<EntityFieldType> getFieldTypes() {
return Collections.singletonList(EntityFieldType.GROUP_MEMBERSHIP);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.Constants;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,8 +29,8 @@ public class OwnerFieldResolverProvider implements EntityFieldResolverProvider {
private final Authentication _systemAuthentication;

@Override
public EntityFieldType getFieldType() {
return EntityFieldType.OWNER;
public List<EntityFieldType> getFieldTypes() {
return Collections.singletonList(EntityFieldType.OWNER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void setup() {

@Test
public void shouldReturnDataPlatformInstanceType() {
assertEquals(EntityFieldType.DATA_PLATFORM_INSTANCE, dataPlatformInstanceFieldResolverProvider.getFieldType());
assertEquals(EntityFieldType.DATA_PLATFORM_INSTANCE, dataPlatformInstanceFieldResolverProvider.getFieldTypes().get(0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setup() {

@Test
public void shouldReturnGroupsMembershipType() {
assertEquals(EntityFieldType.GROUP_MEMBERSHIP, groupMembershipFieldResolverProvider.getFieldType());
assertEquals(EntityFieldType.GROUP_MEMBERSHIP, groupMembershipFieldResolverProvider.getFieldTypes().get(0));
}

@Test
Expand Down

0 comments on commit 9cccd22

Please sign in to comment.