Skip to content

Commit

Permalink
feat(auth): Add backwards compatible field resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro93 committed Oct 24, 2023
1 parent c849246 commit 62b8f6a
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 10 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 @@ -33,6 +33,11 @@ public EntityFieldType getFieldType() {
return EntityFieldType.DATA_PLATFORM_INSTANCE;
}

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

@Override
public FieldResolver getFieldResolver(EntitySpec entitySpec) {
return FieldResolver.getResolverFromFunction(entitySpec, this::getDataPlatformInstance);
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 Down Expand Up @@ -41,6 +42,11 @@ public EntityFieldType getFieldType() {
return EntityFieldType.DOMAIN;
}

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

@Override
public FieldResolver getFieldResolver(EntitySpec entitySpec) {
return FieldResolver.getResolverFromFunction(entitySpec, this::getDomains);
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 @@ -15,6 +16,12 @@ public interface EntityFieldResolverProvider {
*/
EntityFieldType getFieldType();

/**
* List of fields that this hydrator is hydrating.
* @return
*/
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,7 +3,9 @@
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;


/**
Expand All @@ -12,7 +14,12 @@
public class EntityTypeFieldResolverProvider implements EntityFieldResolverProvider {
@Override
public EntityFieldType getFieldType() {
return EntityFieldType.TYPE;
return getFieldTypes().get(0);
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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;


/**
Expand All @@ -15,6 +17,11 @@ public EntityFieldType getFieldType() {
return EntityFieldType.URN;
}

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

@Override
public FieldResolver getFieldResolver(EntitySpec entitySpec) {
return FieldResolver.getResolverFromValues(Collections.singleton(entitySpec.getEntity()));
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 @@ -39,6 +40,11 @@ public EntityFieldType getFieldType() {
return EntityFieldType.GROUP_MEMBERSHIP;
}

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

@Override
public FieldResolver getFieldResolver(EntitySpec entitySpec) {
return FieldResolver.getResolverFromFunction(entitySpec, this::getGroupMembership);
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 @@ -32,6 +33,11 @@ public EntityFieldType getFieldType() {
return EntityFieldType.OWNER;
}

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

@Override
public FieldResolver getFieldResolver(EntitySpec entitySpec) {
return FieldResolver.getResolverFromFunction(entitySpec, this::getOwners);
Expand Down

0 comments on commit 62b8f6a

Please sign in to comment.