Aller au contenu principal

borrowing

In progress

The service is being finished in services/borrowing (contract: docs/dev/phase2-contracts.md, Borrowing service; specs: features.md "Borrowing sheet", security.md).

  • Implemented in code: cmd/borrowing/main.go (HTTP server, outbox relay, member erasure consumer, leased jobs), sheet lifecycle, external persons with encrypted PII and sealed ID photos, identity extraction and QR detection through vision, reminders, retention (archive then crypto-shred), CSV export, MongoDB indexes.
  • Not wired yet: no docker-compose.yml entry, no /v1/borrowing route in GATEWAY_ROUTES, no web screen (/app/borrowing shows a "coming soon" placeholder). The openapi.yaml exists (API reference); tests cover the API, sheets, persons, store, jobs and erasure.

Role: replaces the paper borrowing sheets. It manages borrowers (members or external persons), the sheet lifecycle (items, checkout photos, on-screen signature, approval, partial returns), reminders and the identity data of external borrowers, encrypted and isolated from the rest of the system.

Limits: does not own item status: it publishes checkout/return events that inventory applies. Sheet photos and signatures go through the files service; identity document photos never do (they stay in this service, sealed, in a dedicated bucket). No deposit. Identity extraction and QR detection are delegated to vision, whose results are always editable suggestions.

Model​

  • Borrower: a member (kind = member, Keycloak ID, display name given by the client, no identity document) or an external person (kind = external, ExternalPerson aggregate with its own ULID, optional member_id link). Accepted document types: fr_id_card, epita_card, other.
  • Sheet statuses: draft → pending_approval → checked_out → partially_returned → returned, plus cancelled (only from draft or pending_approval).
  • Person statuses: active → archived (photo and contact deleted) → erased (key shredded, stub kept).
  • Limits in code: 100 items per sheet, 20 photos per list, due date at most 366 days ahead, 10 photos per QR detection.
StepBehaviour in code
CreatePOST /sheets: borrower, site_id, item_ids (may be empty: photos are uploaded and detect-items run on the draft), due_at, purpose, optional project_id. Each item is read from inventory with the forwarded principal; it must be on the sheet site, available and not held by another active sheet. Status draft.
EditPATCH /sheets/:id (drafts only): items, checkout_photo_ids, due date, purpose. Photos must be files owned by atl:borrowing:<site>:sheet/<id> (checked with the files service).
DetectPOST /sheets/:id/detect-items with file_ids: downloads the photos from files, sends them to vision /v1/detect/qr, resolves short codes with inventory /v1/inventory/resolve/:code, returns codes, suggestions and unresolved codes. Allowed on drafts (checkout) and active sheets (return).
SubmitPOST /sheets/:id/submit with signature_file_id and signer_name: at least one item (422 item_ids: required), signature file must belong to the sheet; an external borrower must be active with an ID photo. Status pending_approval.
ApprovePOST /sheets/:id/approve: requires a human user holding borrowing.sheet.approve, or the member borrower themself. Items are checked again, status checked_out, events approved + checked_out.
ReturnPOST /sheets/:id/returns with item_ids (open items only), photo_ids, condition_note: partial returns allowed (partially_returned); all back → returned + returned.
CancelPOST /sheets/:id/cancel before checkout → cancelled.

Borrower and signer names stored on a sheet are sealed with the borrower subject key (member ID or person ID), so erasing a member or shredding a person turns them into a pseudonym (Personne externe #xxxxxx for persons).

Authorization​

ARNs: atl:borrowing:<site>:sheet/<id> (parents: site, optional project) and atl:borrowing:*:person/<id> (parents: the person's sites). Catalogue permissions: borrowing.sheet.read/create/update/delete, borrowing.sheet.approve, borrowing.sheet.read_all (elevated), borrowing.identity.read (sensitive), borrowing.person.manage.

  • The member borrower and the creator of a sheet always pass sheet checks (read, update, cancel, return).
  • Reads fall back to read_all and approve (approvers); lists use the Lookup scope of read (plus read_all in admin mode); ?mine=true lists the caller's own sheets.
  • Identity data and ID photo: borrowing.identity.read and a passkey step-up younger than 15 minutes (step_up_required otherwise). Every access is audited; responses are Cache-Control: no-store.
  • borrowing.sheet.delete is declared in the catalogue but no route uses it.
  • Sheet files (files delegate): the files service delegates denied checks on atl:borrowing:<site>:sheet/<id> to POST /internal/v1/attachments/authorize ({owner, parents, action}, caller X-Atl-Principal required; 204 allowed, 403 denied, 404 unknown owner). Read: same rule as GET /sheets/:id. Write: the member borrower or the creator while the sheet is draft, pending_approval (checkout photos, signature), checked_out or partially_returned (return photos); otherwise borrowing.sheet.update. /internal/... routes are not routed by the gateway.

HTTP API​

All routes are under /v1/borrowing and require an authenticated principal (API reference). Lists are cursor paginated (limit, cursor).

MethodPathPermissionNotes
POST/identity/extractperson.managemultipart image + kind (auto, fr_id_card, epita_card); forwards to vision, stores nothing, audited
GET/personsperson.manage (Lookup)filters q (blind index), status, site
POST/personsperson.manageJSON or multipart with optional photo
GET / PATCH/persons/:idperson.manageview without identity data
PUT/persons/:id/photoperson.managereplaces the ID photo, previous one deleted
GET/persons/:id/identityidentity.read + step-updecrypted names, birth date, contact
GET/persons/:id/photoidentity.read + step-updecrypted photo streamed (never a presigned URL)
GET/sheetssheet.read (Lookup)filters status, site, project_id, borrower_kind, member_id, person_id, item_id, q, overdue, mine
POST/sheetssheet.createperson.manage on the person for an external borrower
GET/sheets/export.csvsheet.read_allsame filters, max 50 000 rows, audited
GET / PATCH/sheets/:idsheet.read / sheet.update
POST/sheets/:id/submit, /cancel, /returns, /detect-itemssheet.update
POST/sheets/:id/approvesheet.approveor self-approval by the member borrower
POST/internal/v1/attachments/authorizesee "Sheet files" aboveinternal (files delegate), not under /v1/borrowing

Photos (identity and detection) are limited to BORROWING_MAX_PHOTO_BYTES and must be JPEG, PNG, WebP or HEIC.

Dependencies​

  • Services: authz (Check / Lookup), inventory (item lookup and QR resolution, forwarded principal), files (sheet photos and signatures: metadata and download, forwarded principal), vision (identity extraction, QR detection, service principal signed with PRINCIPAL_SIGNING_KEY; disabled outside production when the key is unset).
  • Infrastructure: MongoDB, Kafka, S3 bucket atelier-identity, KEK source (env or Barbican).

Kafka​

Events are written with the state through the event store and the outbox, then published by the outbox relay; the topic is derived from the event type (borrowing.sheet.* → borrowing.sheet.v1).

DirectionTopicEvents
outborrowing.sheet.v1created, updated, submitted, approved, checked_out (sheet_id, site_id, item_ids), items_returned (sheet_id, item_ids), returned, cancelled
outborrowing.person.v1created, updated (PII sealed in the payload), photo_replaced, reactivated, archived, shredded
outborrowing.reminder.v1due_soon, overdue (identifiers, channels, due date, open items; no names)
outaudit.records.v1every write, extraction, identity read and export
inmembers.member.v1members.member.erased (consumer group borrowing-erasure)

Inventory consumes checked_out and items_returned from borrowing.sheet.v1 (see flows). Reminders target in_app for members and the stored contact channels for external persons; no consumer exists yet (notifications service planned).

Data​

MongoDB database borrowing:

CollectionContent
sheetscurrent sheet state (names sealed, search_terms blind index)
personsexternal persons: PII fields sealed, photo object key, retention dates
member_erasuresmember erasures deferred while the member still holds items
events, outboxevent log and outbox (shared libraries)
data_keysper-subject data keys wrapped by the KEK (cryptox.Keyring)
locksjob leases and outbox relay lock

Encryption (see encryption): envelope encryption, one data key per subject (person ID, or member ID for member names), wrapped by the KEK. The ID photo is sealed with the person key and stored as opaque bytes at persons/<person-id>/<ulid> in the atelier-identity bucket; it is decrypted only to stream it back. Crypto-shredding: destroying a subject key makes its names, birth date and photo unreadable.

Search: borrower names are encrypted, so q uses a blind index: folded tokens and prefixes (3+ characters) hashed with HMAC-SHA256 keyed by BORROWING_SEARCH_KEY. Tokens are removed on shredding and member erasure.

Jobs​

Run every BORROWING_JOB_INTERVAL under a MongoDB lease (one replica at a time), with the service principal.

JobBehaviour
borrowing-remindersdue_soon once per sheet within BORROWING_REMINDER_DUE_SOON of the due date; overdue once BORROWING_REMINDER_OVERDUE_AFTER after it. Sheets of borrowers without a usable contact are marked as handled without event.
borrowing-erasuresretries deferred member erasures
borrowing-settlestarts retention of persons with no unresolved sheet left (recovers a failed settle)
borrowing-archivewhen archive_at is reached: deletes the ID photo, email and phone; status archived
borrowing-shredwhen shred_at is reached: shreds the person key, deletes any photo, clears the name index, keeps a stub (erased)

Retention: when a person has no unresolved sheet left (after the last return or cancellation), archive_at = now + BORROWING_ID_PHOTO_RETENTION and shred_at = now + BORROWING_ARCHIVE_RETENTION (both from that moment). A new sheet for the person clears both dates (legitimate interest) and reactivates an archived person.

Member erasure: on members.member.erased, drafts and pending sheets are cancelled, the member key held by borrowing is shredded and the sheets are flagged erased; deferred while the member still holds items.

Configuration​

VariableDefaultDescription
HTTP_ADDR:8087Listen address
METRICS_ADDR:9187Prometheus listener
PRINCIPAL_PUBLIC_KEYrequiredVerifies principal tokens
PRINCIPAL_SIGNING_KEYemptyService principal for vision; required in production, vision features disabled otherwise
AUTHZ_URLhttp://localhost:8081authz
INVENTORY_URLhttp://localhost:8085inventory
FILES_URLhttp://localhost:8084files
VISION_URLhttp://localhost:8090vision
MONGO_URImongodb://localhost:27017/?directConnection=trueMongoDB
MONGO_DATABASEborrowingDatabase
KAFKA_BROKERSlocalhost:19092Brokers
BORROWING_IDENTITY_BUCKETatelier-identityBucket of sealed ID photos
BORROWING_SEARCH_KEYderived from CRYPTO_KEK32 base64 bytes; required with a Barbican KEK
BORROWING_MAX_PHOTO_BYTES10485760 (10 MiB)Max photo size
BORROWING_ID_PHOTO_RETENTION24hDelay before photo and contact deletion
BORROWING_ARCHIVE_RETENTION43800h (5 years)Delay before crypto-shredding; must be ≥ photo retention
BORROWING_REMINDER_DUE_SOON24hDue soon reminder offset
BORROWING_REMINDER_OVERDUE_AFTER1hOverdue reminder offset
BORROWING_JOB_INTERVAL15mJob period

Plus the common variables (APP_ENV, LOG_LEVEL, OTEL_EXPORTER_OTLP_ENDPOINT), the S3 variables (S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY, S3_REGION) and the KEK variables (CRYPTO_KEK_SOURCE, CRYPTO_KEK or the Barbican ones), see environment variables.

Differences from the contract​

  • Extra events borrowing.sheet.updated and borrowing.sheet.submitted, and an explicit submit step (signature) before approval.
  • Retention starts after the last return or cancellation; archiving also deletes the contact; the archive delay is counted from the settle time, not from archiving.
  • Not in the contract: blind index search, CSV export, member erasure consumer, person photo replacement.