From d94c5dee4ea536c7b7e87b9817676b844fb36291 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Tue, 6 May 2025 10:10:54 -0300 Subject: [PATCH 1/9] FEAT: init public routes to get data asset by public embed --- docsfera.json | 713 ++++++++---------- src/modules/auth/auth.controller.ts | 2 +- src/modules/catalog/catalog.module.ts | 7 +- src/modules/catalog/catalog.service.ts | 143 +++- .../{ => controllers}/catalog.controller.ts | 302 ++------ .../controllers/catalog.public.controller.ts | 184 +++++ .../connection-test.service.ts | 2 +- .../connection/connection.controller.ts | 2 +- src/modules/customers/customers.controller.ts | 2 +- src/modules/customers/customers.service.ts | 10 + .../network-policy.controller.ts | 2 +- src/modules/oauth/oauth.controller.ts | 2 +- src/modules/open-data/open-data.controller.ts | 2 +- .../pipelinesV2/pipelines.controller.ts | 2 +- src/modules/users/users.controller.ts | 2 +- ... PackTheMetadata.ts => PackTheMetadata.ts} | 0 16 files changed, 724 insertions(+), 653 deletions(-) rename src/modules/catalog/{ => controllers}/catalog.controller.ts (80%) create mode 100644 src/modules/catalog/controllers/catalog.public.controller.ts rename src/utils/{ PackTheMetadata.ts => PackTheMetadata.ts} (100%) diff --git a/docsfera.json b/docsfera.json index b8d9671..2fd799a 100644 --- a/docsfera.json +++ b/docsfera.json @@ -3873,9 +3873,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -3906,11 +3903,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - } ] } }, @@ -3947,9 +3939,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -3981,7 +3970,7 @@ } }, { - "name": "shared", + "name": "c", "required": true, "in": "query", "schema": { @@ -3996,14 +3985,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } ] }, "put": { @@ -4056,9 +4037,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4104,9 +4082,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4147,9 +4122,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4179,6 +4151,14 @@ "schema": { "type": "string" } + }, + { + "name": "c", + "required": true, + "in": "query", + "schema": { + "type": "string" + } } ], "responses": { @@ -4195,14 +4175,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } ] } }, @@ -4229,6 +4201,14 @@ "schema": { "type": "string" } + }, + { + "name": "c", + "required": true, + "in": "query", + "schema": { + "type": "string" + } } ], "responses": { @@ -4245,14 +4225,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } ] } }, @@ -4279,6 +4251,14 @@ "schema": { "type": "string" } + }, + { + "name": "c", + "required": true, + "in": "query", + "schema": { + "type": "string" + } } ], "responses": { @@ -4295,14 +4275,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } ] }, "post": { @@ -4345,9 +4317,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4386,11 +4355,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - } ] } }, @@ -4426,11 +4390,6 @@ }, "tags": [ "Catalog" - ], - "security": [ - { - "access-token": [] - } ] } }, @@ -4485,9 +4444,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4543,9 +4499,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4595,9 +4548,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4645,9 +4595,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4687,9 +4634,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4730,9 +4674,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4779,9 +4720,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4829,9 +4767,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4877,9 +4812,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4919,9 +4851,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4954,9 +4883,6 @@ "Catalog" ], "security": [ - { - "access-token": [] - }, { "access-token": [] } @@ -4996,6 +4922,238 @@ "tags": [ "Catalog" ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/{id}/mfa": { + "post": { + "operationId": "CustomersController_enableMfaEnforce", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/EnforceMfa" + } + } + } + }, + "responses": { + "201": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/{id}/links": { + "get": { + "operationId": "CustomersController_getCustomerLinks", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerLinksResponse" + } + } + } + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + } + ] + }, + "put": { + "operationId": "CustomersController_setCustomerLinks", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CustomerLinkRequest" + } + } + } + }, + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/token": { + "get": { + "operationId": "CustomersController_getCustomerToken", + "parameters": [ + { + "name": "exp", + "required": true, + "in": "query", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "text/plain": { + "schema": { + "type": "string" + } + } + } + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/monitoring-dashboard": { + "get": { + "operationId": "CustomersController_getCustomerMonitoringDashboard", + "parameters": [], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/logs-dashboard": { + "get": { + "operationId": "CustomersController_getCustomerMixPanelLogsDashboard", + "parameters": [], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Customers" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/customers/access-dashboard": { + "get": { + "operationId": "CustomersController_getAccessDashboard", + "parameters": [], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Customers" + ], "security": [ { "access-token": [] @@ -5519,241 +5677,6 @@ ] } }, - "/customers/{id}/mfa": { - "post": { - "operationId": "CustomersController_enableMfaEnforce", - "parameters": [ - { - "name": "id", - "required": true, - "in": "path", - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/EnforceMfa" - } - } - } - }, - "responses": { - "201": { - "description": "", - "content": { - "application/json": { - "schema": { - "type": "object" - } - } - } - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, - "/customers/{id}/links": { - "get": { - "operationId": "CustomersController_getCustomerLinks", - "parameters": [ - { - "name": "id", - "required": true, - "in": "path", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CustomerLinksResponse" - } - } - } - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - } - ] - }, - "put": { - "operationId": "CustomersController_setCustomerLinks", - "parameters": [ - { - "name": "id", - "required": true, - "in": "path", - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CustomerLinkRequest" - } - } - } - }, - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, - "/customers/token": { - "get": { - "operationId": "CustomersController_getCustomerToken", - "parameters": [ - { - "name": "exp", - "required": true, - "in": "query", - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "", - "content": { - "text/plain": { - "schema": { - "type": "string" - } - } - } - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, - "/customers/monitoring-dashboard": { - "get": { - "operationId": "CustomersController_getCustomerMonitoringDashboard", - "parameters": [], - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, - "/customers/logs-dashboard": { - "get": { - "operationId": "CustomersController_getCustomerMixPanelLogsDashboard", - "parameters": [], - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, - "/customers/access-dashboard": { - "get": { - "operationId": "CustomersController_getAccessDashboard", - "parameters": [], - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "Customers" - ], - "security": [ - { - "access-token": [] - }, - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, "/open-data/sharing-ocean-data": { "post": { "operationId": "OpenDataController_createUser", @@ -8435,6 +8358,67 @@ "updated_by" ] }, + "EnforceMfa": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + } + }, + "required": [ + "enabled" + ] + }, + "CustomerLink": { + "type": "object", + "properties": { + "href": { + "type": "string" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "iconSrc": { + "type": "string" + } + }, + "required": [ + "href", + "name", + "description" + ] + }, + "CustomerLinksResponse": { + "type": "object", + "properties": { + "links": { + "type": "array", + "items": { + "$ref": "#/components/schemas/CustomerLink" + } + } + }, + "required": [ + "links" + ] + }, + "CustomerLinkRequest": { + "type": "object", + "properties": { + "links": { + "type": "array", + "items": { + "$ref": "#/components/schemas/CustomerLink" + } + } + }, + "required": [ + "links" + ] + }, "ConnectionTestPingReq": { "type": "object", "properties": { @@ -8729,67 +8713,6 @@ "tags" ] }, - "EnforceMfa": { - "type": "object", - "properties": { - "enabled": { - "type": "boolean" - } - }, - "required": [ - "enabled" - ] - }, - "CustomerLink": { - "type": "object", - "properties": { - "href": { - "type": "string" - }, - "name": { - "type": "string" - }, - "description": { - "type": "string" - }, - "iconSrc": { - "type": "string" - } - }, - "required": [ - "href", - "name", - "description" - ] - }, - "CustomerLinksResponse": { - "type": "object", - "properties": { - "links": { - "type": "array", - "items": { - "$ref": "#/components/schemas/CustomerLink" - } - } - }, - "required": [ - "links" - ] - }, - "CustomerLinkRequest": { - "type": "object", - "properties": { - "links": { - "type": "array", - "items": { - "$ref": "#/components/schemas/CustomerLink" - } - } - }, - "required": [ - "links" - ] - }, "CustomerThemeRequest": { "type": "object", "properties": { diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 226036a..9489563 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -45,7 +45,7 @@ import { AuthSignInRes, BulkEditRequest, } from './dtos/login'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { AuthGuard } from '@nestjs/passport'; import { Request } from 'express'; import ErrorCodes, { OauthErrors } from 'src/utils/errorCodes'; diff --git a/src/modules/catalog/catalog.module.ts b/src/modules/catalog/catalog.module.ts index 45cc06e..d990344 100644 --- a/src/modules/catalog/catalog.module.ts +++ b/src/modules/catalog/catalog.module.ts @@ -2,13 +2,15 @@ import { Module } from '@nestjs/common'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; -import { CatalogController } from './catalog.controller'; +import { CatalogController } from './controllers/catalog.controller'; import { CatalogService } from './catalog.service'; import { CatalogClientConfiguration } from './catalog-client'; import { ClientsModule } from '@nestjs/microservices'; import { PipelinesModule as OldPipelineModule } from 'src/modules/pipelines/pipelines.module'; import { UsersModule } from '../users/users.module'; import { RolesModule } from '../roles/roles.module'; +import { CustomersModule } from '../customers/customers.module'; +import { CatalogPublicController } from './controllers/catalog.public.controller'; const client = new CatalogClientConfiguration(); @@ -18,8 +20,9 @@ const client = new CatalogClientConfiguration(); OldPipelineModule, UsersModule, RolesModule, + CustomersModule ], - controllers: [CatalogController], + controllers: [CatalogController, CatalogPublicController], providers: [CatalogService, DadosferaLogger], exports: [CatalogService], }) diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.ts index a3db9cc..f7a2ed0 100644 --- a/src/modules/catalog/catalog.service.ts +++ b/src/modules/catalog/catalog.service.ts @@ -12,9 +12,11 @@ import { } from '@dadosfera/protospack-v2/dist/lib/PlatformInterfaces'; import { BadRequestException, + ForbiddenException, HttpException, HttpStatus, Inject, + NotFoundException, OnModuleInit, } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; @@ -43,6 +45,11 @@ import { CsvParser } from 'src/utils/FileParser/csv-parser'; import { PDFParser } from 'src/utils/FileParser/pdf-parser'; import { TypeParser } from 'src/utils/FileParser/parser-types'; import { ParserBuilder } from 'src/utils/FileParser/parser.builder'; +import { RequestUser } from 'src/decorators/user.decorator'; +import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; +import { CustomersService } from '../customers/customers.service'; +import { LanguageEnum } from 'src/utils/languages.enum'; class CatalogService implements OnModuleInit { catalogReadService: ReadService.CatalogReadServices; @@ -56,6 +63,7 @@ class CatalogService implements OnModuleInit { private readonly grpcClient: ClientGrpc, private readonly userService: UsersService, private readonly roleService: RolesService, + private readonly customerService: CustomersService ) { this.logger = dadosferaLogger.logger; } @@ -240,6 +248,77 @@ class CatalogService implements OnModuleInit { return { data_assets: response, total }; } + async getOneDataAssetPrivate(user: RequestUser, id: string) { + const { username, user_id, customer_id, customer_name, permissions } = user; + + this.logger.info("getOneDataAssetPrivate: " + user_id + " - " + id) + + this.logger.info(`data: `, { + username, + customer_name, + }); + + const is_data_manager = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + + const is_get_catalog = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.GET.seqid, + ); + let has_permission = false; + const metadata = PackTheMetadata({ + username, + user_id, + customer_id, + customer_name, + }); + + const user_roles = await this.getUserRolesIds(user_id); + + const { data_asset } = await this.getOneDataAsset({ + customer_id, + id, + metadata, + }); + + has_permission = + is_data_manager || + is_get_catalog || + data_asset?.owner === username || + (user_roles as Array).some((r) => + data_asset.p_roles.includes(r), + ) || + data_asset.p_users.includes(user_id); + + + if (!has_permission) + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + delete data_asset.p_roles; + delete data_asset.p_users; + + return { data_asset }; + } + + async getOneDataAssetPublic(customerId: string, id: string) { + this.logger.info("getOneDataAssetPublic: " + customerId + " - " + id) + const metadata = await this.createMetadataToPublicAccess(customerId); + const { data_asset } = await this.getOneDataAsset({ + customer_id: customerId, + id, + metadata, + }); + this.logger.info("found asset: " + JSON.stringify(data_asset)) + delete data_asset.p_roles; + delete data_asset.p_users; + data_asset.share_type = 'public'; + if (data_asset.share_type !== 'public') + throw new NotFoundException(); + + return { data_asset }; + } + async getOneDataAsset(data: { id: string; customer_id: string; @@ -479,27 +558,26 @@ class CatalogService implements OnModuleInit { async createTableMetadata(body: any): Promise { const nimbusUrl = this._getNimbusUrl(body); - this.logger.info(`Nimbus URL: ${nimbusUrl}`, {...body.logMetadata}); + this.logger.info(`Nimbus URL: ${nimbusUrl}`, { ...body.logMetadata }); const endpoint = `${nimbusUrl}/api/catalog/table-metadata/`; - this.logger.info(`Creating table metadata for table ${body.table_metadata.table_name}`, {...body.logMetadata}); - this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); - this.logger.debug(`Payload: ${JSON.stringify(body.table_metadata)}`, {...body.logMetadata}); + this.logger.info(`Creating table metadata for table ${body.table_metadata.table_name}`, { ...body.logMetadata }); + this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); + this.logger.debug(`Payload: ${JSON.stringify(body.table_metadata)}`, { ...body.logMetadata }); try { - const { data, status } = await axios.post(endpoint, {...body.table_metadata}); + const { data, status } = await axios.post(endpoint, { ...body.table_metadata }); this.logger.info( `Table metadata created successfully with status ${status} for table ${body.table_metadata.table_name}`, - {...body.logMetadata}, + { ...body.logMetadata }, ); return data.id; } catch (error) { this.logger.error( - `Failed to create table metadata for table ${body.table_metadata.table_name} failed with status ${ - error.response?.status - } because of ${JSON.stringify(error.response?.data) || error.message}`, {...body.logMetadata}); + `Failed to create table metadata for table ${body.table_metadata.table_name} failed with status ${error.response?.status + } because of ${JSON.stringify(error.response?.data) || error.message}`, { ...body.logMetadata }); throw new Error(error.response?.data?.message || error.message); } } @@ -509,51 +587,49 @@ class CatalogService implements OnModuleInit { this.logger.info(`Nimbus URL: ${nimbusUrl}`, body.logMetadata); const endpoint = `${nimbusUrl}/api/catalog/column-metadata/`; - + try { - this.logger.info(`Creating column metadata for table ${body.column_metadata.table_name}`, {...body.logMetadata}); - this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); - this.logger.debug(`Payload: ${JSON.stringify(body.column_metadata)}`, {...body.logMetadata}); + this.logger.info(`Creating column metadata for table ${body.column_metadata.table_name}`, { ...body.logMetadata }); + this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); + this.logger.debug(`Payload: ${JSON.stringify(body.column_metadata)}`, { ...body.logMetadata }); const { data, status } = await axios.post(endpoint, body.column_metadata); this.logger.info( `Column metadata created successfully with status ${status} for table ${body.column_metadata.table_name}`, - {...body.logMetadata}, + { ...body.logMetadata }, ); return data.map((column) => column.id); } catch (error) { this.logger.error( - `Failed to create column metadata failed with status for table ${body.column_metadata.table_name} ${ - error.response?.status - } because of ${error.response?.data || error.message}`, {...body.logMetadata}); + `Failed to create column metadata failed with status for table ${body.column_metadata.table_name} ${error.response?.status + } because of ${error.response?.data || error.message}`, { ...body.logMetadata }); throw new Error(error.response?.data?.message || error.message); } } async createDataPreview(body: any): Promise { const nimbusUrl = this._getNimbusUrl(body); - this.logger.info(`Nimbus URL: ${nimbusUrl}`, {...body.logMetadata}); + this.logger.info(`Nimbus URL: ${nimbusUrl}`, { ...body.logMetadata }); const endpoint = `${nimbusUrl}/api/catalog/data-preview/`; - this.logger.info(`Creating data preview for table ${body.data_preview.table_name}`, {...body.logMetadata}); - this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); - this.logger.debug(`Payload: ${JSON.stringify(body.data_preview)}`, {...body.logMetadata}); + this.logger.info(`Creating data preview for table ${body.data_preview.table_name}`, { ...body.logMetadata }); + this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); + this.logger.debug(`Payload: ${JSON.stringify(body.data_preview)}`, { ...body.logMetadata }); try { const { data, status } = await axios.post(endpoint, body.data_preview); this.logger.info( `Data preview created successfully with status ${status} for table ${body.data_preview.table_name}`, - {...body.logMetadata}, + { ...body.logMetadata }, ); return data.id; } catch (error) { this.logger.error( - `Failed to create data preview for table ${body.data_preview.table_name} failed with status ${ - error.response?.status + `Failed to create data preview for table ${body.data_preview.table_name} failed with status ${error.response?.status } because of ${error.response?.data || error.message}`, - {...body.logMetadata}, + { ...body.logMetadata }, ); throw new Error(error.response?.data?.message || error.message); } @@ -582,6 +658,23 @@ class CatalogService implements OnModuleInit { ); return res; } + + async createMetadataToPublicAccess(customerId?: string, language?: LanguageEnum) { + if (!customerId) { + throw new NotFoundException(); + } + + const { + customer + } = await this.customerService.getCustomer(customerId); + + return PackTheMetadata({ + customer_id: customer.id, + customer_name: customer.name, + language, + }) + } + } export { CatalogService }; diff --git a/src/modules/catalog/catalog.controller.ts b/src/modules/catalog/controllers/catalog.controller.ts similarity index 80% rename from src/modules/catalog/catalog.controller.ts rename to src/modules/catalog/controllers/catalog.controller.ts index 09daa16..ca5d88a 100644 --- a/src/modules/catalog/catalog.controller.ts +++ b/src/modules/catalog/controllers/catalog.controller.ts @@ -28,11 +28,14 @@ import { RequireAllPermissions, RequireModule, RequireSomePermission, -} from '../../decorators/authentication.decorator'; -import { DADOSFERA_MODULES_KEYS, PERMISSIONS_GROUPS } from '../../authentication/permissions.enum'; -import { CatalogService } from './catalog.service'; +} from '../../../decorators/authentication.decorator'; +import { + DADOSFERA_MODULES_KEYS, + PERMISSIONS_GROUPS, +} from '../../../authentication/permissions.enum'; +import { CatalogService } from '../catalog.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { RequestUser, User } from 'src/decorators/user.decorator'; import { BatchRemoveRlsRulesRequest, @@ -49,7 +52,7 @@ import { IUpdateDataRequest, TriggerCatalogReq, TriggerCatalogRes, -} from './dtos'; +} from '../dtos'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { Language } from 'src/decorators/language.decorator'; import { LanguageEnum } from 'src/utils/languages.enum'; @@ -195,209 +198,6 @@ export class CatalogController { return res; } - @Get('data-asset/:id') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAsset( - @User() user: RequestUser, - @Param('id') id: string, - @Query('shared') shared?: 'true', - ) { - const { username, user_id, customer_id, customer_name, permissions } = user; - - this.logger.info(`GET /data-asset/${id}`, { - username, - customer_name, - }); - - const is_data_manager = permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, - ); - let has_permission = false; - const metadata = PackTheMetadata({ - username, - user_id, - customer_id, - customer_name, - }); - - const user_roles = await this.catalogService.getUserRolesIds(user_id); - - const { data_asset } = await this.catalogService.getOneDataAsset({ - customer_id, - id, - metadata, - }); - has_permission = - is_data_manager || - data_asset?.owner === username || - (user_roles as Array).some((r) => data_asset.p_roles.includes(r)) || - data_asset.p_users.includes(user_id); - if ( - shared === 'true' && - (data_asset.share_type === undefined || data_asset.share_type === 'none') - ) - throw new NotFoundException(); - if (!has_permission) - throw new ForbiddenException( - 'You do not have permission to access this data asset.', - ); - delete data_asset.p_roles; - delete data_asset.p_users; - - return { data_asset }; - } - - @ApiInternalOnlyEndpoint() - @Get('data-asset/rls/:id') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAssetRls( - @User() user: RequestUser, - @Headers() headers, - @Param('id') id: string, - ) { - const { username, user_id, customer_id, customer_name, permissions } = user; - - this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { - username, - customer_name, - }); - - const is_data_manager = permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, - ); - let has_permission = false; - const metadata = PackTheMetadata({ - username, - user_id, - customer_id, - customer_name, - }); - - const user_roles = await this.catalogService.getUserRolesIds(user_id); - - const { data_asset } = await this.catalogService.getOneDataAsset({ - customer_id, - id, - metadata, - }); - - if (data_asset?.owner === username) has_permission = true; - - for (const role of user_roles) { - if (data_asset.p_roles.includes(role)) has_permission = true; - } - - if (data_asset.p_users.includes(user_id)) has_permission = true; - - if (is_data_manager || has_permission) { - delete data_asset.p_roles; - delete data_asset.p_users; - return { data_asset }; - } - - throw new ForbiddenException( - 'You do not have permission to access this data asset.', - ); - } - - @Get('data-asset/:id/columns-metadata') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAssetColumnsMetadata( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - ): Promise { - const { customer_name, customer_id, user_id, username } = user; - - this.logger.info(`/catalog - columns-metadata`, { - user_id, - customer_name, - }); - - const metadata = PackTheMetadata({ - customer_name, - customer_id, - user_id, - username, - language, - }); - - const columns_metadata = - await this.catalogService.getDatasetColumnsMetadata(id, metadata); - - return { columns_metadata }; - } - - @Get('data-asset/:id/preview') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAssetPreview( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - ): Promise { - const { customer_name, customer_id, user_id, username, customer_modules } = user; - - this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { - user_id, - customer_name, - }); - - const metadata = PackTheMetadata({ - customer_name, - customer_id, - user_id, - username, - language, - is_mask: customer_modules.some(mod => mod === 'pii') - }); - - const preview = await this.catalogService.getDatasetPreview(id, metadata); - - return { preview }; - } - - @Get('data-asset/:id/docs') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAssetDocs( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - ): Promise { - const { customer_name, customer_id, user_id, username } = user; - - this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { - user_id, - customer_name, - }); - - const metadata = PackTheMetadata({ - customer_name, - customer_id, - user_id, - username, - language, - }); - - const docs = await this.catalogService.getDataDocs(id, metadata); - - return { docs }; - } - @Put('data-asset/:id') @RequireSomePermission( PERMISSIONS_GROUPS.CATALOG.permissions.UPDATE, @@ -648,6 +448,62 @@ export class CatalogController { return response; } + @ApiInternalOnlyEndpoint() + @Get('data-asset/rls/:id') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAssetRls( + @User() user: RequestUser, + @Headers() headers, + @Param('id') id: string, + ) { + const { username, user_id, customer_id, customer_name, permissions } = user; + + this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { + username, + customer_name, + }); + + const is_data_manager = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + let has_permission = false; + const metadata = PackTheMetadata({ + username, + user_id, + customer_id, + customer_name, + }); + + const user_roles = await this.catalogService.getUserRolesIds(user_id); + + const { data_asset } = await this.catalogService.getOneDataAsset({ + customer_id, + id, + metadata, + }); + + if (data_asset?.owner === username) has_permission = true; + + for (const role of user_roles) { + if (data_asset.p_roles.includes(role)) has_permission = true; + } + + if (data_asset.p_users.includes(user_id)) has_permission = true; + + if (is_data_manager || has_permission) { + delete data_asset.p_roles; + delete data_asset.p_users; + return { data_asset }; + } + + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + @Post('rls-rule') @RequireAllPermissions(PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER) async addRlsRule(@User() user: RequestUser, @Body() body: AddRlsRuleRequest) { @@ -859,29 +715,31 @@ export class CatalogController { } @Get('pii-reporter') - @RequireSomePermission( - PERMISSIONS_GROUPS.USERS.permissions.ADMIN - ) - @RequireModule( - DADOSFERA_MODULES_KEYS.PII - ) - async getPiiReporter(@User() user: RequestUser, @Res() res: Response, @Query('type') contentType: TypeParser = "pdf") { + @RequireSomePermission(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) + @RequireModule(DADOSFERA_MODULES_KEYS.PII) + async getPiiReporter( + @User() user: RequestUser, + @Res() res: Response, + @Query('type') contentType: TypeParser = 'pdf', + ) { this.logger.info('GET pii-reporter'); const metadata = PackTheMetadata(user); try { - const { - file, - filename, - type - } = await this.catalogService.getPiiReporter(metadata, contentType); - res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + const { file, filename, type } = await this.catalogService.getPiiReporter( + metadata, + contentType, + ); + res.setHeader( + 'Content-Disposition', + `attachment; filename="${filename}"`, + ); res.setHeader('Content-Type', type); // use res.end to send buffer return res.end(file); } catch (error) { - console.error(error) + console.error(error); this.logger.error(error.message); } } diff --git a/src/modules/catalog/controllers/catalog.public.controller.ts b/src/modules/catalog/controllers/catalog.public.controller.ts new file mode 100644 index 0000000..6d3be81 --- /dev/null +++ b/src/modules/catalog/controllers/catalog.public.controller.ts @@ -0,0 +1,184 @@ +import { + Controller, + ForbiddenException, + Get, + Headers, + Inject, + Param, + Query, + UseFilters, +} from '@nestjs/common'; +import { + ApiHeaders, + ApiTags, +} from '@nestjs/swagger'; +import { + RequireSomePermission, +} from '../../../decorators/authentication.decorator'; +import { + PERMISSIONS_GROUPS, +} from '../../../authentication/permissions.enum'; +import { CatalogService } from '../catalog.service'; +import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; +import { RequestUser, User } from 'src/decorators/user.decorator'; +import { + IColumnsMetadataResponse, + IDocsResponse, + IPreviewResponse, + +} from '../dtos'; +import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; +import { Language } from 'src/decorators/language.decorator'; +import { LanguageEnum } from 'src/utils/languages.enum'; +import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; + + +@ApiTags('Catalog') +@ApiHeaders([{ name: 'dadosfera-lang', enum: LanguageEnum, required: false }]) +@Controller('catalog') +@UseFilters(new GrpcToHttpExceptionFilter()) +export class CatalogPublicController { + logger: DadosferaLogger; + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + private catalogService: CatalogService, + ) { + this.logger = dadosferaLogger.logger; + } + + private async createAccessToPublicAssets( + user?: RequestUser, + customerId?: string, + language?: LanguageEnum, + ) { + if (user) { + const { customer_name, customer_id, user_id, username } = user; + return PackTheMetadata({ + customer_name, + customer_id, + user_id, + username, + language, + }); + } else { + return await this.catalogService.createMetadataToPublicAccess( + customerId, + language, + ); + } + } + + private checkDefaultPermission(user: RequestUser) { + const is_data_manager = user.permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + + const is_get = user.permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.GET.seqid, + ); + + if (is_data_manager || is_get) { + return; + } + + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + + @Get('data-asset/:id') + async getDataAsset( + @User() user: RequestUser, + @Param('id') id: string, + @Query('c') customerId?: string, + ) { + this.logger.info(`GET /data-asset/${id}`); + try { + if (user) { + return await this.catalogService.getOneDataAssetPrivate(user, id); + } else { + return await this.catalogService.getOneDataAssetPublic(customerId, id); + } + } catch (error) { + this.logger.error(error.message); + throw error; + } + } + + + + @Get('data-asset/:id/columns-metadata') + async getDataAssetColumnsMetadata( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + @Query('c') customerId: string, + ): Promise { + const metadata = await this.createAccessToPublicAssets( + user, + customerId, + language, + ); + this.logger.info(`/catalog - columns-metadata`); + + const columns_metadata = + await this.catalogService.getDatasetColumnsMetadata(id, metadata); + + if (user) { + this.checkDefaultPermission(user); + } + + return { columns_metadata }; + } + + @Get('data-asset/:id/preview') + async getDataAssetPreview( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + @Query('c') customerId: string, + ): Promise { + this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`); + + const metadata = await this.createAccessToPublicAssets( + user, + customerId, + language, + ); + + const preview = await this.catalogService.getDatasetPreview(id, metadata); + + if (user) { + this.checkDefaultPermission(user); + } + + return { preview }; + } + + @Get('data-asset/:id/docs') + async getDataAssetDocs( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + @Query('c') customerId: string, + ): Promise { + this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`); + + const metadata = await this.createAccessToPublicAssets( + user, + customerId, + language, + ); + + const docs = await this.catalogService.getDataDocs(id, metadata); + + if (user) { + this.checkDefaultPermission(user); + } + + return { docs }; + } + +} diff --git a/src/modules/connection-test/connection-test.service.ts b/src/modules/connection-test/connection-test.service.ts index 310555a..3b92e12 100644 --- a/src/modules/connection-test/connection-test.service.ts +++ b/src/modules/connection-test/connection-test.service.ts @@ -20,7 +20,7 @@ import { DatabaseConnectionPropertiesDto, } from '../connection/dtos/connection'; import { RequestUser } from 'src/decorators/user.decorator'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; @Injectable() export class ConnectionTestService { diff --git a/src/modules/connection/connection.controller.ts b/src/modules/connection/connection.controller.ts index 7c4275a..194e9aa 100644 --- a/src/modules/connection/connection.controller.ts +++ b/src/modules/connection/connection.controller.ts @@ -28,7 +28,7 @@ import { UpdateConnectionDto, } from './dtos/connection'; import { CreateConnectionDto } from './dtos/connection'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { Language } from 'src/decorators/language.decorator'; import { LanguageEnum } from 'src/utils/languages.enum'; diff --git a/src/modules/customers/customers.controller.ts b/src/modules/customers/customers.controller.ts index bcab557..2606ebd 100644 --- a/src/modules/customers/customers.controller.ts +++ b/src/modules/customers/customers.controller.ts @@ -25,7 +25,7 @@ import { CustomersService } from './customers.service'; import { CustomerLinkRequest, CustomerLinksResponse } from './dtos/customers'; import { RequestUser, User } from 'src/decorators/user.decorator'; import type { StringValue } from 'ms'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { EnforceMfa } from './dtos/enforce-mfa'; @ApiTags('Customers') diff --git a/src/modules/customers/customers.service.ts b/src/modules/customers/customers.service.ts index 056c5fb..8b28171 100644 --- a/src/modules/customers/customers.service.ts +++ b/src/modules/customers/customers.service.ts @@ -59,6 +59,14 @@ export class CustomersService implements OnModuleInit { ); } + async getCustomer(customerId: string) { + return await lastValueFrom( + this.customerService.CustomerFindOneById({ + id: customerId + }) + ) + } + async getLinks(customerId: string) { try { const result = await lastValueFrom( @@ -141,6 +149,7 @@ export class CustomersService implements OnModuleInit { // const decoded = jwt.decode(jwt_token, { complete: true }); return jwt_token; } + async getMonitoringDashboardUrl(metadata: Metadata) { logger.info('CustomersService - getMonitoringDashboardUrl'); @@ -158,6 +167,7 @@ export class CustomersService implements OnModuleInit { return res; } + async getLogsDashboardUrl(metadata: Metadata) { logger.info('CustomersService - getMixPanelLogsDashboardUrl'); diff --git a/src/modules/network-policy/network-policy.controller.ts b/src/modules/network-policy/network-policy.controller.ts index 8ae8371..f45ffdd 100644 --- a/src/modules/network-policy/network-policy.controller.ts +++ b/src/modules/network-policy/network-policy.controller.ts @@ -8,7 +8,7 @@ import { import { NetworkPoliciesDTO } from './dto/network-policy.dto'; import { RequestUser, User } from 'src/decorators/user.decorator'; import { NetworkPolicyService } from './network-policy.service'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; @Controller('network-policy') export class NetworkPolicyController { diff --git a/src/modules/oauth/oauth.controller.ts b/src/modules/oauth/oauth.controller.ts index 1e87866..e1af4c9 100644 --- a/src/modules/oauth/oauth.controller.ts +++ b/src/modules/oauth/oauth.controller.ts @@ -4,7 +4,7 @@ import { AuthGuard } from '@nestjs/passport'; import { ConnectionClientService } from '../connection/client.service'; import jwt from 'jsonwebtoken'; import DadosferaLogger from '@dadosfera/dadosfera-logs/dist'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; @ApiTags('oauth') @Controller('oauth') diff --git a/src/modules/open-data/open-data.controller.ts b/src/modules/open-data/open-data.controller.ts index b8951ed..a2fa72a 100644 --- a/src/modules/open-data/open-data.controller.ts +++ b/src/modules/open-data/open-data.controller.ts @@ -9,7 +9,7 @@ import { Language } from 'src/decorators/language.decorator'; import { OpenDataService } from './open-data.service'; import { CreateUserOpenDataDTO, WordpressForm } from './dto/wordpres-form'; import { Metadata } from '@grpc/grpc-js'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { request } from 'http'; import { Request } from 'express'; diff --git a/src/modules/pipelinesV2/pipelines.controller.ts b/src/modules/pipelinesV2/pipelines.controller.ts index 512a705..6986f25 100644 --- a/src/modules/pipelinesV2/pipelines.controller.ts +++ b/src/modules/pipelinesV2/pipelines.controller.ts @@ -32,7 +32,7 @@ import { PipelinesService } from './pipelines.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { Messages } from '@dadosfera/protospack-v2/dist/lib/PipelineV2'; import { RequestUser, User } from 'src/decorators/user.decorator'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { PipelinesService as OldPipelineService } from 'src/modules/pipelines/pipelines.service'; import { diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 9e00f7c..5fdc603 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -29,7 +29,7 @@ import { Language } from 'src/decorators/language.decorator'; import { ApiInternalOnlyController } from 'src/decorators/swagger.decorator'; import { RequestUser, User } from 'src/decorators/user.decorator'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; -import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import ErrorBuilder from 'src/utils/ErrorBuilder'; import ErrorCodes from 'src/utils/errorCodes'; import { LanguageEnum } from 'src/utils/languages.enum'; diff --git a/src/utils/ PackTheMetadata.ts b/src/utils/PackTheMetadata.ts similarity index 100% rename from src/utils/ PackTheMetadata.ts rename to src/utils/PackTheMetadata.ts From 62d0e8b041b9926644f3c54f09f5b2d386cb93a1 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 12 May 2025 12:12:58 -0300 Subject: [PATCH 2/9] FEAT: assign pubic key endpoint --- src/modules/assign/assign.controller.ts | 24 +++++++++++++ src/modules/assign/assign.module.ts | 15 +++++++++ src/modules/assign/assign.service.ts | 37 +++++++++++++++++++++ src/modules/assign/dto/create-assign.dto.ts | 3 ++ 4 files changed, 79 insertions(+) create mode 100644 src/modules/assign/assign.controller.ts create mode 100644 src/modules/assign/assign.module.ts create mode 100644 src/modules/assign/assign.service.ts create mode 100644 src/modules/assign/dto/create-assign.dto.ts diff --git a/src/modules/assign/assign.controller.ts b/src/modules/assign/assign.controller.ts new file mode 100644 index 0000000..e83488c --- /dev/null +++ b/src/modules/assign/assign.controller.ts @@ -0,0 +1,24 @@ +import { Controller, Post, Body, Put, Get} from '@nestjs/common'; +import { AssignService } from './assign.service'; +import { CreateAssignDto } from './dto/create-assign.dto'; +import { Authenticated, RequireModule, RequireSomePermission } from 'src/decorators/authentication.decorator'; +import { RequestUser, User } from 'src/decorators/user.decorator'; +import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; + +@Controller('assign') +@Authenticated() +export class AssignController { + constructor(private readonly assignService: AssignService) {} + + @Put('/public-key') + @RequireSomePermission( + PERMISSIONS_GROUPS.USERS.permissions.ADMIN + ) + @RequireModule('public-assign-embed') + create(@Body() createAssignDto: CreateAssignDto, @User() user: RequestUser) { + const metadata = PackTheMetadata(user); + return this.assignService.create(createAssignDto, metadata); + } + +} diff --git a/src/modules/assign/assign.module.ts b/src/modules/assign/assign.module.ts new file mode 100644 index 0000000..3b0fb09 --- /dev/null +++ b/src/modules/assign/assign.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { AssignService } from './assign.service'; +import { AssignController } from './assign.controller'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; +import { ClientsModule } from '@nestjs/microservices'; +import { DucClient } from '../duc/client.config'; + +const client = new DucClient(); + +@Module({ + imports: [ClientsModule.register([client.providerOptions])], + controllers: [AssignController], + providers: [AssignService, DadosferaLogger] +}) +export class AssignModule {} diff --git a/src/modules/assign/assign.service.ts b/src/modules/assign/assign.service.ts new file mode 100644 index 0000000..b48ef80 --- /dev/null +++ b/src/modules/assign/assign.service.ts @@ -0,0 +1,37 @@ +import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; +import { CreateAssignDto } from './dto/create-assign.dto'; +import { Metadata } from '@grpc/grpc-js'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; +import { ClientGrpc } from '@nestjs/microservices'; +import { DucClient } from 'src/modules/duc/client.config'; +import { ProtoServices } from '@dadosfera/protospack-v2/dist/lib/Duc'; +import { lastValueFrom } from 'rxjs';import { AssingProtoService } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/write-service'; + +@Injectable() +export class AssignService implements OnModuleInit { + + ducService: AssingProtoService; + logger: DadosferaLogger; + + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + @Inject(DucClient.name) private readonly grpcClient: ClientGrpc, + ) { + this.logger = dadosferaLogger.logger; + } + + onModuleInit() { + this.ducService =this.grpcClient.getService( + ProtoServices.AssingProtoService, + ); + } + + async create(createAssignDto: CreateAssignDto, metadata: Metadata) { + return await lastValueFrom(this.ducService.CreateOrUpdateAssignPublicKey(createAssignDto, metadata)) + } + + async get(createAssignDto: CreateAssignDto, metadata: Metadata) { + return await lastValueFrom(this.ducService.GetAssignPublicKey(createAssignDto, metadata)) + } +} diff --git a/src/modules/assign/dto/create-assign.dto.ts b/src/modules/assign/dto/create-assign.dto.ts new file mode 100644 index 0000000..67b784e --- /dev/null +++ b/src/modules/assign/dto/create-assign.dto.ts @@ -0,0 +1,3 @@ +export class CreateAssignDto { + publicKey: string; +} From 10580dfa033fb3598bf49bc5fd379b6102ee6ce6 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 12 May 2025 12:13:39 -0300 Subject: [PATCH 3/9] FEAT: share endpoint --- src/modules/catalog/share/share.controller.ts | 87 +++++++ src/modules/catalog/share/share.module.ts | 26 ++ src/modules/catalog/share/share.service.ts | 231 ++++++++++++++++++ .../dto/create-share-metadata.dto.ts | 9 + .../share-metadata.controller.ts | 31 +++ .../share-metadata/share-metadata.module.ts | 16 ++ .../share-metadata/share-metadata.service.ts | 61 +++++ 7 files changed, 461 insertions(+) create mode 100644 src/modules/catalog/share/share.controller.ts create mode 100644 src/modules/catalog/share/share.module.ts create mode 100644 src/modules/catalog/share/share.service.ts create mode 100644 src/modules/share-metadata/dto/create-share-metadata.dto.ts create mode 100644 src/modules/share-metadata/share-metadata.controller.ts create mode 100644 src/modules/share-metadata/share-metadata.module.ts create mode 100644 src/modules/share-metadata/share-metadata.service.ts diff --git a/src/modules/catalog/share/share.controller.ts b/src/modules/catalog/share/share.controller.ts new file mode 100644 index 0000000..a5240bf --- /dev/null +++ b/src/modules/catalog/share/share.controller.ts @@ -0,0 +1,87 @@ +import { + Controller, + Get, + Inject, + Param, + UseFilters, +} from '@nestjs/common'; +import { + ApiHeaders, + ApiTags, +} from '@nestjs/swagger'; +import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; +import { RequestUser, User } from 'src/decorators/user.decorator'; +import { + IColumnsMetadataResponse, + IDocsResponse, + IPreviewResponse, + +} from '../dtos'; +import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; +import { Language } from 'src/decorators/language.decorator'; +import { LanguageEnum } from 'src/utils/languages.enum'; +import { ShareService } from './share.service'; + +@ApiTags('Catalog') +@ApiHeaders([{ name: 'dadosfera-lang', enum: LanguageEnum, required: false }]) +@Controller('catalog/data-asset/share') +@UseFilters(new GrpcToHttpExceptionFilter()) +export class ShareController { + logger: DadosferaLogger; + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + private catalogShareService: ShareService, + ) { + this.logger = dadosferaLogger.logger; + } + + @Get('/:id') + async getShareDataAsset( + @Param('id') id: string, + @User() user: RequestUser + ) { + this.logger.info(`GET //:id`); + return await this.catalogShareService.getOneDataAssetPublic(id, user); + } + + @Get('/:id/columns-metadata') + async getShareDataAssetColumnsMetadata( + @Language() language: LanguageEnum, + @Param('id') id: string, + @User() user: RequestUser + ): Promise { + this.logger.info(`GET /:id/columns-metadata`); + + const columns_metadata = + await this.catalogShareService.getDatasetColumnsMetadata(id, user); + + + return { columns_metadata }; + } + + @Get('/:id/preview') + async getShareDataAssetPreview( + @Language() language: LanguageEnum, + @Param('id') id: string, + @User() user: RequestUser + ): Promise { + this.logger.info(`GET /:id/preview`); + const preview = await this.catalogShareService.getDatasetPreview(id, user); + + return { preview }; + } + + @Get('/:id/docs') + async getShareDataAssetDocs( + @Language() language: LanguageEnum, + @Param('id') id: string, + @User() user: RequestUser + ): Promise { + this.logger.info(`GET /:id/docs`); + const docs = await this.catalogShareService.getDataDocs(id, user); + + return { docs }; + } + +} diff --git a/src/modules/catalog/share/share.module.ts b/src/modules/catalog/share/share.module.ts new file mode 100644 index 0000000..1f0c7fb --- /dev/null +++ b/src/modules/catalog/share/share.module.ts @@ -0,0 +1,26 @@ +import { Module } from "@nestjs/common"; +import { CatalogClientConfiguration } from "../catalog-client"; +import { ClientsModule } from "@nestjs/microservices"; +import { RolesModule } from "src/modules/roles/roles.module"; +import { UsersModule } from "src/modules/users/users.module"; +import { CustomersModule } from "src/modules/customers/customers.module"; +import { ShareMetadataModule } from "src/modules/share-metadata/share-metadata.module"; +import { ShareController } from "./share.controller"; +import DadosferaLogger from "@dadosfera/dadosfera-logs"; +import { ShareService } from "./share.service"; + +const client = new CatalogClientConfiguration(); + +@Module({ + imports: [ + ClientsModule.register([client.providerOptions]), + UsersModule, + RolesModule, + CustomersModule, + ShareMetadataModule + ], + controllers: [ShareController], + providers: [ShareService, DadosferaLogger], + exports: [ShareModule], +}) +export class ShareModule {} \ No newline at end of file diff --git a/src/modules/catalog/share/share.service.ts b/src/modules/catalog/share/share.service.ts new file mode 100644 index 0000000..b81096a --- /dev/null +++ b/src/modules/catalog/share/share.service.ts @@ -0,0 +1,231 @@ +import DadosferaLogger from '@dadosfera/dadosfera-logs'; +import { + ProtoServices, + ReadService, +} from '@dadosfera/protospack-v2/dist/lib/Catalog'; +import { + ForbiddenException, + Inject, + NotFoundException, + OnModuleInit, +} from '@nestjs/common'; +import { CatalogClientConfiguration } from '../catalog-client'; +import { ClientGrpc } from '@nestjs/microservices'; +import { UsersService } from 'src/modules/users/users.service'; +import { RolesService } from 'src/modules/roles/roles.service'; +import { RequestUser } from 'src/decorators/user.decorator'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; +import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; +import { Metadata } from '@grpc/grpc-js'; +import { lastValueFrom } from 'rxjs'; +import { ShareMetadataService } from 'src/modules/share-metadata/share-metadata.service'; +import { isJWT } from 'class-validator'; + +export class ShareService implements OnModuleInit { + catalogReadService: ReadService.CatalogReadServices; + logger: DadosferaLogger; + + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + @Inject(CatalogClientConfiguration.name) + private readonly grpcClient: ClientGrpc, + private readonly userService: UsersService, + private readonly roleService: RolesService, + private shareMetadataService: ShareMetadataService, + ) { + this.logger = dadosferaLogger.logger; + } + + onModuleInit() { + this.catalogReadService = + this.grpcClient.getService( + ProtoServices.CatalogReadServices, + ); + } + + async getDatasetColumnsMetadata(id: string, user: RequestUser) { + const shareMetadata = await this.getShareMetadata(id, user); + const metadata = PackTheMetadata({ + customer_id: shareMetadata.customerId, + customer_name: shareMetadata.customerName, + }); + const { columns_metadata } = await lastValueFrom( + this.catalogReadService.GetDatasetColumnsMetadata( + { id: shareMetadata.assetId, type: undefined }, + metadata, + ), + ); + const result = JSON.parse(columns_metadata); + return result; + } + + async getDatasetPreview(id: string, user: RequestUser) { + const shareMetadata = await this.getShareMetadata(id, user); + const metadata = PackTheMetadata({ + customer_id: shareMetadata.customerId, + customer_name: shareMetadata.customerName, + }); + const { preview } = await lastValueFrom( + this.catalogReadService.GetDatasetPreview( + { id: shareMetadata.assetId, type: undefined }, + metadata, + ), + ); + const result = JSON.parse(preview); + return result; + } + + async getOneDataAssetPublic(id: string, user: RequestUser) { + this.logger.info("getOneDataAssetPublic: " + JSON.stringify({ + id, + userId: user?.user_id, + customerId: user?.customer_id + })) + try { + const shareMetadata = await this.getShareMetadata(id, user); + + this.logger.info("shareMetadata: " + JSON.stringify(shareMetadata)) + const metadata = PackTheMetadata({ + customer_id: shareMetadata.customerId, + customer_name: shareMetadata.customerName, + }); + + const { data_asset } = await this.getOneDataAsset({ + customer_id: shareMetadata.customerId, + id: shareMetadata.assetId, + metadata, + }); + this.logger.info('found asset: ' + JSON.stringify(data_asset)); + delete data_asset.p_roles; + delete data_asset.p_users; + data_asset.share_type = 'public'; + if (data_asset.share_type !== 'public') throw new NotFoundException(); + + return { data_asset }; + } catch (error) { + this.logger.error(error); + throw error; + } + + } + + private async getOneDataAsset(data: { + id: string; + customer_id: string; + metadata: Metadata; + }) { + const { customer_id, id, metadata } = data; + const { data_asset } = await lastValueFrom( + this.catalogReadService.GetOneDataAsset( + { id, type: undefined }, + metadata, + ), + ); + let asset = JSON.parse(data_asset); + asset = { + ...asset, + p_roles: asset.roles, + p_users: asset.users, + }; + asset = await this.getAssetsUsersAndRoles([asset], customer_id); + + return { data_asset: asset[0] }; + } + + async getDataDocs(id: string, user: RequestUser) { + const shareMetadata = await this.getShareMetadata(id, user); + const metadata = PackTheMetadata({ + customer_id: shareMetadata.customerId, + customer_name: shareMetadata.customerName, + }); + + const { documentation } = await lastValueFrom( + this.catalogReadService.GetDatasetDoc({ id, type: undefined }, metadata), + ); + console.log(documentation); + const docs = JSON.parse(documentation); + return docs; + } + + private async getAssetsUsersAndRoles( + data_assets: Array, + customer_id: string, + ) { + const { users: customer_users } = + await this.userService.findAllUsersByCustomerId(customer_id); + const { roles: customer_roles } = await this.roleService.roleSearch( + {}, + { customer_id }, + ); + return data_assets.map((data_asset) => { + const owner = customer_users.find( + (u) => u.id === data_asset.owner, + )?.username; + + const roles = []; + const users = []; + for (const role_id of data_asset.roles) { + const role = customer_roles.find((r) => r.id === role_id); + if (role) roles.push({ id: role.id, name: role.name }); + } + for (const user_id of data_asset.users) { + const user = customer_users.find((r) => r.id === user_id); + if (user) users.push({ id: user.id, username: user.username }); + } + return { + ...data_asset, + roles, + users, + owner, + } as typeof data_asset; + }); + } + + // private async validateShareAssign(token: string) { + // const tokenDecoded = jwt.decode(token, { + // complete: true, + // }); + + // jwt.verify(token, this.pemValue, { + // algorithms: ['RS256'], + // }); + + // const shareId = (tokenDecoded.payload as JwtPayload).sub; + // const metadata = PackTheMetadata({}); + // return await this.shareMetadataService.get(shareId, metadata); + // } + + private async getShareMetadata(id: string, user: RequestUser) { + const metadata = PackTheMetadata({}); + const info = await this.shareMetadataService.get(id, metadata); + if (isJWT(id) && info ){ + return info; + } + + if (info.type === 'private') { + if (!user) { + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + + const is_data_manager = user.permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + + const is_get = user.permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.GET.seqid, + ); + + if (is_data_manager || is_get) { + return; + } + + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + return info; + } +} diff --git a/src/modules/share-metadata/dto/create-share-metadata.dto.ts b/src/modules/share-metadata/dto/create-share-metadata.dto.ts new file mode 100644 index 0000000..db06de6 --- /dev/null +++ b/src/modules/share-metadata/dto/create-share-metadata.dto.ts @@ -0,0 +1,9 @@ +export class CreateShareMetadataDto { + assetId: string; + proposedId: string; +} + +export class UpdateShareMetadataDto { + shareId: string; + type: string; +} diff --git a/src/modules/share-metadata/share-metadata.controller.ts b/src/modules/share-metadata/share-metadata.controller.ts new file mode 100644 index 0000000..bfa6ca5 --- /dev/null +++ b/src/modules/share-metadata/share-metadata.controller.ts @@ -0,0 +1,31 @@ +import { Controller, Get, Body, Param, Put, Patch } from '@nestjs/common'; +import { ShareMetadataService } from './share-metadata.service'; +import { Authenticated } from 'src/decorators/authentication.decorator'; +import { RequestUser, User } from 'src/decorators/user.decorator'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; +import { CreateShareMetadataDto, UpdateShareMetadataDto } from './dto/create-share-metadata.dto'; + +@Controller('share') +@Authenticated() +export class ShareMetadataController { + constructor(private readonly shareMetadataService: ShareMetadataService) {} + + @Put('/') + findOrcreate(@Body() data: CreateShareMetadataDto, @User() user: RequestUser) { + const metadata = PackTheMetadata(user); + return this.shareMetadataService.create(data, metadata); + } + + @Patch('/') + updateShareType(@Body() data: UpdateShareMetadataDto, @User() user: RequestUser) { + const metadata = PackTheMetadata(user); + return this.shareMetadataService.updateShare(data, metadata); + } + + + @Get('/:id') + get(@Param('id') id: string, @User() user: RequestUser) { + const metadata = PackTheMetadata(user); + return this.shareMetadataService.get(id, metadata); + } +} diff --git a/src/modules/share-metadata/share-metadata.module.ts b/src/modules/share-metadata/share-metadata.module.ts new file mode 100644 index 0000000..f75e78e --- /dev/null +++ b/src/modules/share-metadata/share-metadata.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { ShareMetadataService } from './share-metadata.service'; +import { ShareMetadataController } from './share-metadata.controller'; +import { DucClient } from '../duc/client.config'; +import { ClientsModule } from '@nestjs/microservices'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; + +const client = new DucClient(); + +@Module({ + imports: [ClientsModule.register([client.providerOptions])], + controllers: [ShareMetadataController], + providers: [ShareMetadataService, DadosferaLogger], + exports: [ShareMetadataService] +}) +export class ShareMetadataModule {} diff --git a/src/modules/share-metadata/share-metadata.service.ts b/src/modules/share-metadata/share-metadata.service.ts new file mode 100644 index 0000000..0915faa --- /dev/null +++ b/src/modules/share-metadata/share-metadata.service.ts @@ -0,0 +1,61 @@ +import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; +import { Metadata } from '@grpc/grpc-js'; +import { ClientGrpc } from '@nestjs/microservices'; +import { DucClient } from '../duc/client.config'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; +import { ShareMetadataProtoService } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/write-service'; +import { lastValueFrom } from 'rxjs'; +import { ProtoServices } from '@dadosfera/protospack-v2/dist/lib/Duc'; +import { CreateShareMetadataDto, UpdateShareMetadataDto } from './dto/create-share-metadata.dto'; + +@Injectable() +export class ShareMetadataService implements OnModuleInit { + ducService: ShareMetadataProtoService; + logger: DadosferaLogger; + + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + @Inject(DucClient.name) private readonly grpcClient: ClientGrpc, + ) { + this.logger = dadosferaLogger.logger; + } + + onModuleInit() { + this.ducService =this.grpcClient.getService( + ProtoServices.ShareMetadataProtoService, + ); + } + + create(createShareDto: CreateShareMetadataDto, metadata: Metadata) { + return lastValueFrom( + this.ducService.FindOrCreateShareMetadata( + { + assetId: createShareDto.assetId, + proposedId: createShareDto.proposedId + }, + metadata, + ), + ); + } + + updateShare(updateShare: UpdateShareMetadataDto, metadata: Metadata) { + return lastValueFrom( + this.ducService.ChangeShareMetadataType( + updateShare, + metadata, + ), + ); + } + + get(id: string, metadata: Metadata) { + return lastValueFrom( + this.ducService.GetShareMetadata( + { + id, + }, + metadata, + ), + ); + } +} From b312849fffdaf554ab389d55f1dc858c7927a958 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 12 May 2025 12:15:16 -0300 Subject: [PATCH 4/9] FEAT: catalog share endpoint for embeds --- docsfera.json | 492 ++++++++++++++++-- package-lock.json | 8 +- package.json | 2 +- src/app.module.ts | 8 +- .../{controllers => }/catalog.controller.ts | 302 ++++++++--- src/modules/catalog/catalog.module.ts | 11 +- src/modules/catalog/catalog.service.ts | 153 +----- .../controllers/catalog.public.controller.ts | 184 ------- 8 files changed, 719 insertions(+), 441 deletions(-) rename src/modules/catalog/{controllers => }/catalog.controller.ts (80%) delete mode 100644 src/modules/catalog/controllers/catalog.public.controller.ts diff --git a/docsfera.json b/docsfera.json index 2fd799a..762ab8d 100644 --- a/docsfera.json +++ b/docsfera.json @@ -3873,6 +3873,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -3903,6 +3906,11 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + } ] } }, @@ -3939,6 +3947,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -3970,7 +3981,7 @@ } }, { - "name": "c", + "name": "shared", "required": true, "in": "query", "schema": { @@ -3985,6 +3996,14 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } ] }, "put": { @@ -4037,6 +4056,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4082,6 +4104,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4122,6 +4147,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4151,14 +4179,6 @@ "schema": { "type": "string" } - }, - { - "name": "c", - "required": true, - "in": "query", - "schema": { - "type": "string" - } } ], "responses": { @@ -4175,6 +4195,14 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } ] } }, @@ -4201,14 +4229,6 @@ "schema": { "type": "string" } - }, - { - "name": "c", - "required": true, - "in": "query", - "schema": { - "type": "string" - } } ], "responses": { @@ -4225,6 +4245,14 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } ] } }, @@ -4251,14 +4279,6 @@ "schema": { "type": "string" } - }, - { - "name": "c", - "required": true, - "in": "query", - "schema": { - "type": "string" - } } ], "responses": { @@ -4275,6 +4295,14 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } ] }, "post": { @@ -4317,6 +4345,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4355,6 +4386,11 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + } ] } }, @@ -4390,6 +4426,11 @@ }, "tags": [ "Catalog" + ], + "security": [ + { + "access-token": [] + } ] } }, @@ -4444,6 +4485,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4499,6 +4543,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4548,6 +4595,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4595,6 +4645,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4634,6 +4687,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4674,6 +4730,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4720,6 +4779,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4767,6 +4829,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4812,6 +4877,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4851,6 +4919,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4883,6 +4954,9 @@ "Catalog" ], "security": [ + { + "access-token": [] + }, { "access-token": [] } @@ -4926,6 +5000,9 @@ { "access-token": [] }, + { + "access-token": [] + }, { "access-token": [] } @@ -5167,6 +5244,263 @@ ] } }, + "/catalog/data-asset/share/{id}": { + "get": { + "operationId": "ShareController_getShareDataAsset", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Catalog" + ] + } + }, + "/catalog/data-asset/share/{id}/columns-metadata": { + "get": { + "operationId": "ShareController_getShareDataAssetColumnsMetadata", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/IColumnsMetadataResponse" + } + } + } + } + }, + "tags": [ + "Catalog" + ] + } + }, + "/catalog/data-asset/share/{id}/preview": { + "get": { + "operationId": "ShareController_getShareDataAssetPreview", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/IPreviewResponse" + } + } + } + } + }, + "tags": [ + "Catalog" + ] + } + }, + "/catalog/data-asset/share/{id}/docs": { + "get": { + "operationId": "ShareController_getShareDataAssetDocs", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/IDocsResponse" + } + } + } + } + }, + "tags": [ + "Catalog" + ] + } + }, + "/share": { + "put": { + "operationId": "ShareMetadataController_findOrcreate", + "parameters": [], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateShareMetadataDto" + } + } + } + }, + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + } + ] + }, + "patch": { + "operationId": "ShareMetadataController_updateShareType", + "parameters": [], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UpdateShareMetadataDto" + } + } + } + }, + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + } + ] + } + }, + "/share/{id}": { + "get": { + "operationId": "ShareMetadataController_get", + "parameters": [ + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + } + ] + } + }, "/terms-of-use/token": { "get": { "operationId": "TermsOfUseController_getToken", @@ -5796,20 +6130,6 @@ ] } }, - "/health": { - "get": { - "operationId": "HealthController_check", - "parameters": [], - "responses": { - "200": { - "description": "" - } - }, - "tags": [ - "Health" - ] - } - }, "/network-policy": { "get": { "operationId": "NetworkPolicyController_getNetworks", @@ -5899,6 +6219,59 @@ } ] } + }, + "/assign/public-key": { + "put": { + "operationId": "AssignController_create", + "parameters": [], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateAssignDto" + } + } + } + }, + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/health": { + "get": { + "operationId": "HealthController_check", + "parameters": [], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Health" + ] + } } }, "info": { @@ -8419,6 +8792,36 @@ "links" ] }, + "CreateShareMetadataDto": { + "type": "object", + "properties": { + "assetId": { + "type": "string" + }, + "proposedId": { + "type": "string" + } + }, + "required": [ + "assetId", + "proposedId" + ] + }, + "UpdateShareMetadataDto": { + "type": "object", + "properties": { + "shareId": { + "type": "string" + }, + "type": { + "type": "string" + } + }, + "required": [ + "shareId", + "type" + ] + }, "ConnectionTestPingReq": { "type": "object", "properties": { @@ -8779,6 +9182,17 @@ "required": [ "policies" ] + }, + "CreateAssignDto": { + "type": "object", + "properties": { + "publicKey": { + "type": "string" + } + }, + "required": [ + "publicKey" + ] } } } diff --git a/package-lock.json b/package-lock.json index c9d852f..50c8341 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "3.37.0-beta.23", + "@dadosfera/protospack-v2": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", @@ -1400,9 +1400,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "3.37.0-beta.23", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.37.0-beta.23.tgz", - "integrity": "sha512-Dv3rODwbiubHB4u8sI5uJOSm1ETOXzlpaxHNEmzTNG2fLTIJxsaF1GlOBST3x9HTkhJC6hUJ8r77/qQu1FKPpQ==", + "version": "0.0.0", + "resolved": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", + "integrity": "sha512-NfnvIENsIval+liPIPoVkFjixmKCKu9tRqvXQu3qTk67kZicNBvEOd9UezED1h1a5kX70+CbSR4mlXUZZ7ZVGA==", "license": "ISC", "dependencies": { "@grpc/grpc-js": "^1.9.3", diff --git a/package.json b/package.json index fd3eb0b..40b4faa 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "3.37.0-beta.23", + "@dadosfera/protospack-v2": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", diff --git a/src/app.module.ts b/src/app.module.ts index fedb388..c9a8b35 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -29,6 +29,8 @@ import { CustomersModule } from './modules/customers/customers.module'; import { OpenDataModule } from './modules/open-data/open-data.module'; import { ThemeModule } from './modules/theme/theme.module'; import { NetworkPolicyModule } from './modules/network-policy/network-policy.module'; +import { AssignModule } from './modules/assign/assign.module'; +import { ShareMetadataModule } from './modules/share-metadata/share-metadata.module'; @Module({ providers: [ @@ -62,9 +64,11 @@ import { NetworkPolicyModule } from './modules/network-policy/network-policy.mod CustomersModule, OpenDataModule, ThemeModule, + NetworkPolicyModule, + AssignModule, + ShareMetadataModule, //Always leave HealthModule last, so it is on the bottom of swagger - HealthModule, - NetworkPolicyModule + HealthModule ], }) export class AppModule {} diff --git a/src/modules/catalog/controllers/catalog.controller.ts b/src/modules/catalog/catalog.controller.ts similarity index 80% rename from src/modules/catalog/controllers/catalog.controller.ts rename to src/modules/catalog/catalog.controller.ts index ca5d88a..f0727f0 100644 --- a/src/modules/catalog/controllers/catalog.controller.ts +++ b/src/modules/catalog/catalog.controller.ts @@ -28,12 +28,9 @@ import { RequireAllPermissions, RequireModule, RequireSomePermission, -} from '../../../decorators/authentication.decorator'; -import { - DADOSFERA_MODULES_KEYS, - PERMISSIONS_GROUPS, -} from '../../../authentication/permissions.enum'; -import { CatalogService } from '../catalog.service'; +} from '../../decorators/authentication.decorator'; +import { DADOSFERA_MODULES_KEYS, PERMISSIONS_GROUPS } from '../../authentication/permissions.enum'; +import { CatalogService } from './catalog.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { RequestUser, User } from 'src/decorators/user.decorator'; @@ -52,7 +49,7 @@ import { IUpdateDataRequest, TriggerCatalogReq, TriggerCatalogRes, -} from '../dtos'; +} from './dtos'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { Language } from 'src/decorators/language.decorator'; import { LanguageEnum } from 'src/utils/languages.enum'; @@ -198,6 +195,209 @@ export class CatalogController { return res; } + @Get('data-asset/:id') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAsset( + @User() user: RequestUser, + @Param('id') id: string, + @Query('shared') shared?: 'true', + ) { + const { username, user_id, customer_id, customer_name, permissions } = user; + + this.logger.info(`GET /data-asset/${id}`, { + username, + customer_name, + }); + + const is_data_manager = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + let has_permission = false; + const metadata = PackTheMetadata({ + username, + user_id, + customer_id, + customer_name, + }); + + const user_roles = await this.catalogService.getUserRolesIds(user_id); + + const { data_asset } = await this.catalogService.getOneDataAsset({ + customer_id, + id, + metadata, + }); + has_permission = + is_data_manager || + data_asset?.owner === username || + (user_roles as Array).some((r) => data_asset.p_roles.includes(r)) || + data_asset.p_users.includes(user_id); + if ( + shared === 'true' && + (data_asset.share_type === undefined || data_asset.share_type === 'none') + ) + throw new NotFoundException(); + if (!has_permission) + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + delete data_asset.p_roles; + delete data_asset.p_users; + + return { data_asset }; + } + + @ApiInternalOnlyEndpoint() + @Get('data-asset/rls/:id') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAssetRls( + @User() user: RequestUser, + @Headers() headers, + @Param('id') id: string, + ) { + const { username, user_id, customer_id, customer_name, permissions } = user; + + this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { + username, + customer_name, + }); + + const is_data_manager = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + let has_permission = false; + const metadata = PackTheMetadata({ + username, + user_id, + customer_id, + customer_name, + }); + + const user_roles = await this.catalogService.getUserRolesIds(user_id); + + const { data_asset } = await this.catalogService.getOneDataAsset({ + customer_id, + id, + metadata, + }); + + if (data_asset?.owner === username) has_permission = true; + + for (const role of user_roles) { + if (data_asset.p_roles.includes(role)) has_permission = true; + } + + if (data_asset.p_users.includes(user_id)) has_permission = true; + + if (is_data_manager || has_permission) { + delete data_asset.p_roles; + delete data_asset.p_users; + return { data_asset }; + } + + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + + @Get('data-asset/:id/columns-metadata') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAssetColumnsMetadata( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + ): Promise { + const { customer_name, customer_id, user_id, username } = user; + + this.logger.info(`/catalog - columns-metadata`, { + user_id, + customer_name, + }); + + const metadata = PackTheMetadata({ + customer_name, + customer_id, + user_id, + username, + language, + }); + + const columns_metadata = + await this.catalogService.getDatasetColumnsMetadata(id, metadata); + + return { columns_metadata }; + } + + @Get('data-asset/:id/preview') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAssetPreview( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + ): Promise { + const { customer_name, customer_id, user_id, username, customer_modules } = user; + + this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { + user_id, + customer_name, + }); + + const metadata = PackTheMetadata({ + customer_name, + customer_id, + user_id, + username, + language, + is_mask: customer_modules.some(mod => mod === 'pii') + }); + + const preview = await this.catalogService.getDatasetPreview(id, metadata); + + return { preview }; + } + + @Get('data-asset/:id/docs') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async getDataAssetDocs( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Param('id') id: string, + ): Promise { + const { customer_name, customer_id, user_id, username } = user; + + this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, { + user_id, + customer_name, + }); + + const metadata = PackTheMetadata({ + customer_name, + customer_id, + user_id, + username, + language, + }); + + const docs = await this.catalogService.getDataDocs(id, metadata); + + return { docs }; + } + @Put('data-asset/:id') @RequireSomePermission( PERMISSIONS_GROUPS.CATALOG.permissions.UPDATE, @@ -448,62 +648,6 @@ export class CatalogController { return response; } - @ApiInternalOnlyEndpoint() - @Get('data-asset/rls/:id') - @RequireSomePermission( - PERMISSIONS_GROUPS.CATALOG.permissions.GET, - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, - ) - async getDataAssetRls( - @User() user: RequestUser, - @Headers() headers, - @Param('id') id: string, - ) { - const { username, user_id, customer_id, customer_name, permissions } = user; - - this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, { - username, - customer_name, - }); - - const is_data_manager = permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, - ); - let has_permission = false; - const metadata = PackTheMetadata({ - username, - user_id, - customer_id, - customer_name, - }); - - const user_roles = await this.catalogService.getUserRolesIds(user_id); - - const { data_asset } = await this.catalogService.getOneDataAsset({ - customer_id, - id, - metadata, - }); - - if (data_asset?.owner === username) has_permission = true; - - for (const role of user_roles) { - if (data_asset.p_roles.includes(role)) has_permission = true; - } - - if (data_asset.p_users.includes(user_id)) has_permission = true; - - if (is_data_manager || has_permission) { - delete data_asset.p_roles; - delete data_asset.p_users; - return { data_asset }; - } - - throw new ForbiddenException( - 'You do not have permission to access this data asset.', - ); - } - @Post('rls-rule') @RequireAllPermissions(PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER) async addRlsRule(@User() user: RequestUser, @Body() body: AddRlsRuleRequest) { @@ -715,32 +859,30 @@ export class CatalogController { } @Get('pii-reporter') - @RequireSomePermission(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) - @RequireModule(DADOSFERA_MODULES_KEYS.PII) - async getPiiReporter( - @User() user: RequestUser, - @Res() res: Response, - @Query('type') contentType: TypeParser = 'pdf', - ) { + @RequireSomePermission( + PERMISSIONS_GROUPS.USERS.permissions.ADMIN + ) + @RequireModule( + DADOSFERA_MODULES_KEYS.PII + ) + async getPiiReporter(@User() user: RequestUser, @Res() res: Response, @Query('type') contentType: TypeParser = "pdf") { this.logger.info('GET pii-reporter'); const metadata = PackTheMetadata(user); try { - const { file, filename, type } = await this.catalogService.getPiiReporter( - metadata, - contentType, - ); - res.setHeader( - 'Content-Disposition', - `attachment; filename="${filename}"`, - ); + const { + file, + filename, + type + } = await this.catalogService.getPiiReporter(metadata, contentType); + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); res.setHeader('Content-Type', type); // use res.end to send buffer return res.end(file); } catch (error) { - console.error(error); + console.error(error) this.logger.error(error.message); } } -} +} \ No newline at end of file diff --git a/src/modules/catalog/catalog.module.ts b/src/modules/catalog/catalog.module.ts index d990344..8320d37 100644 --- a/src/modules/catalog/catalog.module.ts +++ b/src/modules/catalog/catalog.module.ts @@ -2,15 +2,15 @@ import { Module } from '@nestjs/common'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; -import { CatalogController } from './controllers/catalog.controller'; -import { CatalogService } from './catalog.service'; +import { CatalogController } from './catalog.controller'; import { CatalogClientConfiguration } from './catalog-client'; import { ClientsModule } from '@nestjs/microservices'; import { PipelinesModule as OldPipelineModule } from 'src/modules/pipelines/pipelines.module'; import { UsersModule } from '../users/users.module'; import { RolesModule } from '../roles/roles.module'; import { CustomersModule } from '../customers/customers.module'; -import { CatalogPublicController } from './controllers/catalog.public.controller'; +import { ShareModule } from './share/share.module'; +import { CatalogService } from './catalog.service'; const client = new CatalogClientConfiguration(); @@ -20,9 +20,10 @@ const client = new CatalogClientConfiguration(); OldPipelineModule, UsersModule, RolesModule, - CustomersModule + CustomersModule, + ShareModule, ], - controllers: [CatalogController, CatalogPublicController], + controllers: [CatalogController], providers: [CatalogService, DadosferaLogger], exports: [CatalogService], }) diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.ts index f7a2ed0..9bf0047 100644 --- a/src/modules/catalog/catalog.service.ts +++ b/src/modules/catalog/catalog.service.ts @@ -12,11 +12,9 @@ import { } from '@dadosfera/protospack-v2/dist/lib/PlatformInterfaces'; import { BadRequestException, - ForbiddenException, HttpException, HttpStatus, Inject, - NotFoundException, OnModuleInit, } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; @@ -37,19 +35,8 @@ import { GetRlsRulesRequest, PiiMetadata, } from '@dadosfera/protospack-v2/dist/lib/Catalog/interfaces/messages'; -import { writeFileSync } from 'fs'; -import path from 'path'; -import { HtmlParser } from 'src/utils/FileParser/html-parser'; -import { PiiDto } from './dtos/pii.dto'; -import { CsvParser } from 'src/utils/FileParser/csv-parser'; -import { PDFParser } from 'src/utils/FileParser/pdf-parser'; import { TypeParser } from 'src/utils/FileParser/parser-types'; import { ParserBuilder } from 'src/utils/FileParser/parser.builder'; -import { RequestUser } from 'src/decorators/user.decorator'; -import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; -import { PackTheMetadata } from 'src/utils/PackTheMetadata'; -import { CustomersService } from '../customers/customers.service'; -import { LanguageEnum } from 'src/utils/languages.enum'; class CatalogService implements OnModuleInit { catalogReadService: ReadService.CatalogReadServices; @@ -63,7 +50,6 @@ class CatalogService implements OnModuleInit { private readonly grpcClient: ClientGrpc, private readonly userService: UsersService, private readonly roleService: RolesService, - private readonly customerService: CustomersService ) { this.logger = dadosferaLogger.logger; } @@ -248,77 +234,6 @@ class CatalogService implements OnModuleInit { return { data_assets: response, total }; } - async getOneDataAssetPrivate(user: RequestUser, id: string) { - const { username, user_id, customer_id, customer_name, permissions } = user; - - this.logger.info("getOneDataAssetPrivate: " + user_id + " - " + id) - - this.logger.info(`data: `, { - username, - customer_name, - }); - - const is_data_manager = permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, - ); - - const is_get_catalog = permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.GET.seqid, - ); - let has_permission = false; - const metadata = PackTheMetadata({ - username, - user_id, - customer_id, - customer_name, - }); - - const user_roles = await this.getUserRolesIds(user_id); - - const { data_asset } = await this.getOneDataAsset({ - customer_id, - id, - metadata, - }); - - has_permission = - is_data_manager || - is_get_catalog || - data_asset?.owner === username || - (user_roles as Array).some((r) => - data_asset.p_roles.includes(r), - ) || - data_asset.p_users.includes(user_id); - - - if (!has_permission) - throw new ForbiddenException( - 'You do not have permission to access this data asset.', - ); - delete data_asset.p_roles; - delete data_asset.p_users; - - return { data_asset }; - } - - async getOneDataAssetPublic(customerId: string, id: string) { - this.logger.info("getOneDataAssetPublic: " + customerId + " - " + id) - const metadata = await this.createMetadataToPublicAccess(customerId); - const { data_asset } = await this.getOneDataAsset({ - customer_id: customerId, - id, - metadata, - }); - this.logger.info("found asset: " + JSON.stringify(data_asset)) - delete data_asset.p_roles; - delete data_asset.p_users; - data_asset.share_type = 'public'; - if (data_asset.share_type !== 'public') - throw new NotFoundException(); - - return { data_asset }; - } - async getOneDataAsset(data: { id: string; customer_id: string; @@ -395,7 +310,7 @@ class CatalogService implements OnModuleInit { const { documentation } = await lastValueFrom( this.catalogReadService.GetDatasetDoc({ id, type: undefined }, metadata), ); - console.log(documentation); + const docs = JSON.parse(documentation); return docs; } @@ -558,26 +473,27 @@ class CatalogService implements OnModuleInit { async createTableMetadata(body: any): Promise { const nimbusUrl = this._getNimbusUrl(body); - this.logger.info(`Nimbus URL: ${nimbusUrl}`, { ...body.logMetadata }); + this.logger.info(`Nimbus URL: ${nimbusUrl}`, {...body.logMetadata}); const endpoint = `${nimbusUrl}/api/catalog/table-metadata/`; - this.logger.info(`Creating table metadata for table ${body.table_metadata.table_name}`, { ...body.logMetadata }); - this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); - this.logger.debug(`Payload: ${JSON.stringify(body.table_metadata)}`, { ...body.logMetadata }); + this.logger.info(`Creating table metadata for table ${body.table_metadata.table_name}`, {...body.logMetadata}); + this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); + this.logger.debug(`Payload: ${JSON.stringify(body.table_metadata)}`, {...body.logMetadata}); try { - const { data, status } = await axios.post(endpoint, { ...body.table_metadata }); + const { data, status } = await axios.post(endpoint, {...body.table_metadata}); this.logger.info( `Table metadata created successfully with status ${status} for table ${body.table_metadata.table_name}`, - { ...body.logMetadata }, + {...body.logMetadata}, ); return data.id; } catch (error) { this.logger.error( - `Failed to create table metadata for table ${body.table_metadata.table_name} failed with status ${error.response?.status - } because of ${JSON.stringify(error.response?.data) || error.message}`, { ...body.logMetadata }); + `Failed to create table metadata for table ${body.table_metadata.table_name} failed with status ${ + error.response?.status + } because of ${JSON.stringify(error.response?.data) || error.message}`, {...body.logMetadata}); throw new Error(error.response?.data?.message || error.message); } } @@ -587,49 +503,51 @@ class CatalogService implements OnModuleInit { this.logger.info(`Nimbus URL: ${nimbusUrl}`, body.logMetadata); const endpoint = `${nimbusUrl}/api/catalog/column-metadata/`; - + try { - this.logger.info(`Creating column metadata for table ${body.column_metadata.table_name}`, { ...body.logMetadata }); - this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); - this.logger.debug(`Payload: ${JSON.stringify(body.column_metadata)}`, { ...body.logMetadata }); + this.logger.info(`Creating column metadata for table ${body.column_metadata.table_name}`, {...body.logMetadata}); + this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); + this.logger.debug(`Payload: ${JSON.stringify(body.column_metadata)}`, {...body.logMetadata}); const { data, status } = await axios.post(endpoint, body.column_metadata); this.logger.info( `Column metadata created successfully with status ${status} for table ${body.column_metadata.table_name}`, - { ...body.logMetadata }, + {...body.logMetadata}, ); return data.map((column) => column.id); } catch (error) { this.logger.error( - `Failed to create column metadata failed with status for table ${body.column_metadata.table_name} ${error.response?.status - } because of ${error.response?.data || error.message}`, { ...body.logMetadata }); + `Failed to create column metadata failed with status for table ${body.column_metadata.table_name} ${ + error.response?.status + } because of ${error.response?.data || error.message}`, {...body.logMetadata}); throw new Error(error.response?.data?.message || error.message); } } async createDataPreview(body: any): Promise { const nimbusUrl = this._getNimbusUrl(body); - this.logger.info(`Nimbus URL: ${nimbusUrl}`, { ...body.logMetadata }); + this.logger.info(`Nimbus URL: ${nimbusUrl}`, {...body.logMetadata}); const endpoint = `${nimbusUrl}/api/catalog/data-preview/`; - this.logger.info(`Creating data preview for table ${body.data_preview.table_name}`, { ...body.logMetadata }); - this.logger.info(`Using endpoint: ${endpoint}`, { ...body.logMetadata }); - this.logger.debug(`Payload: ${JSON.stringify(body.data_preview)}`, { ...body.logMetadata }); + this.logger.info(`Creating data preview for table ${body.data_preview.table_name}`, {...body.logMetadata}); + this.logger.info(`Using endpoint: ${endpoint}`, {...body.logMetadata}); + this.logger.debug(`Payload: ${JSON.stringify(body.data_preview)}`, {...body.logMetadata}); try { const { data, status } = await axios.post(endpoint, body.data_preview); this.logger.info( `Data preview created successfully with status ${status} for table ${body.data_preview.table_name}`, - { ...body.logMetadata }, + {...body.logMetadata}, ); return data.id; } catch (error) { this.logger.error( - `Failed to create data preview for table ${body.data_preview.table_name} failed with status ${error.response?.status + `Failed to create data preview for table ${body.data_preview.table_name} failed with status ${ + error.response?.status } because of ${error.response?.data || error.message}`, - { ...body.logMetadata }, + {...body.logMetadata}, ); throw new Error(error.response?.data?.message || error.message); } @@ -658,23 +576,6 @@ class CatalogService implements OnModuleInit { ); return res; } - - async createMetadataToPublicAccess(customerId?: string, language?: LanguageEnum) { - if (!customerId) { - throw new NotFoundException(); - } - - const { - customer - } = await this.customerService.getCustomer(customerId); - - return PackTheMetadata({ - customer_id: customer.id, - customer_name: customer.name, - language, - }) - } - } -export { CatalogService }; +export { CatalogService }; \ No newline at end of file diff --git a/src/modules/catalog/controllers/catalog.public.controller.ts b/src/modules/catalog/controllers/catalog.public.controller.ts deleted file mode 100644 index 6d3be81..0000000 --- a/src/modules/catalog/controllers/catalog.public.controller.ts +++ /dev/null @@ -1,184 +0,0 @@ -import { - Controller, - ForbiddenException, - Get, - Headers, - Inject, - Param, - Query, - UseFilters, -} from '@nestjs/common'; -import { - ApiHeaders, - ApiTags, -} from '@nestjs/swagger'; -import { - RequireSomePermission, -} from '../../../decorators/authentication.decorator'; -import { - PERMISSIONS_GROUPS, -} from '../../../authentication/permissions.enum'; -import { CatalogService } from '../catalog.service'; -import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; -import { PackTheMetadata } from 'src/utils/PackTheMetadata'; -import { RequestUser, User } from 'src/decorators/user.decorator'; -import { - IColumnsMetadataResponse, - IDocsResponse, - IPreviewResponse, - -} from '../dtos'; -import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; -import { Language } from 'src/decorators/language.decorator'; -import { LanguageEnum } from 'src/utils/languages.enum'; -import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; - - -@ApiTags('Catalog') -@ApiHeaders([{ name: 'dadosfera-lang', enum: LanguageEnum, required: false }]) -@Controller('catalog') -@UseFilters(new GrpcToHttpExceptionFilter()) -export class CatalogPublicController { - logger: DadosferaLogger; - constructor( - @Inject(DadosferaLogger) - dadosferaLogger: DadosferaLogger, - private catalogService: CatalogService, - ) { - this.logger = dadosferaLogger.logger; - } - - private async createAccessToPublicAssets( - user?: RequestUser, - customerId?: string, - language?: LanguageEnum, - ) { - if (user) { - const { customer_name, customer_id, user_id, username } = user; - return PackTheMetadata({ - customer_name, - customer_id, - user_id, - username, - language, - }); - } else { - return await this.catalogService.createMetadataToPublicAccess( - customerId, - language, - ); - } - } - - private checkDefaultPermission(user: RequestUser) { - const is_data_manager = user.permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, - ); - - const is_get = user.permissions.includes( - PERMISSIONS_GROUPS.CATALOG.permissions.GET.seqid, - ); - - if (is_data_manager || is_get) { - return; - } - - throw new ForbiddenException( - 'You do not have permission to access this data asset.', - ); - } - - @Get('data-asset/:id') - async getDataAsset( - @User() user: RequestUser, - @Param('id') id: string, - @Query('c') customerId?: string, - ) { - this.logger.info(`GET /data-asset/${id}`); - try { - if (user) { - return await this.catalogService.getOneDataAssetPrivate(user, id); - } else { - return await this.catalogService.getOneDataAssetPublic(customerId, id); - } - } catch (error) { - this.logger.error(error.message); - throw error; - } - } - - - - @Get('data-asset/:id/columns-metadata') - async getDataAssetColumnsMetadata( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - @Query('c') customerId: string, - ): Promise { - const metadata = await this.createAccessToPublicAssets( - user, - customerId, - language, - ); - this.logger.info(`/catalog - columns-metadata`); - - const columns_metadata = - await this.catalogService.getDatasetColumnsMetadata(id, metadata); - - if (user) { - this.checkDefaultPermission(user); - } - - return { columns_metadata }; - } - - @Get('data-asset/:id/preview') - async getDataAssetPreview( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - @Query('c') customerId: string, - ): Promise { - this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`); - - const metadata = await this.createAccessToPublicAssets( - user, - customerId, - language, - ); - - const preview = await this.catalogService.getDatasetPreview(id, metadata); - - if (user) { - this.checkDefaultPermission(user); - } - - return { preview }; - } - - @Get('data-asset/:id/docs') - async getDataAssetDocs( - @User() user: RequestUser, - @Language() language: LanguageEnum, - @Param('id') id: string, - @Query('c') customerId: string, - ): Promise { - this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`); - - const metadata = await this.createAccessToPublicAssets( - user, - customerId, - language, - ); - - const docs = await this.catalogService.getDataDocs(id, metadata); - - if (user) { - this.checkDefaultPermission(user); - } - - return { docs }; - } - -} From 3c4f45678a3365551b49084d29c4a1955f734eb8 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 12 May 2025 12:25:45 -0300 Subject: [PATCH 5/9] CHORE: update protospack --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 50c8341..a67c2e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", + "@dadosfera/protospack-v2": "3.37.0-beta.24", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", @@ -1400,9 +1400,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "0.0.0", - "resolved": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", - "integrity": "sha512-NfnvIENsIval+liPIPoVkFjixmKCKu9tRqvXQu3qTk67kZicNBvEOd9UezED1h1a5kX70+CbSR4mlXUZZ7ZVGA==", + "version": "3.37.0-beta.24", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.37.0-beta.24.tgz", + "integrity": "sha512-uf6cJkq6LjROWxJEGjSXQNengtR74cXtybJUkZRj2c438yjQ6FChEczJdx5053m0qnPpbV86QRrayB/XxVPmvQ==", "license": "ISC", "dependencies": { "@grpc/grpc-js": "^1.9.3", diff --git a/package.json b/package.json index 40b4faa..fc1e8ac 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "file:../protospack-v2/dadosfera-protospack-v2-0.0.0.tgz", + "@dadosfera/protospack-v2": "3.37.0-beta.24", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", From d15cc4642a38a60ac44158fc34e923ef1b857804 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Tue, 13 May 2025 09:21:44 -0300 Subject: [PATCH 6/9] FEAT: assign embed and public key changes --- docsfera.json | 33 ++++++++++++++++++- package-lock.json | 8 ++--- package.json | 2 +- src/authentication/permissions.enum.ts | 3 +- src/modules/assign/assign.controller.ts | 13 ++++++-- src/modules/assign/assign.service.ts | 7 ++-- .../dto/create-share-metadata.dto.ts | 1 + .../share-metadata/share-metadata.service.ts | 3 +- 8 files changed, 57 insertions(+), 13 deletions(-) diff --git a/docsfera.json b/docsfera.json index 762ab8d..f6896c9 100644 --- a/docsfera.json +++ b/docsfera.json @@ -6257,6 +6257,33 @@ "access-token": [] } ] + }, + "get": { + "operationId": "AssignController_get", + "parameters": [], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + }, + { + "access-token": [] + } + ] } }, "/health": { @@ -8800,11 +8827,15 @@ }, "proposedId": { "type": "string" + }, + "type": { + "type": "string" } }, "required": [ "assetId", - "proposedId" + "proposedId", + "type" ] }, "UpdateShareMetadataDto": { diff --git a/package-lock.json b/package-lock.json index a67c2e4..17d66f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "3.37.0-beta.24", + "@dadosfera/protospack-v2": "3.37.0-beta.25", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", @@ -1400,9 +1400,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "3.37.0-beta.24", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.37.0-beta.24.tgz", - "integrity": "sha512-uf6cJkq6LjROWxJEGjSXQNengtR74cXtybJUkZRj2c438yjQ6FChEczJdx5053m0qnPpbV86QRrayB/XxVPmvQ==", + "version": "3.37.0-beta.25", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.37.0-beta.25.tgz", + "integrity": "sha512-UxAlpThepqC/HpEOVHhPdRwQP1QwGP6k5mhlStWzYwr/OsTBv5nzefZ6Y+dwXSoNFbPYt2z9xlDnRnvrUWLjAA==", "license": "ISC", "dependencies": { "@grpc/grpc-js": "^1.9.3", diff --git a/package.json b/package.json index fc1e8ac..70d6f75 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@aws-sdk/client-secrets-manager": "^3.414.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.4", "@dadosfera/protospack": "2.5.3", - "@dadosfera/protospack-v2": "3.37.0-beta.24", + "@dadosfera/protospack-v2": "3.37.0-beta.25", "@grpc/grpc-js": "^1.9.3", "@grpc/proto-loader": "^0.7.9", "@nestjs/cli": "^9.5.0", diff --git a/src/authentication/permissions.enum.ts b/src/authentication/permissions.enum.ts index ee74e22..839a52b 100644 --- a/src/authentication/permissions.enum.ts +++ b/src/authentication/permissions.enum.ts @@ -614,7 +614,8 @@ export const DADOSFERA_MODULES_KEYS = { LOG_DASHBOARD: 'logs-dashboard', ACCESS_DASHBOARD: 'access-dashboard', DANGER_ZONE: 'danger-zone', - PII: 'pii' + PII: 'pii', + PUBLIC_ASSIGN_EMBED: 'public-assign-embed', } export const DADOSFERA_MODULES: Array = [ diff --git a/src/modules/assign/assign.controller.ts b/src/modules/assign/assign.controller.ts index e83488c..361524c 100644 --- a/src/modules/assign/assign.controller.ts +++ b/src/modules/assign/assign.controller.ts @@ -3,7 +3,7 @@ import { AssignService } from './assign.service'; import { CreateAssignDto } from './dto/create-assign.dto'; import { Authenticated, RequireModule, RequireSomePermission } from 'src/decorators/authentication.decorator'; import { RequestUser, User } from 'src/decorators/user.decorator'; -import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; +import { DADOSFERA_MODULES_KEYS, PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { PackTheMetadata } from 'src/utils/PackTheMetadata'; @Controller('assign') @@ -15,10 +15,19 @@ export class AssignController { @RequireSomePermission( PERMISSIONS_GROUPS.USERS.permissions.ADMIN ) - @RequireModule('public-assign-embed') + @RequireModule(DADOSFERA_MODULES_KEYS.PUBLIC_ASSIGN_EMBED) create(@Body() createAssignDto: CreateAssignDto, @User() user: RequestUser) { const metadata = PackTheMetadata(user); return this.assignService.create(createAssignDto, metadata); } + @Get('/public-key') + @RequireSomePermission( + PERMISSIONS_GROUPS.USERS.permissions.ADMIN + ) + @RequireModule(DADOSFERA_MODULES_KEYS.PUBLIC_ASSIGN_EMBED) + async get(@User() user: RequestUser) { + const metadata = PackTheMetadata(user); + return await this.assignService.get(metadata); + } } diff --git a/src/modules/assign/assign.service.ts b/src/modules/assign/assign.service.ts index b48ef80..01a8c20 100644 --- a/src/modules/assign/assign.service.ts +++ b/src/modules/assign/assign.service.ts @@ -28,10 +28,11 @@ export class AssignService implements OnModuleInit { } async create(createAssignDto: CreateAssignDto, metadata: Metadata) { - return await lastValueFrom(this.ducService.CreateOrUpdateAssignPublicKey(createAssignDto, metadata)) + const data = await lastValueFrom(this.ducService.CreateOrUpdateAssignPublicKey(createAssignDto, metadata)) + return data; } - async get(createAssignDto: CreateAssignDto, metadata: Metadata) { - return await lastValueFrom(this.ducService.GetAssignPublicKey(createAssignDto, metadata)) + async get(metadata: Metadata) { + return await lastValueFrom(this.ducService.GetAssignPublicKey(metadata)) } } diff --git a/src/modules/share-metadata/dto/create-share-metadata.dto.ts b/src/modules/share-metadata/dto/create-share-metadata.dto.ts index db06de6..6935de1 100644 --- a/src/modules/share-metadata/dto/create-share-metadata.dto.ts +++ b/src/modules/share-metadata/dto/create-share-metadata.dto.ts @@ -1,6 +1,7 @@ export class CreateShareMetadataDto { assetId: string; proposedId: string; + type: string; } export class UpdateShareMetadataDto { diff --git a/src/modules/share-metadata/share-metadata.service.ts b/src/modules/share-metadata/share-metadata.service.ts index 0915faa..e69865b 100644 --- a/src/modules/share-metadata/share-metadata.service.ts +++ b/src/modules/share-metadata/share-metadata.service.ts @@ -32,7 +32,8 @@ export class ShareMetadataService implements OnModuleInit { this.ducService.FindOrCreateShareMetadata( { assetId: createShareDto.assetId, - proposedId: createShareDto.proposedId + proposedId: createShareDto.proposedId, + type: createShareDto.type }, metadata, ), From ced2a975472cbd9a86fc5513765d006de598bdc7 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 19 May 2025 16:36:54 -0300 Subject: [PATCH 7/9] FIX: add logger and return info --- src/modules/catalog/share/share.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/catalog/share/share.service.ts b/src/modules/catalog/share/share.service.ts index b81096a..1729f43 100644 --- a/src/modules/catalog/share/share.service.ts +++ b/src/modules/catalog/share/share.service.ts @@ -198,6 +198,7 @@ export class ShareService implements OnModuleInit { private async getShareMetadata(id: string, user: RequestUser) { const metadata = PackTheMetadata({}); + this.logger.info('GET share metadata') const info = await this.shareMetadataService.get(id, metadata); if (isJWT(id) && info ){ return info; @@ -219,7 +220,7 @@ export class ShareService implements OnModuleInit { ); if (is_data_manager || is_get) { - return; + return info; } throw new ForbiddenException( From 6fc8c0b4a89a405a0200b5b241e2e8851fa548f3 Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Tue, 20 May 2025 16:50:04 -0300 Subject: [PATCH 8/9] FEAT: add mixpanel service to track share access --- docsfera.json | 27 ++-- src/modules/catalog/catalog.module.ts | 1 + src/modules/catalog/share/share.controller.ts | 7 +- src/modules/catalog/share/share.module.ts | 4 +- src/modules/catalog/share/share.service.ts | 19 ++- src/modules/mixpanel/mixpanel.controller.ts | 80 +----------- src/modules/mixpanel/mixpanel.module.ts | 7 +- src/modules/mixpanel/mixpanel.service.ts | 118 ++++++++++++++++++ 8 files changed, 165 insertions(+), 98 deletions(-) create mode 100644 src/modules/mixpanel/mixpanel.service.ts diff --git a/docsfera.json b/docsfera.json index f6896c9..ebc41e9 100644 --- a/docsfera.json +++ b/docsfera.json @@ -5501,6 +5501,17 @@ ] } }, + "/trackEvent/{id}": { + "post": { + "operationId": "MixpanelController_trackEvent", + "parameters": [], + "responses": { + "201": { + "description": "" + } + } + } + }, "/terms-of-use/token": { "get": { "operationId": "TermsOfUseController_getToken", @@ -5995,22 +6006,6 @@ ] } }, - "/trackEvent/{id}": { - "post": { - "operationId": "MixpanelController_trackEvent", - "parameters": [], - "responses": { - "201": { - "description": "" - } - }, - "security": [ - { - "access-token": [] - } - ] - } - }, "/open-data/sharing-ocean-data": { "post": { "operationId": "OpenDataController_createUser", diff --git a/src/modules/catalog/catalog.module.ts b/src/modules/catalog/catalog.module.ts index 8320d37..8ce1579 100644 --- a/src/modules/catalog/catalog.module.ts +++ b/src/modules/catalog/catalog.module.ts @@ -11,6 +11,7 @@ import { RolesModule } from '../roles/roles.module'; import { CustomersModule } from '../customers/customers.module'; import { ShareModule } from './share/share.module'; import { CatalogService } from './catalog.service'; +import { MixpanelModule } from '../mixpanel/mixpanel.module'; const client = new CatalogClientConfiguration(); diff --git a/src/modules/catalog/share/share.controller.ts b/src/modules/catalog/share/share.controller.ts index a5240bf..efc394e 100644 --- a/src/modules/catalog/share/share.controller.ts +++ b/src/modules/catalog/share/share.controller.ts @@ -3,6 +3,7 @@ import { Get, Inject, Param, + Req, UseFilters, } from '@nestjs/common'; import { @@ -21,6 +22,7 @@ import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filt import { Language } from 'src/decorators/language.decorator'; import { LanguageEnum } from 'src/utils/languages.enum'; import { ShareService } from './share.service'; +import { Request } from 'express'; @ApiTags('Catalog') @ApiHeaders([{ name: 'dadosfera-lang', enum: LanguageEnum, required: false }]) @@ -39,10 +41,11 @@ export class ShareController { @Get('/:id') async getShareDataAsset( @Param('id') id: string, - @User() user: RequestUser + @User() user: RequestUser, + @Req() request: Request ) { this.logger.info(`GET //:id`); - return await this.catalogShareService.getOneDataAssetPublic(id, user); + return await this.catalogShareService.getOneDataAssetPublic(id, user, request); } @Get('/:id/columns-metadata') diff --git a/src/modules/catalog/share/share.module.ts b/src/modules/catalog/share/share.module.ts index 1f0c7fb..ce7021c 100644 --- a/src/modules/catalog/share/share.module.ts +++ b/src/modules/catalog/share/share.module.ts @@ -8,6 +8,7 @@ import { ShareMetadataModule } from "src/modules/share-metadata/share-metadata.m import { ShareController } from "./share.controller"; import DadosferaLogger from "@dadosfera/dadosfera-logs"; import { ShareService } from "./share.service"; +import { MixpanelModule } from "src/modules/mixpanel/mixpanel.module"; const client = new CatalogClientConfiguration(); @@ -17,7 +18,8 @@ const client = new CatalogClientConfiguration(); UsersModule, RolesModule, CustomersModule, - ShareMetadataModule + ShareMetadataModule, + MixpanelModule ], controllers: [ShareController], providers: [ShareService, DadosferaLogger], diff --git a/src/modules/catalog/share/share.service.ts b/src/modules/catalog/share/share.service.ts index 1729f43..da8d3fe 100644 --- a/src/modules/catalog/share/share.service.ts +++ b/src/modules/catalog/share/share.service.ts @@ -20,6 +20,8 @@ import { Metadata } from '@grpc/grpc-js'; import { lastValueFrom } from 'rxjs'; import { ShareMetadataService } from 'src/modules/share-metadata/share-metadata.service'; import { isJWT } from 'class-validator'; +import { MixpanelService } from 'src/modules/mixpanel/mixpanel.service'; +import { Request } from 'express'; export class ShareService implements OnModuleInit { catalogReadService: ReadService.CatalogReadServices; @@ -32,7 +34,8 @@ export class ShareService implements OnModuleInit { private readonly grpcClient: ClientGrpc, private readonly userService: UsersService, private readonly roleService: RolesService, - private shareMetadataService: ShareMetadataService, + private readonly shareMetadataService: ShareMetadataService, + private readonly mixpanelService: MixpanelService, ) { this.logger = dadosferaLogger.logger; } @@ -76,7 +79,7 @@ export class ShareService implements OnModuleInit { return result; } - async getOneDataAssetPublic(id: string, user: RequestUser) { + async getOneDataAssetPublic(id: string, user: RequestUser, request: Request) { this.logger.info("getOneDataAssetPublic: " + JSON.stringify({ id, userId: user?.user_id, @@ -85,6 +88,18 @@ export class ShareService implements OnModuleInit { try { const shareMetadata = await this.getShareMetadata(id, user); + const mixpanelTracker = { + asset: shareMetadata.assetId, + type: isJWT(id) ? 'assigned' : shareMetadata.type, + customer: shareMetadata.customerName + } + + if (user) { + await this.mixpanelService.track("share_page", user, request, mixpanelTracker); + } else { + await this.mixpanelService.trackShare(request, mixpanelTracker); + } + this.logger.info("shareMetadata: " + JSON.stringify(shareMetadata)) const metadata = PackTheMetadata({ customer_id: shareMetadata.customerId, diff --git a/src/modules/mixpanel/mixpanel.controller.ts b/src/modules/mixpanel/mixpanel.controller.ts index 04e8950..477569b 100644 --- a/src/modules/mixpanel/mixpanel.controller.ts +++ b/src/modules/mixpanel/mixpanel.controller.ts @@ -3,14 +3,13 @@ import { init } from 'mixpanel'; import { Authenticated } from 'src/decorators/authentication.decorator'; import { ApiInternalOnlyController } from 'src/decorators/swagger.decorator'; import { RequestUser, User } from 'src/decorators/user.decorator'; +import { MixpanelService } from './mixpanel.service'; @ApiInternalOnlyController() -@Authenticated() @Controller('trackEvent') export class MixpanelController { constructor( - @Inject('MIXPANEL_TOKEN') - private readonly mixpanelToken: string, + private mixpanelService: MixpanelService ) {} @Post(':id') async trackEvent( @@ -20,82 +19,11 @@ export class MixpanelController { @Req() request ) { delete body.info; - const mixpanel = init(this.mixpanelToken); - - const separator = user.username.includes('-') ? '-' : '.'; - const removeValues = [ - '.dadosferatech.dadosfera', - '.demo.dadosfera', - '.dadosferademo', - '.dadosferarh.dadosfera', - '.dadosferatech.dadosfera2', - '.dadosferatech.dadosfera', - '.dadosfera.fin', - '.dadosferafin.dadosfera', - '.praxio.dadosfera', - '.dadosfera.tech', - '.treinamentos@dadosfera.ai', - '.dadosfera2', - '.treinamentosfera', - '.dadosfera', - ]; - - let username = user.username; - - removeValues.forEach((value) => { - username = username.replace(value, ''); - }); - - username = username.split('@')?.[0]; - username = username.split('+')?.[0]; - - let firstName = username - .substring(0, username.indexOf(separator)) - .replace('dadosfera', ''); - let lastName = username - .substring(username.lastIndexOf(separator) + 1) - .replace('dadosfera', ''); - - if (!firstName) { - firstName = lastName; - lastName = ''; - } - - firstName = this.capitalize(firstName); - lastName = this.capitalize(lastName); - - await mixpanel.people.set(user.username, { - $first_name: firstName, - $last_name: lastName, - $name: this.getFullName(firstName, lastName), - $email: user.username.includes('@') - ? user.username - : user.username + '@dadosfera.ai', - customer_name: user.customer_name - }); - - await mixpanel.track(id, { - distinct_id: user.username, - customer: user.customer_name, - env: process.env.ENV, - $ip: request.ip, - $os: request.headers['sec-ch-ua-platform'] || '', - $browser: request.headers['user-agent'], - ...body, - }); + + await this.mixpanelService.track(id, user, request, body) return { id, body, user: user.username }; } - capitalize(sentence: string): string { - if (!sentence) { - return ''; - } - return sentence[0].toUpperCase() + sentence.substring(1); - } - - getFullName(firstName: string, lastName: string) { - return `${firstName}${lastName ? ' ' + lastName : ''}`; - } } diff --git a/src/modules/mixpanel/mixpanel.module.ts b/src/modules/mixpanel/mixpanel.module.ts index 98f130d..f3ed70e 100644 --- a/src/modules/mixpanel/mixpanel.module.ts +++ b/src/modules/mixpanel/mixpanel.module.ts @@ -1,6 +1,8 @@ import { Module } from '@nestjs/common'; import { getSecretFromSecretsManager } from 'src/utils/SecretManager'; import { MixpanelController } from './mixpanel.controller'; +import { MixpanelService } from './mixpanel.service'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; @Module({ controllers: [MixpanelController], @@ -8,9 +10,12 @@ import { MixpanelController } from './mixpanel.controller'; { provide: 'MIXPANEL_TOKEN', useValue: getSecretFromSecretsManager( - `${process.env.ENV}/root/mixpanel_token`, + `prd/root/mixpanel_token`, ), }, + MixpanelService, + DadosferaLogger ], + exports: [MixpanelService] }) export class MixpanelModule {} diff --git a/src/modules/mixpanel/mixpanel.service.ts b/src/modules/mixpanel/mixpanel.service.ts new file mode 100644 index 0000000..3dd1879 --- /dev/null +++ b/src/modules/mixpanel/mixpanel.service.ts @@ -0,0 +1,118 @@ +import DadosferaLogger from '@dadosfera/dadosfera-logs'; +import { Inject } from '@nestjs/common'; +import { Request } from 'express'; +import mixpanel, { init } from 'mixpanel'; +import { RequestUser } from 'src/decorators/user.decorator'; + +export class MixpanelService { + logger: DadosferaLogger; + + constructor( + @Inject('MIXPANEL_TOKEN') + private readonly mixpanelToken: string, + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + ) { + this.logger = dadosferaLogger.logger; + } + + async track(eventName: string, user: RequestUser, request: Request, body: any) { + this.logger.info("track: " + JSON.stringify({ + eventName, + ...body + })) + const mixpanel = init(this.mixpanelToken); + await this.setPeople(user, mixpanel); + + await mixpanel.track(eventName, { + distinct_id: user.username, + customer: user.customer_name, + env: process.env.ENV, + $ip: request.ip, + $os: request.headers['sec-ch-ua-platform'] || '', + $browser: request.headers['user-agent'], + ...body, + }); + return; + } + + async trackShare(request: Request, body: any) { + this.logger.info("trackShare: " + JSON.stringify(body)) + const mixpanel = init(this.mixpanelToken); + + await mixpanel.track("share_page", { + env: process.env.ENV, + $ip: request.ip, + $os: request.headers['sec-ch-ua-platform'] || '', + $browser: request.headers['user-agent'], + ...body, + }); + return; + } + + private async setPeople(user: RequestUser, mixpanel: mixpanel.Mixpanel) { + const separator = user.username.includes('-') ? '-' : '.'; + const removeValues = [ + '.dadosferatech.dadosfera', + '.demo.dadosfera', + '.dadosferademo', + '.dadosferarh.dadosfera', + '.dadosferatech.dadosfera2', + '.dadosferatech.dadosfera', + '.dadosfera.fin', + '.dadosferafin.dadosfera', + '.praxio.dadosfera', + '.dadosfera.tech', + '.treinamentos@dadosfera.ai', + '.dadosfera2', + '.treinamentosfera', + '.dadosfera', + ]; + + let username = user.username; + + removeValues.forEach((value) => { + username = username.replace(value, ''); + }); + + username = username.split('@')?.[0]; + username = username.split('+')?.[0]; + + let firstName = username + .substring(0, username.indexOf(separator)) + .replace('dadosfera', ''); + let lastName = username + .substring(username.lastIndexOf(separator) + 1) + .replace('dadosfera', ''); + + if (!firstName) { + firstName = lastName; + lastName = ''; + } + + firstName = this.capitalize(firstName); + lastName = this.capitalize(lastName); + + await mixpanel.people.set(user.username, { + $first_name: firstName, + $last_name: lastName, + $name: this.getFullName(firstName, lastName), + $email: user.username.includes('@') + ? user.username + : user.username + '@dadosfera.ai', + customer_name: user.customer_name, + }); + } + + private capitalize(sentence: string): string { + if (!sentence) { + return ''; + } + + return sentence[0].toUpperCase() + sentence.substring(1); + } + + private getFullName(firstName: string, lastName: string) { + return `${firstName}${lastName ? ' ' + lastName : ''}`; + } +} From 53059c0a0d66a7e1c2e946e10be5cfdd8f82cb8c Mon Sep 17 00:00:00 2001 From: marcos-silva-rodrigues Date: Mon, 16 Jun 2025 18:09:25 -0300 Subject: [PATCH 9/9] FIX: import --- docsfera.json | 243 ++++++++++++++++++++++++- src/modules/api-key/api-key.service.ts | 2 +- 2 files changed, 236 insertions(+), 9 deletions(-) diff --git a/docsfera.json b/docsfera.json index ebc41e9..96612ac 100644 --- a/docsfera.json +++ b/docsfera.json @@ -20,14 +20,6 @@ ], "type": "string" } - }, - { - "name": "origin", - "required": true, - "in": "header", - "schema": { - "type": "string" - } } ], "requestBody": { @@ -6281,6 +6273,158 @@ ] } }, + "/api-key": { + "post": { + "operationId": "ApiKeyController_create", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyDto" + } + } + } + }, + "responses": { + "201": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyResponseDto" + } + } + } + }, + "default": { + "description": "", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyResponseDto" + } + } + } + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + }, + "get": { + "operationId": "ApiKeyController_findAll", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ApiKeyBaseResponseDto" + } + } + } + } + }, + "default": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ApiKeyBaseResponseDto" + } + } + } + } + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + } + }, + "/api-key/{id}": { + "delete": { + "operationId": "ApiKeyController_remove", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "id", + "required": true, + "in": "path", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "ApiKey" + ], + "security": [ + { + "access-token": [] + } + ] + } + }, "/health": { "get": { "operationId": "HealthController_check", @@ -9219,6 +9363,89 @@ "required": [ "publicKey" ] + }, + "CreateApiKeyDto": { + "type": "object", + "properties": { + "permissions": { + "description": "Array of permission IDs", + "type": "array", + "items": { + "type": "number" + } + } + }, + "required": [ + "permissions" + ] + }, + "CreateApiKeyResponseDto": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "key_mask": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PermissionDto" + } + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "created_by": { + "type": "string" + }, + "key": { + "type": "string" + } + }, + "required": [ + "id", + "key_mask", + "permissions", + "created_at", + "created_by", + "key" + ] + }, + "ApiKeyBaseResponseDto": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "key_mask": { + "type": "string" + }, + "permissions": { + "type": "array", + "items": { + "$ref": "#/components/schemas/PermissionDto" + } + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "created_by": { + "type": "string" + } + }, + "required": [ + "id", + "key_mask", + "permissions", + "created_at", + "created_by" + ] } } } diff --git a/src/modules/api-key/api-key.service.ts b/src/modules/api-key/api-key.service.ts index ea96801..3e92053 100644 --- a/src/modules/api-key/api-key.service.ts +++ b/src/modules/api-key/api-key.service.ts @@ -3,10 +3,10 @@ import { ClientGrpc } from '@nestjs/microservices'; import { CreateApiKeyDto, CreateApiKeyResponseDto, ApiKeyBaseResponseDto } from './dto/api-key.dto'; import { RequestUser } from 'src/decorators/user.decorator'; import { DucClient } from '../duc/client.config'; -import { PackTheMetadata } from '../../utils/ PackTheMetadata'; import { ApiKeyWriteProtoService } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/write-service'; import { lastValueFrom } from 'rxjs'; import { ProtoServices } from '@dadosfera/protospack-v2/dist/lib/Duc'; +import { PackTheMetadata } from 'src/utils/PackTheMetadata'; @Injectable() export class ApiKeyService implements OnModuleInit {