{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c00-a6cd8283",
   "metadata": {},
   "source": [
    "# Encryption and authentication — practical\n",
    "\n",
    "The lab's secure broker only accepts TLS connections with a SCRAM login. It starts without a\n",
    "certificate and waits. You make a certificate authority and the broker's certificate, create\n",
    "users for Brightlane's services, connect, and then try three clients that get it wrong.\n",
    "\n",
    "**Lab services:** kafka-secure (`docker compose --profile secure up -d`; the notebook starts it)\n",
    "· **Lesson:** `modules/08-security-and-operations/01-encryption-and-authentication.md`"
   ]
  },
  {
   "cell_type": "code",
   "id": "c01-9e37b325",
   "metadata": {},
   "source": [
    "# Setup: find the lab's shared helpers\n",
    "import sys, pathlib\n",
    "lab_root = next(p for p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents] if (p / \"common\").is_dir())\n",
    "sys.path.insert(0, str(lab_root))\n",
    "\n",
    "import logging, time\n",
    "from common import settings, lab, secure"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c02-ce281bec",
   "metadata": {},
   "source": [
    "## Step 1: A broker without a certificate\n",
    "The notebook removes any earlier secure broker and starts a fresh one. Its start script waits for\n",
    "a certificate."
   ]
  },
  {
   "cell_type": "code",
   "id": "c03-66912a4c",
   "metadata": {},
   "source": [
    "secure.reset_broker()\n",
    "lab.compose(\"--profile\", \"secure\", \"up\", \"-d\", \"kafka-secure\")\n",
    "time.sleep(5)\n",
    "print(lab.compose(\"--profile\", \"secure\", \"logs\", \"--tail\", \"1\", \"kafka-secure\", echo=False))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c04-c92c90b8",
   "metadata": {},
   "source": [
    "## Step 2: A certificate authority and the broker's certificate\n",
    "`secure.make_certificates()` creates a CA (a key pair and a self-signed certificate) and a\n",
    "certificate for the broker, signed by the CA. The broker's certificate lists the names clients\n",
    "may use to reach it."
   ]
  },
  {
   "cell_type": "code",
   "id": "c05-1d3d695b",
   "metadata": {},
   "source": [
    "from cryptography import x509\n",
    "\n",
    "ca_pem, server_pem = secure.make_certificates()\n",
    "ca = x509.load_pem_x509_certificate(ca_pem)\n",
    "server = x509.load_pem_x509_certificates(server_pem)[0]\n",
    "print(\"CA:         \", ca.subject.rfc4514_string())\n",
    "print(\"broker:     \", server.subject.rfc4514_string(), \"| signed by:\", server.issuer.rfc4514_string())\n",
    "print(\"valid until:\", server.not_valid_after_utc.date())\n",
    "print(\"names:      \", [str(n.value) for n in server.extensions.get_extension_for_class(x509.SubjectAlternativeName).value])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c06-ac572023",
   "metadata": {},
   "source": [
    "The broker gets its key and certificate; clients only get the CA certificate, to check the\n",
    "broker's. Then the broker starts."
   ]
  },
  {
   "cell_type": "code",
   "id": "c07-4b45f0b9",
   "metadata": {},
   "source": [
    "secure.install_certificates(ca_pem, server_pem)\n",
    "started = time.monotonic()\n",
    "lab.compose(\"--profile\", \"secure\", \"up\", \"-d\", \"--wait\", \"kafka-secure\")\n",
    "print(f\"broker ready after {time.monotonic() - started:.0f} s\")\n",
    "log = lab.compose(\"--profile\", \"secure\", \"logs\", \"--no-log-prefix\", \"kafka-secure\", echo=False)\n",
    "shown = dict.fromkeys(line.strip() for line in log.splitlines()\n",
    "                      if line.strip().startswith((\"listeners =\", \"listener.security.protocol.map =\")))\n",
    "print(\"\\n\".join(shown))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c08-be6150c1",
   "metadata": {},
   "source": [
    "## Step 3: Log in as admin, create the services' users\n",
    "The admin's SCRAM credentials were added when the broker formatted its storage. Admin creates\n",
    "one user per service."
   ]
  },
  {
   "cell_type": "code",
   "id": "c09-6d263876",
   "metadata": {},
   "source": [
    "from confluent_kafka.admin import AdminClient, NewTopic\n",
    "\n",
    "admin = AdminClient(secure.client_config(*secure.ADMIN))\n",
    "cluster = admin.describe_cluster(request_timeout=15).result()\n",
    "print(\"connected over SASL_SSL to cluster\", cluster.cluster_id)\n",
    "secure.create_users(admin)\n",
    "for user, future in admin.describe_user_scram_credentials(list(secure.USERS)).items():\n",
    "    print(user, [(info.mechanism.name, info.iterations) for info in future.result().scram_credential_infos])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c10-755fac9c",
   "metadata": {},
   "source": [
    "The broker stores a salted hash per user, never the password, and shows only the mechanism and\n",
    "the number of hash iterations.\n",
    "\n",
    "## Step 4: Write and read, as admin and as a service"
   ]
  },
  {
   "cell_type": "code",
   "id": "c11-b1f5e176",
   "metadata": {},
   "source": [
    "from confluent_kafka import Producer\n",
    "\n",
    "admin.create_topics([NewTopic(\"charging.telemetry\", 3, 1)])[\"charging.telemetry\"].result()\n",
    "\n",
    "def write_one(config):\n",
    "    report = []\n",
    "    producer = Producer({**config, \"message.timeout.ms\": 10000})\n",
    "    producer.produce(\"charging.telemetry\", key=b\"BL-N03-04\", value=b'{\"power_kw\": 48.2}',\n",
    "                     on_delivery=lambda err, msg: report.append(err.str() if err else f\"stored at offset {msg.offset()}\"))\n",
    "    producer.flush(15)\n",
    "    return report[0]\n",
    "\n",
    "print(\"admin:      \", write_one(secure.client_config(*secure.ADMIN)))\n",
    "print(\"svc-gateway:\", write_one(secure.client_config(\"svc-gateway\", secure.USERS[\"svc-gateway\"])))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c12-2be8a5b8",
   "metadata": {},
   "source": [
    "svc-gateway logged in, so the broker knows who it is: authentication worked. But no rule allows it\n",
    "to write anything yet. That's authorization, lesson 8.2.\n",
    "\n",
    "## Break it: three clients that get it wrong\n",
    "`why()` tries to connect for a few seconds and returns what the client reports: its first log\n",
    "line about the connection if there is one, otherwise its first error."
   ]
  },
  {
   "cell_type": "code",
   "id": "c13-d14c002b",
   "metadata": {},
   "source": [
    "def why(config, seconds=6):\n",
    "    logged, errors = [], []\n",
    "    class Keep(logging.Handler):\n",
    "        def emit(self, record):\n",
    "            logged.append(record.getMessage().split(\"/bootstrap: \")[-1])\n",
    "    log = logging.getLogger(f\"why-{time.monotonic()}\")\n",
    "    log.setLevel(logging.INFO)                      # connection errors are logged at INFO\n",
    "    log.addHandler(Keep())\n",
    "    log.propagate = False\n",
    "    producer = Producer({**config, \"logger\": log, \"error_cb\": lambda err: errors.append(err.str())})\n",
    "    until = time.monotonic() + seconds\n",
    "    while time.monotonic() < until and not logged:\n",
    "        producer.poll(0.5)\n",
    "    return (logged or errors or [\"no problem\"])[0]\n",
    "\n",
    "good = secure.client_config(\"svc-gateway\", secure.USERS[\"svc-gateway\"])\n",
    "print(\"wrong password:  \", why({**good, \"sasl.password\": \"guess\"}))\n",
    "print(\"plaintext client:\", why({\"bootstrap.servers\": settings.SECURE_BOOTSTRAP}))\n",
    "print(\"CA not trusted:  \", why({k: v for k, v in good.items() if k != \"ssl.ca.location\"}))"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c14-a8cad667",
   "metadata": {},
   "source": [
    "## Clean up\n",
    "Delete the topic. The secure broker keeps running: lesson 8.2 continues with it. To stop it:\n",
    "`docker compose --profile secure stop`."
   ]
  },
  {
   "cell_type": "code",
   "id": "c15-ce349373",
   "metadata": {},
   "source": [
    "admin.delete_topics([\"charging.telemetry\"])[\"charging.telemetry\"].result()\n",
    "print(\"cleaned up\")"
   ],
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
