Skip to content

Commit

Permalink
fix UserScopes + minor formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Leobouloc committed Oct 24, 2023
1 parent 361a143 commit 8c1968e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ralph/api/auth/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_basic_auth_user(
headers={"WWW-Authenticate": "Basic"},
)

user = AuthenticatedUser(scopes=UserScopes(user.scopes), agent=user.agent)
user = AuthenticatedUser(scopes=user.scopes, agent=dict(user.agent))

# Restrict access by scopes
if settings.LRS_RESTRICT_BY_SCOPES:
Expand Down
10 changes: 9 additions & 1 deletion src/ralph/api/auth/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from functools import lru_cache
from typing import Dict, FrozenSet, Literal

from ralph.conf import settings

from pydantic import BaseModel

Scope = Literal[
Expand All @@ -22,7 +24,7 @@
class UserScopes(FrozenSet[Scope]):
"""Scopes available to users."""

@lru_cache()
@lru_cache(maxsize=1024)
def is_authorized(self, requested_scope: Scope):
"""Check if the requested scope can be accessed based on user scopes."""
expanded_scopes = {
Expand Down Expand Up @@ -54,6 +56,12 @@ def is_authorized(self, requested_scope: Scope):

return requested_scope in expanded_user_scopes

@classmethod
def __get_validators__(cls): # noqa: D105
def validate(value: FrozenSet[Scope]) -> UserScopes:
"""Transform value to an instance of UserScopes."""
return cls(value)
yield validate

class AuthenticatedUser(BaseModel):
"""Pydantic model for user authentication.
Expand Down
4 changes: 2 additions & 2 deletions src/ralph/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def check_restriction_compatibility(cls, values):
"LRS_RESTRICT_BY_AUTHORITY"
):
raise ConfigurationException(
"`LRS_RESTRICT_BY_AUTHORITY` must be set to `True` if using "
"`LRS_RESTRICT_BY_SCOPES=True`"
"LRS_RESTRICT_BY_AUTHORITY must be set to True if using "
"LRS_RESTRICT_BY_SCOPES=True"
)
return values

Expand Down
10 changes: 3 additions & 7 deletions tests/api/test_statements_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ def insert_clickhouse_statements(statements):


@pytest.fixture(params=["es", "mongo", "clickhouse"])
# pylint: disable=unused-argument
def insert_statements_and_monkeypatch_backend(
request, es, mongo, clickhouse, monkeypatch
):
"""(Security) Return a function that inserts statements into each backend."""
# pylint: disable=invalid-name
# pylint: disable=invalid-name,unused-argument

def _insert_statements_and_monkeypatch_backend(statements):
"""Inserts statements once into each backend."""
Expand Down Expand Up @@ -126,8 +125,7 @@ def test_api_statements_get_mine(
"""(Security) Test that the get statements API route, given a "mine=True"
query parameter returns a list of statements filtered by authority.
"""
# pylint: disable=redefined-outer-name
# pylint: disable=invalid-name
# pylint: disable=redefined-outer-name,invalid-name

# Create two distinct agents
if ifi == "account_same_home_page":
Expand Down Expand Up @@ -770,9 +768,7 @@ def test_api_statements_get_scopes(
monkeypatch, fs, es, auth_method, scopes, is_authorized
):
"""Test that getting statements behaves properly according to user scopes."""
# pylint: disable=invalid-name
# pylint: disable=too-many-locals
# pylint: disable=too-many-arguments
# pylint: disable=invalid-name,too-many-locals,too-many-arguments

monkeypatch.setattr(
"ralph.api.routers.statements.settings.LRS_RESTRICT_BY_SCOPES", True
Expand Down
4 changes: 2 additions & 2 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def test_conf_forbidden_scopes_without_authority(monkeypatch):
with pytest.raises(
ConfigurationException,
match=(
"`LRS_RESTRICT_BY_AUTHORITY` must be set to `True` if using "
"`LRS_RESTRICT_BY_SCOPES=True`"
"LRS_RESTRICT_BY_AUTHORITY must be set to True if using "
"LRS_RESTRICT_BY_SCOPES=True"
),
):
reload(conf)

0 comments on commit 8c1968e

Please sign in to comment.