OpenCyber Academy
Bachelor's · Chapter 8 · English
Course code: OCA-104Chapter 08: OCA-104-C008

Chapter 8 — Programming II

Open, free learning for anyone who wants to protect people and technology. No account is required to read this text or its worked answers.

Lesson map

Lesson 08 published. Map, example, EX022.

flowchart TD
  N0["Learning outcomes"]
  N1["8.1 Functions"]
  N2["8.2 Files"]
  N3["8.3 Missing data"]
  N4["8.4 Errors"]
  N5["8.5 Modules"]
  N6["Combining checks"]
  N7["Logging the program's own runs"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7
  N7 --> EX["Worked example"]
  EX --> Q["Exercise EX022"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["8.1 Functions"]
  N2["8.2 Files"]
  N3["8.3 Missing data"]
  N4["8.4 Errors"]
  N5["8.5 Modules"]
  N6["Combining checks"]
  N7["Logging the program's own runs"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7 --> Q["EX022"]
[Learning outcomes]
    |
    v
[8.1 Functions]
    |
    v
[8.2 Files]
    |
    v
[8.3 Missing data]
    |
    v
[8.4 Errors]
    |
    v
[8.5 Modules]
    |
    v
[Combining checks]
    |
    v
[Logging the program's own runs]
    |
    v
[Worked example] --> [Exercise EX022]

Learning outcomes

You will (1) build larger checks with functions and modules; (2) read CSV and JSON; (3) handle errors and missing data.

8.1 Functions

Aisha writes functions such as load_devices(path) and check_encryption(device), each doing one job and tested separately.

Each function has a test with a known answer, and Aisha keeps the tests next to the code. check_encryption returns one of three values, encrypted, not encrypted or unknown, never a bare true or false. load_devices only reads the file and returns a list of dictionaries, so a format change affects one function and nothing else in the program.

8.2 Files

Device exports come as CSV; agent reports as JSON. Python's csv and json modules read both.

The management server's export is a CSV file with one row per laptop, while the agent reports arrive as one JSON document per device. Aisha's loader reads both and joins them on the asset tag, such as BHA-L03. When a tag appears in one source but not the other, the loader records the mismatch instead of dropping the row, so no device disappears from the summary.

8.3 Missing data

A device with no encryption field is neither encrypted nor unencrypted; it is unknown. Aisha reports unknowns separately rather than guessing.

In the synthetic exports, two laptops have no encryption field at all, because their agents had not reported since the setting was added. Treating those two as encrypted would raise the reported figure; treating them as unencrypted would send Gareth chasing problems that may not exist. The unknown category keeps both mistakes out and puts the two laptops on Gareth's follow-up list.

8.4 Errors

If a file is missing, the program stops with a clear message.

Aisha's message names the missing file and the expected folder, for example "devices.csv not found in exports/", and the program exits without printing a partial summary. A half-finished summary is worse than none, because Gareth could take a count of 30 laptops as the whole fleet. Empty files are treated the same way and stop the run with a message. A missing field inside a file is handled differently from a missing file: the run continues and the affected devices are reported as unknown, as the missing-data rule requires.

8.5 Modules

Shared functions live in one module, used by several checks.

Aisha's module, called checks.py, holds load_devices, check_encryption and the update-age check, each with its own tests. The encryption report and the weekly summary both import it, so a fix to one function reaches every script at once. Gareth only needs to update one file when the export format changes, which matters for a single IT officer. The module's tests run before every change to any script that imports it, so a fix for one check cannot silently break the encryption report or the weekly summary.

Combining checks

Aisha's program now runs three checks, encryption, update age and agent last seen, and prints a summary table with one row per laptop and a final count for each check. The summary is short enough to read in a minute, and details are saved to a file for follow-up.

She adds tests for each check and for the summary, using synthetic data with known answers, and runs them before every change. When she later adds a fourth check, the tests confirm she has not broken the first three.

She documents how to run the program in a short readme so Gareth can run it without her.

Logging the program's own runs

Each run records when it ran and how many devices it read, so a failed or partial run is noticed.

Applying it

Aisha writes a one-paragraph summary of this chapter for Gareth, focusing on the practical decision it supports and the report that will show whether it is working.

Worked example

Running on synthetic exports, Aisha's program reports 40 encrypted, 2 encrypting, 1 not encrypted and 2 unknown; without the unknown category, those two would have been silently counted as compliant.

Synthetic case. Synthetic.

Errors and limits

Treating unknown as compliant hides problems.

Exercises

Synthesis

Handle missing data explicitly.

Continue studying — chapter 08 of 40

← Chapter 07 (draft) · Course chapter index · Chapter 09 (draft) →

Worked answers

EX022. The csv and json modules.

EX023. As unknown, reported separately.

EX024. 40 encrypted, 2 encrypting, 1 not encrypted, 2 unknown.