forked from mtusi2026/mtusi2026lab2
116 lines
3.1 KiB
Python
Executable File
116 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Verify deployed short session TTL, wrong password and logout."""
|
|
import base64
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import time
|
|
|
|
from client import Client, Failure, arguments, check, run
|
|
|
|
|
|
def execute_hook(script, action, ttl_seconds):
|
|
env = dict(os.environ)
|
|
env['SESSION_TTL_ACTION'] = action
|
|
env['SHORT_SESSION_TTL_SECONDS'] = str(ttl_seconds)
|
|
try:
|
|
result = subprocess.run(
|
|
[str(script)],
|
|
env=env,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
timeout=90,
|
|
check=False,
|
|
)
|
|
except subprocess.TimeoutExpired:
|
|
raise Failure('session TTL hook exceeded 90 seconds') from None
|
|
check(
|
|
result.returncode == 0,
|
|
'session TTL hook failed for action ' + action,
|
|
)
|
|
|
|
|
|
def basic(username, password):
|
|
encoded = base64.b64encode((username + ':' + password).encode()).decode()
|
|
return {'Authorization': 'Basic ' + encoded}
|
|
|
|
|
|
def main():
|
|
def configure(parser):
|
|
parser.add_argument(
|
|
'--action-script',
|
|
required=True,
|
|
help='Executable hook controlled by SESSION_TTL_ACTION=short|restore',
|
|
)
|
|
parser.add_argument('--ttl-seconds', type=int, default=2)
|
|
|
|
args = arguments(
|
|
'Short deployed session TTL, wrong-password and logout checks',
|
|
configure,
|
|
)
|
|
check(
|
|
1 <= args.ttl_seconds <= 10,
|
|
'ttl-seconds must be between 1 and 10 for this test',
|
|
)
|
|
|
|
script = Path(args.action_script).expanduser().resolve()
|
|
check(
|
|
script.is_file() and os.access(script, os.X_OK),
|
|
'action script must exist and be executable (chmod +x)',
|
|
)
|
|
|
|
execute_hook(script, 'short', args.ttl_seconds)
|
|
try:
|
|
client = Client(args.base_url, args.ca_file, timeout=10)
|
|
actor = client.user('short_ttl')
|
|
registration = actor['registration']
|
|
|
|
client.call(
|
|
'POST',
|
|
'/api/v1/auth/sessions',
|
|
401,
|
|
headers=basic(registration['username'], 'Wrong-password-123'),
|
|
)
|
|
client.call(
|
|
'GET',
|
|
'/api/v1/users/me',
|
|
actor=actor,
|
|
schema='User',
|
|
)
|
|
|
|
time.sleep(args.ttl_seconds + 1.25)
|
|
client.call('GET', '/api/v1/users/me', 401, actor=actor)
|
|
|
|
session = client.call(
|
|
'POST',
|
|
'/api/v1/auth/sessions',
|
|
201,
|
|
headers=basic(
|
|
registration['username'],
|
|
registration['password'],
|
|
),
|
|
schema='Session',
|
|
)['json']
|
|
fresh_actor = {
|
|
'id': actor['id'],
|
|
'token': session['token'],
|
|
}
|
|
client.call(
|
|
'DELETE',
|
|
'/api/v1/auth/sessions/current',
|
|
204,
|
|
actor=fresh_actor,
|
|
)
|
|
client.call('GET', '/api/v1/users/me', 401, actor=fresh_actor)
|
|
finally:
|
|
execute_hook(script, 'restore', args.ttl_seconds)
|
|
|
|
print(
|
|
'PASS: deployed short SESSION_TTL_SECONDS expired the token; '
|
|
'wrong password and logout remained enforced.'
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|