Skip to content

Instantly share code, notes, and snippets.

@neovintage
Created September 30, 2015 15:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neovintage/d93baefc7f623a8f599f to your computer and use it in GitHub Desktop.
Save neovintage/d93baefc7f623a8f599f to your computer and use it in GitHub Desktop.
SSL Support for Airflow
from flask import Blueprint, redirect, url_for, request, current_app
from airflow.plugins_manager import AirflowPlugin
YEAR_IN_SECS = 31536000
ssl_bp = Blueprint('ssl_everything', __name__)
@ssl_bp.before_app_request
def before_request():
app = current_app._get_current_object()
criteria = [
request.is_secure,
app.debug,
request.headers.get('X-Forwarded-Proto', 'http') == 'https'
]
if not any(criteria):
if request.url.startswith('http://'):
url = request.url.replace('http://', 'https://', 1)
r = redirect(url, code=302)
return r
@ssl_bp.after_app_request
def after_request(response):
hsts_policy = 'max-age={0}'.format(YEAR_IN_SECS)
response.headers.setdefault('Strict-Transport-Security', hsts_policy)
return response
class AirflowSSLPlugin(AirflowPlugin):
name = 'ssl_everything'
flask_blueprints = [ssl_bp]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment