Chapter 37: a key that cannot be printed, built

A service that calls another one holds a secret. Chapter 37's Sensitive wraps it so it cannot be printed, interpolated or serialised by accident, and only a class marked Privileged may reveal it. This lesson builds that class and a program that tries to print the key. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-sync-key.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.notesservice.synckey

  defines constant
    SYNC_KEY_NAME <- "NOTES_SYNC_KEY"

  defines class

    NoteSync with trait of Privileged
      syncKey as Sensitive?

      NoteSync()
        -> key as Sensitive
        syncKey: key

      default private NoteSync()

      authorisation()
        <- rtn as String: String()

        if syncKey?
          rtn: authorisationFor(syncKey.reveal())

      default operator ?

  defines function

    authorisationFor() as pure
      -> header as String
      <- rtn as String: `Bearer header of ${length header} characters`

  defines program

    ShowSyncKey()
      stdout <- Stdout()

      key <- EnvVars().sensitiveGet(SYNC_KEY_NAME)
      if key?
        sync <- NoteSync(key)
        stdout.println(`key: ${key}`)
        stdout.println(`as JSON: ${$$key}`)
        stdout.println(sync.authorisation())
      else
        stdout.println(`${SYNC_KEY_NAME} is not set`)

//EOF

Step 1: A constant

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

SYNC_KEY_NAME is the constant's name and "NOTES_SYNC_KEY" its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: SYNC_KEY_NAME is the constant's name and "NOTES_SYNC_KEY" its value - a literal in the Value field, from which the type is inferred

The preview shows SYNC_KEY_NAME about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows SYNC_KEY_NAME about to be written

Step 2: authorisationFor — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

authorisationFor is the function's name. Add one parameter, header of type String, set Returns to String and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: authorisationFor is the function's name

The preview shows authorisationFor about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows authorisationFor about to be written

Select the authorisationFor() as pure line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the authorisationFor() as pure line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as String? down to its //AI line, and type authorisationFor's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type authorisationFor's own return and work, as the book writes them

Step 3: NoteSync — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

NoteSync is the class's name. It is the one class that may reveal the key. Choose Preview.

The EK9 IDE just before choosing Preview: NoteSync is the class's name

The preview shows NoteSync about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows NoteSync about to be written

Right-click the NoteSync line and choose Adopt Trait…

The EK9 IDE once this is done: Right-click the NoteSync line and choose Adopt Trait…

Type Privileged as the trait - Implement method stubs is already chosen - and choose OK.

The EK9 IDE just before choosing OK: Type Privileged as the trait - Implement method stubs is already chosen - and choose OK

Adopt Trait changes the open file in the editor, not yet saved. Choose File > Save.

The EK9 IDE once this is done: Adopt Trait changes the open file in the editor, not yet saved

Select the NoteSync line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the NoteSync line and, above it, type the blank line the book opens the block with

Type the constructor that takes the Sensitive key and keeps it, and the private default that stops a key-less NoteSync. They assign a field the next step declares. Saves when typed.

The EK9 IDE once this is done: Type the constructor that takes the Sensitive key and keeps it, and the private default that stops a key-less NoteSync

Right-click the NoteSync line and choose Add Field…

The EK9 IDE once this is done: Right-click the NoteSync line and choose Add Field…

Type syncKey as Sensitive?. The constructor assigns it. Untick 'Take it in a new constructor' - the constructor is the book's own. Choose OK.

The EK9 IDE just before choosing OK: Type syncKey as Sensitive?

The preview shows syncKey added - and default operator ? with it, NoteSync's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows syncKey added - and default operator ? with it, NoteSync's first field

Right-click the NoteSync line and choose Add Method…

The EK9 IDE once this is done: Right-click the NoteSync line and choose Add Method…

authorisation is the method's name. Set Return type to String. Choose Apply.

The EK9 IDE just before choosing Apply: authorisation is the method's name

Select the stub and type it: only inside a Privileged class does reveal() compile. Saves when typed.

The EK9 IDE once this is done: Select the stub and type it

Step 4: ShowSyncKey — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

ShowSyncKey is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: ShowSyncKey is the program's name

The preview shows ShowSyncKey about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ShowSyncKey about to be written

Select the ShowSyncKey line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the ShowSyncKey line and, above it, type the blank line the book opens the block with

Select the stub, from ready <- true down to its //AI line, and type the program exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type the program exactly as the chapter shows it

Step 5: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

NOTES_SYNC_KEY is not set in this run, so the program says so; set it in a run configuration and the key still never prints. Choose Close.

The EK9 IDE just before choosing Close: NOTES_SYNC_KEY is not set in this run, so the program says so; set it in a run configuration and the key still never prints

The result

SyncKey.ek9 now reads exactly as chapter 37 of EK9 in Depth shows it. The key prints as nothing useful - stdout, string interpolation and JSON all refuse to show it - and only NoteSync, which is Privileged, can read it to build the header.

The EK9 IDE when the lesson has finished

Where this lesson comes from