Deployment of a dash app having a Google OAuth authentication using Docker

Hello,

I have implemented a dash app that uses Google OAuth as authentication mecanism. I used Docker and docker-compose to run the service of the dashboard locally. It worked.
But when I try to deploy the docker image to a website, my-website.com, the git pipeline is run successfully, then I get a 502 Bad Gateway error.

This is a part of my code:


server = Flask(__name__)
server.secret_key = os.urandom(24)

server.config.update({
    'GOOGLE_OAUTH_CLIENT_ID': GOOGLE_CLIENT_ID,
    'GOOGLE_OAUTH_CLIENT_SECRET': GOOGLE_CLIENT_SECRET,
})
...
app = Dash(__name__,  
           meta_tags=[
               {'name': 'viewport',
                'content': 'width=device-width, initial-scale=1.0, maximum-scale=1.2, minimum-scale=0.5,'}],
           external_stylesheets=external_stylesheets,
           server=server,
           url_base_pathname='/',
           prevent_initial_callbacks=True
           )
authorized_emails = ['user1@gmail.com',
                     'user2@gmail.com']
additional_scopes = ['openid',
                     'https://www.googleapis.com/auth/userinfo.email',
                     'https://www.googleapis.com/auth/userinfo.profile']

auth = GoogleOAuth(app, authorized_emails=authorized_emails)

@server.route("/")
def index():
    if not google.authorized:
        return redirect(url_for("google.login"))
    resp = google.get("/oauth2/v2/userinfo")
    assert resp.ok, resp.text
    return "You are {resp.json()['email']} on Google"

...

context = ('server.crt', 'server.key')
app.run_server(host='0.0.0.0',
               port=port,
               debug=True,
               ssl_context=context
               )

I also defined as “Authorized redirect URIs” in Google dev console, the deployment url: https://my-website.com/login/google/authorized
and https://127.0.0.1:8050/login/google/authorized

Do you have any ideas please ?

Thank you

Hello @K_dash,

What are you trying to host this application on?

Is there any way that you can look and see the logs of the docker trying to spin up the app in your environment?

Hello,

Thank you for your response.

The app is deployed using Kubernetes. No error messages were detected apart from 502 Bad Gateway.

502 Bad Gateway means that the backend server is not running.

Please ssh in and try to run the app manually to make sure the app actually spins up.

Hello,

According to k8s pod logs, the app is running:
Dash is running on https://0.0.0.0:8050/ * Serving Flask app 'dashboard' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on [https://127.0.0.1:8050](https://127.0.0.1:8050/) * Running on [https://10.42.5.53:8050](https://10.42.5.53:8050/)

Hmm.

Try changing the port to 8000.

Hello,

I remember having changed the port. It led to the same results.

I think the problem comes from adding ssl_context. When I removed it, a simple app was deployed successfully.
But Google OAuth requires https. So I am still seeing how to solve this problem.

Yes. That is problematic.

Is your nginx set to pass the ssl on to the request to the backend? Also, are you making sure that the route being called by the server to the flask server is https and not http.

By default, this will normally be http.

Also, maybe you should be doing the cert and stuff from nginx instead, and not worry about the http because from nginx to the flask server it’s treated as a localhost, which doesn’t have the same requirement because it’s local. Then nginx sends it back to you with ssl.

Hello,

After checking with devops team, nginx uses ssl, so no need to add ssl_context to app.run in the dash app.
Also I added in google console the uri starting with http:// in addition to the one starting with https:// and now it works :smiley:

Thank you for your help !

1 Like