gitoriaLog in with ident

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8cda54128cda5412notes mission 002 (1/4): code order — files moved: lib/notes.hl, lib/users.hl, lib/jsoncheck.hl, components/styles.hl (imports only); gates 50/0 + 18/0, live-data run identicalmre8cda5412/tools/migrate-short-ids.hl

6.2 KB

  1. // tools/migrate-short-ids.hl — ONE-OFF MIGRATION (mission 039, ident#23): ident answers every identity's public
  2. // 5-character SHORT ID (e.g. `a68sz`) instead of the old per-app id (32 hex). notes.worldapi.org keeps ident ids in ONE place:
  3. // users.db `identity` (users.hl usersTable, unique index). notes.db `owner` is the users @id.
  4. // Everything else points at the users @id, which does not change — sessions (`user = { id = <users @id> }`) stay signed in.
  5. //
  6. // What it does: POST <ident>/api/migrate-ids {key, secret} (NEVER `finish` — that is a separate, later step, runbook
  7. // antcolony-docs/docs/short-id-switch.md) → { ids: { <old id>: <short id> } }, then for every user:
  8. // * old id in the map → `identity` rewritten to the short id (MAPPED)
  9. // * already a short id → left alone (ALREADY) — so a second run changes nothing (idempotent)
  10. // * old id NOT in the map → left alone (UNMAPPED): an identity deleted in ident since; nobody can log in as it
  11. // * the short id is already another user's `identity` → left alone (CONFLICT; cannot happen while the app was
  12. // stopped between the ident switch and this run — needs a human, runbook "Conflicts"; the tool then exits non-zero)
  13. // Prints counts; the key/secret are read from the environment and never printed. Exit is non-zero on any refusal.
  14. //
  15. // RUN IT WITH THE APP STOPPED (the server holds the tables), after ident#23 is live, storage backed up:
  16. // NOTES_STORAGE=$PWD/storage/mpackdb IDENT_API_KEY=… IDENT_API_SECRET=… IDENT_EXCHANGE_URL=<ident> bin/hybriel tools/migrate-short-ids.hl
  17. // On Byrodin (the key/secret come from the app's .env through docker's --env-file; hybriel itself only loads a .env
  18. // beside the ENTRY script, i.e. tools/.env, which does not exist):
  19. // docker run --rm --network host --env-file .env -e NOTES_STORAGE=/home/notes/storage/mpackdb \
  20. // -e IDENT_EXCHANGE_URL=http://127.0.0.1:45002 -v $PWD:/home/notes -w /home/notes \
  21. // worldapi-hybriel-runtime:debian12 ./bin/hybriel tools/migrate-short-ids.hl
  22. // NOTES_STORAGE must be ABSOLUTE: Hybriel resolves a relative path against the entry script's folder (tools/).
  23. // Test: tests/short-id-switch.mjs (old ident → data → new ident → this tool twice → the same user logs in again).
  24. import { env } from 'hl:proc'
  25. import { fetch } from 'hl:fetch'
  26. import { usersTable, identKey, identSecret, identExchangeUrl, countOfList, firstOf } from '../lib/users.hl'
  27. storage = env('NOTES_STORAGE')
  28. if (storage == null || !storage.startsWith('/')) {
  29. hlError('set NOTES_STORAGE to the ABSOLUTE table directory, e.g. NOTES_STORAGE=$PWD/storage/mpackdb')
  30. }
  31. if (identKey == '' || identSecret == '') {
  32. hlError('IDENT_API_KEY / IDENT_API_SECRET are not set (on Byrodin: docker run --env-file .env …)')
  33. }
  34. // the characters of an id, as ident#23 makes them: 5+ of 2-9 and a-z without i, l, o (lower case)
  35. static isShortId = (s) => {
  36. if (s == null || hlTypeName(s) != 'String' || s.length < 5 || s.length > 16) { return false }
  37. let i = 0
  38. while (i < s.length) {
  39. let c = s.charCodeAt(i)
  40. let digit = c >= 50 && c <= 57
  41. let letter = c >= 97 && c <= 122 && c != 105 && c != 108 && c != 111
  42. if (!digit && !letter) { return false }
  43. i = i + 1
  44. }
  45. return true
  46. }
  47. // the old per-app id: exactly 32 lowercase hex
  48. static isOldId = (s) => {
  49. if (s == null || hlTypeName(s) != 'String' || s.length != 32) { return false }
  50. let i = 0
  51. while (i < s.length) {
  52. let c = s.charCodeAt(i)
  53. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }
  54. i = i + 1
  55. }
  56. return true
  57. }
  58. // every field of the record, `identity` replaced (hl:mpackdb wants the whole @id record, its `id` included)
  59. static withIdentity = (rec, identity) => {
  60. let out = {}
  61. for (k of rec.keys()) { out[k] = rec[k] }
  62. out.identity = identity
  63. return out
  64. }
  65. console.log('migrate-short-ids: asking ' + identExchangeUrl + '/api/migrate-ids (storage ' + storage + ')')
  66. r = fetch(identExchangeUrl + '/api/migrate-ids', { method = 'POST' json = { key = identKey secret = identSecret } headers = { 'user-agent' = 'notes.worldapi.org (migrate-short-ids)' } timeoutMs = 30000 })
  67. if (r == null || r.status == null || r.status == 0) { hlError('ident did not answer at ' + identExchangeUrl) }
  68. if (r.status != 200) {
  69. let e = r.json()
  70. hlError('ident refused migrate-ids (' + r.status + (e != null && e.error != null ? ': ' + e.error : '') + ') — is ident#23 live? right key/secret?')
  71. }
  72. j = r.json()
  73. if (j == null || j.ids == null || hlTypeName(j.ids) != 'Hybrid') { hlError('ident answered no id map: ' + JSON.stringify(j)) }
  74. ids = j.ids
  75. console.log('ident: ' + j.count + ' old ids in the map, finished = ' + j.finished)
  76. seen = 0
  77. mapped = 0
  78. already = 0
  79. unmapped = 0
  80. conflicts = 0
  81. failed = 0
  82. all = usersTable.find(null, null)
  83. if (countOfList(all) > 0) {
  84. for (u of all) {
  85. seen = seen + 1
  86. let old = u.identity
  87. let sid = isOldId(old) ? ids[old] : null
  88. if (sid != null && isShortId(sid)) {
  89. let other = firstOf(usersTable.find('identity', sid))
  90. if (other != null && other.id != u.id) {
  91. conflicts = conflicts + 1
  92. console.log('CONFLICT user ' + u.id + ': its short id ' + sid + ' is already user ' + other.id + ' — left alone')
  93. } else {
  94. usersTable.update(u.id, withIdentity(u, sid))
  95. let after = usersTable.fetch(u.id)
  96. if (after != null && after.identity == sid && firstOf(usersTable.find('identity', sid)) != null && countOfList(usersTable.find('identity', old)) == 0) {
  97. mapped = mapped + 1
  98. console.log('MAPPED user ' + u.id + ' → ' + sid)
  99. } else {
  100. failed = failed + 1
  101. console.log('FAILED user ' + u.id + ': ' + usersTable.lastError())
  102. }
  103. }
  104. } else if (isShortId(old) && !isOldId(old)) {
  105. already = already + 1
  106. } else {
  107. unmapped = unmapped + 1
  108. console.log('UNMAPPED user ' + u.id + ' (ident does not know its old id any more — deleted identity?)')
  109. }
  110. }
  111. }
  112. console.log('migrate-short-ids: users seen ' + seen + ', mapped ' + mapped + ', already short ' + already + ', unmapped ' + unmapped + ', conflicts ' + conflicts + ', failed ' + failed)
  113. if (failed > 0) { hlError('some users could not be written — restore the backup and look') }
  114. if (conflicts > 0) { hlError('CONFLICTS: the app made a new user for a short id before this ran (it was not stopped?) — the old user keeps its old id; see the runbook "Conflicts"') }

Branches

Latest commits

  • 8cda5412notes mission 002 (1/4): code order — files moved: lib/notes.hl, lib/users.hl, lib/jsoncheck.hl, components/styles.hl (imports only); gates 50/0 + 18/0, live-data run identicalmre
  • 47dad68bnotes: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 50/0 + 18/0mre
  • a4a2b2aeantcolony#40: tracker missions moved too — references to them in missions/reports/LOG.md updatedmre
  • 3dea0ef2notes: Hybriel master 190aa11d (fc838894 GC correctness, #126 closure scopes, #127); gate 50/0mre
  • e27c7d71notes: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy; audit: nothing to fix; gate 50/0)mre
  • 124613b6antcolony#40: mission references point to the moved missionsmre
  • 1ca2f34dantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 2bebebdanotes: Hybriel master ff51cf46 (re-vendor round)mre
  • 3eff126dnotes#3: installable app (manifest + own icon/favicon; notes' own sw.js kept)mre
  • 9883c540deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • eee693b8deploy.sh: never send .git or .gitignore to Byrodinmre
  • c8904061State of 2026-09-27, before the move to gitoriamre