test: expired JWT shows empty role in logs
This commit is contained in:
committed by
Steve Chavez
parent
e39740cca3
commit
8105375b64
@@ -7,7 +7,7 @@ import time
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from config import BASEDIR, CONFIGSDIR, FIXTURES, SECRET
|
from config import BASEDIR, CONFIGSDIR, FIXTURES, SECRET
|
||||||
from util import authheader, jwtauthheader, parse_server_timings_header
|
from util import authheader, jwtauthheader, parse_server_timings_header, relativeSeconds
|
||||||
from postgrest import (
|
from postgrest import (
|
||||||
run,
|
run,
|
||||||
sleep_until_postgrest_config_reload,
|
sleep_until_postgrest_config_reload,
|
||||||
@@ -72,9 +72,6 @@ def test_jwt_errors(defaultenv):
|
|||||||
|
|
||||||
env = {**defaultenv, "PGRST_JWT_SECRET": SECRET, "PGRST_JWT_AUD": "io tests"}
|
env = {**defaultenv, "PGRST_JWT_SECRET": SECRET, "PGRST_JWT_AUD": "io tests"}
|
||||||
|
|
||||||
def relativeSeconds(sec):
|
|
||||||
return int((datetime.now(timezone.utc) + timedelta(seconds=sec)).timestamp())
|
|
||||||
|
|
||||||
with run(env=env) as postgrest:
|
with run(env=env) as postgrest:
|
||||||
headers = jwtauthheader({}, "other secret")
|
headers = jwtauthheader({}, "other secret")
|
||||||
response = postgrest.session.get("/", headers=headers)
|
response = postgrest.session.get("/", headers=headers)
|
||||||
|
|||||||
+20
-1
@@ -8,7 +8,7 @@ import time
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from config import CONFIGSDIR, FIXTURES, SECRET
|
from config import CONFIGSDIR, FIXTURES, SECRET
|
||||||
from util import Thread, jwtauthheader, parse_server_timings_header
|
from util import Thread, jwtauthheader, parse_server_timings_header, relativeSeconds
|
||||||
from postgrest import (
|
from postgrest import (
|
||||||
freeport,
|
freeport,
|
||||||
is_ipv6,
|
is_ipv6,
|
||||||
@@ -1028,6 +1028,25 @@ def test_log_query(level, defaultenv):
|
|||||||
assert len(pre_reqs) == 1
|
assert len(pre_reqs) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_expired_jwt_log_lacks_role(defaultenv):
|
||||||
|
"Expired JWT requests are logged without a role."
|
||||||
|
|
||||||
|
env = {**defaultenv, "PGRST_JWT_SECRET": SECRET}
|
||||||
|
headers = jwtauthheader({"exp": relativeSeconds(-35)}, SECRET)
|
||||||
|
|
||||||
|
with run(env=env) as postgrest:
|
||||||
|
response = postgrest.session.get("/authors_only", headers=headers)
|
||||||
|
assert response.status_code == 401
|
||||||
|
|
||||||
|
output = postgrest.read_stdout(nlines=1)
|
||||||
|
|
||||||
|
assert len(output) == 1
|
||||||
|
assert re.match(
|
||||||
|
r'- - - \[.+\] "GET /authors_only HTTP/1.1" 401 \d+ "" "python-requests/.+"',
|
||||||
|
output[0],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
|
def test_no_pool_connection_required_on_bad_http_logic(defaultenv):
|
||||||
"no pool connection should be consumed for failing on invalid http logic"
|
"no pool connection should be consumed for failing on invalid http logic"
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import threading
|
import threading
|
||||||
import jwt
|
import jwt
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
|
|
||||||
class Thread(threading.Thread):
|
class Thread(threading.Thread):
|
||||||
@@ -31,6 +32,10 @@ def jwtauthheader(claim, secret):
|
|||||||
return authheader(jwt.encode(claim, secret))
|
return authheader(jwt.encode(claim, secret))
|
||||||
|
|
||||||
|
|
||||||
|
def relativeSeconds(sec):
|
||||||
|
return int((datetime.now(timezone.utc) + timedelta(seconds=sec)).timestamp())
|
||||||
|
|
||||||
|
|
||||||
def parse_server_timings_header(header):
|
def parse_server_timings_header(header):
|
||||||
"""Parse the Server-Timing header into a dict of metric names to values.
|
"""Parse the Server-Timing header into a dict of metric names to values.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user