fief-dev / fief

Users and authentication management SaaS
https://www.fief.dev
Other
488 stars 42 forks source link

ORM error with MySQL backend #115

Closed MattiaFailla closed 1 year ago

MattiaFailla commented 1 year ago

Describe the bug

Hi Team, following the self-hosted docker-compose guide with MYSQL database the following error is issued:

fief-server_1  | WorkspaceDatabaseConnectionError: (pymysql.err.OperationalError) (1170, 
fief-server_1  | "BLOB/TEXT column 'message' used in key specification without a key length")
fief-server_1  | [SQL: CREATE INDEX ix_fief_audit_logs_message ON fief_audit_logs (message)]

To Reproduce

Steps to reproduce the behavior:

  1. Create the docker-compose as in the tutorial
  2. Configure the .env file with the data outputted after the configuration container (docker run -it --rm ghcr.io/fief-dev/fief:latest fief quickstart --docker)
  3. Edit .env file and configure a mysql database
  4. Execute docker-compose up

Expected behavior

Fief server should start and migration should be performed with a MySQL backend.

Configuration

Additional context

Here is the full log:

fief-server_1  | │ ╭───────────────────────────────── locals ─────────────────────────────────╮ │
fief-server_1  | │ │                         config = <alembic.config.Config object at        │ │
fief-server_1  | │ │                                  0x7f36ed0281c0>                         │ │
fief-server_1  | │ │                     connection = <sqlalchemy.engine.base.Connection      │ │
fief-server_1  | │ │                                  object at 0x7f36ecfd7a00>               │ │
fief-server_1  | │ │ database_connection_parameters = (                                       │ │
fief-server_1  | │ │                                  │                                       │ │
fief-server_1  | │ │                                  mysql+pymysql://root:***@db:3306/fasta… │ │
fief-server_1  | │ │                                  │   {}                                  │ │
fief-server_1  | │ │                                  )                                       │ │
fief-server_1  | │ │                         engine = Engine(mysql+pymysql://root:***@db:330… │ │
fief-server_1  | │ │                    schema_name = 'f6a6bc6b-dd82-4414-b92f-e9c8bfcf8097'  │ │
fief-server_1  | │ │                           self = <fief.services.workspace_db.WorkspaceD… │ │
fief-server_1  | │ │                                  object at 0x7f36ecfd7040>               │ │
fief-server_1  | │ ╰──────────────────────────────────────────────────────────────────────────╯ │
fief-server_1  | ╰──────────────────────────────────────────────────────────────────────────────╯
fief-server_1  | WorkspaceDatabaseConnectionError: (pymysql.err.OperationalError) (1170, 
fief-server_1  | "BLOB/TEXT column 'message' used in key specification without a key length")
fief-server_1  | [SQL: CREATE INDEX ix_fief_audit_logs_message ON fief_audit_logs (message)]
fief-server_1  | (Background on this error at: https://sqlalche.me/e/14/e3q8)

errorlog.txt

fief-bailiff[bot] commented 1 year ago

Hail, @MattiaFailla 👋 Welcome to Fief's kingdom!

Our team will get back to you very soon to help.

In the meantime, take a minute to star our repository ⭐️

star-fief

Farewell!

frankie567 commented 1 year ago

Hello @MattiaFailla 👋

Hmm, that's indeed an error during the creation of the database schema. It's strange because Fief is tested against MySQL during CI.

Could you tell me which version of MySQL you are using?

MattiaFailla commented 1 year ago

Hey @frankie567

I'm using MySQL Server 8.0.31-1.el8

Here is the docker-compose file: (Please note that I've specified the 0.18 Fief-server version just for testing but this issue is present in every Fief version.)

version: '3'

services:
  db:
    image: mysql
    ports:
      - "0.0.0.0:3308:3306"
    env_file:
      - .env

  fief-server:
    image: ghcr.io/fief-dev/fief:0.18
    command: fief run-server --port 8585
    depends_on:
      - db
    expose:
      - 8585
    env_file:
      - .env

  fief-worker:
    image: ghcr.io/fief-dev/fief:latest
    command: fief run-worker -p 1 -t 1
    env_file:
      - .env

  redis:
    image: redis:alpine
    command: redis-server --save 60 1 --loglevel warning
    volumes:
      - redis-data:/data

volumes:
  redis-data:

And this is the .env

DEBUG=True
MYSQL_DATABASE=fastapi
MYSQL_ROOT_PASSWORD=root

# Reference: https://docs.fief.dev/self-hosting/environment-variables/

SECRET=XXX
FIEF_CLIENT_ID=XXX
FIEF_CLIENT_SECRET=XXX
ENCRYPTION_KEY=XXX=
PORT=8000
ROOT_DOMAIN=localhost:8000
FIEF_DOMAIN=localhost:8000
FIEF_MAIN_USER_EMAIL=test@example.com
FIEF_MAIN_USER_PASSWORD=exampleexample
CSRF_COOKIE_SECURE=False
LOGIN_SESSION_COOKIE_SECURE=False
SESSION_COOKIE_SECURE=False
FIEF_ADMIN_SESSION_COOKIE_SECURE=False

# Read more: https://docs.fief.dev/self-hosting/deployment/setup-database/
DATABASE_TYPE=MYSQL
DATABASE_HOST=db
DATABASE_PORT=3306
DATABASE_USERNAME=root
DATABASE_PASSWORD=root
DATABASE_NAME=fastapi

REDIS_URL=redis://redis:6379

LOGIN_SESSION_COOKIE_NAME=login_session_data
REGISTRATION_SESSION_COOKIE_NAME=registration_session_data
fief-bailiff[bot] commented 1 year ago

Hail, @MattiaFailla 👋

I've noticed you shared secret values: SECRET, FIEF_CLIENT_ID, FIEF_CLIENT_SECRET, ENCRYPTION_KEY. Those are highly sensitive and you should keep them secret.

For your security, I've taken the liberty to replace them with dummy values.

frankie567 commented 1 year ago

Thank you for those details!

So it turns out it works with MariaDB, but not MySQL (🙄). I've added it to our CI so we can catch those errors earlier.

The culprit is actually an index on the message column of the AuditLog model. It's a TEXT column, so it's indeed a bit weird to have an index on this, don't really know why I did this.

I'll push a fix soon!