Search code examples
sql-serverdjangopython-3.xdockerpyodbc

pyodbc error: using django in docker container


I am using a docker container running django application.

T get the below error whenever I try to run function that manipulates data on 2 separate databases:

error: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol] (-1) (SQLDriverConnect)')

This is the function I run

def preStepBtn3(request):
    sourcePE = request.GET.get('sourcePE')
    targetPE = request.GET.get('targetPE')
    inputFTID = request.session['username']
    datetime_object = datetime.datetime.now()
    step_name = "preStepBtn3"
    table_name = "null"
    Action_taken = "change router status to MIG"
    MassivePortalSessionID = request.session['MassivePortalSessionID']

    try:
        with connections['DataAdmin'].cursor() as cursor:
            sql = """DECLARE @out nvarchar(max); exec DTA.mig_sop_ce_status2mig_django 0, %s, @param_out = @out OUTPUT; SELECT @out AS the_output; """
            params = [sourcePE]
            cursor.execute(sql, params)            
            rows = cursor.fetchall()
            result = []
            result.append(rows)
            logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE,
                      table_name, step_name, datetime_object, Action_taken)
            print("data inserted in log DB")
            while rows:
                print(rows)
                if cursor.nextset():
                    result.append(cursor.fetchall())
                else:
                    print(result)
                    return JsonResponse(result,  safe=False)
    except Exception as ex:
        error = ex
        print(error)
        context = {'text': error}
        logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE,
                  table_name, step_name, datetime_object, Action_taken)
        print("data inserted in log DB during exception")
        return render(request, 'posts/textArea.html', context)

Whenever I remove the logDB_sql it works perfectly. This is code for logDB_sql

def logDB_sql(MassivePortalSessionID, inputFTID, sourcePE, targetPE, table_name, step_name, datetime_object, Action_taken):
    params = [MassivePortalSessionID, inputFTID, sourcePE, targetPE,
              table_name, step_name, datetime_object, Action_taken]
    print(targetPE)
    print(sourcePE)
    print(MassivePortalSessionID)
    print(inputFTID)
    if sourcePE != None and MassivePortalSessionID != None and targetPE != None and inputFTID != None:
        sql = " insert into MASSIVE_MIGRATION_PORTAL_LOGS values (%s,%s,%s,%s,%s,%s,%s,%s )"
        print(sql)
        print(params)
        with connections['logDB'].cursor() as cursor:
            cursor.execute(sql, params)
            cursor.close()
            print("data inserted")

This is connection info in settings.py. note that they are different hosts

    'logDB': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'log_db',
        'HOST': 'xx.xx.xx.xx',
        'USER': 'user1',
        'PASSWORD': 'password1',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'timeout': 1000,
        }

    },

    'DataAdmin': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'another_db',
        'HOST': 'xx.xx.xx.xx',
        'USER': 'user1,
        'PASSWORD': 'password1',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'timeout': 1000,
        }
    },

}

And this is the error message

 File "/app/posts/views.py", line 387, in preStepBtn2
    table_name, step_name, datetime_object, Action_taken)
  File "/app/posts/views.py", line 90, in logDB_sql
    with connections['logDB'].cursor() as cursor:
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/sql_server/pyodbc/base.py", line 307, in get_new_connection
    timeout=timeout)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol] (-1) (SQLDriverConnect)')

I have tried it locally on my windows machine and it works without any issues, not sure why it is failing when i move it to docker container.

Any ideas?


Solution

  • I have resolved this issue by updating the openssl.cnf file in /etc/ssl/

    Changed MinProtocol = TLSv1.2 to MinProtocol = TLSv1.0 & CipherString = DEFAULT@SECLEVEL=2 to CipherString = DEFAULT@SECLEVEL=1

    Hope this helps.