# Anonymised copies of a database
A developer's machine works on a **pseudonymised copy** of a client's database, not on the client's data
(ISO/IEC 27001 A.8.11, A.8.33). `MMO\mongodb\Anonymizer` makes one, driven by the classification of the entities
(mmo.core's `#[Classification]`, `Schema::classificationOf()`): only what is `personnel` or `sante` changes.
```bash
php admin.php anonymize --dry-run # what would be replaced, collection by collection; writes nothing
php admin.php anonymize # the copy, into the NEW database
php admin.php anonymize --replace # make it again: must be an anonymised copy (its marker)
php admin.php anonymize-status # the databases of the server, each anonymised or not (read only)
```
The commands are `MongoShellController`'s: list it in the routes of the application's command line
(`'=routes' => [..., \MMO\mongodb\MongoShellController::class, ...]`). Its `repository` is the application's
`repository` unless configured. From PHP: `$repository->anonymize($from, $to)` (a `MongoRepository`), or
`Anonymizer::Shared()->anonymize($repository, $from, $to, replace: false, dryRun: false)`.
mmo.admin's `backup --restore-locally` uses it: the dump is restored into a scratch database, copied anonymised into
the local database, then the scratch database is dropped (mmo.admin's `docs/backups.md`).
## What is done to each value
Each value is read with the level of its path (an embedded entity has its own: an accident's place, `Adresse`, is
`interne`; an employee's addresses are `personnel`).
| value | `personnel` | `sante` |
|---|---|---|
| a string | a pseudonym **word by word**: the same word, the same pseudonym, in every document of every collection (what joins on a name, a registration number or an e-mail address still joins), its case kept | a pseudonym of the **whole value**: no word of it can be recognised in another |
| its shape | a letter for a letter, a digit for a digit, the rest as it was: a NIR stays 15 digits, an e-mail address stays one (of no real domain: no mail can reach a real person) | the same |
| the `pattern` of its schema | a value that matched it still does (another deterministic try until one does: a NIR's first digit) | the same |
| a date (BSON, or `YYYY-MM-DD...`) | moved back by the same number of whole weeks everywhere in the copy: the intervals and the days of the week are kept | the same |
| an enum, a `time`, a `status` | kept | kept |
| a number, a boolean, an id, a reference (`_id`, `{$ref, $id}`, `mongo://...`, 24 hex digits) | kept | kept |
What is `interne` or `public` is copied as it is, and so are `_id`, `status`, `ctime`, `mtime`, `eid` at any level.
A collection of no entity the repository knows is read as `unclassified` (`personnel` by default: every string).
## Configuration
```json
"MMO\\mongodb\\Anonymizer": {
"keyFile": "{configs}/keys/anonymize.key",
"empty": ["mailLogs", "accessTokens"],
"keep": {"utilisateurs": {"username": "god"}},
"unclassified": "personnel",
"batch": 500
}
```
- `keyFile`: the key the pseudonyms follow from, 32 random bytes written on first use (mode 600). It stays on the
machine that anonymises -- outside the database, outside git -- and never goes into a copy. With the same key the
same names give the same pseudonyms from one copy to the next; without it a pseudonym cannot be traced back. A new
key gives new pseudonyms (and a new shift of the dates).
- `empty`: the collections copied without their documents (their indexes only). A journal (`LogQuery`, which holds
the values written in clear) is always copied empty.
- `keep`: per collection, a filter of the documents copied as they are -- a developer's own account, to log into the
copy.
- `unclassified`: the level of a collection of no known entity.
## What it refuses
- to write over its source, or into a database that exists and is no anonymised copy, `--replace` or not: no
existing database is ever written. An anonymised copy holds a marker, the collection `_anonymized` (from, date,
the key's fingerprint, the counts), which `--replace` and `anonymize-status` read.
- `overwrite` (PHP only, mmo.admin's restore here) drops the target whatever it holds, as a restore always did.
## Limits
- It is a **pseudonymisation**: with the key, and the source, a pseudonym can be matched. Keep the key on the machine.
- Free texts classified `interne` are copied in clear: the classification decides (a description that names a person
must be classified `personnel`).
- What is not in the database (the files of mmo.edms) is not copied.
- 487,000 documents take about 100 s (24,000 in 4 s for my.oviepro's demo).