# The journal: logs of writes and reads, chained by hash
A repository with a collection of `MMO\core\entities\LogQuery` (`"logQuerys": "MMO\\core\\entities\\LogQuery"`, or a
subclass of the application's) keeps a journal: what was written to it, and -- for classified data -- what was read.
ISO/IEC 27001 A.8.15, A.5.28, A.5.33 (my.oviepro's board iso-27001, #24 and #25).
## What a line holds
A `LogQuery` is an event before it is a line: `aRepository::logInsert()`, `logUpdate()`, `logDelete()`, `logStatus()`
and `logRead()` build it (`createLog()`), dispatch it -- a listener completes it (the user) or ignores it --, and the
repository writes it. `type` is the operation (`cLogQuery`: `INSERT`, `UPDATE`, `DELETE`, `STATUS`, `READ`), `origin`
who did it, `collectionName`, `filters` (`{id}` for one entity: `getEntity()` finds it), `data` what was written, a
hidden property's value as `[hidden]`.
mmo.mongodb's repository writes its logs to its LogQuery collection, those of a commit before its entities. mmo.pdo's
writes none: its logs are events only.
## Reads of classified data
`RepositoryController::$reads` says which reads of the data api are journaled, by the classification of the collection
(`#[Classification]`, [Schema.md](Schema.md)):
```json
{"MMO\\core\\controllers\\RepositoryController": {"reads": {"levels": ["sante"], "unpaginated": ["personnel", "sante"]}}}
```
- `levels` (`sante` by default): `GET data.json?uid=` (`how: entity`), `GET data/{repository}/{collection}.json`
(`list`), `.csv` and `.xlsx` (`csv`, `xlsx`);
- `unpaginated` (`personnel` and `sante` by default): a json listing without `page[limit]` (`all`), a whole collection
read, which no export journal counts.
One `READ` line per request, whatever its number of entities: `data` holds `how`, `count`, `ids`, `classification`,
`user`, `ip` (`REMOTE_ADDR`: a header could say any other), `forwardedFor`, `url` (without its query), `filters` (as
json) -- never a value of the entities. `RepositoryController::logRead()` is what a route of its own calls: mmo.edms's
downloads of a file attached to a classified record (`how: download`, `file`). When the repository keeps no log, the
read is a `READ` notice of the application's log instead, which only lasts if the server writes notices
(`MMO_DEBUG_STDERR` to `notice+`, or a stream).
`RepositoryController::getReads($entity)` and `GET data/reads.json?uid=` answer who was shown an entity, the most recent
first. Their query on the journal is dispatched as a listing's: whoever may not list the journal gets nothing.
## No route writes the journal
The data api refuses (403) any `POST`, `PUT`, `PATCH` or `DELETE` on a collection of `LogQuery`, whatever the
client's rights (`RepositoryController::isReadonly()`).
## An entity removed
A line refers to whoever and whatever it names (a user property of the project's own `LogQuery`): those references never
block a removal and are never rewritten (`Entity::loopReferences()` skips the `LogQuery` classes), so a user named by the
journal can be deleted and its lines stay as they were chained. What it was is the project's to keep: a listener of
`LogQuery` can give the DELETE line its identity (my.oviepro: `data` = id, username, nom, prenom), the last line naming it,
so the purge (oldest lines first) removes it after the others.
## The chain
mmo.mongodb's `MongoRepository::$logChain` (`false` by default: the logs are written as before) chains the lines
(`MMO\core\helpers\LogChain`): each one stores in `chain` its number `seq`, the hash of the line before `previous` and
its own `hash`, the SHA-256 of `seq`, `previous` and its content (`LogChain::Canonical()`: json, the keys of each map
sorted, a BSON type as its extended json).
```json
{"repository": {"$": "MMO\\mongodb\\MongoRepository", "uri": "mongodb://127.0.0.1/", "database": "app", "logChain": true}}
```
- **Concurrency**: a unique index on `chain.seq` (made by the first chained write of a process) refuses a second line
with the same number. Two processes that read the same last line: the second one's insert fails, it reads the new
last line and chains again -- ten tries, then the commit fails.
- **Verification**: `php admin.php log-verify` (`MongoRepository::verifyLogChain()`, `LogChain::Verify()`) reads the
chain by number and lists its breaks: `modified` (a line changed), `missing` (lines removed), `unlinked` (a line
replaced, or the chain rewritten from it); exit code 1. `-m` answers json. The lines written before the chain was on
are counted apart (`unchained`).
- **Purge**: `MongoRepository::purgeLogs($before, $dryRun)` removes the oldest lines only, up to the first one written
at `$before` or later, and keeps the last one it removes (`seq`, `hash`) in the collection `logSeals`: the first line
left chains to it.
- **What it does not prevent**: whoever can write to the database can compute the hashes too and rewrite the chain from
a line to the last, or remove the last lines. `log-verify` prints the head, `:`: keep it out of the
database (a backup, a ticket, the monitoring) and check it later, `log-verify --head=:` (`head`: the line
has another hash, `truncated`: it is gone).