2020-07-28 18:38:30 +02:00
|
|
|
// Copyright 2020 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 sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2022-07-25 11:39:22 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/keyserver/storage/sqlite3/deltas"
|
2020-07-28 18:38:30 +02:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/storage/tables"
|
|
|
|
)
|
|
|
|
|
|
|
|
var keyChangesSchema = `
|
|
|
|
-- Stores key change information about users. Used to determine when to send updated device lists to clients.
|
|
|
|
CREATE TABLE IF NOT EXISTS keyserver_key_changes (
|
2022-01-21 10:56:06 +01:00
|
|
|
change_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2020-07-28 18:38:30 +02:00
|
|
|
-- The key owner
|
|
|
|
user_id TEXT NOT NULL,
|
2022-01-21 10:56:06 +01:00
|
|
|
UNIQUE (user_id)
|
2020-07-28 18:38:30 +02:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
2022-01-21 10:56:06 +01:00
|
|
|
// Replace based on user ID. We don't care how many times the user's keys have changed, only that they
|
|
|
|
// have changed, hence we can just keep bumping the change ID for this user.
|
2020-07-28 18:38:30 +02:00
|
|
|
const upsertKeyChangeSQL = "" +
|
2022-01-21 10:56:06 +01:00
|
|
|
"INSERT OR REPLACE INTO keyserver_key_changes (user_id)" +
|
|
|
|
" VALUES ($1)" +
|
|
|
|
" RETURNING change_id"
|
2020-07-28 18:38:30 +02:00
|
|
|
|
|
|
|
const selectKeyChangesSQL = "" +
|
2022-01-21 10:56:06 +01:00
|
|
|
"SELECT user_id, change_id FROM keyserver_key_changes WHERE change_id > $1 AND change_id <= $2"
|
2020-07-28 18:38:30 +02:00
|
|
|
|
|
|
|
type keyChangesStatements struct {
|
|
|
|
db *sql.DB
|
|
|
|
upsertKeyChangeStmt *sql.Stmt
|
|
|
|
selectKeyChangesStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:29:45 +02:00
|
|
|
func NewSqliteKeyChangesTable(db *sql.DB) (tables.KeyChanges, error) {
|
2020-07-28 18:38:30 +02:00
|
|
|
s := &keyChangesStatements{
|
2020-08-25 11:29:45 +02:00
|
|
|
db: db,
|
2020-07-28 18:38:30 +02:00
|
|
|
}
|
|
|
|
_, err := db.Exec(keyChangesSchema)
|
2022-07-25 11:39:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
// TODO: Remove when we are sure we are not having goose artefacts in the db
|
|
|
|
// This forces an error, which indicates the migration is already applied, since the
|
|
|
|
// column partition was removed from the table
|
2022-08-08 10:18:57 +02:00
|
|
|
var count int
|
|
|
|
err = db.QueryRow("SELECT partition FROM keyserver_key_changes LIMIT 1;").Scan(&count)
|
2022-07-25 11:39:22 +02:00
|
|
|
if err == nil {
|
|
|
|
m := sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations(sqlutil.Migration{
|
|
|
|
Version: "keyserver: refactor key changes",
|
|
|
|
Up: deltas.UpRefactorKeyChanges,
|
|
|
|
})
|
|
|
|
return s, m.Up(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2022-01-21 10:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *keyChangesStatements) Prepare() (err error) {
|
|
|
|
if s.upsertKeyChangeStmt, err = s.db.Prepare(upsertKeyChangeSQL); err != nil {
|
|
|
|
return err
|
2020-07-28 18:38:30 +02:00
|
|
|
}
|
2022-01-21 10:56:06 +01:00
|
|
|
if s.selectKeyChangesStmt, err = s.db.Prepare(selectKeyChangesSQL); err != nil {
|
|
|
|
return err
|
2020-07-28 18:38:30 +02:00
|
|
|
}
|
2022-01-21 10:56:06 +01:00
|
|
|
return nil
|
2020-07-28 18:38:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:56:06 +01:00
|
|
|
func (s *keyChangesStatements) InsertKeyChange(ctx context.Context, userID string) (changeID int64, err error) {
|
|
|
|
err = s.upsertKeyChangeStmt.QueryRowContext(ctx, userID).Scan(&changeID)
|
|
|
|
return
|
2020-07-28 18:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *keyChangesStatements) SelectKeyChanges(
|
2022-01-21 10:56:06 +01:00
|
|
|
ctx context.Context, fromOffset, toOffset int64,
|
2020-07-28 18:38:30 +02:00
|
|
|
) (userIDs []string, latestOffset int64, err error) {
|
2020-12-18 12:11:21 +01:00
|
|
|
latestOffset = fromOffset
|
2022-01-21 10:56:06 +01:00
|
|
|
rows, err := s.selectKeyChangesStmt.QueryContext(ctx, fromOffset, toOffset)
|
2020-07-28 18:38:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectKeyChangesStmt: rows.close() failed")
|
|
|
|
for rows.Next() {
|
|
|
|
var userID string
|
|
|
|
var offset int64
|
|
|
|
if err := rows.Scan(&userID, &offset); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
if offset > latestOffset {
|
|
|
|
latestOffset = offset
|
|
|
|
}
|
|
|
|
userIDs = append(userIDs, userID)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|