Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with pylint
run: |
pylint conditional
pylint conditional --disable=logging-fstring-interpolation
111 changes: 63 additions & 48 deletions conditional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@

# Sentry setup
sentry_sdk.init(
dsn=app.config['SENTRY_DSN'],
dsn=app.config["SENTRY_DSN"],
integrations=[FlaskIntegration(), SqlalchemyIntegration()],
environment=app.config['SENTRY_ENV'],
environment=app.config["SENTRY_ENV"],
)

ldap = CSHLDAP(app.config['LDAP_BIND_DN'],
app.config['LDAP_BIND_PW'],
ro=app.config['LDAP_RO'])
ldap = CSHLDAP(
app.config["LDAP_BIND_DN"], app.config["LDAP_BIND_PW"], ro=app.config["LDAP_RO"]
)

client_metadata = ClientMetadata(app.config["OIDC_CLIENT_CONFIG"])
provider_config = ProviderConfiguration(issuer=app.config["OIDC_ISSUER"], client_registration_info=client_metadata)
provider_config = ProviderConfiguration(
issuer=app.config["OIDC_ISSUER"], client_registration_info=client_metadata
)
frosh_provider_config = ProviderConfiguration(
issuer=app.config["FROSH_OIDC_ISSUER"], client_registration_info=client_metadata
)

auth = OIDCAuthentication({'default': provider_config}, app)
auth = OIDCAuthentication(
{"default": provider_config, "frosh": frosh_provider_config}, app
)

app.secret_key = app.config["SECRET_KEY"]

Expand All @@ -60,42 +67,48 @@ def start_of_year():


# Configure Logging
def request_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
if 'request' in event_dict:
flask_request = event_dict['request']
event_dict['ip'] = flask_request.remote_addr
event_dict['method'] = flask_request.method
event_dict['blueprint'] = flask_request.blueprint
event_dict['path'] = flask_request.full_path
if 'auth_dict' in event_dict:
auth_dict = event_dict['auth_dict']
event_dict['user'] = auth_dict['username']
def request_processor(
logger, log_method, event_dict
): # pylint: disable=unused-argument, redefined-outer-name
if "request" in event_dict:
flask_request = event_dict["request"]
event_dict["ip"] = flask_request.remote_addr
event_dict["method"] = flask_request.method
event_dict["blueprint"] = flask_request.blueprint
event_dict["path"] = flask_request.full_path
if "auth_dict" in event_dict:
auth_dict = event_dict["auth_dict"]
event_dict["user"] = auth_dict["username"]
return event_dict


def database_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
if 'request' in event_dict:
if event_dict['method'] != 'GET':
def database_processor(
logger, log_method, event_dict
): # pylint: disable=unused-argument, redefined-outer-name
if "request" in event_dict:
if event_dict["method"] != "GET":
log = UserLog(
ipaddr=event_dict['ip'],
user=event_dict['user'],
method=event_dict['method'],
blueprint=event_dict['blueprint'],
path=event_dict['path'],
description=event_dict['event']
ipaddr=event_dict["ip"],
user=event_dict["user"],
method=event_dict["method"],
blueprint=event_dict["blueprint"],
path=event_dict["path"],
description=event_dict["event"],
)
db.session.add(log)
db.session.flush()
db.session.commit()
del event_dict['request']
del event_dict["request"]
return event_dict


structlog.configure(processors=[
request_processor,
database_processor,
structlog.processors.KeyValueRenderer()
])
structlog.configure(
processors=[
request_processor,
database_processor,
structlog.processors.KeyValueRenderer(),
]
)

logger = structlog.get_logger()

Expand Down Expand Up @@ -133,16 +146,16 @@ def database_processor(logger, log_method, event_dict): # pylint: disable=unuse
from .util.ldap import ldap_get_member


@app.route('/<path:path>')
@app.route("/<path:path>")
def static_proxy(path):
# send_static_file will guess the correct MIME type
return app.send_static_file(path)


@app.route('/')
@app.route("/")
@auth.oidc_auth("default")
def default_route():
return redirect('/dashboard')
return redirect("/dashboard")


@app.route("/logout")
Expand All @@ -156,7 +169,7 @@ def health():
"""
Shows an ok status if the application is up and running
"""
return {'status': 'ok'}
return {"status": "ok"}


@app.errorhandler(404)
Expand All @@ -167,17 +180,17 @@ def route_errors(error, user_dict=None):
data = {}

# Handle the case where the header isn't present
if user_dict['username'] is not None:
data['username'] = user_dict['account'].uid
data['name'] = user_dict['account'].cn
if user_dict["username"] is not None:
data["username"] = user_dict["account"].uid
data["name"] = user_dict["account"].cn
else:
data['username'] = "unknown"
data['name'] = "Unknown"
data["username"] = "unknown"
data["name"] = "Unknown"

# Figure out what kind of error was passed
if isinstance(error, int):
code = error
elif hasattr(error, 'code'):
elif hasattr(error, "code"):
code = error.code
else:
# Unhandled exception
Expand All @@ -189,11 +202,13 @@ def route_errors(error, user_dict=None):
else:
error_desc = type(error).__name__

return render_template('errors.html',
error=error_desc,
error_code=code,
event_id=sentry_sdk.last_event_id(),
**data), int(code)
return render_template(
"errors.html",
error=error_desc,
error_code=code,
event_id=sentry_sdk.last_event_id(),
**data
), int(code)


logger.info('conditional started')
logger.info("conditional started")
Loading