{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FIPS Dataset class\n",
    "\n",
    "This notebook illustrates basic functionality with the `FIPSDataset` class that holds FIPS 140 dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sec_certs.dataset.fips import FIPSDataset\n",
    "from sec_certs.sample import FIPSCertificate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get fresh dataset snapshot from mirror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset: FIPSDataset = FIPSDataset.from_web_latest()\n",
    "print(len(dset))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Do some basic dataset serialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dump dataset into json and load it back\n",
    "dset.to_json(\"./fips_dataset.json\")\n",
    "new_dset = FIPSDataset.from_json(\"./fips_dataset.json\")\n",
    "assert dset == new_dset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple dataset manipulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get certificates from a single manufacturer\n",
    "cisco_certs = [cert for cert in dset if \"Cisco\" in cert.web_scan.vendor]\n",
    "\n",
    "# Get certificates with some CVE\n",
    "vulnerable_certs = [cert for cert in dset if cert.heuristics.related_cves]\n",
    "\n",
    "# Show CVE ids of some vulnerable certificate\n",
    "print(f\"{vulnerable_certs[0].heuristics.related_cves=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dissect single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Select a certificate and print some attributes\n",
    "cert: FIPSCertificate = dset[\"542cacae1d41132a\"]\n",
    "\n",
    "print(f\"{cert.web_scan.module_name=}\")\n",
    "print(f\"{cert.heuristics.cpe_matches=}\")\n",
    "print(f\"{cert.web_scan.level=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serialize single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "cert.to_json(\"./cert.json\")\n",
    "new_cert: FIPSCertificate = FIPSCertificate.from_json(\"./cert.json\")\n",
    "assert new_cert == cert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create new dataset and fully process it\n",
    "\n",
    "*Warning*: It's not good idea to run this from notebook. It may take several hours to finnish. We recommend using `from_web_latest()` or turning this into a Python script."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset = FIPSDataset()\n",
    "dset.get_certs_from_web()\n",
    "dset.convert_all_pdfs()\n",
    "dset.pdf_scan()\n",
    "dset.extract_certs_from_tables()\n",
    "dset.finalize_results()\n",
    "\n",
    "# TODO: Resolve https://github.com/crocs-muni/sec-certs/issues/210 and refactor this\n",
    "# if not no_download_algs:\n",
    "#     aset.get_certs_from_web()\n",
    "#     logger.info(f\"Finished parsing. Have algorithm dataset with {len(aset)} algorithm numbers.\")\n",
    "#     dset.algorithms = aset\n",
    "\n",
    "# TODO: Resolve https://github.com/crocs-muni/sec-certs/issues/211 and uncomment\n",
    "# dset.plot_graphs(show=False)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "6386d1612879d92d026c363e7667e428bc38d86c5a080d58c3d70e7cd43df67d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.1 ('certsvenv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
