2018-07-17 15:36:04 +01:00
|
|
|
// Copyright 2018 New Vector 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 query handles requests from other internal dendrite components when
|
|
|
|
// they interact with the AppServiceQueryAPI.
|
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2018-08-08 16:17:09 +01:00
|
|
|
"context"
|
2022-11-02 10:17:53 +00:00
|
|
|
"encoding/json"
|
2024-02-03 17:56:13 +00:00
|
|
|
"fmt"
|
2022-11-02 10:17:53 +00:00
|
|
|
"io"
|
2018-07-17 15:36:04 +01:00
|
|
|
"net/http"
|
2018-08-08 16:17:09 +01:00
|
|
|
"net/url"
|
2022-11-02 10:17:53 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-07-17 15:36:04 +01:00
|
|
|
|
2018-08-08 16:17:09 +01:00
|
|
|
"github.com/matrix-org/dendrite/appservice/api"
|
2023-03-13 15:45:14 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2018-07-17 15:36:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// AppServiceQueryAPI is an implementation of api.AppServiceQueryAPI
|
|
|
|
type AppServiceQueryAPI struct {
|
2022-11-02 10:17:53 +00:00
|
|
|
Cfg *config.AppServiceAPI
|
|
|
|
ProtocolCache map[string]api.ASProtocolResponse
|
|
|
|
CacheMu sync.Mutex
|
2018-07-17 15:36:04 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 16:17:09 +01:00
|
|
|
// RoomAliasExists performs a request to '/room/{roomAlias}' on all known
|
|
|
|
// handling application services until one admits to owning the room
|
|
|
|
func (a *AppServiceQueryAPI) RoomAliasExists(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.RoomAliasExistsRequest,
|
|
|
|
response *api.RoomAliasExistsResponse,
|
|
|
|
) error {
|
2023-03-13 15:45:14 +00:00
|
|
|
trace, ctx := internal.StartRegion(ctx, "ApplicationServiceRoomAlias")
|
|
|
|
defer trace.EndRegion()
|
2018-08-08 16:17:09 +01:00
|
|
|
|
|
|
|
// Determine which application service should handle this request
|
|
|
|
for _, appservice := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
if appservice.URL != "" && appservice.IsInterestedInRoomAlias(request.Alias) {
|
2024-02-03 17:56:13 +00:00
|
|
|
path := api.ASRoomAliasExistsPath
|
|
|
|
if a.Cfg.LegacyPaths {
|
|
|
|
path = api.ASRoomAliasExistsLegacyPath
|
|
|
|
}
|
2018-08-08 16:17:09 +01:00
|
|
|
// The full path to the rooms API, includes hs token
|
2024-02-03 17:56:13 +00:00
|
|
|
URL, err := url.Parse(appservice.RequestUrl() + path)
|
2021-08-16 14:19:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-08 16:17:09 +01:00
|
|
|
URL.Path += request.Alias
|
2024-02-03 17:56:13 +00:00
|
|
|
if a.Cfg.LegacyAuth {
|
|
|
|
q := URL.Query()
|
|
|
|
q.Set("access_token", appservice.HSToken)
|
|
|
|
URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
apiURL := URL.String()
|
2018-08-08 16:17:09 +01:00
|
|
|
|
|
|
|
// Send a request to each application service. If one responds that it has
|
|
|
|
// created the room, immediately return.
|
|
|
|
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-03 17:56:13 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", appservice.HSToken))
|
2022-05-10 11:08:10 +01:00
|
|
|
req = req.WithContext(ctx)
|
2018-08-08 16:17:09 +01:00
|
|
|
|
2023-04-04 08:42:46 +01:00
|
|
|
resp, err := appservice.HTTPClient.Do(req)
|
2018-08-08 16:17:09 +01:00
|
|
|
if resp != nil {
|
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"appservice_id": appservice.ID,
|
|
|
|
"status_code": resp.StatusCode,
|
|
|
|
}).WithError(err).Error("Unable to close application service response body")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("Issue querying room alias on application service %s", appservice.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
// OK received from appservice. Room exists
|
|
|
|
response.AliasExists = true
|
|
|
|
return nil
|
|
|
|
case http.StatusNotFound:
|
|
|
|
// Room does not exist
|
|
|
|
default:
|
|
|
|
// Application service reported an error. Warn
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"appservice_id": appservice.ID,
|
|
|
|
"status_code": resp.StatusCode,
|
|
|
|
}).Warn("Application service responded with non-OK status code")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AliasExists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:47:14 +01:00
|
|
|
// UserIDExists performs a request to '/users/{userID}' on all known
|
|
|
|
// handling application services until one admits to owning the user ID
|
|
|
|
func (a *AppServiceQueryAPI) UserIDExists(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.UserIDExistsRequest,
|
|
|
|
response *api.UserIDExistsResponse,
|
|
|
|
) error {
|
2023-03-13 15:45:14 +00:00
|
|
|
trace, ctx := internal.StartRegion(ctx, "ApplicationServiceUserID")
|
|
|
|
defer trace.EndRegion()
|
2018-08-10 15:47:14 +01:00
|
|
|
|
|
|
|
// Determine which application service should handle this request
|
|
|
|
for _, appservice := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
if appservice.URL != "" && appservice.IsInterestedInUserID(request.UserID) {
|
|
|
|
// The full path to the rooms API, includes hs token
|
2024-02-03 17:56:13 +00:00
|
|
|
path := api.ASUserExistsPath
|
|
|
|
if a.Cfg.LegacyPaths {
|
|
|
|
path = api.ASUserExistsLegacyPath
|
|
|
|
}
|
|
|
|
URL, err := url.Parse(appservice.RequestUrl() + path)
|
2021-08-16 14:19:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-10 15:47:14 +01:00
|
|
|
URL.Path += request.UserID
|
2024-02-03 17:56:13 +00:00
|
|
|
if a.Cfg.LegacyAuth {
|
|
|
|
q := URL.Query()
|
|
|
|
q.Set("access_token", appservice.HSToken)
|
|
|
|
URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
apiURL := URL.String()
|
2018-08-10 15:47:14 +01:00
|
|
|
|
|
|
|
// Send a request to each application service. If one responds that it has
|
|
|
|
// created the user, immediately return.
|
|
|
|
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-03 17:56:13 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", appservice.HSToken))
|
2023-04-04 08:42:46 +01:00
|
|
|
resp, err := appservice.HTTPClient.Do(req.WithContext(ctx))
|
2018-08-10 15:47:14 +01:00
|
|
|
if resp != nil {
|
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"appservice_id": appservice.ID,
|
|
|
|
"status_code": resp.StatusCode,
|
|
|
|
}).Error("Unable to close application service response body")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"appservice_id": appservice.ID,
|
|
|
|
}).WithError(err).Error("issue querying user ID on application service")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
// StatusOK received from appservice. User ID exists
|
|
|
|
response.UserIDExists = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log non OK
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"appservice_id": appservice.ID,
|
|
|
|
"status_code": resp.StatusCode,
|
|
|
|
}).Warn("application service responded with non-OK status code")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.UserIDExists = false
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
|
|
|
|
type thirdpartyResponses interface {
|
|
|
|
api.ASProtocolResponse | []api.ASUserResponse | []api.ASLocationResponse
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
func requestDo[T thirdpartyResponses](as *config.ApplicationService, url string, response *T) error {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-02 10:17:53 +00:00
|
|
|
}
|
2024-02-03 17:56:13 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", as.HSToken))
|
|
|
|
resp, err := as.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close() // nolint: errcheck
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(body, &response)
|
2022-11-02 10:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppServiceQueryAPI) Locations(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.LocationRequest,
|
|
|
|
resp *api.LocationResponse,
|
|
|
|
) error {
|
|
|
|
params, err := url.ParseQuery(req.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
path := api.ASLocationPath
|
|
|
|
if a.Cfg.LegacyPaths {
|
|
|
|
path = api.ASLocationLegacyPath
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
for _, as := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
var asLocations []api.ASLocationResponse
|
2024-02-03 17:56:13 +00:00
|
|
|
if a.Cfg.LegacyAuth {
|
|
|
|
params.Set("access_token", as.HSToken)
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
url := as.RequestUrl() + path
|
2022-11-02 10:17:53 +00:00
|
|
|
if req.Protocol != "" {
|
|
|
|
url += "/" + req.Protocol
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
if err := requestDo[[]api.ASLocationResponse](&as, url+"?"+params.Encode(), &asLocations); err != nil {
|
2023-08-03 08:26:42 +01:00
|
|
|
log.WithError(err).WithField("application_service", as.ID).Error("unable to get 'locations' from application service")
|
2022-11-02 10:17:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Locations = append(resp.Locations, asLocations...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Locations) == 0 {
|
|
|
|
resp.Exists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resp.Exists = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppServiceQueryAPI) User(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.UserRequest,
|
|
|
|
resp *api.UserResponse,
|
|
|
|
) error {
|
|
|
|
params, err := url.ParseQuery(req.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
path := api.ASUserPath
|
|
|
|
if a.Cfg.LegacyPaths {
|
|
|
|
path = api.ASUserLegacyPath
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
for _, as := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
var asUsers []api.ASUserResponse
|
2024-02-03 17:56:13 +00:00
|
|
|
if a.Cfg.LegacyAuth {
|
|
|
|
params.Set("access_token", as.HSToken)
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
url := as.RequestUrl() + path
|
2022-11-02 10:17:53 +00:00
|
|
|
if req.Protocol != "" {
|
|
|
|
url += "/" + req.Protocol
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:56:13 +00:00
|
|
|
if err := requestDo[[]api.ASUserResponse](&as, url+"?"+params.Encode(), &asUsers); err != nil {
|
2023-08-03 08:26:42 +01:00
|
|
|
log.WithError(err).WithField("application_service", as.ID).Error("unable to get 'user' from application service")
|
2022-11-02 10:17:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Users = append(resp.Users, asUsers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Users) == 0 {
|
|
|
|
resp.Exists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resp.Exists = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppServiceQueryAPI) Protocols(
|
|
|
|
ctx context.Context,
|
|
|
|
req *api.ProtocolRequest,
|
|
|
|
resp *api.ProtocolResponse,
|
|
|
|
) error {
|
2024-02-03 17:56:13 +00:00
|
|
|
protocolPath := api.ASProtocolPath
|
|
|
|
if a.Cfg.LegacyPaths {
|
|
|
|
protocolPath = api.ASProtocolLegacyPath
|
|
|
|
}
|
2022-11-02 10:17:53 +00:00
|
|
|
|
|
|
|
// get a single protocol response
|
|
|
|
if req.Protocol != "" {
|
|
|
|
|
|
|
|
a.CacheMu.Lock()
|
|
|
|
defer a.CacheMu.Unlock()
|
|
|
|
if proto, ok := a.ProtocolCache[req.Protocol]; ok {
|
|
|
|
resp.Exists = true
|
|
|
|
resp.Protocols = map[string]api.ASProtocolResponse{
|
|
|
|
req.Protocol: proto,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response := api.ASProtocolResponse{}
|
|
|
|
for _, as := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
var proto api.ASProtocolResponse
|
2024-02-03 17:56:13 +00:00
|
|
|
if err := requestDo[api.ASProtocolResponse](&as, as.RequestUrl()+protocolPath+req.Protocol, &proto); err != nil {
|
2023-08-03 08:26:42 +01:00
|
|
|
log.WithError(err).WithField("application_service", as.ID).Error("unable to get 'protocol' from application service")
|
2022-11-02 10:17:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Instances) != 0 {
|
|
|
|
response.Instances = append(response.Instances, proto.Instances...)
|
|
|
|
} else {
|
|
|
|
response = proto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Instances) == 0 {
|
|
|
|
resp.Exists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Exists = true
|
|
|
|
resp.Protocols = map[string]api.ASProtocolResponse{
|
|
|
|
req.Protocol: response,
|
|
|
|
}
|
|
|
|
a.ProtocolCache[req.Protocol] = response
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make(map[string]api.ASProtocolResponse, len(a.Cfg.Derived.ApplicationServices))
|
|
|
|
|
|
|
|
for _, as := range a.Cfg.Derived.ApplicationServices {
|
|
|
|
for _, p := range as.Protocols {
|
|
|
|
var proto api.ASProtocolResponse
|
2024-02-03 17:56:13 +00:00
|
|
|
if err := requestDo[api.ASProtocolResponse](&as, as.RequestUrl()+protocolPath+p, &proto); err != nil {
|
2023-08-03 08:26:42 +01:00
|
|
|
log.WithError(err).WithField("application_service", as.ID).Error("unable to get 'protocol' from application service")
|
2022-11-02 10:17:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
existing, ok := response[p]
|
|
|
|
if !ok {
|
|
|
|
response[p] = proto
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
existing.Instances = append(existing.Instances, proto.Instances...)
|
|
|
|
response[p] = existing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response) == 0 {
|
|
|
|
resp.Exists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
a.CacheMu.Lock()
|
|
|
|
defer a.CacheMu.Unlock()
|
|
|
|
a.ProtocolCache = response
|
|
|
|
|
|
|
|
resp.Exists = true
|
|
|
|
resp.Protocols = response
|
|
|
|
return nil
|
|
|
|
}
|