hasura / graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
https://hasura.io
Apache License 2.0
31.18k stars 2.77k forks source link

Internal Server Error when using Hasura Cloud GraphQL API to create cloud_free_v2 tenant with env vars #9731

Open osdiab opened 1 year ago

osdiab commented 1 year ago

Version Information

Server Version: N/A CLI Version (for CLI related issue): N/A

Environment

Cloud

What is the current behaviour?

Tried this query:

mutation MyMutation {
  createTenant(
    cloud: "aws"
    region: "eu-central-1"
    envs: [{key: "HASURA_GRAPHQL_SECRET", value: "abcd"}, {key: "HASURA_GRAPHQL_DATABASE_URL", value: "abcd"}]
    name: "test-personal-website-pr-2"
    plan: "cloud_free_v2"
  ) {
    tenant {
      slug
    }
  }
}

It failed with

{
  "errors": [
    {
      "extensions": {
        "code": "GenericServerError",
        "id": "59c12a95-83d6-4704-9130-3b84f38d6ed9"
      },
      "message": "there was an internal error while serving your request"
    }
  ]
}

The tenant did get created, but the env vars were missing.

But then if I comment out the line with the env vars, it doesn't error.

What is the expected behaviour?

it succeeds.

How to reproduce the issue?

Run the above query in the public GraphiQL UI with a PAT pointing to https://data.pro.hasura.io/v1/graphql

Screenshots or Screencast

Please provide any traces or logs that could help here.

don't got any.

Any possible solutions/workarounds you're aware of?

make it without the env vars and then add them later

Keywords

manasag commented 1 year ago

Thanks for raising this issue. I have assigned to the product team for clarification. cc @ManushiKhanna

osdiab commented 1 year ago

also found out this happens even if I don't use the reserved HASURA_GRAPHQL prefix (which errors when you try to set it by calling the updateTenantEnv mutation), so it's not that.

shraddhaag commented 1 year ago

Hey @osdiab, thanks for reporting this issue. There are two issues in the query you mentioned here, listing them down below:

  1. in the envs param, trying to set the env variable HASURA_GRAPHQL_SECRET will fail due to the reserved prefix HASURA_GRAPHQL_. Please either remove this env var, or change the key to one that is valid.
  2. secondly, after investigating internally, we were able to find an issue in env var application. We have fixed the same. (this will also help the preview app issue https://github.com/hasura/hasura-cloud-preview-apps/issues/91)

The mutation should work as expected now. @osdiab please do try it out and lemme know if you run into further issues!

sebpalluel commented 1 year ago

Hello. I'm having a similar error while using the github action hasura/hasura-cloud-preview-apps@v0.1.10

2023-07-10T15:56:51.1868584Z Creating Hasura Cloud preview app.
2023-07-10T15:56:51.1869270Z 
2023-07-10T15:56:51.3233783Z Scheduled creation of preview app:
2023-07-10T15:56:51.3234588Z ***
2023-07-10T15:56:51.3255845Z   "githubPreviewAppJobID": "590d9f87-2fb4-4a13-8fb6-615d7da23eb0"
2023-07-10T15:56:51.3256707Z ***
2023-07-10T15:56:51.3257119Z 
2023-07-10T15:56:51.3257775Z Polling the preview app creation status...
2023-07-10T15:56:51.3258252Z 
2023-07-10T15:57:06.3388859Z ***
2023-07-10T15:57:06.3389816Z   id: '9a66ce53-d5a5-40ca-ba4f-c64ba7e36eae',
2023-07-10T15:57:06.3392375Z   event_type: 'failed',
2023-07-10T15:57:06.3394446Z   public_event_data: null,
2023-07-10T15:57:06.3398134Z   error: 'error creating tenant: Message: there was an internal error while serving your request, Locations: [*** Extensions: map[code:GenericServerError id:bc566e17-1966-48b2-807b-8f202759fe01]'
2023-07-10T15:57:06.3399150Z ***
2023-07-10T15:57:06.3432434Z ##[error]error creating tenant: Message: there was an internal error while serving your request, Locations: [*** Extensions: map[code:GenericServerError id:bc566e17-1966-48b2-807b-8f202759fe01]

What's peculiar is that i have 5 instances created each time instead of one, none are set with the hasuraEnv vars i've setted and not metadata is applied. I reference the hasura path to be ./hasura/app which correction to my hasura project location.

All my hasuraEnv variables seem to be provided correctly.

Here is my complete github action:

name: Deploy preview apps

on:
  pull_request:
    types: [opened, reopened, synchronize]
  workflow_dispatch:

env:
  VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

jobs:
  deploy-preview:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Set up environment and cache dependencies
        uses: sebpalluel/ci/.github/actions/setup-environment@latest

      - name: Get PR sha
        id: get_pull_request_sha
        run: |
          pull_request_sha=$(jq --raw-output .pull_request.base.sha "$GITHUB_EVENT_PATH" | cut -c1-8)
          echo "PULL_REQUEST_SHA=$pull_request_sha" >> $GITHUB_ENV

      - name: Neon Database Create Branch Action
        uses: neondatabase/create-branch-action@v3
        id: create_branch
        with:
          project_id: ${{ secrets.NEON_PROJECT_ID }}
          branch_name: ${{ env.PULL_REQUEST_SHA }}
          api_key: ${{ secrets.NEON_API_KEY }}
          username: ${{ secrets.PG_USERNAME }}
          password: ${{ secrets.PG_PASSWORD }}

      - name: Install Vercel CLI
        run: npm install --global vercel@latest

      - name: Vercel Environment Information
        run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}

      - name: Debug Secrets
        run: |
          echo "Length of HASURA_CLOUD_ACCESS_TOKEN: ${#HASURA_CLOUD_ACCESS_TOKEN}"
          echo "Length of HASURA_GRAPHQL_ADMIN_SECRET: ${#HASURA_GRAPHQL_ADMIN_SECRET}"
          echo "Length of HYGRAPH_CMS_WEBHOOK_READ_URL: ${#HYGRAPH_CMS_WEBHOOK_READ_URL}"
          echo "Length of HYGRAPH_CMS_READ_TOKEN: ${#HYGRAPH_CMS_READ_TOKEN}"
          echo "Length of HASURA_GRAPHQL_JWT_SECRET: ${#HASURA_GRAPHQL_JWT_SECRET}"
          echo "Length of HASURA_GRAPHQL_DATABASE_URL: ${#HASURA_GRAPHQL_DATABASE_URL}"
        env:
          HASURA_CLOUD_ACCESS_TOKEN: ${{secrets.HASURA_CLOUD_ACCESS_TOKEN}}
          HASURA_GRAPHQL_ADMIN_SECRET: ${{secrets.HASURA_GRAPHQL_ADMIN_SECRET}}
          HYGRAPH_CMS_WEBHOOK_READ_URL: ${{secrets.HYGRAPH_CMS_WEBHOOK_READ_URL}}
          HYGRAPH_CMS_READ_TOKEN: ${{secrets.HYGRAPH_CMS_READ_TOKEN}}
          HASURA_GRAPHQL_JWT_SECRET: ${{secrets.HASURA_GRAPHQL_JWT_SECRET}}
          HASURA_GRAPHQL_DATABASE_URL: ${{ steps.create_branch.outputs.db_url_with_pooler }}/neondb

      - name: Create Hasura Cloud preview app
        id: hasura_cloud_preview
        uses: hasura/hasura-cloud-preview-apps@v0.1.10
        with:
          name: 'marketplace-${{github.event.number}}'
          hasuraProjectDirectoryPath: ./hasura/app
          region: eu-central-1
          tier: cloud_free_v2
          hasuraEnv: |
            HASURA_GRAPHQL_CORS_DOMAINS=https://marketplace-*-offline.vercel.app,https://cloud.hasura.io
            HASURA_GRAPHQL_DATABASE_URL=${{ steps.create_branch.outputs.db_url_with_pooler }}/neondb
            HYGRAPH_CMS_WEBHOOK_READ_URL=${{ secrets.HYGRAPH_CMS_WEBHOOK_READ_URL }}
            HYGRAPH_CMS_READ_TOKEN=${{ secrets.HYGRAPH_CMS_READ_TOKEN }}
            HASURA_GRAPHQL_UNAUTHORIZED_ROLE=anonymous
            HASURA_GRAPHQL_ADMIN_SECRET=${{ secrets.HASURA_GRAPHQL_ADMIN_SECRET }}
            HASURA_GRAPHQL_JWT_SECRET=${{ secrets.HASURA_GRAPHQL_JWT_SECRET }}
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
          HASURA_CLOUD_ACCESS_TOKEN: ${{secrets.HASURA_CLOUD_ACCESS_TOKEN}}

      - name: Set Hasura GraphQL endpoint to .env
        run: |
          echo HASURA_GRAPHQL_ENDPOINT=${{steps.hasura_cloud_preview.outputs.graphQLEndpoint}} >> .env
          echo NEXT_PUBLIC_HASURA_PROJECT_ENDPOINT=${{steps.hasura_cloud_preview.outputs.graphQLEndpoint}} >> .env

      - name: Build Vercel Project Artifacts
        run: vercel build --token=${{ secrets.VERCEL_TOKEN }}

      - name: Deploy Preview to Vercel
        id: deploy_vercel
        run: |
          preview_url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }})
          echo "PREVIEW_URL=$preview_url" >> $GITHUB_ENV

      - name: Comment on Pull Request
        uses: thollander/actions-comment-pull-request@v2
        with:
          message: |
            Vercel Preview URL :rocket: : ${{ env.PREVIEW_URL }}
            Neon branch :elephant: : https://console.neon.tech/app/projects/${{ secrets.NEON_PROJECT_ID }}/branches/${{ steps.create_branch.outputs.branch_id }}
            Hasura Console :computer: : ${{ steps.hasura_cloud_preview.outputs.consoleURL }}

      - name: Save PREVIEW_URL as artifact
        run: echo ${{ env.PREVIEW_URL }} > preview_url.txt

      - name: Upload PREVIEW_URL artifact
        uses: actions/upload-artifact@v2
        with:
          name: preview-url-${{ env.PULL_REQUEST_SHA }}
          path: ./preview_url.txt
sebpalluel commented 1 year ago

I've tried with:

region: us-west-1
tier: cloud_free

And i can confirm that the issue is with cloud_free_v2. It doesn't set the name or anything correctly (like the taking the path to my hasura project) and create 5 projects instead of one.

I do have issues now while applying metadata. My hasuraEnv variables are not applied. I don't see them in the project settings env var and while checking the git deploy logs on the hasura cloud project, the log at Applied metadata confirm that it cannot find the needed env:

{
  "is_consistent": false,
  "inconsistent_objects": [
    {
      "name": "remote_schema Hygraph CMS Remote Schema",
      "type": "remote_schema",
      "reason": "Inconsistent object: Value for environment variables not found: HYGRAPH_CMS_WEBHOOK_READ_URL",
      "definition": {
        "name": "Hygraph CMS Remote Schema",
        "comment": "",
        "definition": {
          "url": "{{HYGRAPH_CMS_WEBHOOK_READ_URL}}",
          "headers": [
            {
              "name": "Authorization",
              "value_from_env": "HYGRAPH_CMS_READ_TOKEN"
            }
          ],
          "customization": {},
          "timeout_seconds": 60,
          "forward_client_headers": false
        },
        "permissions": [],
        "remote_relationships": [
          {
            "type_name": "Organizer",
            "relationships": []
          },
          {
            "type_name": "EventPass",
            "relationships": [
              {
                "name": "eventPassOrderSums",
                "definition": {
                  "to_source": {
                    "table": {
                      "name": "eventPassOrderSums",
                      "schema": "public"
                    },
                    "source": "default",
                    "field_mapping": {
                      "id": "eventPassId"
                    },
                    "relationship_type": "object"
                  }
                }
              }
            ]
          }
        ]
      }
    },
    {
      "name": "source default",
      "type": "source",
      "reason": "Inconsistent object: environment variable 'HASURA_GRAPHQL_DATABASE_URL' not set",
      "definition": "default"
    }
  ]
}

I'm guessing there is issues in the parsing of the env vars here ? Or the format i'm using in hasuraEnv is wrong ?