From be7a785e521bbd7e8cf23f9224707561d2bcf17f Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Thu, 19 Oct 2017 13:00:31 +0200 Subject: [PATCH 1/6] Create roomserver QueryReserveRoomID TODO: Query prefix is not really correct for this specific query because it will not do readonly stuff. Maybe rename it? --- .../dendrite/roomserver/api/query.go | 31 +++++++++++++++++-- .../dendrite/roomserver/query/query.go | 31 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/roomserver/api/query.go b/src/github.com/matrix-org/dendrite/roomserver/api/query.go index 248850bff..36961d383 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -21,10 +21,9 @@ import ( "fmt" "net/http" + opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" - "github.com/opentracing/opentracing-go" - "github.com/matrix-org/gomatrixserverlib" ) @@ -155,6 +154,18 @@ type QueryServerAllowedToSeeEventResponse struct { AllowedToSeeEvent bool `json:"can_see_event"` } +// QueryReserveRoomIDRequest is a request to QueryServerAllowedToSeeEvent +type QueryReserveRoomIDRequest struct { + // TODO: Docs + RoomID string `json:"room_id"` +} + +// QueryReserveRoomIDResponse is a response to QueryServerAllowedToSeeEvent +type QueryReserveRoomIDResponse struct { + // TODO: Docs + Success bool `json:"success"` +} + // RoomserverQueryAPI is used to query information from the room server. type RoomserverQueryAPI interface { // Query the latest events and state for a room from the room server. @@ -218,6 +229,9 @@ const RoomserverQueryInvitesForUserPath = "/api/roomserver/queryInvitesForUser" // RoomserverQueryServerAllowedToSeeEventPath is the HTTP path for the QueryServerAllowedToSeeEvent API const RoomserverQueryServerAllowedToSeeEventPath = "/api/roomserver/queryServerAllowedToSeeEvent" +// RoomserverQueryReserveRoomIDPath is the HTTP path for the QueryReserveRoomID API +const RoomserverQueryReserveRoomIDPath = "/api/roomserver/queryReserveRoomID" + // NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API. // If httpClient is nil then it uses the http.DefaultClient func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI { @@ -310,6 +324,19 @@ func (h *httpRoomserverQueryAPI) QueryServerAllowedToSeeEvent( return postJSON(ctx, span, h.httpClient, apiURL, request, response) } +// TODO: Docs +func (h *httpRoomserverQueryAPI) QueryReserveRoomID( + ctx context.Context, + request *QueryReserveRoomIDRequest, + response *QueryReserveRoomIDResponse, +) (err error) { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryReserveRoomID") + defer span.Finish() + + apiURL := h.roomserverURL + RoomserverQueryReserveRoomIDPath + return postJSON(ctx, span, h.httpClient, apiURL, request, response) +} + func postJSON( ctx context.Context, span opentracing.Span, httpClient *http.Client, apiURL string, request, response interface{}, diff --git a/src/github.com/matrix-org/dendrite/roomserver/query/query.go b/src/github.com/matrix-org/dendrite/roomserver/query/query.go index 265e4ad3d..c897a9b4e 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query.go @@ -418,6 +418,23 @@ func (r *RoomserverQueryAPI) QueryServerAllowedToSeeEvent( return nil } +// QueryReserveRoomID implements api.RoomserverQueryAPI +func (r *RoomserverQueryAPI) QueryReserveRoomID( + ctx context.Context, + request *api.QueryReserveRoomIDRequest, + response *api.QueryReserveRoomIDResponse, +) error { + nid, err := r.DB.RoomNID(ctx, request.RoomID) + if nid != 0 { + response.Success = false + return nil + } + + response.Success = true + + return err +} + // SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux. // nolint: gocyclo func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) { @@ -505,4 +522,18 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) { return util.JSONResponse{Code: 200, JSON: &response} }), ) + servMux.Handle( + api.RoomserverQueryReserveRoomIDPath, + common.MakeInternalAPI("queryReserveRoomID", func(req *http.Request) util.JSONResponse { + var request api.QueryReserveRoomIDRequest + var response api.QueryReserveRoomIDResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.QueryReserveRoomID(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: 200, JSON: &response} + }), + ) } From 528f7b89039818f0d0ba63e39777d02285ab3f1a Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 15 Nov 2017 21:14:56 +0100 Subject: [PATCH 2/6] Create new table roomserver_reserved_rooms --- .../roomserver/storage/reserved_rooms.go | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/github.com/matrix-org/dendrite/roomserver/storage/reserved_rooms.go diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/reserved_rooms.go b/src/github.com/matrix-org/dendrite/roomserver/storage/reserved_rooms.go new file mode 100644 index 000000000..487da3dbe --- /dev/null +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/reserved_rooms.go @@ -0,0 +1,73 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 storage + +import ( + "context" + "database/sql" + "time" +) + +const reservedRoomSchema = ` +-- The events table holds metadata for each event, the actual JSON is stored +-- separately to keep the size of the rows small. +CREATE SEQUENCE IF NOT EXISTS roomserver_event_nid_seq; +CREATE TABLE IF NOT EXISTS roomserver_reserved_rooms ( + --- The Room ID --- + room_id TEXT PRIMARY KEY, + reserved_since TIMESTAMP NOT NULL +); +` + +const insertReservedRoomSQL = "" + + "INSERT INTO roomserver_reserved_rooms (room_id, reserved_since)" + + " VALUES ($1, $2)" + +const selectReservedRoomSQL = "" + + "SELECT reserved_since FROM roomserver_reserved_rooms WHERE room_id = $1" + +type reservedRoomStatements struct { + insertReservedRoomStmt *sql.Stmt + selectReservedRoomStmt *sql.Stmt +} + +func (s *reservedRoomStatements) prepare(db *sql.DB) (err error) { + _, err = db.Exec(reservedRoomSchema) + if err != nil { + return + } + + return statementList{ + {&s.insertReservedRoomStmt, insertReservedRoomSQL}, + {&s.selectReservedRoomStmt, selectReservedRoomSQL}, + }.prepare(db) +} + +func (s *reservedRoomStatements) insertReservedRoom( + ctx context.Context, + roomID string, + reservedSince time.Time, +) error { + _, err := s.insertReservedRoomStmt.ExecContext(ctx, roomID, reservedSince) + return err +} + +func (s *reservedRoomStatements) selectReservedRoom( + ctx context.Context, roomID string, +) (time.Time, error) { + var reservedSince time.Time + err := s.selectReservedRoomStmt.QueryRowContext(ctx, roomID).Scan(&reservedSince) + return reservedSince, err +} From 30c65f13521051699ec2e8b4fa07af0e87074cd5 Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 15 Nov 2017 21:15:37 +0100 Subject: [PATCH 3/6] Progress on #267 --- .../dendrite/cmd/dendrite-room-server/main.go | 2 +- .../dendrite/roomserver/query/query.go | 28 ++++++++++++++++++- .../dendrite/roomserver/storage/sql.go | 2 ++ .../dendrite/roomserver/storage/storage.go | 12 ++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go index f607d1ec2..06773972a 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-room-server/main.go @@ -20,7 +20,6 @@ import ( _ "net/http/pprof" "os" - log "github.com/sirupsen/logrus" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/roomserver/alias" @@ -28,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/query" "github.com/matrix-org/dendrite/roomserver/storage" "github.com/prometheus/client_golang/prometheus" + log "github.com/sirupsen/logrus" sarama "gopkg.in/Shopify/sarama.v1" ) diff --git a/src/github.com/matrix-org/dendrite/roomserver/query/query.go b/src/github.com/matrix-org/dendrite/roomserver/query/query.go index c897a9b4e..bddc9cf86 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query.go @@ -16,8 +16,10 @@ package query import ( "context" + "database/sql" "encoding/json" "net/http" + "time" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/roomserver/api" @@ -74,6 +76,10 @@ type RoomserverQueryAPIDatabase interface { EventStateKeys( context.Context, []types.EventStateKeyNID, ) (map[types.EventStateKeyNID]string, error) + // Lookup if the roomID has been reserved, and when that reservation was made. + RoomIDReserved(ctx context.Context, roomID string) (time.Time, error) + // Reserve the room ID. + ReserveRoomID(ctx context.Context, roomID string) error } // RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI @@ -425,14 +431,34 @@ func (r *RoomserverQueryAPI) QueryReserveRoomID( response *api.QueryReserveRoomIDResponse, ) error { nid, err := r.DB.RoomNID(ctx, request.RoomID) + if err != nil && err != sql.ErrNoRows { + return err + } + if nid != 0 { response.Success = false return nil } + // Check if Room ID has already been reserved. + reservedSince, err := r.DB.RoomIDReserved(ctx, request.RoomID) + if reservedSince != (time.Time{}) { + response.Success = false + return nil + } + + if err != nil && err != sql.ErrNoRows { + return err + } + + err = r.DB.ReserveRoomID(ctx, request.RoomID) + if err != nil { + return err + } + response.Success = true - return err + return nil } // SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux. diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go index a24dbb1d3..1a8f497f5 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/sql.go @@ -30,6 +30,7 @@ type statements struct { roomAliasesStatements inviteStatements membershipStatements + reservedRoomStatements } func (s *statements) prepare(db *sql.DB) error { @@ -47,6 +48,7 @@ func (s *statements) prepare(db *sql.DB) error { s.roomAliasesStatements.prepare, s.inviteStatements.prepare, s.membershipStatements.prepare, + s.reservedRoomStatements.prepare, } { if err = prepare(db); err != nil { return err diff --git a/src/github.com/matrix-org/dendrite/roomserver/storage/storage.go b/src/github.com/matrix-org/dendrite/roomserver/storage/storage.go index ad4fed659..ee812710a 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/storage/storage.go +++ b/src/github.com/matrix-org/dendrite/roomserver/storage/storage.go @@ -17,6 +17,7 @@ package storage import ( "context" "database/sql" + "time" // Import the postgres database driver. _ "github.com/lib/pq" @@ -651,6 +652,17 @@ func (d *Database) GetMembershipEventNIDsForRoom( return d.statements.selectMembershipsFromRoom(ctx, roomNID) } +// RoomIDReserved implements query.RoomserverQueryAPIDB +func (d *Database) RoomIDReserved(ctx context.Context, roomID string) (time.Time, error) { + return d.statements.selectReservedRoom(ctx, roomID) +} + +// ReserveRoomID implements query.RoomserverQueryAPIDB +// It saved the given room ID as reserved. +func (d *Database) ReserveRoomID(ctx context.Context, roomID string) error { + return d.statements.insertReservedRoom(ctx, roomID, time.Now()) +} + type transaction struct { ctx context.Context txn *sql.Tx From a6d47d2fa97745718b9d3a99c39d3516b9aa06f0 Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 15 Nov 2017 21:57:13 +0100 Subject: [PATCH 4/6] Implement new ReserveRoomID in CreateRoom --- .../dendrite/clientapi/routing/createroom.go | 22 +++++++++++++++---- .../dendrite/clientapi/routing/routing.go | 2 +- .../dendrite/roomserver/api/query.go | 7 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index 078a7319b..17941d202 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -23,7 +23,6 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" - log "github.com/sirupsen/logrus" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -33,6 +32,7 @@ import ( "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + log "github.com/sirupsen/logrus" ) // https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom @@ -90,10 +90,24 @@ type fledglingEvent struct { func CreateRoom(req *http.Request, device *authtypes.Device, cfg config.Dendrite, producer *producers.RoomserverProducer, accountDB *accounts.Database, aliasAPI api.RoomserverAliasAPI, + queryAPI api.RoomserverQueryAPI, ) util.JSONResponse { - // TODO (#267): Check room ID doesn't clash with an existing one, and we - // probably shouldn't be using pseudo-random strings, maybe GUIDs? - roomID := fmt.Sprintf("!%s:%s", util.RandomString(16), cfg.Matrix.ServerName) + // Generate a room ID and reserve it. + // Keep trying until we have one which is unused. + var roomID string + for roomID == "" { + checkRoomID := util.RandomString(16) + checkRoomID = fmt.Sprintf("!%s:%s", checkRoomID, cfg.Matrix.ServerName) + + queryReq := api.QueryReserveRoomIDRequest{RoomID: checkRoomID} + var queryResp api.QueryReserveRoomIDResponse + queryAPI.QueryReserveRoomID(req.Context(), &queryReq, &queryResp) + + if queryResp.Success { + roomID = checkRoomID + } + } + return createRoom(req, device, cfg, roomID, producer, accountDB, aliasAPI) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 0b9e4172a..586646937 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -71,7 +71,7 @@ func Setup( r0mux.Handle("/createRoom", common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI) + return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI, queryAPI) }), ).Methods("POST", "OPTIONS") r0mux.Handle("/join/{roomIDOrAlias}", diff --git a/src/github.com/matrix-org/dendrite/roomserver/api/query.go b/src/github.com/matrix-org/dendrite/roomserver/api/query.go index 36961d383..578cfe444 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -209,6 +209,13 @@ type RoomserverQueryAPI interface { request *QueryServerAllowedToSeeEventRequest, response *QueryServerAllowedToSeeEventResponse, ) error + + // Query if a room ID is available and reserve it. + QueryReserveRoomID( + ctx context.Context, + request *QueryReserveRoomIDRequest, + response *QueryReserveRoomIDResponse, + ) error } // RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API. From 66b69a43b515b3de10b387445352dbf3a39e8ec5 Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 15 Nov 2017 22:23:31 +0100 Subject: [PATCH 5/6] Make linters happy --- .../matrix-org/dendrite/clientapi/routing/createroom.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index 17941d202..29aa12081 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -101,7 +101,10 @@ func CreateRoom(req *http.Request, device *authtypes.Device, queryReq := api.QueryReserveRoomIDRequest{RoomID: checkRoomID} var queryResp api.QueryReserveRoomIDResponse - queryAPI.QueryReserveRoomID(req.Context(), &queryReq, &queryResp) + err := queryAPI.QueryReserveRoomID(req.Context(), &queryReq, &queryResp) + if err != nil { + return httputil.LogThenError(req, err) + } if queryResp.Success { roomID = checkRoomID From b1c613f49efe2ae5e8c00beb0d673455af3f4f3f Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Sun, 19 Nov 2017 19:41:48 +0100 Subject: [PATCH 6/6] Add missing documentation/comments --- src/github.com/matrix-org/dendrite/roomserver/api/query.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/roomserver/api/query.go b/src/github.com/matrix-org/dendrite/roomserver/api/query.go index 578cfe444..a4b495120 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -156,13 +156,14 @@ type QueryServerAllowedToSeeEventResponse struct { // QueryReserveRoomIDRequest is a request to QueryServerAllowedToSeeEvent type QueryReserveRoomIDRequest struct { - // TODO: Docs + // The room ID to check and reserve. RoomID string `json:"room_id"` } // QueryReserveRoomIDResponse is a response to QueryServerAllowedToSeeEvent type QueryReserveRoomIDResponse struct { - // TODO: Docs + // Whether the room ID has been reserved. + // False means that the room ID is already is use, or already reserved. Success bool `json:"success"` } @@ -331,7 +332,7 @@ func (h *httpRoomserverQueryAPI) QueryServerAllowedToSeeEvent( return postJSON(ctx, span, h.httpClient, apiURL, request, response) } -// TODO: Docs +// QueryReserveRoomID implements RoomserverQueryAPI func (h *httpRoomserverQueryAPI) QueryReserveRoomID( ctx context.Context, request *QueryReserveRoomIDRequest,