43 lines
1.5 KiB
Python
Executable File
43 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Registers N synthetic users, opens one session each and writes the tokens, one per line, to a file.
|
|
|
|
The file holds live credentials of synthetic users only; the caller keeps it outside the repository
|
|
(mode 600) and deletes it after the run.
|
|
"""
|
|
import base64
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.request
|
|
import uuid
|
|
|
|
PASSWORD = "track1-stampede-password"
|
|
|
|
|
|
def request(method, url, body=None, headers=None):
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
req = urllib.request.Request(url, data=data, method=method, headers=headers or {})
|
|
if data is not None:
|
|
req.add_header("Content-Type", "application/json")
|
|
with urllib.request.urlopen(req, timeout=10) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def main():
|
|
base_url, count, path = sys.argv[1], int(sys.argv[2]), sys.argv[3]
|
|
tokens = []
|
|
for i in range(count):
|
|
username = f"t1_{uuid.uuid4().hex[:12]}"
|
|
request("POST", f"{base_url}/api/v1/users",
|
|
{"username": username, "display_name": f"Stampede {i}", "password": PASSWORD})
|
|
basic = base64.b64encode(f"{username}:{PASSWORD}".encode()).decode()
|
|
session = request("POST", f"{base_url}/api/v1/auth/sessions", headers={"Authorization": f"Basic {basic}"})
|
|
tokens.append(session["token"])
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
with os.fdopen(fd, "w") as f:
|
|
f.write("\n".join(tokens) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|