Onboarding a legacy database

This page is for a team with a running database that was built without Custodian. It covers the first part of onboarding. You check which clients use the database, scan it into classification files, review those files and render them as a report. The commands are part of the command-line tool.

What is built and what is not

Onboarding has three parts. Only the first is built.

  1. Scan and classify. The readiness check, the scan, sampling, the classification files and the review report. This page describes it.
  2. Runtime adoption. Loading the classification files into your application, tracking each table's state and refusing unclassified tables at startup. Not built. Your application cannot load these files yet.
  3. Takeover. Linking existing rows to subjects, encrypting them in place and switching enforcement on. Not built.

Part 1 changes nothing in the database. It reads, and it writes files in your repository. Its result is useful as a readiness report, and it is the input that parts 2 and 3 will use.

Custodian takes over only a database that one .NET application owns. SQL Server 2022 and PostgreSQL 16 or later are supported.

The connection string

The three commands read the connection string of the database from the environment variable CUSTODIAN_ONBOARD_CONNECTION. They never read it from the command line and never print it.

export CUSTODIAN_ONBOARD_CONNECTION="..."

The commands take no --assembly and do not load your application.

1. Check who else uses the database

custodian onboard clients --provider sqlserver --watch 60 --output clients.md

The readiness check lists the clients connected to the database, as login, application name and host. With --watch <minutes> it polls every 30 seconds for that long and counts how often it saw each client. Without it, it polls once. The report is Markdown, written to standard output or to --output.

The report is evidence, not proof. A client that did not connect while the tool watched is not listed, and the report says so (The_report_states_that_it_is_evidence_and_not_proof). Its purpose is to make the single-owner precondition checkable.

The login must be able to see other sessions. That is the permission to view server state on SQL Server, or membership in pg_read_all_stats on PostgreSQL. When it cannot, the report says the list is incomplete and the command exits with 3. See Clients_exits_3_and_prints_the_limited_visibility_sentence_for_a_login_that_cannot.

The check does not report which tables each client has touched.

2. Scan

custodian onboard scan --provider sqlserver

The scan reads the tables, columns, types, lengths, keys and foreign keys of the schema. For each table it proposes a classification and writes it to one JSON file under custodian/classification, or the directory you pass with --directory. Views, system schemas and the schema named gdpr are not scanned (Views_system_tables_and_the_gdpr_schema_are_not_read). The scan changes nothing in the database (Reading_the_schema_changes_nothing_in_the_database).

Commit the files. They are the record of what was proposed and what was decided.

The tool decides nothing. It only ever proposes the status pending, and a proposal counts for nothing until a person accepts it. Where it cannot tell, it says so and leaves the question open. A table whose rows concern several people, such as a message between two customers, gets an unresolved subject and a reason (Messages_is_left_pending_with_an_unresolved_subject_naming_both_keys_and_the_unsupported_status_a_person_may_set). So does a table whose personal columns describe a party to the row who has no table of their own, such as a billing email on an invoice.

Running it again

Run the scan again whenever the schema changes. It merges with the files already there.

3. Sample, if you choose to

custodian onboard scan --provider sqlserver --sample --sample-rows 5000

A name such as ref1 says little about what a column holds. With --sample the scan also reads up to --sample-rows rows of each table, 5000 unless you say otherwise, and tests the text values against the formats and checksums of an email address, telephone number, IBAN, payment card number, Swedish personal number, date of birth and IP address. It turns what it sees into counts and refines the proposals from them. A reason then reads like the one in the file below, with a percentage.

Sampling reads personal data, so it runs only when you pass the flag on that run. Without it no row is read (Scan_without_sample_reads_no_rows_prints_no_warning_and_logs_no_run).

A sampled value never leaves memory. Each row is folded into counters and dropped. No value is written to a classification file, the report, a log or an error message.

Each sampling run is recorded in sampling-runs.jsonl in the classification directory, with who ran it, the tables read and the row counts. Commit it with the files. See With_the_sample_option_the_run_is_logged_with_actor_provider_tables_and_row_counts_and_no_values.

Sampling never overrides a person. A column someone has reviewed is not changed (An_accepted_column_is_not_changed_by_sampling). Free text with a few matches among prose is marked unclassified for a person to decide (Notes_with_a_few_emails_among_prose_is_unclassified_and_mixed).

The classification files

There is one file per table, named after schema and table, for example dbo.Customers.json. This test pins the format. It shows a fresh proposal for a Customers table.

Assert.Equal(
    """
    {
      "formatVersion": 1,
      "table": {
        "schema": "dbo",
        "name": "Customers"
      },
      "status": "pending",
      "pending": {
        "owner": null,
        "deadline": null
      },
      "notPersonal": null,
      "unsupported": null,
      "accepted": null,
      "reopened": null,
      "proposedBy": "custodian-tool:alex",
      "subject": {
        "kind": "column",
        "column": "Id",
        "path": [],
        "identifier": "Email",
        "proposal": {
          "confidence": "high",
          "reason": "table name 'Customers'; columns Email, FirstName, LastName",
          "kind": "column",
          "column": "Id",
          "path": [],
          "identifier": "Email"
        }
      },
      "tenant": {
        "kind": "unresolved",
        "column": null,
        "fixed": null
      },
      "purposes": [],
      "lawfulBasis": null,
      "retention": null,
      "columns": [
        {
          "name": "Email",
          "category": "contact",
          "handling": [
            "encrypt",
            "blind-index"
          ],
          "purposes": [],
          "proposal": {
            "confidence": "high",
            "reason": "named 'Email', nvarchar(320); 87% of 5000 sampled values match an email pattern",
            "category": "contact",
            "handling": [
              "encrypt",
              "blind-index"
            ]
          }
        }
      ]
    }

    """,
    text);
Field Meaning
status pending, protected, not-personal or unsupported-multiple-subjects. See below.
pending The owner and deadline of a decision that is deferred.
accepted, notPersonal, unsupported Signatures. Each is a name in by and a date in on.
reopened What was signed before a new column took the decision back.
proposedBy Who ran the scan, as custodian-tool:<operating-system user>.
subject How a row finds its data subject. kind is column, path (foreign-key hops to a person table), none or unresolved. identifier names the column that identifies the person to the outside.
tenant How a row finds its tenant. kind is column, fixed (one tenant id for every row) or unresolved.
purposes, lawfulBasis, retention Why you hold the table's data, on what basis and for how long. The tool cannot guess these and leaves them empty.
columns Per column a category (not-personal, unclassified or a data category), the handling (encrypt, blind-index, pseudonymise, mask-on-read) and optional purposes.
proposal On a column or the subject while it is unreviewed. It holds the tool's guess, a confidence of high, medium or low, and the reason. Never a value.

The four statuses:

Status Meaning Needs
pending Not decided yet. The only status the tool writes. An owner and a deadline once anything is accepted.
protected Personal data that Custodian is to protect. An accepted signature, a resolved subject and tenant, purposes, lawful basis and retention.
not-personal Declared to hold no personal data. A notPersonal signature by someone other than the person who ran the scan.
unsupported-multiple-subjects Each row concerns several people. Custodian has one subject per row, so the table is a known gap and is not taken over. An unsupported signature by someone other than the person who ran the scan.

A file that breaks these rules does not load, and the command exits with 1 and names the file and the problem. See A_not_personal_signature_by_the_proposer_is_invalid and An_invalid_classification_file_exits_1_with_its_path_and_the_problem.

4. Review

Review is editing the files, in a pull request. A person, normally with the DPO, does the following for each table:

A signature cannot sit next to an unreviewed proposal (An_accepted_table_with_an_unreviewed_column_proposal_is_invalid).

5. Render the report

custodian onboard report

This renders the files as one HTML page for the DPO to read, at custodian/classification-report.html unless you pass --output. The page starts with a summary and a list of everything that still needs a decision, then shows every table grouped by status. The page is generated. Change the files, never the page.

The page is self-contained. It references no external resource and contains no script (The_document_references_no_external_resource_and_contains_no_script).

Exit codes

Code Meaning
0 Done, and nothing is left for a person.
1 Bad usage, a missing connection variable, a failed connection, or a file that cannot be read or is invalid.
3 The command finished, and something is left that only a person can settle.

scan exits with 3 while any table still needs a person. That means a table nobody signed, an unreviewed proposal, an unresolved subject or tenant, an unclassified column, a pending table without owner and deadline, a reopened table, or missing purposes, lawful basis or retention. It also exits with 3 when it found new, missing or reopened tables or columns, when a sample failed, or when the login saw no table. It exits with 0 only when every table is signed and complete and nothing is new (Scan_exits_0_when_every_table_is_signed_and_complete_and_nothing_is_new). That makes it usable as a check in your build.

clients exits with 3 when the login cannot see every session. report exits with 3 when the directory holds no files (Report_on_an_empty_existing_directory_renders_the_empty_page_and_exits_3).

Next