forked from mtusi2026/mtusi2026lab7
37 lines
1.7 KiB
Python
37 lines
1.7 KiB
Python
import subprocess
|
|
|
|
from common import ROOT, login, new_user, redis_cli, session_key
|
|
|
|
|
|
def without_password():
|
|
"""lab7: по TLS с сертификатом, но без AUTH — сервер отвечает NOAUTH; без TLS — соединение не устанавливается."""
|
|
tls = ("redis-cli --tls --cacert /run/tls-ca/ca.crt --cert /run/tls/tls.crt --key /run/tls/tls.key "
|
|
"-h 127.0.0.1 --sni redis get anything")
|
|
plain = "redis-cli -h 127.0.0.1 -p 6379 get anything"
|
|
outputs = []
|
|
for command in (tls, plain):
|
|
result = subprocess.run(["docker", "compose", "exec", "-T", "redis", "sh", "-c", command + " 2>&1"],
|
|
cwd=ROOT, capture_output=True, check=False)
|
|
outputs.append((result.stdout + result.stderr).decode("utf-8", "replace").strip()[:80])
|
|
return f"TLS without AUTH -> {outputs[0]!r}; plaintext -> {outputs[1]!r}"
|
|
|
|
|
|
def main():
|
|
user = new_user("info")
|
|
token = login(user["username"], user["password"]).json["token"]
|
|
key = session_key(token)
|
|
print(f"session key: {key[:24]}...; value is the user id: {redis_cli('get', key) == user['id']}")
|
|
print(f"TTL of the key, seconds: {redis_cli('ttl', key)}")
|
|
keys = redis_cli("--scan", "--pattern", "'session:*'").splitlines()
|
|
print(f"sessions in Redis: {len(keys)}")
|
|
for name in ("appendonly", "appendfsync", "maxmemory-policy", "port", "tls-port", "tls-auth-clients"):
|
|
print("config", " = ".join(redis_cli("config", "get", name).split()))
|
|
for line in redis_cli("info", "persistence").splitlines():
|
|
if line.startswith(("aof_enabled", "aof_last_write_status")):
|
|
print(line)
|
|
print(f"without password: {without_password()}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|