1

it's my first question on stack overflow because I can't find relevant information in Django documentation. Is it possible to force mysql server authentication with ssl using django.db.backends.mysql? I have checked its implementation in Django Github and it seems it supports only 3 ssl arguments: ca, cert and key. What I need is equivalent of --ssl-mode=VERIFY_IDENTITY. Has anyone found some workaround for this problem? Here is my current configuration. TLS channel is working as expected, but identity of MySQL server is not validated.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env('DB_NAME'),
        'USER': env('DB_USER'),
        'PASSWORD': env('DB_PASSWORD'),
        'HOST': env('DB_HOST'),
        'PORT': env('DB_PORT'),
        'CONN_MAX_AGE': 600,
        'OPTIONS':{
            'ssl':{
                'ca': env('CA_CERT'),
                'cert': env('CERT'),
                'key': env('KEY')
            }
        }
    }
}
1
  • options are passed as kwargs so --ssl-mode is ssl_mode Commented Oct 26 at 13:10

1 Answer 1

1

Yes - but Django doesn’t expose ssl-mode directly, you must pass it through the MySQL driver (mysqlclient or PyMySQL) using OPTIONS.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried doing something like this: 'OPTIONS':{ 'ssl':{ 'ca': env('CA_CERT'), 'cert': env('CERT'), 'key': env('KEY'), 'ssl-mode': 'VERIFY_IDENTITY' } } but it didn't work and then I checked django.db.backends.mysql code where I found only three arguments parsed mentioned earlier.
You’re right, Django only parses the three SSL fields (ca, cert, key) from the ssl dict. ssl-mode inside there will be ignored. Instead, you can try passing ssl_mode at the top level of OPTIONS, like this - 'OPTIONS': { 'ssl_mode': 'VERIFY_IDENTITY', 'ssl': { 'ca': env('CA_CERT'), 'cert': env('CERT'), 'key': env('KEY'), }, } ssl_mode is supported by the underlying MySQL drivers, so this should enable identity verification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.