dendrite/keyserver/storage/sqlite3/cross_signing_keys_table.go

100 lines
3.3 KiB
Go
Raw Normal View History

2021-07-28 12:48:23 +00: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 sqlite3
import (
2021-07-28 14:52:52 +00:00
"context"
2021-07-28 12:48:23 +00:00
"database/sql"
2021-07-29 08:48:09 +00:00
"fmt"
2021-07-28 12:48:23 +00:00
2021-07-28 14:52:52 +00:00
"github.com/matrix-org/dendrite/internal"
2021-07-29 08:48:09 +00:00
"github.com/matrix-org/dendrite/internal/sqlutil"
2021-07-28 14:52:52 +00:00
"github.com/matrix-org/dendrite/keyserver/api"
2021-07-28 12:48:23 +00:00
"github.com/matrix-org/dendrite/keyserver/storage/tables"
2021-07-28 14:52:52 +00:00
"github.com/matrix-org/gomatrixserverlib"
2021-07-28 12:48:23 +00:00
)
var crossSigningKeysSchema = `
CREATE TABLE IF NOT EXISTS keyserver_cross_signing_keys (
user_id TEXT NOT NULL,
key_type TEXT NOT NULL,
key_data TEXT NOT NULL,
stream_id BIGINT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS keyserver_cross_signing_keys_idx ON keyserver_cross_signing_keys(user_id, key_type, stream_id);
2021-07-28 12:48:23 +00:00
`
2021-07-28 14:52:52 +00:00
const selectCrossSigningKeysForUserSQL = "" +
2021-07-28 15:41:53 +00:00
"SELECT key_type, key_data FROM " +
2021-07-28 14:52:52 +00:00
" (SELECT * FROM keyserver_cross_signing_keys WHERE user_id = $1 ORDER BY stream_id DESC)" +
" GROUP BY user_id, key_type"
2021-07-29 08:48:09 +00:00
const insertCrossSigningKeysForUserSQL = "" +
"INSERT INTO keyserver_cross_signing_keys (user_id, key_type, key_data, stream_id)" +
" VALUES($1, $2, $3, $4)"
2021-07-28 12:48:23 +00:00
type crossSigningKeysStatements struct {
2021-07-28 14:52:52 +00:00
db *sql.DB
selectCrossSigningKeysForUserStmt *sql.Stmt
2021-07-29 08:48:09 +00:00
insertCrossSigningKeysForUserStmt *sql.Stmt
2021-07-28 12:48:23 +00:00
}
func NewSqliteCrossSigningKeysTable(db *sql.DB) (tables.CrossSigningKeys, error) {
s := &crossSigningKeysStatements{
db: db,
}
_, err := db.Exec(crossSigningKeysSchema)
if err != nil {
return nil, err
}
2021-07-28 14:52:52 +00:00
if s.selectCrossSigningKeysForUserStmt, err = db.Prepare(selectCrossSigningKeysForUserSQL); err != nil {
return nil, err
}
2021-07-29 08:48:09 +00:00
if s.insertCrossSigningKeysForUserStmt, err = db.Prepare(insertCrossSigningKeysForUserSQL); err != nil {
return nil, err
}
2021-07-28 12:48:23 +00:00
return s, nil
}
2021-07-28 14:52:52 +00:00
func (s *crossSigningKeysStatements) SelectCrossSigningKeysForUser(
2021-07-29 08:48:09 +00:00
ctx context.Context, txn *sql.Tx, userID string,
2021-07-28 14:52:52 +00:00
) (r api.CrossSigningKeyMap, err error) {
2021-07-29 08:48:09 +00:00
rows, err := sqlutil.TxStmt(txn, s.selectCrossSigningKeysForUserStmt).QueryContext(ctx, userID)
2021-07-28 14:52:52 +00:00
if err != nil {
return nil, err
}
defer internal.CloseAndLogIfError(ctx, rows, "selectCrossSigningKeysForUserStmt: rows.close() failed")
2021-07-28 15:27:19 +00:00
r = api.CrossSigningKeyMap{}
2021-07-28 14:52:52 +00:00
for rows.Next() {
var keyType gomatrixserverlib.CrossSigningKeyPurpose
var keyData gomatrixserverlib.Base64Bytes
2021-07-28 15:41:53 +00:00
if err := rows.Scan(&keyType, &keyData); err != nil {
2021-07-28 14:52:52 +00:00
return nil, err
}
2021-07-28 15:41:53 +00:00
r[keyType] = keyData
2021-07-28 14:52:52 +00:00
}
return
}
2021-07-29 08:48:09 +00:00
func (s *crossSigningKeysStatements) InsertCrossSigningKeysForUser(
ctx context.Context, txn *sql.Tx, userID string, keyType gomatrixserverlib.CrossSigningKeyPurpose, keyData gomatrixserverlib.Base64Bytes, streamID int64,
) error {
if _, err := sqlutil.TxStmt(txn, s.insertCrossSigningKeysForUserStmt).ExecContext(ctx, userID, keyType, keyData, streamID); err != nil {
return fmt.Errorf("s.insertCrossSigningKeysForUserStmt: %w", err)
}
return nil
}