Skip to content

Commit

Permalink
Create new Query Example permission policy (#1633)
Browse files Browse the repository at this point in the history
Co-authored-by: vburlachenko <[email protected]>
Co-authored-by: ayemets-corcentric <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent 516a9d1 commit 57a4ec3
Show file tree
Hide file tree
Showing 28 changed files with 513 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class URLResourceExtractor implements ResourceExtractor {
@Override
public boolean handles(final AuthorizationManagerType type) {
return type == AuthorizationManagerType.DATA_ENTITY || type == AuthorizationManagerType.TERM
|| type == AuthorizationManagerType.QUERY_EXAMPLE
|| type == AuthorizationManagerType.DEG;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.opendatadiscovery.oddplatform.auth.manager.AuthorizationManagerType.DATASET_FIELD;
import static org.opendatadiscovery.oddplatform.auth.manager.AuthorizationManagerType.DATA_ENTITY;
import static org.opendatadiscovery.oddplatform.auth.manager.AuthorizationManagerType.NO_CONTEXT;
import static org.opendatadiscovery.oddplatform.auth.manager.AuthorizationManagerType.QUERY_EXAMPLE;
import static org.opendatadiscovery.oddplatform.auth.manager.AuthorizationManagerType.TERM;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.COLLECTOR_CREATE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.COLLECTOR_DELETE;
Expand Down Expand Up @@ -290,18 +291,18 @@ DATA_ENTITY, new PathPatternParserServerWebExchangeMatcher(
AuthorizationManagerType.DEG,
new PathPatternParserServerWebExchangeMatcher("/api/dataentitygroups/{data_entity_group_id}", PUT),
DATA_ENTITY_GROUP_UPDATE),
new SecurityRule(NO_CONTEXT,
new SecurityRule(QUERY_EXAMPLE,
new PathPatternParserServerWebExchangeMatcher("/api/queryexample/{example_id}", PUT),
QUERY_EXAMPLE_UPDATE),
new SecurityRule(NO_CONTEXT,
new SecurityRule(QUERY_EXAMPLE,
new PathPatternParserServerWebExchangeMatcher("/api/queryexample/{example_id}", DELETE),
QUERY_EXAMPLE_DELETE),
new SecurityRule(NO_CONTEXT,
new PathPatternParserServerWebExchangeMatcher("/api/queryexample/{example_id}/dataset", POST),
new SecurityRule(DATA_ENTITY,
new PathPatternParserServerWebExchangeMatcher("/api/dataentities/{data_entity_id}/queryexample", POST),
QUERY_EXAMPLE_DATASET_CREATE),
new SecurityRule(NO_CONTEXT,
new SecurityRule(DATA_ENTITY,
new PathPatternParserServerWebExchangeMatcher(
"/api/queryexample/{example_id}/dataset/{data_entity_id}", DELETE),
"/api/dataentities/{data_entity_id}/queryexample/{example_id}", DELETE),
QUERY_EXAMPLE_DATASET_DELETE),
new SecurityRule(NO_CONTEXT,
new PathPatternParserServerWebExchangeMatcher("/api/referencedata/table/{lookup_table_id}", PUT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityGroupLineageList;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityLineage;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityList;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityQueryExampleFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityRef;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityStatus;
import org.opendatadiscovery.oddplatform.api.contract.model.DataEntityStatusFormData;
Expand All @@ -41,6 +42,7 @@
import org.opendatadiscovery.oddplatform.api.contract.model.Ownership;
import org.opendatadiscovery.oddplatform.api.contract.model.OwnershipFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.OwnershipUpdateFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExample;
import org.opendatadiscovery.oddplatform.api.contract.model.Tag;
import org.opendatadiscovery.oddplatform.api.contract.model.TagsFormData;
import org.opendatadiscovery.oddplatform.dto.alert.AlertStatusEnum;
Expand All @@ -53,6 +55,7 @@
import org.opendatadiscovery.oddplatform.service.MessageService;
import org.opendatadiscovery.oddplatform.service.MetricService;
import org.opendatadiscovery.oddplatform.service.OwnershipService;
import org.opendatadiscovery.oddplatform.service.QueryExampleService;
import org.opendatadiscovery.oddplatform.service.activity.ActivityService;
import org.opendatadiscovery.oddplatform.service.term.TermService;
import org.springframework.http.ResponseEntity;
Expand All @@ -75,6 +78,7 @@ public class DataEntityController implements DataEntityApi {
private final MessageService messageService;
private final AlertHaltConfigService alertHaltConfigService;
private final MetricService metricService;
private final QueryExampleService queryExampleService;

@Override
public Mono<ResponseEntity<DataEntityRef>> createDataEntityGroup(final Mono<DataEntityGroupFormData> formData,
Expand Down Expand Up @@ -428,4 +432,23 @@ public Mono<ResponseEntity<DataEntityDomainList>> getDomains(final ServerWebExch
return dataEntityService.getDomainsInfo()
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<QueryExample>>
createQueryExampleToDatasetRelationshipNew(final Long dataEntityId,
final Mono<DataEntityQueryExampleFormData> formDataMono,
final ServerWebExchange exchange) {
return formDataMono
.flatMap(item ->
queryExampleService.createQueryExampleToDatasetRelationship(item.getQueryExampleId(), dataEntityId))
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<Void>> deleteQueryExampleToDatasetRelationshipNew(final Long dataEntityId,
final Long exampleId,
final ServerWebExchange exchange) {
return queryExampleService.deleteQueryExampleDatasetRelationship(exampleId, dataEntityId)
.thenReturn(ResponseEntity.noContent().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.opendatadiscovery.oddplatform.api.contract.api.QueryExampleApi;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExample;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDatasetFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDetails;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleList;
Expand Down Expand Up @@ -50,24 +48,6 @@ public Mono<ResponseEntity<Void>> deleteQueryExample(final Long exampleId,
.thenReturn(ResponseEntity.noContent().build());
}

@Override
public Mono<ResponseEntity<QueryExample>>
createQueryExampleToDatasetRelationship(final Long exampleId,
final Mono<QueryExampleDatasetFormData> queryExampleDatasetFormData,
final ServerWebExchange exchange) {
return queryExampleDatasetFormData
.flatMap(item -> queryExampleService.createQueryExampleToDatasetRelationship(exampleId, item))
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<Void>> deleteQueryExampleToDatasetRelationship(final Long exampleId,
final Long dataEntityId,
final ServerWebExchange exchange) {
return queryExampleService.deleteQueryExampleDatasetRelationship(exampleId, dataEntityId)
.thenReturn(ResponseEntity.noContent().build());
}

@Override
public Mono<ResponseEntity<QueryExampleList>> getQueryExampleByDatasetId(final Long dataEntityId,
final ServerWebExchange exchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import org.opendatadiscovery.oddplatform.model.tables.pojos.DataEntityPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.QueryExamplePojo;

public record QueryExampleDto(QueryExamplePojo queryExamplePojo, List<DataEntityPojo> linkedEntities) {
public record QueryExampleDto(QueryExamplePojo queryExamplePojo,
List<DataEntityPojo> linkedEntities) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum PolicyConditionKeyDto {
TERM_NAMESPACE_NAME("term:namespace:name"),
TERM_TAG_NAME("term:tag:name"),
TERM_OWNER("term:owner"),
TERM_OWNER_TITLE("term:owner:title");
TERM_OWNER_TITLE("term:owner:title"),
QUERY_EXAMPLE_NAME("queryexample:name");

private final String value;

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

import static org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto.DATA_ENTITY;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto.MANAGEMENT;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto.QUERY_EXAMPLE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto.TERM;

@RequiredArgsConstructor
Expand Down Expand Up @@ -36,6 +37,8 @@ public enum PolicyPermissionDto {
DATA_ENTITY_GROUP_UPDATE(DATA_ENTITY),
DATA_ENTITY_ATTACHMENT_MANAGE(DATA_ENTITY),
DATA_ENTITY_STATUS_UPDATE(DATA_ENTITY),
QUERY_EXAMPLE_DATASET_CREATE(DATA_ENTITY),
QUERY_EXAMPLE_DATASET_DELETE(DATA_ENTITY),
TERM_CREATE(MANAGEMENT),
TERM_UPDATE(TERM),
TERM_DELETE(TERM),
Expand Down Expand Up @@ -69,10 +72,8 @@ public enum PolicyPermissionDto {
ROLE_UPDATE(MANAGEMENT),
ROLE_DELETE(MANAGEMENT),
QUERY_EXAMPLE_CREATE(MANAGEMENT),
QUERY_EXAMPLE_UPDATE(MANAGEMENT),
QUERY_EXAMPLE_DELETE(MANAGEMENT),
QUERY_EXAMPLE_DATASET_CREATE(MANAGEMENT),
QUERY_EXAMPLE_DATASET_DELETE(MANAGEMENT),
QUERY_EXAMPLE_UPDATE(QUERY_EXAMPLE),
QUERY_EXAMPLE_DELETE(QUERY_EXAMPLE),
LOOKUP_TABLE_CREATE(MANAGEMENT),
LOOKUP_TABLE_UPDATE(MANAGEMENT),
LOOKUP_TABLE_DELETE(MANAGEMENT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum PolicyTypeDto {
DATA_ENTITY(true),
TERM(true),
QUERY_EXAMPLE(true),
MANAGEMENT(false);

private final boolean hasContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.opendatadiscovery.oddplatform.dto.policy;

import org.opendatadiscovery.oddplatform.dto.QueryExampleDto;
import org.opendatadiscovery.oddplatform.model.tables.pojos.OwnerPojo;

public record QueryExamplePolicyResolverContext(QueryExampleDto detailsDto, OwnerPojo currentOwner) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.opendatadiscovery.oddplatform.service;

import org.opendatadiscovery.oddplatform.api.contract.model.QueryExample;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDatasetFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDetails;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleList;
Expand All @@ -14,8 +13,7 @@ public interface QueryExampleService {
Mono<QueryExampleDetails> updateQueryExample(final Long exampleId, final QueryExampleFormData formData);

Mono<QueryExample> createQueryExampleToDatasetRelationship(
final Long queryExampleId,
final QueryExampleDatasetFormData queryExampleDatasetFormData);
final Long queryExampleId, final Long datasetId);

Mono<Void> deleteQueryExampleDatasetRelationship(final Long exampleId, final Long dataEntityId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.RequiredArgsConstructor;
import org.opendatadiscovery.oddplatform.annotation.ReactiveTransactional;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExample;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDatasetFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleDetails;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.QueryExampleList;
Expand Down Expand Up @@ -55,10 +54,9 @@ public Mono<QueryExampleDetails> updateQueryExample(final Long exampleId, final
@Override
@ReactiveTransactional
public Mono<QueryExample> createQueryExampleToDatasetRelationship(
final Long queryExampleId,
final QueryExampleDatasetFormData queryExampleDatasetFormData) {
final Long queryExampleId, final Long datasetId) {
return dataEntityToQueryExampleRepository
.createRelationWithDataEntity(queryExampleDatasetFormData.getDatasetId(), queryExampleId)
.createRelationWithDataEntity(datasetId, queryExampleId)
.switchIfEmpty(Mono.error(() -> new BadUserRequestException("Dataset assigned to Query Example")))
.then(dataEntityToQueryExampleRepository.getQueryExampleDatasetRelations(queryExampleId))
.map(dto -> queryExampleMapper.mapToQueryExample(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.opendatadiscovery.oddplatform.service.permission.extractor;

import java.util.Collection;
import org.opendatadiscovery.oddplatform.auth.AuthIdentityProvider;
import org.opendatadiscovery.oddplatform.dto.QueryExampleDto;
import org.opendatadiscovery.oddplatform.dto.policy.PolicyDto;
import org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto;
import org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto;
import org.opendatadiscovery.oddplatform.dto.policy.QueryExamplePolicyResolverContext;
import org.opendatadiscovery.oddplatform.mapper.PolicyMapper;
import org.opendatadiscovery.oddplatform.model.tables.pojos.OwnerPojo;
import org.opendatadiscovery.oddplatform.repository.reactive.ReactiveDataEntityQueryExampleRelationRepository;
import org.opendatadiscovery.oddplatform.service.PolicyService;
import org.opendatadiscovery.oddplatform.service.policy.PolicyPermissionExtractor;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
public class QueryExamplePermissionExtractor
extends AbstractContextualPermissionExtractor<QueryExamplePolicyResolverContext> {

private final AuthIdentityProvider authIdentityProvider;
private final ReactiveDataEntityQueryExampleRelationRepository repository;
private final PolicyPermissionExtractor permissionExtractor;

public QueryExamplePermissionExtractor(final PolicyService policyService,
final PolicyMapper policyMapper,
final AuthIdentityProvider authIdentityProvider,
final ReactiveDataEntityQueryExampleRelationRepository repository,
final PolicyPermissionExtractor permissionExtractor) {
super(policyService, policyMapper);
this.authIdentityProvider = authIdentityProvider;
this.repository = repository;
this.permissionExtractor = permissionExtractor;
}

@Override
protected Mono<QueryExamplePolicyResolverContext> getContext(final long resourceId) {
final Mono<QueryExampleDto> dtoMono = repository.getQueryExampleDatasetRelations(resourceId);

final Mono<OwnerPojo> ownerPojoMono = authIdentityProvider.fetchAssociatedOwner();
return ownerPojoMono
.zipWith(dtoMono)
.map(tuple
-> new QueryExamplePolicyResolverContext(tuple.getT2(), tuple.getT1()))
.switchIfEmpty(Mono.defer(()
-> dtoMono.map(dto -> new QueryExamplePolicyResolverContext(dto, null))));
}

@Override
protected Collection<PolicyPermissionDto> getPermissions(final PolicyDto policyDto,
final QueryExamplePolicyResolverContext context) {
return permissionExtractor.extractQueryExamplePermissions(policyDto.getStatements(), context);
}

@Override
public PolicyTypeDto getResourceType() {
return PolicyTypeDto.QUERY_EXAMPLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto;
import org.opendatadiscovery.oddplatform.dto.policy.PolicyStatementDto;
import org.opendatadiscovery.oddplatform.dto.policy.PolicyTypeDto;
import org.opendatadiscovery.oddplatform.dto.policy.QueryExamplePolicyResolverContext;
import org.opendatadiscovery.oddplatform.dto.policy.TermPolicyResolverContext;
import org.opendatadiscovery.oddplatform.service.policy.resolver.ConditionResolver;
import org.opendatadiscovery.oddplatform.service.policy.resolver.DataEntityConditionResolver;
import org.opendatadiscovery.oddplatform.service.policy.resolver.NoContextConditionResolver;
import org.opendatadiscovery.oddplatform.service.policy.resolver.QueryExampleConditionResolver;
import org.opendatadiscovery.oddplatform.service.policy.resolver.TermConditionResolver;
import org.springframework.stereotype.Component;

Expand All @@ -20,6 +22,7 @@
public class PolicyPermissionExtractor {
private final DataEntityConditionResolver dataEntityResolver;
private final TermConditionResolver termResolver;
private final QueryExampleConditionResolver queryExampleResolver;
private final NoContextConditionResolver noContextConditionResolver;

public Collection<PolicyPermissionDto> extractDataEntityPermissions(final List<PolicyStatementDto> statements,
Expand All @@ -32,6 +35,12 @@ public Collection<PolicyPermissionDto> extractTermPermissions(final List<PolicyS
return permissions(statements, termResolver, PolicyTypeDto.TERM, context);
}

public Collection<PolicyPermissionDto>
extractQueryExamplePermissions(final List<PolicyStatementDto> statements,
final QueryExamplePolicyResolverContext context) {
return permissions(statements, queryExampleResolver, PolicyTypeDto.QUERY_EXAMPLE, context);
}

public Collection<PolicyPermissionDto> extractManagementPermissions(final List<PolicyStatementDto> statements) {
return permissions(statements, noContextConditionResolver, PolicyTypeDto.MANAGEMENT, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.opendatadiscovery.oddplatform.service.policy.comparer.queryexample;

import java.util.function.Function;
import org.opendatadiscovery.oddplatform.dto.QueryExampleDto;
import org.opendatadiscovery.oddplatform.dto.policy.QueryExamplePolicyResolverContext;
import org.opendatadiscovery.oddplatform.service.policy.comparer.Comparer;

public final class QueryExampleComparorFactory {
private QueryExampleComparorFactory() {}

public static Comparer<QueryExamplePolicyResolverContext>
queryExample(final Function<QueryExampleDto, String> fieldExtractor) {
return new QueryExampleFieldComparer(fieldExtractor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.opendatadiscovery.oddplatform.service.policy.comparer.queryexample;

import java.util.function.Function;
import org.opendatadiscovery.oddplatform.dto.QueryExampleDto;
import org.opendatadiscovery.oddplatform.dto.policy.QueryExamplePolicyResolverContext;
import org.opendatadiscovery.oddplatform.service.policy.comparer.SimpleFieldComparer;

public class QueryExampleFieldComparer extends SimpleFieldComparer<QueryExamplePolicyResolverContext> {
public QueryExampleFieldComparer(final Function<QueryExampleDto, String> fieldExtractor) {
super(context -> fieldExtractor.apply(getQueryExample(context)));
}

private static QueryExampleDto getQueryExample(final QueryExamplePolicyResolverContext context) {
return context.detailsDto();
}
}
Loading

0 comments on commit 57a4ec3

Please sign in to comment.