diff --git a/.github/workflows/deploy-manually.yml b/.github/workflows/deploy-manually.yml index 87e306e..5f95da0 100644 --- a/.github/workflows/deploy-manually.yml +++ b/.github/workflows/deploy-manually.yml @@ -13,11 +13,6 @@ on: options: - stg - prd - push_to_dockerhub: - description: "Push image to Dockerhub?" - required: true - type: boolean - default: false jobs: extract_environment: @@ -118,23 +113,6 @@ jobs: docker compose -f build.docker-compose.yml build docker compose -f build.docker-compose.yml push - - name: Login to Docker Hub - if: ${{inputs.push_to_dockerhub}} - uses: docker/login-action@v2 - with: - username: dadosfera - password: ${{ secrets.DOCKERHUB_PASSWORD }} - - - name: Build, Tag, and Push Image to Dockerhub - if: ${{inputs.push_to_dockerhub}} - env: - ENV: ${{ needs.extract_environment.outputs.environment }} - IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }} - ACCOUNT_ID: ${{ steps.aws.outputs.aws-account-id }} - run: | - docker compose -f build.docker-compose.dockerhub.yml build - docker compose -f build.docker-compose.dockerhub.yml push - # - name: Create ZIP file to Deploy AWS Beanstalk # env: # ENV: ${{ needs.extract_environment.outputs.environment }} @@ -166,7 +144,10 @@ jobs: helmfile-deploy: needs: [extract_environment, semantic_release, build_ecr_image] - runs-on: [self-hosted, "prd-azure"] + env: + HOME: /home/runner + runs-on: [self-hosted, "prd-oracle"] + environment: ${{ needs.extract_environment.outputs.environment }} steps: - name: Checkout code @@ -177,13 +158,17 @@ jobs: with: version: 'v3.9.0' - - name: Install Azure ClI + - name: Install OCI CLI run: | - curl -sL https://aka.ms/InstallAzureCLIDeb | bash + bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)" -- --accept-all-defaults + echo "$HOME/bin" >> $GITHUB_PATH - - uses: azure/login@v2 - with: - creds: '{"clientId":"${{ secrets.ARM_CLIENT_ID }}","clientSecret":"${{ secrets.ARM_CLIENT_SECRET }}","subscriptionId":"${{ secrets.ARM_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.ARM_TENANT_ID }}"}' + - name: Configure OCI CLI + run: | + mkdir -p ~/.oci || true + echo "${{ secrets.OCI_CONFIG }}" > ~/.oci/config + echo "${{ secrets.OCI_PRIVATE_KEY }}" > ~/.oci/oci_api_key.pem + chmod 600 ~/.oci/oci_api_key.pem - name: Set up Python uses: actions/setup-python@v4 @@ -194,24 +179,31 @@ jobs: run: | wget https://github.com/helmfile/helmfile/releases/download/v0.148.0/helmfile_0.148.0_linux_amd64.tar.gz tar -xzf helmfile_0.148.0_linux_amd64.tar.gz - mv helmfile /usr/local/bin/ + sudo mv helmfile /usr/local/bin/ helmfile --version - name: Install Helm Diff Plugin run: helm plugin install https://github.com/databus23/helm-diff || true - - name: Setup kubectl - uses: azure/setup-kubectl@v1 - with: - version: 'v1.30.1' - - - name: Authenticate with cluster + - name: Authenticate with OKE cluster env: - CLUSTER_NAME: platform-${{ needs.extract_environment.outputs.environment }} - run: az aks get-credentials --resource-group dadosfera-prd --name ${CLUSTER_NAME} --overwrite-existing + ENV: ${{ needs.extract_environment.outputs.environment }} + STG_CLUSTER_ID: "ocid1.cluster.oc1.sa-saopaulo-1.aaaaaaaagh3jvln52a3ebm3dodx6emmhv5bmfs7i7sv2k4zkbcbrzcl6v37q" + PRD_CLUSTER_ID: "ocid1.cluster.oc1.sa-saopaulo-1.aaaaaaaanf3vptl6hc2tzd4enfd2hfpsht3wikxww5xejc3l7cwfm6l3sndq" + run: | + if [ "$ENV" = "stg" ]; then + CLUSTER_ID=$STG_CLUSTER_ID + elif [ "$ENV" = "prd" ]; then + CLUSTER_ID=$PRD_CLUSTER_ID + else + echo "Unknown environment: $ENV" + exit 1 + fi + + oci ce cluster create-kubeconfig --cluster-id ${CLUSTER_ID} --file $HOME/.kube/config --region sa-saopaulo-1 --token-version 2.0.0 --kube-endpoint PRIVATE_ENDPOINT - name: Run Helmfile Apply env: ENV: ${{ needs.extract_environment.outputs.environment }} IMAGE_TAG: ${{ needs.semantic_release.outputs.new_release_version }} - run: helmfile -f helmfiles/${ENV}.yaml sync --set image.tag=$IMAGE_TAG + run: helmfile -f deploy/helmfiles/${ENV}.yaml sync --set image.tag=$IMAGE_TAG diff --git a/.github/workflows/validate-k8s.yml b/.github/workflows/validate-k8s.yml index 17ed975..4d23e66 100644 --- a/.github/workflows/validate-k8s.yml +++ b/.github/workflows/validate-k8s.yml @@ -4,7 +4,7 @@ on: pull_request: branches: - main - - stg + - beta jobs: extract_environment: @@ -21,17 +21,13 @@ jobs: fi id: extract_environment - helmfile-deploy: + helmfile-check: + env: + HOME: /home/runner needs: [extract_environment] - runs-on: [self-hosted, "prd-azure"] - + environment: ${{ needs.extract_environment.outputs.environment }} + runs-on: [self-hosted, "prd-oracle"] steps: - - name: Summary - env: - ENV: ${{ needs.extract_environment.outputs.environment }} - run: | - echo "### :rocket: Deploy da branch \`$GITHUB_REF_NAME\` para o environment ($ENV)" >> $GITHUB_STEP_SUMMARY - - name: Checkout code uses: actions/checkout@v3 @@ -40,13 +36,28 @@ jobs: with: version: 'v3.9.0' - - name: Install Azure ClI + - name: Determine DNS_HOST based on environment + id: set_dns + env: + ENV: ${{ needs.extract_environment.outputs.environment }} run: | - curl -sL https://aka.ms/InstallAzureCLIDeb | bash + if [ "$ENV" = "prd" ]; then + echo "dns_host=dadosfera.ai" >> $GITHUB_OUTPUT + elif [ "$ENV" = "stg" ]; then + echo "dns_host=stg.dadosfera.ai" >> $GITHUB_OUTPUT + fi + + - name: Install OCI CLI + run: | + bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)" -- --accept-all-defaults + echo "$HOME/bin" >> $GITHUB_PATH - - uses: azure/login@v2 - with: - creds: '{"clientId":"${{ secrets.ARM_CLIENT_ID }}","clientSecret":"${{ secrets.ARM_CLIENT_SECRET }}","subscriptionId":"${{ secrets.ARM_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.ARM_TENANT_ID }}"}' + - name: Configure OCI CLI + run: | + mkdir -p ~/.oci || true + echo "${{ secrets.OCI_CONFIG }}" > ~/.oci/config + echo "${{ secrets.OCI_PRIVATE_KEY }}" > ~/.oci/oci_api_key.pem + chmod 600 ~/.oci/oci_api_key.pem - name: Set up Python uses: actions/setup-python@v4 @@ -57,23 +68,35 @@ jobs: run: | wget https://github.com/helmfile/helmfile/releases/download/v0.148.0/helmfile_0.148.0_linux_amd64.tar.gz tar -xzf helmfile_0.148.0_linux_amd64.tar.gz - mv helmfile /usr/local/bin/ + sudo mv helmfile /usr/local/bin/ helmfile --version - name: Install Helm Diff Plugin run: helm plugin install https://github.com/databus23/helm-diff || true + - name: Authenticate with OKE cluster + env: + ENV: ${{ needs.extract_environment.outputs.environment }} + STG_CLUSTER_ID: "ocid1.cluster.oc1.sa-saopaulo-1.aaaaaaaagh3jvln52a3ebm3dodx6emmhv5bmfs7i7sv2k4zkbcbrzcl6v37q" + PRD_CLUSTER_ID: "ocid1.cluster.oc1.sa-saopaulo-1.aaaaaaaanf3vptl6hc2tzd4enfd2hfpsht3wikxww5xejc3l7cwfm6l3sndq" + run: | + if [ "$ENV" = "stg" ]; then + CLUSTER_ID=$STG_CLUSTER_ID + elif [ "$ENV" = "prd" ]; then + CLUSTER_ID=$PRD_CLUSTER_ID + else + echo "Unknown environment: $ENV" + exit 1 + fi + + oci ce cluster create-kubeconfig --cluster-id ${CLUSTER_ID} --file $HOME/.kube/config --region sa-saopaulo-1 --token-version 2.0.0 --kube-endpoint PRIVATE_ENDPOINT + - name: Setup kubectl uses: azure/setup-kubectl@v1 with: version: 'v1.30.1' - - name: Authenticate with cluster - env: - CLUSTER_NAME: platform-${{ needs.extract_environment.outputs.environment }} - run: az aks get-credentials --resource-group dadosfera-prd --name ${CLUSTER_NAME} --overwrite-existing - - name: Run Helmfile Diff env: ENV: ${{ needs.extract_environment.outputs.environment }} - run: helmfile -f helmfiles/${ENV}.yaml diff + run: helmfile -f deploy/helmfiles/${ENV}.yaml diff diff --git a/maestro/Chart.yaml b/deploy/helm-chart/Chart.yaml similarity index 100% rename from maestro/Chart.yaml rename to deploy/helm-chart/Chart.yaml diff --git a/maestro/templates/NOTES.txt b/deploy/helm-chart/templates/NOTES.txt similarity index 100% rename from maestro/templates/NOTES.txt rename to deploy/helm-chart/templates/NOTES.txt diff --git a/maestro/templates/deployment.yaml b/deploy/helm-chart/templates/deployment.yaml similarity index 82% rename from maestro/templates/deployment.yaml rename to deploy/helm-chart/templates/deployment.yaml index c648514..c03758a 100644 --- a/maestro/templates/deployment.yaml +++ b/deploy/helm-chart/templates/deployment.yaml @@ -1,16 +1,16 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: maestro + name: {{ .Values.app_name }} namespace: applications labels: - app: maestro + app: {{ .Values.app_name }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: - app: maestro + app: {{ .Values.app_name }} strategy: rollingUpdate: @@ -20,22 +20,17 @@ spec: template: metadata: labels: - app: maestro + app: {{ .Values.app_name }} spec: imagePullSecrets: - name: {{ .Values.imagePullSecrets }} nodeSelector: "beta.kubernetes.io/os": linux + {{- if .Values.affinity }} affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: application - operator: In - values: - - general + {{- toYaml .Values.affinity | nindent 8 }} + {{- end }} tolerations: - key: "kubernetes.azure.com/scalesetpriority" @@ -48,13 +43,10 @@ spec: image: {{ .Values.image.repository }}:{{ .Values.image.tag }} ports: - containerPort: {{ .Values.containerPort }} + {{- if .Values.resources }} resources: - requests: - cpu: {{ .Values.resources.requests.cpu }} - memory: {{ .Values.resources.requests.memory }} - limits: - cpu: {{ .Values.resources.limits.cpu }} - memory: {{ .Values.resources.limits.memory }} + {{- toYaml .Values.resources | nindent 12 }} + {{- end }} env: - name: AWS_IDENTITY_POOL_ID value: {{ .Values.maestro.aws_identity_pool_id }} @@ -100,6 +92,8 @@ spec: value: {{ .Values.maestro.open_customer_id }} - name: OPEN_GROUP_ID value: {{ .Values.maestro.open_group_id }} + - name: DEDICATED_PROXY + value: {{ .Values.maestro.dedicated_proxy }} - name: JWT_PRIVATE_KEY valueFrom: secretKeyRef: @@ -108,15 +102,15 @@ spec: - name: AWS_ACCESS_KEY_ID valueFrom: secretKeyRef: - name: prd-maestro + name: prd-{{ .Values.app_name }} key: AWS_ACCESS_KEY_ID - name: AWS_SECRET_ACCESS_KEY valueFrom: secretKeyRef: - name: prd-maestro + name: prd-{{ .Values.app_name }} key: AWS_SECRET_ACCESS_KEY - name: AWS_DEFAULT_REGION valueFrom: secretKeyRef: - name: prd-maestro + name: prd-{{ .Values.app_name }} key: AWS_DEFAULT_REGION diff --git a/maestro/templates/ingress-private.yaml b/deploy/helm-chart/templates/ingress-private.yaml similarity index 78% rename from maestro/templates/ingress-private.yaml rename to deploy/helm-chart/templates/ingress-private.yaml index 97708bc..1ace950 100644 --- a/maestro/templates/ingress-private.yaml +++ b/deploy/helm-chart/templates/ingress-private.yaml @@ -10,8 +10,12 @@ metadata: generation: 1 labels: - app: maestro + app: {{ .Values.app_name }} + {{- if .Values.maestro.dedicated_proxy}} + name: open-data-{{ .Values.app_name }} + {{- else }} name: open-data + {{- end }} namespace: applications spec: ingressClassName: nginx @@ -21,7 +25,7 @@ spec: paths: - backend: service: - name: maestro + name: {{ .Values.app_name }} port: number: {{ .Values.ingress.port }} path: /open-data/sharing-ocean-data diff --git a/maestro/templates/ingress.yaml b/deploy/helm-chart/templates/ingress.yaml similarity index 55% rename from maestro/templates/ingress.yaml rename to deploy/helm-chart/templates/ingress.yaml index faf1305..1594baa 100644 --- a/maestro/templates/ingress.yaml +++ b/deploy/helm-chart/templates/ingress.yaml @@ -3,14 +3,20 @@ kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/proxy-body-size: "0" + nginx.ingress.kubernetes.io/proxy-read-timeout: "300" + nginx.ingress.kubernetes.io/proxy-connect-timeout: "300" + nginx.ingress.kubernetes.io/proxy-send-timeout: "300" nginx.ingress.kubernetes.io/server-snippet: | underscores_in_headers on; ignore_invalid_headers on; + {{- if .Values.maestro.restricted_ip}} + nginx.ingress.kubernetes.io/whitelist-source-range: {{ .Values.maestro.restricted_ip }} + {{- end }} generation: 1 labels: - app: maestro - name: maestro + app: {{ .Values.app_name }} + name: {{ .Values.app_name }} namespace: applications spec: ingressClassName: nginx @@ -20,7 +26,7 @@ spec: paths: - backend: service: - name: maestro + name: {{ .Values.app_name }} port: number: {{ .Values.ingress.port }} path: / diff --git a/maestro/templates/secret.yaml b/deploy/helm-chart/templates/secret.yaml similarity index 89% rename from maestro/templates/secret.yaml rename to deploy/helm-chart/templates/secret.yaml index 9e2fd37..3bbf5ec 100644 --- a/maestro/templates/secret.yaml +++ b/deploy/helm-chart/templates/secret.yaml @@ -1,17 +1,17 @@ apiVersion: external-secrets.io/v1beta1 kind: ExternalSecret metadata: - name: prd-maestro + name: prd-{{ .Values.app_name }} namespace: applications labels: - app: maestro + app: {{ .Values.app_name }} spec: refreshInterval: 1h secretStoreRef: name: secretsmanager-prd kind: SecretStore target: - name: prd-maestro + name: prd-{{ .Values.app_name }} creationPolicy: Owner data: - secretKey: AWS_ACCESS_KEY_ID diff --git a/maestro/templates/service.yaml b/deploy/helm-chart/templates/service.yaml similarity index 63% rename from maestro/templates/service.yaml rename to deploy/helm-chart/templates/service.yaml index 75f635a..1f7c988 100644 --- a/maestro/templates/service.yaml +++ b/deploy/helm-chart/templates/service.yaml @@ -1,18 +1,18 @@ apiVersion: v1 kind: Service metadata: - name: maestro + name: {{ .Values.app_name }} namespace: applications labels: - app: maestro + app: {{ .Values.app_name }} spec: type: ClusterIP ports: - - name: maestro + - name: {{ .Values.app_name }} protocol: TCP port: {{ .Values.service.port }} targetPort: {{ .Values.service.targetPort }} selector: - app: maestro + app: {{ .Values.app_name }} diff --git a/maestro/values.yaml b/deploy/helm-chart/values.yaml similarity index 83% rename from maestro/values.yaml rename to deploy/helm-chart/values.yaml index 14dd9e4..6c39327 100644 --- a/maestro/values.yaml +++ b/deploy/helm-chart/values.yaml @@ -9,6 +9,7 @@ image: pullPolicy: IfNotPresent # Overrides the image tag whose default is the chart appVersion. tag: 1.56.0 +app_name: maestro containerPort: 3333 imagePullSecrets: "applications-secrets-ecr-auth-token-external-secret" service: @@ -42,9 +43,21 @@ maestro: upload_file_agent_connection: cbc2f881-58c4-4d60-8003-0979b0b5b911 open_customer_id: f239718a-a271-4ef9-ae7e-02a2f0f3aa6e open_group_id: 401573bb-334f-44b2-b30e-88d4cea31ae9 + dedicated_proxy: "" + restricted_ip: "" autoscaling: enabled: false minReplicas: 1 maxReplicas: 100 targetCPUUtilizationPercentage: 80 targetMemoryUtilizationPercentage: 80 + +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: name + operator: In + values: + - general diff --git a/deploy/helmfiles/prd.yaml b/deploy/helmfiles/prd.yaml new file mode 100644 index 0000000..e712071 --- /dev/null +++ b/deploy/helmfiles/prd.yaml @@ -0,0 +1,53 @@ +charts: + - name: maestro + chart: ../helm-chart + values: + - ../helm-chart/values.yaml + set: + - name: app_name + value: maestro + - name: maestro.duc_url + value: duc.dadosfera.ai + - name: hostname + value: maestro.dadosfera.ai + - name: maestro.pi_factory_url + value: pi-factory.dadosfera.ai + - name: maestro.in_factory_url + value: in-factory.dadosfera.ai + - name: maestro.tr_factory_url + value: in-factory.dadosfera.ai + - name: maestro.open_customer_id + value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d + - name: maestro.open_group_id + value: c0afdcce-c5be-40d0-9d1d-2d271121f14a + - name: replicaCount + value: 2 + + - name: unimed-maestro + chart: ../helm-chart + values: + - ../helm-chart/values.yaml + set: + - name: app_name + value: maestro-unimed + - name: maestro.duc_url + value: duc.dadosfera.ai + - name: hostname + value: maestro-unimed.dadosfera.ai + - name: maestro.pi_factory_url + value: pi-factory.dadosfera.ai + - name: maestro.in_factory_url + value: in-factory.dadosfera.ai + - name: maestro.tr_factory_url + value: in-factory.dadosfera.ai + - name: maestro.open_customer_id + value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d + - name: maestro.open_group_id + value: c0afdcce-c5be-40d0-9d1d-2d271121f14a + # Customer id + - name: maestro.dedicated_proxy + value: dea2c27f-0973-4588-a2e0-9e31b64c7ffd + - name: replicaCount + value: 1 + - name: maestro.restricted_ip + value: "177.52.172.0/24,57.151.113.140/30" diff --git a/deploy/helmfiles/stg.yaml b/deploy/helmfiles/stg.yaml new file mode 100644 index 0000000..3fb9d2d --- /dev/null +++ b/deploy/helmfiles/stg.yaml @@ -0,0 +1,56 @@ +charts: + - name: maestro + chart: ../helm-chart + values: + - ../helm-chart/values.yaml + set: + - name: maestro.duc_url + value: duc.stg.dadosfera.ai + - name: hostname + value: maestro.stg.dadosfera.ai + - name: maestro.pi_factory_url + value: pi-factory.stg.dadosfera.ai + - name: maestro.in_factory_url + value: in-factory.stg.dadosfera.ai + - name: maestro.tr_factory_url + value: in-factory.stg.dadosfera.ai + - name: maestro.open_customer_id + value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d + - name: maestro.open_group_id + value: e3f98a2f-7748-4981-8505-7695c8ca8218 + - name: replicaCount + value: 1 + + # Environment to test Network Policies + - name: private-maestro + chart: ../helm-chart + values: + - ../helm-chart/values.yaml + set: + - name: app_name + value: maestro-private + - name: maestro.duc_url + value: duc.stg.dadosfera.ai + - name: hostname + value: private-maestro.stg.dadosfera.ai + - name: maestro.pi_factory_url + value: pi-factory.dadosfera.ai + - name: maestro.in_factory_url + value: in-factory.stg.dadosfera.ai + - name: maestro.tr_factory_url + value: in-factory.dadosfera.ai + - name: maestro.open_customer_id + value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d + - name: maestro.open_group_id + value: e3f98a2f-7748-4981-8505-7695c8ca8218 + # Customer id + - name: maestro.dedicated_proxy + value: 14d52fd4-d83d-4cdd-be34-bf11cc28b3bd + - name: replicaCount + value: 1 + - name: affinity + value: null + - name: resources + value: null + - name: maestro.restricted_ip + value: "57.151.113.140/30" diff --git a/docsfera.json b/docsfera.json index a3c2b47..04ae33d 100644 --- a/docsfera.json +++ b/docsfera.json @@ -969,6 +969,41 @@ ] } }, + "/users/download": { + "get": { + "operationId": "UsersController_downloadUsersInCsv", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Users" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, "/users/hierarchies": { "get": { "operationId": "UsersController_getAllHierarchies", @@ -3874,6 +3909,85 @@ ] } }, + "/catalog/download": { + "get": { + "operationId": "CatalogController_dowloadAsserts", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "size", + "required": false, + "in": "query", + "description": "Número de registros a ser retornado", + "schema": { + "minimum": 1, + "maximum": 10000, + "type": "number" + } + }, + { + "name": "page", + "required": false, + "in": "query", + "description": "Usado para paginação, em conjunto com `size`", + "schema": { + "minimum": 1, + "type": "number" + } + }, + { + "name": "sort_by", + "required": false, + "in": "query", + "description": "Campo do data asset para usar na ordenação. Padrão: `created_at` ", + "schema": { + "type": "string" + } + }, + { + "name": "order", + "required": false, + "in": "query", + "description": "Tipo de ordenação - `asc`: crescente; `desc`: decrescente ", + "schema": { + "default": "asc", + "enum": [ + "asc", + "desc" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, "/catalog/data-asset": { "get": { "operationId": "CatalogController_findByPipelineAndObject", @@ -5001,6 +5115,509 @@ ] } }, + "/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": [] + } + ] + } + }, + "/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": [] + } + ] + } + }, + "/trackEvent/{id}": { + "post": { + "operationId": "MixpanelController_trackEvent", + "parameters": [], + "responses": { + "201": { + "description": "" + } + } + } + }, "/terms-of-use/token": { "get": { "operationId": "TermsOfUseController_getToken", @@ -5495,257 +6112,6 @@ ] } }, - "/trackEvent/{id}": { - "post": { - "operationId": "MixpanelController_trackEvent", - "parameters": [], - "responses": { - "201": { - "description": "" - } - }, - "security": [ - { - "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": [] - }, - { - "access-token": [] - }, - { - "access-token": [] - } - ] - } - }, "/open-data/sharing-ocean-data": { "post": { "operationId": "OpenDataController_createUser", @@ -6169,6 +6535,238 @@ } ] } + }, + "/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": [] + } + ] + }, + "get": { + "operationId": "AssignController_get", + "parameters": [], + "responses": { + "200": { + "description": "", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, + "/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", + "parameters": [], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Health" + ] + } } }, "info": { @@ -8628,6 +9226,101 @@ "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" + ] + }, + "CreateShareMetadataDto": { + "type": "object", + "properties": { + "assetId": { + "type": "string" + }, + "proposedId": { + "type": "string" + }, + "type": { + "type": "string" + } + }, + "required": [ + "assetId", + "proposedId", + "type" + ] + }, + "UpdateShareMetadataDto": { + "type": "object", + "properties": { + "shareId": { + "type": "string" + }, + "type": { + "type": "string" + } + }, + "required": [ + "shareId", + "type" + ] + }, "ConnectionTestPingReq": { "type": "object", "properties": { @@ -8922,67 +9615,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": { @@ -9123,6 +9755,100 @@ "required": [ "policies" ] + }, + "CreateAssignDto": { + "type": "object", + "properties": { + "publicKey": { + "type": "string" + } + }, + "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/environment.d.ts b/environment.d.ts index c910516..3558f26 100644 --- a/environment.d.ts +++ b/environment.d.ts @@ -14,6 +14,7 @@ declare global { AWS_REGION: string; OPEN_GROUP_ID: string; OPEN_CUSTOMER_ID: string; + DEDICATED_PROXY: string; } } } diff --git a/helmfiles/prd.yaml b/helmfiles/prd.yaml deleted file mode 100644 index e4146e8..0000000 --- a/helmfiles/prd.yaml +++ /dev/null @@ -1,22 +0,0 @@ -charts: - - name: maestro - chart: ../maestro - values: - - ../maestro/values.yaml - set: - - name: maestro.duc_url - value: duc.dadosfera.ai - - name: hostname - value: maestro.dadosfera.ai - - name: maestro.pi_factory_url - value: pi-factory.dadosfera.ai - - name: maestro.in_factory_url - value: in-factory.dadosfera.ai - - name: maestro.tr_factory_url - value: in-factory.dadosfera.ai - - name: maestro.open_customer_id - value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d - - name: maestro.open_group_id - value: c0afdcce-c5be-40d0-9d1d-2d271121f14a - - name: replicaCount - value: 2 diff --git a/helmfiles/stg.yaml b/helmfiles/stg.yaml deleted file mode 100644 index 5782e36..0000000 --- a/helmfiles/stg.yaml +++ /dev/null @@ -1,22 +0,0 @@ -charts: - - name: maestro - chart: ../maestro - values: - - ../maestro/values.yaml - set: - - name: maestro.duc_url - value: duc.stg.dadosfera.ai - - name: hostname - value: maestro.stg.dadosfera.ai - - name: maestro.pi_factory_url - value: pi-factory.dadosfera.ai - - name: maestro.in_factory_url - value: in-factory.stg.dadosfera.ai - - name: maestro.tr_factory_url - value: in-factory.dadosfera.ai - - name: maestro.open_customer_id - value: b3e3dfe5-b992-4586-a73c-c0b0c00f615d - - name: maestro.open_group_id - value: e3f98a2f-7748-4981-8505-7695c8ca8218 - - name: replicaCount - value: 1 \ No newline at end of file diff --git a/maestro/.helmignore b/maestro/.helmignore deleted file mode 100644 index 0e8a0eb..0000000 --- a/maestro/.helmignore +++ /dev/null @@ -1,23 +0,0 @@ -# Patterns to ignore when building packages. -# This supports shell glob matching, relative path matching, and -# negation (prefixed with !). Only one pattern per line. -.DS_Store -# Common VCS dirs -.git/ -.gitignore -.bzr/ -.bzrignore -.hg/ -.hgignore -.svn/ -# Common backup files -*.swp -*.bak -*.tmp -*.orig -*~ -# Various IDEs -.project -.idea/ -*.tmproj -.vscode/ diff --git a/src/app.module.ts b/src/app.module.ts index 1cb7032..9de9b19 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -30,6 +30,9 @@ import { OpenDataModule } from './modules/open-data/open-data.module'; import { ThemeModule } from './modules/theme/theme.module'; import { IdentityProviderModule } from './modules/identity-provider/identity-provider.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'; +import { ApiKeyModule } from './modules/api-key/api-key.module'; @Module({ providers: [ @@ -63,10 +66,15 @@ import { NetworkPolicyModule } from './modules/network-policy/network-policy.mod CustomersModule, OpenDataModule, ThemeModule, + NetworkPolicyModule, + AssignModule, + ShareMetadataModule, + NetworkPolicyModule, + ApiKeyModule, + IdentityProviderModule, + NetworkPolicyModule, //Always leave HealthModule last, so it is on the bottom of swagger HealthModule, - IdentityProviderModule, - NetworkPolicyModule ], }) export class AppModule {} diff --git a/src/authentication/authentication.guard.ts b/src/authentication/authentication.guard.ts index bc69f8c..9543362 100644 --- a/src/authentication/authentication.guard.ts +++ b/src/authentication/authentication.guard.ts @@ -4,6 +4,7 @@ import { OnApplicationBootstrap, ExecutionContext, Inject, + ForbiddenException, } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import assert from 'assert'; @@ -17,6 +18,7 @@ import { import { RequestUser } from '../decorators/user.decorator'; import ErrorBuilder from '../utils/ErrorBuilder'; import ErrorCodes from '../utils/errorCodes'; +import { ApiKeyService } from 'src/modules/api-key/api-key.service'; @Injectable() export class AuthenticationGuard @@ -31,6 +33,7 @@ export class AuthenticationGuard dadosferaLogger: DadosferaLogger, private reflector: Reflector, private authClient: AuthClientService, + private apiKeyService: ApiKeyService ) { this.pems = new Map(); this.logger = dadosferaLogger.logger; @@ -48,20 +51,40 @@ export class AuthenticationGuard }); } - canActivate(ctx: ExecutionContext): boolean { + async canActivate(ctx: ExecutionContext): Promise { const authFunctions = this.reflector.getAllAndMerge< AuthenticationFunction[] >(AUTH_FUNCTION_KEY, [ctx.getClass(), ctx.getHandler()]); const mustBeAuthenticated = authFunctions.length > 0; - const request = ctx.switchToHttp().getRequest(); - const accessToken = this.validateToken(request, mustBeAuthenticated); - if (!mustBeAuthenticated) { // no need to be authenticated return true; } + const request = ctx.switchToHttp().getRequest(); + const apiKey = request.get('X-api-key'); + if (apiKey) { + const { + api_key + } = await this.apiKeyService.get(apiKey); + + request.user = { + user_id: api_key.user_id, + username: api_key.username, + permissions: api_key.permissions, + customer_id: api_key.customer_id, + customer_name: api_key.customer_name, + customer_tier: api_key.customer_tier, + customer_modules: api_key.customer_modules, + access_token: apiKey, + }; + + return true; + } + + const accessToken = this.validateToken(request, mustBeAuthenticated); + if (!accessToken) { // couldn't load valid token throw new ErrorBuilder(ErrorCodes.AUTH.UNAUTHORIZED); @@ -113,6 +136,18 @@ export class AuthenticationGuard return false; } + // Bloquear outros customer de usar o maestor dedicado + const DEDICATED_PROXY = process.env.DEDICATED_PROXY || ''; + if (DEDICATED_PROXY !== '' && DEDICATED_PROXY !== accessTokenPayload.customer_id) { + throw new ErrorBuilder(ErrorCodes.AUTH.FORBIDDEN); + } + + // Bloquear o customer de acesso o maestro publico + // const hasNetworkPolicyModule = accessTokenPayload.customer_modules.includes('network-policy'); + // if (hasNetworkPolicyModule && DEDICATED_PROXY === '') { + // throw new ForbiddenException(); + // } + request.accessTokenPayload = accessTokenPayload; request.user = { user_id: accessTokenPayload.user_id, 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/main.ts b/src/main.ts index eff6350..a35e310 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import helmet from 'helmet'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; +import { json, urlencoded } from 'express'; import { AppModule } from './app.module'; import { writeFileSync } from 'fs'; @@ -31,6 +32,12 @@ async function bootstrap() { resave: false, saveUninitialized: false, })); + + if (process.env.ENV === 'prd') { + app.use('/catalog/register-dataset', json({ limit: '10mb' })); + app.use('/catalog/register-dataset', urlencoded({ extended: true, limit: '10mb' })); + } + configureSwagger(app); await app.listen(3333); if (process.env.KILL_AFTER_START) await app.close(); diff --git a/src/modules/api-key/api-key.controller.ts b/src/modules/api-key/api-key.controller.ts new file mode 100644 index 0000000..bc2c51c --- /dev/null +++ b/src/modules/api-key/api-key.controller.ts @@ -0,0 +1,68 @@ +import { Controller, Get, Post, Body, Param, Delete, UseFilters, Inject } from '@nestjs/common'; +import { ApiKeyService } from './api-key.service'; +import { CreateApiKeyDto, CreateApiKeyResponseDto, ApiKeyBaseResponseDto } from './dto/api-key.dto'; +import { Authenticated } from 'src/decorators/authentication.decorator'; +import { ApiHeaders, ApiTags, ApiResponse } from '@nestjs/swagger'; +import { LanguageEnum } from 'src/utils/languages.enum'; +import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; +import { RequestUser, User } from 'src/decorators/user.decorator'; +import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; + +@Controller('api-key') +@Authenticated() +@ApiTags('ApiKey') +@ApiHeaders([{ name: 'dadosfera-lang', enum: LanguageEnum, required: false }]) +@UseFilters(new GrpcToHttpExceptionFilter()) +export class ApiKeyController { + logger: DadosferaLogger; + + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + private readonly apiKeyService: ApiKeyService, + ) { + this.logger = dadosferaLogger.logger; + } + + @Post() + @ApiResponse({ type: CreateApiKeyResponseDto }) + async create(@Body() createApiKeyDto: CreateApiKeyDto, @User() user: RequestUser): Promise { + this.logger.info('POST /api-key', { + permissions: createApiKeyDto.permissions, + method: 'create' + }); + const result = await this.apiKeyService.create(createApiKeyDto, user); + this.logger.info('POST /api-key success', { + id: result.id, + method: 'create' + }); + return result; + } + + @Get() + @ApiResponse({ type: [ApiKeyBaseResponseDto] }) + async findAll(@User() user: RequestUser): Promise { + this.logger.info('GET /api-key', { + method: 'findAll' + }); + const result = await this.apiKeyService.findAll(user); + this.logger.info('GET /api-key success', { + count: result.length, + method: 'findAll' + }); + return result; + } + + @Delete(':id') + async remove(@Param('id') id: string, @User() user: RequestUser): Promise { + this.logger.info('DELETE /api-key/:id', { + id, + method: 'remove' + }); + await this.apiKeyService.remove(id, user); + this.logger.info('DELETE /api-key/:id success', { + id, + method: 'remove' + }); + } +} diff --git a/src/modules/api-key/api-key.module.ts b/src/modules/api-key/api-key.module.ts new file mode 100644 index 0000000..1455425 --- /dev/null +++ b/src/modules/api-key/api-key.module.ts @@ -0,0 +1,18 @@ +import { Module } from '@nestjs/common'; +import { ApiKeyService } from './api-key.service'; +import { ApiKeyController } from './api-key.controller'; +import { ClientsModule } from '@nestjs/microservices'; +import { DucClient } from '../duc/client.config'; +import DadosferaLogger from '@dadosfera/dadosfera-logs'; + +const ducClient = new DucClient(); + +@Module({ + imports: [ + ClientsModule.register([ducClient.providerOptions]) + ], + controllers: [ApiKeyController], + providers: [ApiKeyService, DadosferaLogger], + exports: [ApiKeyService] +}) +export class ApiKeyModule {} diff --git a/src/modules/api-key/api-key.service.ts b/src/modules/api-key/api-key.service.ts new file mode 100644 index 0000000..3e92053 --- /dev/null +++ b/src/modules/api-key/api-key.service.ts @@ -0,0 +1,50 @@ +import { Injectable, Inject, OnModuleInit } from '@nestjs/common'; +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 { 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 { + private apiKeyService: ApiKeyWriteProtoService; + + constructor( + @Inject(DucClient.name) private readonly client: ClientGrpc, + ) {} + + onModuleInit() { + this.apiKeyService = this.client.getService(ProtoServices.ApiKeyWriteProtoService); + } + + create(createApiKeyDto: CreateApiKeyDto, user: RequestUser): Promise { + const metadata = PackTheMetadata(user); + + return lastValueFrom(this.apiKeyService.CreateApiKey({ + permissions: createApiKeyDto.permissions + }, metadata)); + } + + async findAll(user: RequestUser): Promise { + const metadata = PackTheMetadata(user); + console.log(metadata) + + const data = await lastValueFrom(this.apiKeyService.ListApiKeys({}, metadata)); + return data.api_keys; + } + + async remove(id: string, user: RequestUser) { + const metadata = PackTheMetadata(user); + + await lastValueFrom(this.apiKeyService.DeleteApiKey({ id }, metadata)); + } + + async get(key: string) { + const metadata = PackTheMetadata({}); + + return await lastValueFrom(this.apiKeyService.GetApiKey({ key }, metadata)); + } +} diff --git a/src/modules/api-key/dto/api-key.dto.ts b/src/modules/api-key/dto/api-key.dto.ts new file mode 100644 index 0000000..2bbabf3 --- /dev/null +++ b/src/modules/api-key/dto/api-key.dto.ts @@ -0,0 +1,39 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsArray, IsNumber } from 'class-validator'; + +export class PermissionDto { + @ApiProperty({ type: Number }) + id: number; + + @ApiProperty({ type: String }) + name: string; +} + +export class ApiKeyBaseResponseDto { + @ApiProperty({ type: String, format: 'uuid' }) + id: string; + + @ApiProperty({ type: String }) + key_mask: string; + + @ApiProperty({ type: [PermissionDto] }) + permissions: PermissionDto[]; + + @ApiProperty({ type: String, format: 'date-time' }) + created_at: string; + + @ApiProperty({ type: String }) + created_by: string; +} + +export class CreateApiKeyResponseDto extends ApiKeyBaseResponseDto { + @ApiProperty({ type: String }) + key: string; +} + +export class CreateApiKeyDto { + @ApiProperty({ type: [Number], description: 'Array of permission IDs' }) + @IsArray() + @IsNumber({}, { each: true }) + permissions: number[]; +} \ No newline at end of file diff --git a/src/modules/assign/assign.controller.ts b/src/modules/assign/assign.controller.ts new file mode 100644 index 0000000..361524c --- /dev/null +++ b/src/modules/assign/assign.controller.ts @@ -0,0 +1,33 @@ +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 { DADOSFERA_MODULES_KEYS, 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(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.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..01a8c20 --- /dev/null +++ b/src/modules/assign/assign.service.ts @@ -0,0 +1,38 @@ +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) { + const data = await lastValueFrom(this.ducService.CreateOrUpdateAssignPublicKey(createAssignDto, metadata)) + return data; + } + + async get(metadata: Metadata) { + return await lastValueFrom(this.ducService.GetAssignPublicKey(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; +} diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 4a46d47..e2f5e40 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'; @@ -86,11 +86,9 @@ export class AuthController { async signIn( @Body() { username, password, totp }: AuthSignInReq, @Language() language: LanguageEnum, - @Headers('origin') origin = '', ): Promise { this.logger.info('/auth - SignIn'); - const frontHost = origin.replace(/^https?:\/\//, ''); - const metadata = PackTheMetadata({ language, custom_host: frontHost }); + const metadata = PackTheMetadata({ language }); this.logger.info('metadata: ' + JSON.stringify(metadata.toJSON())); return this.authClient.signIn({ username, password, totp }, metadata); } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 8b1b4f2..c94a8d0 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -1,4 +1,4 @@ -import { OnModuleInit, Inject, Injectable } from '@nestjs/common'; +import { OnModuleInit, Inject, Injectable, ForbiddenException } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { lastValueFrom } from 'rxjs'; @@ -17,6 +17,7 @@ import { AuthResetPasswordRequest, AuthVerifyResetPasswordCodeRequest, AuthConfirmResetPasswordRequest, + AuthSignInResponse, } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/messages'; import { DucClient } from '../duc/client.config'; import { Metadata } from '@grpc/grpc-js'; @@ -62,15 +63,49 @@ export class AuthClientService implements OnModuleInit { return lastValueFrom(this.authService.AuthSnowflakeSignIn(input)); } + checkDedicatedProxy({ + customer + }: AuthSignInResponse) { + const DEDICATED_PROXY = process.env.DEDICATED_PROXY || ''; + this.logger.info('SignIn - Setting customer ID for dedicated proxy: ' + DEDICATED_PROXY); + this.logger.info('Customer ID: ' + customer.id); + + if (DEDICATED_PROXY !== '' && DEDICATED_PROXY !== customer.id) { + throw new ForbiddenException(); + } + + // Bloquear o customer de acesso o maestro publico + this.logger.info('Check if customer have network policy: ' + customer.modules); + const hasNetworkPolicyModule = customer.modules.includes('network-policy'); + if (hasNetworkPolicyModule && DEDICATED_PROXY === '') { + throw new ForbiddenException(); + } + } + async signIn( { username, password, totp }: AuthSignInRequest, metadata: Metadata, ) { this.logger.info('SignIn'); - return lastValueFrom( - this.authService.AuthSignIn({ username, password, totp }, metadata), - ); + let result: AuthSignInResponse; + + try { + result = await lastValueFrom( + this.authService.AuthSignIn({ username, password, totp }, metadata), + ); + + } catch (error) { + this.logger.error('SignIn - Error during sign-in'); + this.logger.error(error); + throw error; + } + + if (result.customer) { + this.checkDedicatedProxy(result); + } + + return result } async refreshAccessToken( diff --git a/src/modules/catalog/catalog.controller.ts b/src/modules/catalog/catalog.controller.ts index 09daa16..b87829b 100644 --- a/src/modules/catalog/catalog.controller.ts +++ b/src/modules/catalog/catalog.controller.ts @@ -32,7 +32,7 @@ import { 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, @@ -117,6 +117,52 @@ export class CatalogController { return res; } + @Get('/download') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async dowloadAsserts( + @User() user: RequestUser, + @Query() query: ICatalogAllRequest, + @Res() res: Response + ) { + const { user_id, customer_name, customer_id, username, permissions } = user; + this.logger.info(`/catalog/download - searchCatalog`, { + user_id, + customer_name, + }); + + const is_data_manager = permissions.includes( + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid, + ); + + const roles = await this.catalogService.getUserRolesIds(user_id); + + const metadata = PackTheMetadata({ + user_id, + customer_id, + customer_name, + username, + roles, + is_data_manager, + }); + + const { + file, + filename + } = await this.catalogService.downloadAssets( + query, + metadata, + customer_id, + ); + + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Type', 'text/csv'); + + res.end(file); + } + @ApiInternalOnlyEndpoint() @Get('data-asset') async findByPipelineAndObject(@User() user: RequestUser, @Query() query) { @@ -885,4 +931,4 @@ export class CatalogController { 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 45cc06e..8ce1579 100644 --- a/src/modules/catalog/catalog.module.ts +++ b/src/modules/catalog/catalog.module.ts @@ -3,12 +3,15 @@ import { Module } from '@nestjs/common'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { CatalogController } from './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 { ShareModule } from './share/share.module'; +import { CatalogService } from './catalog.service'; +import { MixpanelModule } from '../mixpanel/mixpanel.module'; const client = new CatalogClientConfiguration(); @@ -18,6 +21,8 @@ const client = new CatalogClientConfiguration(); OldPipelineModule, UsersModule, RolesModule, + CustomersModule, + ShareModule, ], controllers: [CatalogController], providers: [CatalogService, DadosferaLogger], diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.ts index a3db9cc..fafc14a 100644 --- a/src/modules/catalog/catalog.service.ts +++ b/src/modules/catalog/catalog.service.ts @@ -25,6 +25,7 @@ import { UsersService } from '../users/users.service'; import { RolesService } from '../roles/roles.service'; import { Metadata } from '@grpc/grpc-js'; import { + AssetReporter, BatchRemoveRlsRulesRequest, IUpdateDataRequest, TriggerCatalogReq, @@ -35,12 +36,6 @@ 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'; @@ -240,6 +235,34 @@ class CatalogService implements OnModuleInit { return { data_assets: response, total }; } + async downloadAssets( + query: Record, + metadata: Metadata, + customer_id: string, + ) { + const data = await this.searchDataAssets(query, metadata, customer_id); + + const formatData = data.data_assets.map(asset => ({ + id: asset.id, + display_name: asset.display_name, + data_asset_type: asset.data_asset_type, + created_at: asset.created_at, + tags: '[' + asset.tags.join(', ') + ']' + })) + + const parser = ParserBuilder.build('csv'); + + const file = await parser.parse(formatData); + + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const filename = `dadosfera_assets_${timestamp}.csv`; + + return { + file, + filename + } + } + async getOneDataAsset(data: { id: string; customer_id: string; @@ -316,7 +339,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; } @@ -584,4 +607,4 @@ class CatalogService implements OnModuleInit { } } -export { CatalogService }; +export { CatalogService }; \ No newline at end of file diff --git a/src/modules/catalog/dtos/index.ts b/src/modules/catalog/dtos/index.ts index b869e5e..ef12e78 100644 --- a/src/modules/catalog/dtos/index.ts +++ b/src/modules/catalog/dtos/index.ts @@ -320,3 +320,11 @@ export class BatchRemoveRlsRulesRequest { @ApiPropertyOptional() id_rls?: string; } + +export type AssetReporter = { + id: string; + display_name: string; + data_asset_type: string; + created_at: string; + tags: string; +} diff --git a/src/modules/catalog/share/share.controller.ts b/src/modules/catalog/share/share.controller.ts new file mode 100644 index 0000000..efc394e --- /dev/null +++ b/src/modules/catalog/share/share.controller.ts @@ -0,0 +1,90 @@ +import { + Controller, + Get, + Inject, + Param, + Req, + 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'; +import { Request } from 'express'; + +@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, + @Req() request: Request + ) { + this.logger.info(`GET //:id`); + return await this.catalogShareService.getOneDataAssetPublic(id, user, request); + } + + @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..ce7021c --- /dev/null +++ b/src/modules/catalog/share/share.module.ts @@ -0,0 +1,28 @@ +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"; +import { MixpanelModule } from "src/modules/mixpanel/mixpanel.module"; + +const client = new CatalogClientConfiguration(); + +@Module({ + imports: [ + ClientsModule.register([client.providerOptions]), + UsersModule, + RolesModule, + CustomersModule, + ShareMetadataModule, + MixpanelModule + ], + 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..da8d3fe --- /dev/null +++ b/src/modules/catalog/share/share.service.ts @@ -0,0 +1,247 @@ +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'; +import { MixpanelService } from 'src/modules/mixpanel/mixpanel.service'; +import { Request } from 'express'; + +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 readonly shareMetadataService: ShareMetadataService, + private readonly mixpanelService: MixpanelService, + ) { + 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, request: Request) { + this.logger.info("getOneDataAssetPublic: " + JSON.stringify({ + id, + userId: user?.user_id, + customerId: user?.customer_id + })) + 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, + 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({}); + this.logger.info('GET share metadata') + 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 info; + } + + throw new ForbiddenException( + 'You do not have permission to access this data asset.', + ); + } + return info; + } +} 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/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 : ''}`; + } +} 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..dd2f984 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'; @@ -45,11 +45,11 @@ export class OpenDataController { ) { this.logger.info('createUser for open data' + JSON.stringify(request.headers)); - const corslist = ["https://devsbm.dadosfera.io", "https://sharingoceandata.com"]; - if (!corslist.includes(origin)) { - this.logger.info('block request by cors list: '+ origin); - throw new ForbiddenException(); - } + // const corslist = ["https://devsbm.dadosfera.io", "https://sharingoceandata.com"]; + // if (!corslist.includes(origin)) { + // this.logger.info('block request by cors list: '+ origin); + // throw new ForbiddenException(); + // } const OPENDATA_CUSTOMER_ID = process.env.OPEN_CUSTOMER_ID; const OPENDATA_GROUP_ID = process.env.OPEN_GROUP_ID; 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/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..6935de1 --- /dev/null +++ b/src/modules/share-metadata/dto/create-share-metadata.dto.ts @@ -0,0 +1,10 @@ +export class CreateShareMetadataDto { + assetId: string; + proposedId: string; + type: 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..e69865b --- /dev/null +++ b/src/modules/share-metadata/share-metadata.service.ts @@ -0,0 +1,62 @@ +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, + type: createShareDto.type + }, + 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, + ), + ); + } +} diff --git a/src/modules/users/dtos/entities.ts b/src/modules/users/dtos/entities.ts index 9272e52..8e8b852 100644 --- a/src/modules/users/dtos/entities.ts +++ b/src/modules/users/dtos/entities.ts @@ -174,3 +174,14 @@ export class GetAllDepartmentsRes { @ApiProperty() departments: string[]; } + + +export interface UserReporter { + name: string; + email: string; + mfaStatus: string; + status: string; + lastLogin: string; + createdAt: string; + updatedAt: string; +} diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 9e00f7c..e565f18 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -12,6 +12,7 @@ import { Post, Put, Query, + Res, UseFilters, } from '@nestjs/common'; import { @@ -24,12 +25,13 @@ import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { Authenticated, RequireAllPermissions, + RequireSomePermission, } from 'src/decorators/authentication.decorator'; 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'; @@ -52,6 +54,7 @@ import { UpdateUserRes, } from './dtos/entities'; import { UsersService } from './users.service'; +import { Response } from 'express'; @ApiInternalOnlyController() @ApiTags('Users') @@ -80,6 +83,26 @@ export class UsersController { return await this.userService.findAllUsersByCustomerId(user.customer_id); } + @Get('/download') + @RequireSomePermission(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) + async downloadUsersInCsv( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Res() res: Response + ) { + this.logger.info('downloadUsersInCsv'); + this.userService.setLanguage(language); + const { + file, + filename + } = await this.userService.downloadUsersInCsv(user.customer_id); + + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Type', 'text/csv'); + + res.end(file); + } + @Get('hierarchies') @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @ApiOkResponse({ type: GetAllHierarchiesRes }) diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index e6717fa..3057010 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -21,6 +21,7 @@ import { IUserByCustomer, SetUserRolesReq, UpdateUserReq, + UserReporter, } from './dtos/entities'; import { RolesService } from '../roles/roles.service'; import { HIERARCHIES } from './hierarchies'; @@ -29,6 +30,7 @@ import { UserByCustomer } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces import { EnrichErrorCode } from 'src/utils/ErrorBuilder'; import { DucClient } from '../duc/client.config'; import { Metadata } from '@grpc/grpc-js'; +import { ParserBuilder } from 'src/utils/FileParser/parser.builder'; @Injectable() export class UsersService implements OnModuleInit { @@ -78,6 +80,34 @@ export class UsersService implements OnModuleInit { }; } + async downloadUsersInCsv(customerId: string) { + const { users } = await lastValueFrom( + this.usersClientService.UserFindAllByCustomerId({ customerId }), + ); + + const formatUsers: UserReporter[] = users.map(user => ({ + createdAt: user.createdAt, + email: user.email, + lastLogin: user.lastLogin, + mfaStatus: user.mfaStatus, + name: user.name, + status: user.status, + updatedAt: user.updatedAt + })) + + const parser = ParserBuilder.build('csv'); + + const file = await parser.parse(formatUsers); + + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const filename = `dadosfera_users_${timestamp}.csv`; + + return { + file, + filename + } + } + async findOneById(id: string): Promise<{ user: IUserByCustomer }> { const { user } = await lastValueFrom( this.usersClientService.UserFindOneById({ id }), diff --git a/src/utils/FileParser/pdf-parser.ts b/src/utils/FileParser/pdf-parser.ts index 1294b34..a954708 100644 --- a/src/utils/FileParser/pdf-parser.ts +++ b/src/utils/FileParser/pdf-parser.ts @@ -22,7 +22,7 @@ export class PDFParser implements Parser { const html = await this.htmlParser.parse(data); await page.setContent(html, { waitUntil: 'networkidle0', - timeout: 30000, + timeout: 90000, }); // Configurações adicionais para garantir um PDF válido @@ -43,7 +43,7 @@ export class PDFParser implements Parser { pageRanges: '', tagged: true, outline: false, - timeout: 30000, + timeout: 90000, }); await browser.close(); 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