PRISMA: Authentication token is invalid: 'Authorization' header not provided

Viewed 1098

Running Prisma on my locally without secret runs fine..Now i am trying to run it for Production i am always encountering this error ERROR: Authentication token is invalid: 'Authorization' header not provided on my server and locally. i am missing something for sure but don't know what. Please help following are my prisma.yml and docker-compose.yml files.

Prisma.yml

# This service is based on the type definitions in the two files
# databasetypes.prisma` and `database/enums.prisma`
datamodel:
          - ./packages/routes/index.directives.graphql
          - ./packages/routes/index.scalar.graphql
          - ./packages/routes/account/index.enum.graphql
          - ./packages/routes/account/index.prisma
          ...

# Generate a Prisma client in JavaScript and store in
# a folder called `generated/prisma-client`.
# It also downloads the Prisma GraphQL schema and stores it
# in `generated/prisma.graphql`.
generate:
  - generator: javascript-client
    output: ./prisma

# The endpoint represents the HTTP endpoint for your Prisma API.
# It encodes several pieces of information:
# * Prisma server (`localhost:4466` in this example)
# * Service name (`myservice` in this example)
# * Stage (`dev` in this example)
# NOTE: When service name and stage are set to `default`, they
# can be omitted.
# Meaning http://myserver.com/default/default can be written
# as http://myserver.com.
endpoint: 'http://127.0.0.1:4466/soul/dev'

# The secret is used to create JSON web tokens (JWTs). These
# tokens need to be attached in the `Authorization` header
# of HTTP requests made against the Prisma endpoint.
# WARNING: If the secret is not provided, the Prisma API can
# be accessed without authentication!
secret: ${env:SECRET}

Docker-compose.yml

version: '3'
services:
  server:
    container_name: soul
    restart: always
    build: .
    command: 'npm run dev'
    links:
      - redis
      - prisma
    env_file:
      - ./.env
    volumes:
      - .:/node/soul/
    working_dir: /node/soul/
    ports:
      - '3000:3000'
  redis:
    container_name: "redisserver"
    image: redis:latest
    restart: always
    command: ["redis-server", "--bind", "redis", "--port", "6379"]
  prisma:
    image: prismagraphql/prisma:1.34
    restart: always
    ports:
      - '4466:4466'
    environment:
      PRISMA_CONFIG: |
        managementApiSecret: ${SECRET}
        port: 4466
        databases:
          default:
            connector: mysql
            host: mysql
            port: 3306
            user: root
            password: ******
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ******
    volumes:
      - mysql:/var/lib/mysql
volumes:
  mysql: ~
1 Answers

It looks like you're using the API Management Secret where you are supposed to be using a Service Secret.

According to the Prisma docs, the Service Secret and API Management Secret are two different things.

For Prisma v1.34 you can read about the differences here: https://v1.prisma.io/docs/1.34/prisma-server/authentication-and-security-kke4/#prisma-server

Quote from that page:

A Prisma server provides the runtime environment for one or more Prisma services. To create, delete and modify the Prisma services on a Prisma server, the Management API is used. The Management API is protected with the Management API secret specified in the Docker Compose files when the Prisma server is deployed. Learn more here.

Prisma services are secured via the service secret that's specified in your prisma.yml. A Prisma service typically serves application data that's stored in relation to a certain datamodel. Learn more here.

const db = new Prisma({
    typeDefs: 'src/generated/prisma.graphql',
    endpoint: process.env.PRISMA_ENDPOINT,
    secret: <YOUR_PRISMA_SERVICE_SECRET>, // Note: This must match what is in your prisma.yml
});
# prisma.yml

endpoint: ${env:PRISMA_ENDPOINT}
datamodel: mydatamodel.graphql
secret: <YOUR_PRISMA_SERVICE_SECRET>

In their Prisma 1.34 docs, Prsima recommends using an environment variable to get the secret into the prisma.yml file. There are risks associated with this but that is what is in their docs.

See: https://v1.prisma.io/docs/1.34/prisma-cli-and-configuration/prisma-yml-5cy7/#environment-variable

Quote from that page:

In the following example, an environment variable is referenced to determine the Prisma service secret:

# prisma.yml (as per the docs in the above link)
secret: ${env:PRISMA_SECRET}
Related