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
6 changes: 5 additions & 1 deletion ingestion/examples/sample_data/topics/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"maximumMessageSize":167,
"cleanupPolicies": ["delete"],
"schemaType": "Avro",
"schemaText": "{\"namespace\":\"openmetadata.kafka\",\"name\":\"Customer\",\"type\":\"record\",\"fields\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"first_name\",\"type\":\"string\"},{\"name\":\"last_name\",\"type\":\"string\"},{\"name\":\"email\",\"type\":\"string\"},{\"name\":\"address_line_1\",\"type\":\"string\"},{\"name\":\"address_line_2\",\"type\":\"string\"},{\"name\":\"post_code\",\"type\":\"string\"},{\"name\":\"country\",\"type\":\"string\"}]}"
"schemaText": "{\"namespace\":\"openmetadata.kafka\",\"name\":\"Customer\",\"type\":\"record\",\"fields\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"first_name\",\"type\":\"string\"},{\"name\":\"last_name\",\"type\":\"string\"},{\"name\":\"email\",\"type\":\"string\"},{\"name\":\"address_line_1\",\"type\":\"string\"},{\"name\":\"address_line_2\",\"type\":\"string\"},{\"name\":\"post_code\",\"type\":\"string\"},{\"name\":\"country\",\"type\":\"string\"}]}",
"sampleData": [
"{\"id\": 1, \"first_name\": \"Lévy\", \"country\": \"ES\"}",
"{\"id\": 2, \"first_name\": \"Lima\", \"country\": \"ES\"}"
]
},
{
"name": "product_events",
Expand Down
21 changes: 19 additions & 2 deletions ingestion/src/metadata/ingestion/source/database/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
TableData,
TableProfile,
)
from metadata.generated.schema.entity.data.topic import Topic, TopicSampleData
from metadata.generated.schema.entity.policies.policy import Policy
from metadata.generated.schema.entity.services.connections.database.customDatabaseConnection import (
CustomDatabaseConnection,
Expand All @@ -86,7 +87,7 @@
from metadata.generated.schema.tests.testSuite import TestSuite
from metadata.generated.schema.type.entityLineage import EntitiesEdge, LineageDetails
from metadata.generated.schema.type.entityReference import EntityReference
from metadata.generated.schema.type.schema import Topic
from metadata.generated.schema.type.schema import Topic as TopicSchema
from metadata.ingestion.api.common import Entity
from metadata.ingestion.api.source import InvalidSourceException, Source
from metadata.ingestion.models.pipeline_status import OMetaPipelineStatus
Expand Down Expand Up @@ -679,7 +680,7 @@ def ingest_topics(self) -> Iterable[CreateTopicRequest]:
)
schema_fields = load_parser_fn(topic["name"], topic["schemaText"])

create_topic.messageSchema = Topic(
create_topic.messageSchema = TopicSchema(
schemaText=topic["schemaText"],
schemaType=topic["schemaType"],
schemaFields=schema_fields,
Expand All @@ -688,6 +689,22 @@ def ingest_topics(self) -> Iterable[CreateTopicRequest]:
self.status.scanned(f"Topic Scanned: {create_topic.name.__root__}")
yield create_topic

if topic.get("sampleData"):

topic_fqn = fqn.build(
self.metadata,
entity_type=Topic,
service_name=self.kafka_service.name.__root__,
topic_name=topic["name"],
)

topic_entity = self.metadata.get_by_name(entity=Topic, fqn=topic_fqn)

self.metadata.ingest_topic_sample_data(
topic=topic_entity,
sample_data=TopicSampleData(messages=topic["sampleData"]),
)

def ingest_looker(self) -> Iterable[Entity]:
"""
Looker sample data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.http.HttpResponse;
import java.security.KeyStoreException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -219,10 +220,11 @@ public List<PipelineStatus> getQueuedPipelineStatus(IngestionPipeline ingestionP
} catch (Exception e) {
throw PipelineServiceClientException.byMessage(ingestionPipeline.getName(), e.getMessage());
}
throw PipelineServiceClientException.byMessage(
ingestionPipeline.getName(),
"Failed to fetch ingestion pipeline runs",
Response.Status.fromStatusCode(response.statusCode()));
// Return an empty list. We'll just show the stored status from the Ingestion Pipeline
LOG.error(
String.format(
"Got status code [%s] trying to get queued statuses: [%s]", response.statusCode(), response.body()));
return new ArrayList<>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.openmetadata.service.resources.databases.DatabaseUtil;
import org.openmetadata.service.resources.databases.TableResource;
import org.openmetadata.service.resources.feeds.MessageParser.EntityLink;
import org.openmetadata.service.security.mask.PIIMasker;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.EntityUtil.Fields;
import org.openmetadata.service.util.FullyQualifiedName;
Expand Down Expand Up @@ -243,7 +244,11 @@ public Table getSampleData(UUID tableId, boolean authorizePII) throws IOExceptio
setFieldsInternal(table, Fields.EMPTY_FIELDS);

// Set the column tags. Will be used to mask the sample data
if (!authorizePII) getColumnTags(true, table.getColumns());
if (!authorizePII) {
getColumnTags(true, table.getColumns());
table.setTags(getTags(table.getFullyQualifiedName()));
return PIIMasker.getSampleData(table);
}

return table;
}
Expand Down Expand Up @@ -472,7 +477,10 @@ public Table getLatestTableProfile(String fqn, boolean authorizePII) throws IOEx
setColumnProfile(table.getColumns());

// Set the column tags. Will be used to hide the data
if (!authorizePII) getColumnTags(true, table.getColumns());
if (!authorizePII) {
getColumnTags(true, table.getColumns());
return PIIMasker.getTableProfile(table);
}

return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.BiPredicate;
import java.util.function.Function;
Expand All @@ -40,6 +42,7 @@
import org.openmetadata.schema.type.topic.TopicSampleData;
import org.openmetadata.service.Entity;
import org.openmetadata.service.resources.topics.TopicResource;
import org.openmetadata.service.security.mask.PIIMasker;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.EntityUtil.Fields;
import org.openmetadata.service.util.FullyQualifiedName;
Expand Down Expand Up @@ -119,7 +122,6 @@ public void storeRelationships(Topic topic) {
public Topic setFields(Topic topic, Fields fields) throws IOException {
topic.setService(getContainer(topic.getId()));
topic.setFollowers(fields.contains(FIELD_FOLLOWERS) ? getFollowers(topic) : null);
topic.setSampleData(fields.contains("sampleData") ? getSampleData(topic) : null);
if (topic.getMessageSchema() != null) {
getFieldTags(fields.contains(FIELD_TAGS), topic.getMessageSchema().getSchemaFields());
}
Expand All @@ -138,10 +140,25 @@ public void setService(Topic topic, EntityReference service) {
}
}

private TopicSampleData getSampleData(Topic topic) throws IOException {
return JsonUtils.readValue(
daoCollection.entityExtensionDAO().getExtension(topic.getId().toString(), "topic.sampleData"),
TopicSampleData.class);
public Topic getSampleData(UUID topicId, boolean authorizePII) throws IOException {
// Validate the request content
Topic topic = dao.findEntityById(topicId);

TopicSampleData sampleData =
JsonUtils.readValue(
daoCollection.entityExtensionDAO().getExtension(topic.getId().toString(), "topic.sampleData"),
TopicSampleData.class);
topic.setSampleData(sampleData);
setFieldsInternal(topic, Fields.EMPTY_FIELDS);

// Set the fields tags. Will be used to mask the sample data
if (!authorizePII) {
getFieldTags(true, topic.getMessageSchema().getSchemaFields());
topic.setTags(getTags(topic.getFullyQualifiedName()));
return PIIMasker.getSampleData(topic);
}

return topic;
}

@Transaction
Expand Down Expand Up @@ -249,6 +266,17 @@ public List<TagLabel> getAllTags(EntityInterface entity) {
return allTags;
}

public static Set<TagLabel> getAllFieldTags(Field field) {
Set<TagLabel> tags = new HashSet<>();
if (!listOrEmpty(field.getTags()).isEmpty()) {
tags.addAll(field.getTags());
}
for (Field c : listOrEmpty(field.getChildren())) {
tags.addAll(getAllFieldTags(c));
}
return tags;
}

public class TopicUpdater extends EntityUpdater {
public static final String FIELD_DATA_TYPE_DISPLAY = "dataTypeDisplay";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.mask.PIIMasker;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.security.policyevaluator.ResourceContext;
import org.openmetadata.service.util.EntityUtil.Fields;
Expand Down Expand Up @@ -553,8 +552,8 @@ public Table getSampleData(
authorizer.authorize(securityContext, operationContext, resourceContext);
boolean authorizePII = authorizer.authorizePII(securityContext, resourceContext.getOwner());

Table maskedTable = PIIMasker.getSampleData(repository.getSampleData(id, authorizePII), authorizePII);
return addHref(uriInfo, maskedTable);
Table table = repository.getSampleData(id, authorizePII);
return addHref(uriInfo, table);
}

@DELETE
Expand Down Expand Up @@ -673,7 +672,7 @@ public Table getLatestTableProfile(
authorizer.authorize(securityContext, operationContext, resourceContext);
boolean authorizePII = authorizer.authorizePII(securityContext, resourceContext.getOwner());

return PIIMasker.getTableProfile(repository.getLatestTableProfile(fqn, authorizePII), authorizePII);
return repository.getLatestTableProfile(fqn, authorizePII);
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.json.JsonPatch;
import javax.validation.Valid;
import javax.validation.constraints.Max;
Expand All @@ -39,12 +37,10 @@
import org.openmetadata.schema.api.data.RestoreEntity;
import org.openmetadata.schema.api.tests.CreateLogicalTestCases;
import org.openmetadata.schema.api.tests.CreateTestCase;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.tests.TestCase;
import org.openmetadata.schema.tests.TestSuite;
import org.openmetadata.schema.tests.type.TestCaseResult;
import org.openmetadata.schema.tests.type.TestSummary;
import org.openmetadata.schema.type.Column;
import org.openmetadata.schema.type.EntityHistory;
import org.openmetadata.schema.type.Include;
import org.openmetadata.schema.type.MetadataOperation;
Expand All @@ -55,7 +51,6 @@
import org.openmetadata.service.jdbi3.TestCaseRepository;
import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.resources.feeds.MessageParser;
import org.openmetadata.service.resources.feeds.MessageParser.EntityLink;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.mask.PIIMasker;
Expand Down Expand Up @@ -189,41 +184,7 @@ public ResultList<TestCase> list(
ResultList<TestCase> tests =
super.listInternal(
uriInfo, securityContext, fields, filter, limitParam, before, after, operationContext, resourceContext);

List<TestCase> maskedTests =
tests.getData().stream()
.map(
testCase -> {
try {
EntityLink testCaseLink = MessageParser.EntityLink.parse(testCase.getEntityLink());
Table table =
Entity.getEntityByName(
Entity.TABLE, testCaseLink.getEntityFQN(), "owner,tags", Include.NON_DELETED);

// Ignore table tests
if (testCaseLink.getFieldName() == null) return testCase;

Optional<Column> referencedColumn =
table.getColumns().stream()
.filter(
col -> testCaseLink.getFullyQualifiedFieldValue().equals(col.getFullyQualifiedName()))
.findFirst();

if (referencedColumn.isPresent()) {
Column col = referencedColumn.get();
// We need the table owner to know if we can authorize the access
boolean authorizePII = authorizer.authorizePII(securityContext, table.getOwner());
return PIIMasker.getTestCase(col, testCase, authorizePII);
}
return testCase;
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());

tests.setData(maskedTests);
return tests;
return PIIMasker.getTestCases(tests, authorizer, securityContext);
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.mask.PIIMasker;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.ResultList;
Expand Down Expand Up @@ -143,7 +144,9 @@ public ResultList<Query> listQueries(
if (!CommonUtil.nullOrEmpty(entityId)) {
filter.addQueryParam("entityId", entityId.toString());
}
return super.listInternal(uriInfo, securityContext, fieldsParam, filter, limitParam, before, after);
ResultList<Query> queries =
super.listInternal(uriInfo, securityContext, fieldsParam, filter, limitParam, before, after);
return PIIMasker.getQueries(queries, authorizer, securityContext);
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.security.policyevaluator.ResourceContext;
import org.openmetadata.service.util.ResultList;

@Path("/v1/topics")
Expand All @@ -76,7 +77,7 @@
@Collection(name = "topics")
public class TopicResource extends EntityResource<Topic, TopicRepository> {
public static final String COLLECTION_PATH = "v1/topics/";
static final String FIELDS = "owner,followers,tags,sampleData,extension";
static final String FIELDS = "owner,followers,tags,extension";

@Override
public Topic addHref(UriInfo uriInfo, Topic topic) {
Expand Down Expand Up @@ -347,6 +348,32 @@ public Topic addSampleData(
return addHref(uriInfo, topic);
}

@GET
@Path("/{id}/sampleData")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the GET sample data endpoint to follow the same approach as with tables. This helps us validate the permissions for masking sample data

@Operation(
operationId = "getSampleData",
summary = "Get sample data",
description = "Get sample data from the topic.",
responses = {
@ApiResponse(
responseCode = "200",
description = "Successfully obtained the Topic",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Topic.class)))
})
public Topic getSampleData(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the topic", schema = @Schema(type = "UUID")) @PathParam("id") UUID id)
throws IOException {
OperationContext operationContext = new OperationContext(entityType, MetadataOperation.VIEW_SAMPLE_DATA);
ResourceContext resourceContext = getResourceContextById(id);
authorizer.authorize(securityContext, operationContext, resourceContext);
boolean authorizePII = authorizer.authorizePII(securityContext, resourceContext.getOwner());

Topic topic = repository.getSampleData(id, authorizePII);
return addHref(uriInfo, topic);
}

@PUT
@Path("/{id}/followers")
@Operation(
Expand Down
Loading