121 lines
5.5 KiB
Python
121 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise real native HTTP auth on CPU only; destroy temporary test keys."""
|
|
import http.client
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import secrets
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
|
|
def main():
|
|
binary = Path(sys.argv[1]).resolve()
|
|
with tempfile.TemporaryDirectory(prefix='strata-auth-smoke-') as directory:
|
|
root = Path(directory)
|
|
key = root / 'api.key'
|
|
token = secrets.token_urlsafe(32)
|
|
key.write_text(token + '\n')
|
|
key.chmod(0o600)
|
|
with socket.socket() as probe:
|
|
probe.bind(('127.0.0.1', 0))
|
|
port = probe.getsockname()[1]
|
|
config = root / 'config.json'
|
|
config.write_text(json.dumps({'bind': f'127.0.0.1:{port}', 'api_key_file': str(key), 'model': 'auth-cpu-smoke'}))
|
|
env = {'PATH': os.defpath}
|
|
|
|
def request(path, headers=(), body=None):
|
|
conn = http.client.HTTPConnection('127.0.0.1', port, timeout=2)
|
|
conn.putrequest('POST' if body is not None else 'GET', path)
|
|
for name, value in headers:
|
|
conn.putheader(name, value)
|
|
if body is not None:
|
|
conn.putheader('Content-Type', 'application/json')
|
|
conn.putheader('Content-Length', str(len(body)))
|
|
conn.endheaders(body)
|
|
response = conn.getresponse()
|
|
result = response.status, dict(response.getheaders()), response.read().decode()
|
|
conn.close()
|
|
assert token not in result[2]
|
|
return result
|
|
|
|
with tempfile.TemporaryFile() as logs:
|
|
process = subprocess.Popen([binary, 'serve', '--config', config], env=env, stdout=logs, stderr=logs)
|
|
try:
|
|
deadline = time.monotonic() + 8
|
|
while True:
|
|
try:
|
|
assert request('/health')[0] == 200
|
|
break
|
|
except OSError:
|
|
assert process.poll() is None and time.monotonic() < deadline
|
|
time.sleep(0.02)
|
|
for headers in [(), (('Authorization', 'Bearer wrong'),),
|
|
(('Authorization', 'Bearer ' + token), ('Authorization', 'Bearer ' + token))]:
|
|
code, response_headers, _ = request('/v1/responses', headers, b'not-json')
|
|
assert code == 401
|
|
assert response_headers.get('www-authenticate') == 'Bearer'
|
|
auth = (('Authorization', 'Bearer ' + token),)
|
|
assert request('/status')[0] == 401
|
|
assert request('/ready')[0] == 401
|
|
status = request('/status', auth)
|
|
assert status[0] == 200 and json.loads(status[2])['model'] == 'auth-cpu-smoke'
|
|
assert request('/ready', auth)[0] == 503
|
|
assert request('/v1/responses', auth, b'not-json')[0] == 400
|
|
finally:
|
|
process.terminate()
|
|
try:
|
|
process.wait(timeout=8)
|
|
except subprocess.TimeoutExpired:
|
|
process.kill()
|
|
process.wait()
|
|
logs.seek(0)
|
|
assert token.encode() not in logs.read()
|
|
|
|
# Each invalid source must fail before binding, with no secret echo.
|
|
for content, mode in [(b'short', 0o600), (b'A' * 32 + b'\ninside', 0o600),
|
|
(b'A' * 515, 0o600), (token.encode(), 0o644)]:
|
|
key.write_bytes(content)
|
|
key.chmod(mode)
|
|
result = subprocess.run([binary, 'serve', '--config', config], env=env,
|
|
capture_output=True, timeout=8)
|
|
assert result.returncode != 0
|
|
assert token.encode() not in result.stdout + result.stderr
|
|
fifo = root / 'not-a-file'
|
|
os.mkfifo(fifo, 0o600)
|
|
result = subprocess.run([binary, 'serve', '--config', config],
|
|
env={**env, 'STRATA_API_KEY_FILE': str(fifo)}, capture_output=True, timeout=8)
|
|
assert result.returncode != 0 and b'regular file' in result.stderr
|
|
# An environment override is authoritative, including invalid paths.
|
|
result = subprocess.run([binary, 'serve', '--config', config],
|
|
env={**env, 'STRATA_API_KEY_FILE': str(root/'missing')}, capture_output=True, timeout=8)
|
|
assert result.returncode != 0
|
|
config.write_text(json.dumps({'bind': f'127.0.0.1:{port}', 'model': 'auth-off-fixture'}))
|
|
with tempfile.TemporaryFile() as logs:
|
|
process = subprocess.Popen([binary, 'serve', '--config', config], env=env, stdout=logs, stderr=logs)
|
|
try:
|
|
deadline = time.monotonic() + 8
|
|
while True:
|
|
try:
|
|
assert request('/status')[0] == 200
|
|
break
|
|
except OSError:
|
|
assert process.poll() is None and time.monotonic() < deadline
|
|
time.sleep(0.02)
|
|
assert request('/v1/responses', body=b'not-json')[0] == 400
|
|
finally:
|
|
process.terminate()
|
|
try:
|
|
process.wait(timeout=8)
|
|
except subprocess.TimeoutExpired:
|
|
process.kill()
|
|
process.wait()
|
|
print('Native auth passed: protected routes, duplicate rejection, public health, private key validation, FIFO/missing source and no key echo')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|