Skip to content

Commit c921caf

Browse files
committed
add support for service parameter in authz payload
1 parent d99aaf1 commit c921caf

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

paasta_tools/api/tweens/auth.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logging
1717
import os
1818
from typing import NamedTuple
19+
from typing import Optional
1920

2021
import cachetools.func
2122
import pyramid
@@ -53,7 +54,12 @@ def __call__(self, request: Request) -> Response:
5354
"""
5455
token = request.headers.get("Authorization", "").strip()
5556
token = token.split()[-1] if token else "" # removes "Bearer" prefix
56-
auth_outcome = self.is_request_authorized(request.path, token, request.method)
57+
auth_outcome = self.is_request_authorized(
58+
request.path,
59+
token,
60+
request.method,
61+
request.swagger_data.get("service", None),
62+
)
5763
if self.enforce and not auth_outcome.authorized:
5864
return HTTPForbidden(
5965
body=json.dumps({"reason": auth_outcome.reason}),
@@ -65,7 +71,11 @@ def __call__(self, request: Request) -> Response:
6571

6672
@cachetools.func.ttl_cache(maxsize=AUTH_CACHE_SIZE, ttl=AUTH_CACHE_TTL)
6773
def is_request_authorized(
68-
self, path: str, token: str, method: str
74+
self,
75+
path: str,
76+
token: str,
77+
method: str,
78+
service: Optional[str],
6979
) -> AuthorizationOutcome:
7080
"""Check if API request is authorized
7181
@@ -83,6 +93,7 @@ def is_request_authorized(
8393
"backend": "paasta",
8494
"token": token,
8595
"method": method,
96+
"service": service,
8697
},
8798
},
8899
timeout=2,

tests/api/tweens/test_auth.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ def test_call(mock_auth_tween):
3939
path="/something",
4040
method="post",
4141
headers={"Authorization": "Bearer aaa.bbb.ccc"},
42+
swagger_data={"service": "foobar"},
4243
)
4344
with patch.object(mock_auth_tween, "is_request_authorized") as mock_is_authorized:
4445
mock_is_authorized.return_value = auth.AuthorizationOutcome(True, "Ok")
4546
mock_auth_tween(mock_request)
46-
mock_is_authorized.assert_called_once_with("/something", "aaa.bbb.ccc", "post")
47+
mock_is_authorized.assert_called_once_with(
48+
"/something", "aaa.bbb.ccc", "post", "foobar"
49+
)
4750
mock_auth_tween.handler.assert_called_once_with(mock_request)
4851

4952

@@ -52,6 +55,7 @@ def test_call_deny(mock_auth_tween):
5255
path="/something",
5356
method="post",
5457
headers={"Authorization": "Bearer aaa.bbb.ccc"},
58+
swagger_data={},
5559
)
5660
with patch.object(mock_auth_tween, "is_request_authorized") as mock_is_authorized:
5761
mock_is_authorized.return_value = auth.AuthorizationOutcome(False, "Denied")
@@ -65,7 +69,7 @@ def test_is_request_authorized(mock_auth_tween):
6569
"result": {"allowed": True, "reason": "User allowed"}
6670
}
6771
assert mock_auth_tween.is_request_authorized(
68-
"/allowed", "aaa.bbb.ccc", "get"
72+
"/allowed", "aaa.bbb.ccc", "get", "foobar"
6973
) == auth.AuthorizationOutcome(True, "User allowed")
7074
mock_auth_tween.session.post.assert_called_once_with(
7175
url="http://localhost:31337",
@@ -75,6 +79,7 @@ def test_is_request_authorized(mock_auth_tween):
7579
"backend": "paasta",
7680
"token": "aaa.bbb.ccc",
7781
"method": "get",
82+
"service": "foobar",
7883
}
7984
},
8085
timeout=2,
@@ -84,12 +89,18 @@ def test_is_request_authorized(mock_auth_tween):
8489
def test_is_request_authorized_fail(mock_auth_tween):
8590
mock_auth_tween.session.post.side_effect = Exception
8691
assert mock_auth_tween.is_request_authorized(
87-
"/allowed", "eee.ddd.fff", "get"
92+
"/allowed",
93+
"eee.ddd.fff",
94+
"get",
95+
"foobar",
8896
) == auth.AuthorizationOutcome(False, "Auth backend error")
8997

9098

9199
def test_is_request_authorized_malformed(mock_auth_tween):
92100
mock_auth_tween.session.post.return_value.json.return_value = {"foo": "bar"}
93101
assert mock_auth_tween.is_request_authorized(
94-
"/allowed", "eee.ddd.fff", "post"
102+
"/allowed",
103+
"eee.ddd.fff",
104+
"post",
105+
"foobar",
95106
) == auth.AuthorizationOutcome(False, "Malformed auth response")

0 commit comments

Comments
 (0)