2021-08-17 13:44:30 +01:00
|
|
|
// Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package consumers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-01-05 17:44:49 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2021-08-17 13:44:30 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-02-04 13:08:13 +00:00
|
|
|
"github.com/nats-io/nats.go"
|
2021-08-17 13:44:30 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OutputCrossSigningKeyUpdateConsumer struct {
|
2022-02-04 13:08:13 +00:00
|
|
|
ctx context.Context
|
|
|
|
keyDB storage.Database
|
|
|
|
keyAPI api.KeyInternalAPI
|
|
|
|
serverName string
|
|
|
|
jetstream nats.JetStreamContext
|
|
|
|
durable string
|
|
|
|
topic string
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOutputCrossSigningKeyUpdateConsumer(
|
|
|
|
process *process.ProcessContext,
|
|
|
|
cfg *config.Dendrite,
|
2022-02-04 13:08:13 +00:00
|
|
|
js nats.JetStreamContext,
|
2021-08-17 13:44:30 +01:00
|
|
|
keyDB storage.Database,
|
|
|
|
keyAPI api.KeyInternalAPI,
|
|
|
|
) *OutputCrossSigningKeyUpdateConsumer {
|
|
|
|
// The keyserver both produces and consumes on the TopicOutputKeyChangeEvent
|
|
|
|
// topic. We will only produce events where the UserID matches our server name,
|
|
|
|
// and we will only consume events where the UserID does NOT match our server
|
|
|
|
// name (because the update came from a remote server).
|
|
|
|
s := &OutputCrossSigningKeyUpdateConsumer{
|
2022-02-04 13:08:13 +00:00
|
|
|
ctx: process.Context(),
|
|
|
|
keyDB: keyDB,
|
|
|
|
jetstream: js,
|
|
|
|
durable: cfg.Global.JetStream.Durable("KeyServerCrossSigningConsumer"),
|
|
|
|
topic: cfg.Global.JetStream.TopicFor(jetstream.OutputKeyChangeEvent),
|
|
|
|
keyAPI: keyAPI,
|
|
|
|
serverName: string(cfg.Global.ServerName),
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OutputCrossSigningKeyUpdateConsumer) Start() error {
|
2022-02-04 13:08:13 +00:00
|
|
|
return jetstream.JetStreamConsumer(
|
|
|
|
s.ctx, s.jetstream, s.topic, s.durable, s.onMessage,
|
|
|
|
nats.DeliverAll(), nats.ManualAck(),
|
|
|
|
)
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// onMessage is called in response to a message received on the
|
|
|
|
// key change events topic from the key server.
|
2022-02-04 13:08:13 +00:00
|
|
|
func (t *OutputCrossSigningKeyUpdateConsumer) onMessage(ctx context.Context, msg *nats.Msg) bool {
|
2021-08-17 13:44:30 +01:00
|
|
|
var m api.DeviceMessage
|
2022-02-04 13:08:13 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &m); err != nil {
|
2021-08-17 13:44:30 +01:00
|
|
|
logrus.WithError(err).Errorf("failed to read device message from key change topic")
|
2022-02-04 13:08:13 +00:00
|
|
|
return true
|
2021-11-16 09:27:49 +00:00
|
|
|
}
|
|
|
|
if m.OutputCrossSigningKeyUpdate == nil {
|
|
|
|
// This probably shouldn't happen but stops us from panicking if we come
|
|
|
|
// across an update that doesn't satisfy either types.
|
2022-02-04 13:08:13 +00:00
|
|
|
return true
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
switch m.Type {
|
|
|
|
case api.TypeCrossSigningUpdate:
|
|
|
|
return t.onCrossSigningMessage(m)
|
|
|
|
default:
|
2022-02-04 13:08:13 +00:00
|
|
|
return true
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:08:13 +00:00
|
|
|
func (s *OutputCrossSigningKeyUpdateConsumer) onCrossSigningMessage(m api.DeviceMessage) bool {
|
2021-08-17 13:44:30 +01:00
|
|
|
output := m.CrossSigningKeyUpdate
|
|
|
|
_, host, err := gomatrixserverlib.SplitID('@', output.UserID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("eduserver output log: user ID parse failure")
|
2022-02-04 13:08:13 +00:00
|
|
|
return true
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
if host == gomatrixserverlib.ServerName(s.serverName) {
|
|
|
|
// Ignore any messages that contain information about our own users, as
|
|
|
|
// they already originated from this server.
|
2022-02-04 13:08:13 +00:00
|
|
|
return true
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|
|
|
|
uploadReq := &api.PerformUploadDeviceKeysRequest{
|
|
|
|
UserID: output.UserID,
|
|
|
|
}
|
|
|
|
if output.MasterKey != nil {
|
|
|
|
uploadReq.MasterKey = *output.MasterKey
|
|
|
|
}
|
|
|
|
if output.SelfSigningKey != nil {
|
|
|
|
uploadReq.SelfSigningKey = *output.SelfSigningKey
|
|
|
|
}
|
|
|
|
uploadRes := &api.PerformUploadDeviceKeysResponse{}
|
|
|
|
s.keyAPI.PerformUploadDeviceKeys(context.TODO(), uploadReq, uploadRes)
|
2022-02-04 13:08:13 +00:00
|
|
|
if uploadRes.Error != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2021-08-17 13:44:30 +01:00
|
|
|
}
|