misskey icon indicating copy to clipboard operation
misskey copied to clipboard

perf(federation): 連合の署名をEd25519鍵に対応

Open tamaina opened this issue 5 months ago • 15 comments

#14278 のやり直し Fix #14273 Fix of https://github.com/misskey-dev/misskey/pull/13464 Resolve https://github.com/misskey-dev/misskey/issues/11129 Fix https://github.com/misskey-dev/misskey/issues/7506

What

  • [x] ローカルユーザーのEd25519鍵を発行する
    • 持っていなかったらDeliverManager.executeの時に発行する
  • [x] AP PersonにadditionalPublicKeysを追加
    • publicKeyの配列をとる
    • Spec: https://github.com/misskey-dev/node-http-message-signatures/tree/0.0.1?tab=readme-ov-file#additionalpublickeys
  • [x] InboxProcessorService.processで複数の公開鍵に対応する
  • [x] HTTP Signatureの実装ライブラリを自作して根本的に変える: https://github.com/misskey-dev/node-http-message-signatures
    • 今までのライブラリはEd25519の検証がとても遅いため
    • Web Crypto APIベースでPromiseになった(並列度が高い状況では高速に?)
    • RSA鍵の処理でもパフォーマンスに対してポジティブな効果が得られました
  • [x] nodeinfoにHTTP Signatureの対応状況(暗号方式は?RFC 9421に対応しているか?)を掲示
    • Spec: https://github.com/misskey-dev/node-http-message-signatures/tree/0.0.1?tab=readme-ov-file#http-message-signatures-implementation-level
  • [x] nodeinfoの情報を基にサーバーごとに署名を出し分ける
  • [x] ローカルでの連合試験用にhttpリクエストの仕様変更 (fetchInstanceMetadataが作動するように、環境変数名変更)
  • [x] ジョブキューの並列度を見直し
    • deliverJobConcurrency: 16 (←128)
    • deliverJobPerSec: 1024 (←128)
    • inboxJobConcurrency: 4 (←16)
    • inboxJobPerSec: 64 (←32)

RFC 9421への対応はここでは行わない

  • 署名検証で同一名キーが見つからない場合のupdatePersonなどの最短間隔を12分から5分に短縮
  • inboxで署名検証が失格になった場合、キューを再試行しないようにしていたがするように
  • admin/show-userで公開鍵(リモートユーザー)/秘密鍵公開鍵ペア(ローカルユーザー)を確認できるように
  • updatePersonの後に発火するremoteUserUpdatedイベントにより、ApDbResolverServiceのpublicKeyキャッシュpublicKeyByUserIdCacheをリフレッシュするように(ユーザーごと)
  • deliverでinboxに投げて401が返ってきた場合、時計が狂っている場合などを考慮しリトライできるように
  • signature parseにおいてheaders.dateとサーバー時間のズレは両方向に300sを許容するように (headers.date - サーバー時間 <= 2000しか許容していなかったがこれだと問題が起きた)

Why

Deliverやsigned Getの軽量化

Additional info (optional)

CHANGELOG

  • Feat: 連合に使うHTTP SignaturesがEd25519鍵に対応するように #16250

    • Ed25519署名に対応するサーバーが増えると、deliverで要求されるサーバーリソースが削減されます
      • ジョブキューのconfig設定のデフォルト値を変更しました。
        default.ymlでジョブキューの並列度を設定している場合は、concurrencyの値を下げるとパフォーマンスが改善する可能性があります。
        • deliverJobConcurrency: 16 (←128)
        • deliverJobPerSec: 1024 (←128)
        • inboxJobConcurrency: 4 (←16)
        • inboxJobPerSec: 64 (←32)
  • [x] Includes and depends on https://github.com/misskey-dev/misskey/pull/16284

  • [ ] Includes and depends on https://github.com/misskey-dev/misskey/pull/16279

  • [ ] Includes and depends on https://github.com/misskey-dev/misskey/pull/16297

  • [ ] Includes and depends on https://github.com/misskey-dev/misskey/pull/16268

diff https://github.com/tamaina/misskey/compare/remote-suspend..re2-ed25519

Checklist

  • [x] Read the contribution guide
  • [x] Test working in a local environment
  • [ ] (If needed) Add story of storybook
  • [ ] (If needed) Update CHANGELOG.md
  • [ ] (If possible) Add tests

tamaina avatar Jul 04 '25 11:07 tamaina

Codecov Report

:x: Patch coverage is 47.72162% with 631 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 63.05%. Comparing base (e98252a) to head (f26f778). :warning: Report is 1128 commits behind head on develop.

Files with missing lines Patch % Lines
...ackend/src/core/activitypub/ApDbResolverService.ts 23.35% 128 Missing :warning:
...end/src/core/activitypub/models/ApPersonService.ts 17.50% 99 Missing :warning:
packages/backend/src/core/UserKeypairService.ts 39.59% 90 Missing :warning:
...kend/src/queue/processors/InboxProcessorService.ts 2.63% 74 Missing :warning:
...ges/backend/src/server/ActivityPubServerService.ts 20.45% 35 Missing :warning:
...s/backend/src/core/activitypub/ApRequestService.ts 62.22% 34 Missing :warning:
...nd/src/queue/processors/DeliverProcessorService.ts 0.00% 29 Missing :warning:
packages/backend/src/queue/types.ts 0.00% 21 Missing :warning:
packages/backend/src/core/AccountUpdateService.ts 66.07% 19 Missing :warning:
...s/backend/src/core/FetchInstanceMetadataService.ts 57.89% 16 Missing :warning:
... and 29 more
Additional details and impacted files
@@             Coverage Diff              @@
##           develop   #16250       +/-   ##
============================================
+ Coverage    42.56%   63.05%   +20.48%     
============================================
  Files         1685     1151      -534     
  Lines       170650   115973    -54677     
  Branches      4223     7992     +3769     
============================================
+ Hits         72643    73123      +480     
+ Misses       97546    40690    -56856     
- Partials       461     2160     +1699     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jul 04 '25 11:07 codecov[bot]

このPRによるapi.jsonの差分

差分はこちら
--- base
+++ head
@@ -16624,6 +16624,9 @@
                     "isSuspended": {
                       "type": "boolean"
                     },
+                    "isRemoteSuspended": {
+                      "type": "boolean"
+                    },
                     "isHibernated": {
                       "type": "boolean"
                     },
@@ -16675,6 +16678,67 @@
                           "roleId"
                         ]
                       }
+                    },
+                    "publicKeys": {
+                      "type": [
+                        "array",
+                        "null"
+                      ],
+                      "items": {
+                        "type": "object",
+                        "properties": {
+                          "userId": {
+                            "type": "string"
+                          },
+                          "keyId": {
+                            "type": "string"
+                          },
+                          "keyPem": {
+                            "type": "string"
+                          }
+                        },
+                        "required": [
+                          "userId",
+                          "keyId",
+                          "keyPem"
+                        ]
+                      }
+                    },
+                    "keyPairs": {
+                      "type": [
+                        "object",
+                        "null"
+                      ],
+                      "properties": {
+                        "userId": {
+                          "type": "string"
+                        },
+                        "publicKey": {
+                          "type": "string"
+                        },
+                        "privateKey": {
+                          "type": "string"
+                        },
+                        "ed25519PublicKey": {
+                          "type": [
+                            "string",
+                            "null"
+                          ]
+                        },
+                        "ed25519PrivateKey": {
+                          "type": [
+                            "string",
+                            "null"
+                          ]
+                        }
+                      },
+                      "required": [
+                        "userId",
+                        "publicKey",
+                        "privateKey",
+                        "ed25519PublicKey",
+                        "ed25519PrivateKey"
+                      ]
                     }
                   },
                   "required": [
@@ -16695,13 +16759,16 @@
                     "isModerator",
                     "isSilenced",
                     "isSuspended",
+                    "isRemoteSuspended",
                     "isHibernated",
                     "lastActiveDate",
                     "moderationNote",
                     "signins",
                     "policies",
                     "roles",
-                    "roleAssigns"
+                    "roleAssigns",
+                    "publicKeys",
+                    "keyPairs"
                   ]
                 }
               }
@@ -91774,6 +91841,9 @@
               "string",
               "null"
             ]
+          },
+          "httpMessageSignaturesImplementationLevel": {
+            "type": "string"
           }
         },
         "required": [
@@ -91801,7 +91871,8 @@
           "faviconUrl",
           "themeColor",
           "infoUpdatedAt",
-          "latestRequestReceivedAt"
+          "latestRequestReceivedAt",
+          "httpMessageSignaturesImplementationLevel"
         ]
       },
       "GalleryPost": {

Get diff files from Workflow Page

github-actions[bot] avatar Jul 04 '25 11:07 github-actions[bot]

federation test、テストでエラーが出たところでlogのどの位置が該当箇所なのかわからん

tamaina avatar Jul 06 '25 14:07 tamaina

image

なんかユーザーをresolveできてない?

tamaina avatar Jul 06 '25 15:07 tamaina

isDeletedがtrueになってた

なんで?

tamaina avatar Jul 06 '25 16:07 tamaina

  • alice/bobの作成はbeforeAllだからdescribe内でalice/bobは同一
  • suspendは外部に対してDeleteである
  • unsuspendはUndo DeleteだがそれはMisskeyは処理しない(unsuspendはリモートに伝わらない)
  • よってAlice can follow Bobはaliceをsuspendする処理が走った後そのまま実行される、aliceがisDeletedになっているのは全く理にかなっている

tamaina avatar Jul 06 '25 17:07 tamaina

ようはリモートユーザーが削除済/中なのにinboxに何かしらを送りつけてくるパターンなんだけど、受信側がそれをどう解釈するかは結構悩ましいかも

tamaina avatar Jul 06 '25 17:07 tamaina

ようわからんけどとりあえず #13256 先にやるか

tamaina avatar Jul 06 '25 17:07 tamaina

問題1: ap/showでuriでの参照がすり抜けて、createPersonでalready registeredエラーが出る

tamaina avatar Jul 24 '25 13:07 tamaina

動いたんだが

tamaina avatar Jul 24 '25 15:07 tamaina

p1.a9z.devに適用した

tamaina avatar Jul 24 '25 15:07 tamaina

https://codeberg.org/fediverse/fep/src/branch/main/fep/521a/fep-521a.md に対応すべき?(すべき)

tamaina avatar Jul 25 '25 04:07 tamaina

ASN.1←→Multibaseの変換処理が大きくなりそうなのでこのPRはこのままで

tamaina avatar Jul 25 '25 05:07 tamaina

https://github.com/misskey-dev/misskey/issues/14244

これか

tamaina avatar Jul 25 '25 05:07 tamaina

conflict resolved

tamaina avatar Aug 25 '25 11:08 tamaina