◇ OpenCyber Academy
Technologist · Chapter 3 · English

Chapter 3 — Programming foundations

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

Chapter 3 — Programming foundations

Lesson map

Lesson 03 published. Map, example, EX007.

flowchart TD
  N0["Learning outcomes"]
  N1["3.1 Why scripting helps"]
  N2["3.2 Values, lists and dictionaries"]
  N3["3.3 Loops and conditions"]
  N4["3.4 Clear output"]
  N5["3.5 Standard library only"]
  N6["3.6 Reading before running"]
  N7["Comments and names"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7
  N7 --> EX["Worked example"]
  EX --> Q["Exercise EX007"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["3.1 Why scripting helps"]
  N2["3.2 Values, lists and dictionaries"]
  N3["3.3 Loops and conditions"]
  N4["3.4 Clear output"]
  N5["3.5 Standard library only"]
  N6["3.6 Reading before running"]
  N7["Comments and names"]
  N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> N6 --> N7 --> Q["EX007"]
[Learning outcomes]
    |
    v
[3.1 Why scripting helps]
    |
    v
[3.2 Values, lists and dictionaries]
    |
    v
[3.3 Loops and conditions]
    |
    v
[3.4 Clear output]
    |
    v
[3.5 Standard library only]
    |
    v
[3.6 Reading before running]
    |
    v
[Comments and names]
    |
    v
[Worked example] --> [Exercise EX007]

Learning outcomes

You will (1) read short Python scripts; (2) use lists and dictionaries to hold records; (3) write small checks that print clear results.

3.1 Why scripting helps

The six-hour search happened because staff checked each client by hand. A short script that reads a list of workloads and prints which images they use could answer the same question in seconds. During the advisory, staff listed each client's workloads and read the image names one by one, comparing them by eye with the advisory text. The work was slow and error-prone, because a tired person can miss one line among dozens. A script performs the same comparison identically every time, and its output can be saved as evidence of what was checked and when. Priya's goal is not to replace the team's judgement but to remove the repetitive search that consumed most of those six hours.

3.2 Values, lists and dictionaries

A list holds items in order. A dictionary maps names to values, such as workload name to image. Most of the labs in this course use these two structures with synthetic data. A list suits ordered data, such as the sequence of hops a request takes or a series of audit events. A dictionary suits lookups, such as asking which image the workload called booking uses. Priya also learns sets, which hold unique items and answer membership questions quickly; the approved image list in her inventory lab is a set because the only question asked of it is whether an image belongs. Choosing the structure that matches the question makes a script shorter and easier to check.

3.3 Loops and conditions

A loop goes through each item; a condition decides what to do with it. Together they let a script find every workload whose image is not on an approved list. Priya writes the logic in words before writing code: for each workload, look up its image; if that image is not on the approved list, record the workload. The same pattern appears in later labs, such as checking each cluster setting against the baseline or each proposed change against pipeline rules. Conditions must be precise, because a loose test such as "name contains node" could match an unrelated image and produce a false finding. Testing the loop on a few known records first shows whether the condition behaves as intended.

3.4 Clear output

A good check prints counts first and then the details, so a reader sees the size of the problem before the list. Priya adopts this habit. Her first inventory output was a long list of workloads with no total, and Kwame had to count lines to judge the scale. The revised version prints one summary line, stating how many workloads run approved images and how many do not, followed by one line per finding. Every lab in the course follows this layout, and it also suits handover: a colleague reading results at the start of a shift can decide in seconds whether anything needs attention that day.

3.5 Standard library only

The course labs use only Python's standard library, so they run anywhere without installing packages. This also avoids bringing in code that Cobalt has not reviewed. Installing packages from a public index means trusting code written by others, along with every package it depends on, and Cobalt has not reviewed that code. The standard library already provides what the labs need: dictionaries, sets, string handling and counting with the collections module. When Cobalt later chooses real scanners and checkers, it does so deliberately, against written criteria. Keeping the labs free of dependencies also means a student can run them on a polytechnic computer without administrator rights.

3.6 Reading before running

Priya reads every script before running it, even one written by a colleague. Kwame says this is good practice for any code that touches client systems. Reading first means checking what a script reads, what it writes and whether it contacts any system. A script that only prints results from a synthetic list is low risk; one that changes cluster settings or deletes resources needs review and a test environment first. Priya writes a one-line note at the top of each of her scripts stating whether it reads or changes anything, so colleagues can make the same judgement quickly. All of the course labs only read data and print results.

Comments and names

Clear variable names such as approved_images and short comments make a script readable months later by someone else.

Worked example

Priya writes a few lines that loop over a dictionary of workloads and print those whose image name starts with docker.io. It finds two workloads on its first run. She adds a count at the top of the output and a comment explaining what the script does.

Synthetic case. Scripts and data are synthetic.

Errors and limits

Scripts reflect the data they are given; if the list of workloads is incomplete, the answer is incomplete.

Exercises

Synthesis

Small, readable scripts turn slow manual searches into quick, repeatable checks.

Continue studying — chapter 03 of 30

← Chapter 02 (draft) · Course chapter index · Chapter 04 (draft) →