Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): properly handle async Flask routes in decorators #231

Merged
merged 1 commit into from
Aug 29, 2024
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
6 changes: 3 additions & 3 deletions bento_lib/auth/middleware/flask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from flask import Flask, Request, Response, request, g
from flask import Flask, Request, Response, current_app, g, request
from functools import wraps

from bento_lib.auth.exceptions import BentoAuthException
Expand Down Expand Up @@ -91,14 +91,14 @@ def wrapper(*args, **kwargs):
self.check_authz_evaluate(request, permissions, resource, require_token, set_authz_flag)
except BentoAuthException as e:
return self._make_auth_error(e)
return func(*args, **kwargs)
return current_app.ensure_sync(func)(*args, **kwargs)
return wrapper
return decorator

def deco_public_endpoint(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
res = current_app.ensure_sync(func)(*args, **kwargs)
if self.enabled:
self.mark_authz_done(request)
return res
Expand Down
93 changes: 47 additions & 46 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bento-lib"
version = "12.1.0"
version = "12.1.1"
description = "A set of common utilities and helpers for Bento platform services."
authors = [
"David Lougheed <[email protected]>",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_platform_flask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bento_lib.responses.flask_errors as fe

import asyncio
import logging
import pytest
import responses
Expand Down Expand Up @@ -80,12 +81,14 @@ def auth_post_exempted():

@test_app_auth.route("/post-public", methods=["POST"])
@auth_middleware.deco_public_endpoint
def auth_post_public():
async def auth_post_public(): # ensure Flask public decorator works for async code too
await asyncio.sleep(0.1)
return jsonify(request.json)

@test_app_auth.route("/post-private", methods=["POST"])
@auth_middleware.deco_require_permissions_on_resource(frozenset({P_INGEST_DATA}))
def auth_post_private():
async def auth_post_private(): # ensure Flask private decorator works for async code too
await asyncio.sleep(0.1)
return jsonify(request.json)

@test_app_auth.route("/post-private-no-flag", methods=["POST"])
Expand Down