Skip to content

Commit

Permalink
Merge pull request uc-cdis#1122 from uc-cdis/fix/authz-does-not-inclu…
Browse files Browse the repository at this point in the history
…de-open-access-for-some-users

Pull resources from auth_mapping
  • Loading branch information
k-burt-uch authored Nov 17, 2023
2 parents 93195da + beefc2d commit e11eb66
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@
"filename": "tests/conftest.py",
"hashed_secret": "1348b145fa1a555461c1b790a2f66614781091e9",
"is_verified": false,
"line_number": 1556
"line_number": 1559
},
{
"type": "Base64 High Entropy String",
"filename": "tests/conftest.py",
"hashed_secret": "227dea087477346785aefd575f91dd13ab86c108",
"is_verified": false,
"line_number": 1579
"line_number": 1582
}
],
"tests/credentials/google/test_credentials.py": [
Expand Down Expand Up @@ -395,5 +395,5 @@
}
]
},
"generated_at": "2023-10-20T20:37:17Z"
"generated_at": "2023-11-16T21:15:57Z"
}
4 changes: 1 addition & 3 deletions fence/resources/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ def get_user_info(current_session, username):

if hasattr(flask.current_app, "arborist"):
try:
resources = flask.current_app.arborist.list_resources_for_user(
user.username
)
auth_mapping = flask.current_app.arborist.auth_mapping(user.username)
resources = list(auth_mapping.keys())
except ArboristError as exc:
logger.error(
f"request to arborist for user's resources failed; going to list empty. Error: {exc}"
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ def mock_arborist_requests(request):

def do_patch(urls_to_responses=None):
urls_to_responses = urls_to_responses or {}
defaults = {"arborist/health": {"GET": ("", 200)}}
defaults = {
"arborist/health": {"GET": ("", 200)},
"arborist/auth/mapping": {"POST": ({}, "200")},
}
defaults.update(urls_to_responses)
urls_to_responses = defaults

Expand Down
29 changes: 28 additions & 1 deletion tests/oidc/core/user_info/test_userinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import json

import pytest
from gen3authz.client.arborist.errors import ArboristError

from fence.models import UserGoogleAccount


@pytest.fixture(autouse=True)
def mock_arborist(mock_arborist_requests):
def mock_arborist(mock_arborist_requests, encoded_creds_jwt):
mock_arborist_requests()


Expand Down Expand Up @@ -56,3 +57,29 @@ def test_userinfo_extra_claims_get(
assert resp.json["name"]
assert resp.json["linked_google_account"]
assert resp.status_code == 200


def test_userinfo_arborist_authz(
client, encoded_creds_jwt, mock_arborist_requests, app
):
"""
Tests that the userinfo endpoint populates authz and resource based on the /auth/mapping from Arborist
"""
expected_authz = {"/open": [{"service": "peregrine", "method": "read"}]}
expected_resources = list(expected_authz.keys())
mock_arborist_requests(
{
f"arborist/auth/mapping": {"POST": (expected_authz, 200)},
}
)

resp = client.post(
"/user",
headers={"Authorization": "Bearer " + encoded_creds_jwt["jwt"]},
).json

actual_authz = resp.get("authz", {})
actual_resources = resp.get("resources", [])

assert actual_authz == expected_authz
assert actual_resources == expected_resources
7 changes: 7 additions & 0 deletions tests/test_logout.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import mock
import urllib.request, urllib.parse, urllib.error

import pytest

from fence.auth import build_redirect_url
from fence.config import config
from fence.resources.storage.cdis_jwt import create_session_token


@pytest.fixture(autouse=True)
def mock_arborist(mock_arborist_requests):
mock_arborist_requests()


def test_redirect_url():
assert build_redirect_url("", "/") == "/"
assert build_redirect_url("host.domain", "/fred") == "https://host.domain/fred"
Expand Down

0 comments on commit e11eb66

Please sign in to comment.