Implement Lab 7 security and infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""Exercise Nginx login rate limiting; run against the HTTPS ingress."""
|
||||
import argparse
|
||||
import base64
|
||||
import ssl
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--base-url", default="https://localhost:8443")
|
||||
p.add_argument("--ca-file", default="secrets/tls/ca.crt")
|
||||
args = p.parse_args()
|
||||
context = ssl.create_default_context(cafile=args.ca_file)
|
||||
auth = base64.b64encode(b"not-a-user:deliberately-wrong-password").decode()
|
||||
statuses = []
|
||||
retry_after = None
|
||||
for _ in range(12):
|
||||
request = urllib.request.Request(
|
||||
args.base_url.rstrip("/") + "/api/v1/auth/sessions",
|
||||
data=b"{}",
|
||||
headers={"Authorization": "Basic " + auth, "Content-Type": "application/json"},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, context=context, timeout=5) as response:
|
||||
statuses.append(response.status)
|
||||
except urllib.error.HTTPError as exc:
|
||||
statuses.append(exc.code)
|
||||
if exc.code == 429:
|
||||
retry_after = exc.headers.get("Retry-After")
|
||||
time.sleep(0.05)
|
||||
assert 429 in statuses, f"rate limiter did not return 429; statuses={statuses}"
|
||||
assert retry_after, "429 response lacks Retry-After"
|
||||
print("PASS: login rate limit returned 429 with Retry-After")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user