Add config option to disable HS lookup for directory searches
This commit is contained in:
59
src/main/java/io/kamax/mxisd/config/DirectoryConfig.java
Normal file
59
src/main/java/io/kamax/mxisd/config/DirectoryConfig.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* mxisd - Matrix Identity Server Daemon
|
||||||
|
* Copyright (C) 2017 Maxime Dor
|
||||||
|
*
|
||||||
|
* https://max.kamax.io/
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.kamax.mxisd.config;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties("directory")
|
||||||
|
public class DirectoryConfig {
|
||||||
|
|
||||||
|
private final transient Logger log = LoggerFactory.getLogger(DnsOverwriteConfig.class);
|
||||||
|
|
||||||
|
public static class Exclude {
|
||||||
|
|
||||||
|
private boolean homeserver;
|
||||||
|
|
||||||
|
public boolean getHomeserver() {
|
||||||
|
return homeserver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Exclude setHomeserver(boolean homeserver) {
|
||||||
|
this.homeserver = homeserver;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Exclude exclude = new Exclude();
|
||||||
|
|
||||||
|
public Exclude getExclude() {
|
||||||
|
return exclude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExclude(Exclude exclude) {
|
||||||
|
this.exclude = exclude;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -23,6 +23,7 @@ package io.kamax.mxisd.directory;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import io.kamax.matrix.MatrixErrorInfo;
|
import io.kamax.matrix.MatrixErrorInfo;
|
||||||
|
import io.kamax.mxisd.config.DirectoryConfig;
|
||||||
import io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchRequest;
|
import io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchRequest;
|
||||||
import io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult;
|
import io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult;
|
||||||
import io.kamax.mxisd.dns.ClientDnsOverwrite;
|
import io.kamax.mxisd.dns.ClientDnsOverwrite;
|
||||||
@@ -54,6 +55,7 @@ public class DirectoryManager {
|
|||||||
|
|
||||||
private Logger log = LoggerFactory.getLogger(DirectoryManager.class);
|
private Logger log = LoggerFactory.getLogger(DirectoryManager.class);
|
||||||
|
|
||||||
|
private DirectoryConfig cfg;
|
||||||
private List<IDirectoryProvider> providers;
|
private List<IDirectoryProvider> providers;
|
||||||
|
|
||||||
private ClientDnsOverwrite dns;
|
private ClientDnsOverwrite dns;
|
||||||
@@ -61,7 +63,8 @@ public class DirectoryManager {
|
|||||||
private Gson gson;
|
private Gson gson;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public DirectoryManager(List<IDirectoryProvider> providers, ClientDnsOverwrite dns) {
|
public DirectoryManager(DirectoryConfig cfg, List<IDirectoryProvider> providers, ClientDnsOverwrite dns) {
|
||||||
|
this.cfg = cfg;
|
||||||
this.dns = dns;
|
this.dns = dns;
|
||||||
this.client = HttpClients.custom().setUserAgent("mxisd").build(); //FIXME centralize
|
this.client = HttpClients.custom().setUserAgent("mxisd").build(); //FIXME centralize
|
||||||
this.gson = GsonUtil.build();
|
this.gson = GsonUtil.build();
|
||||||
@@ -76,37 +79,41 @@ public class DirectoryManager {
|
|||||||
log.info("Original request URL: {}", target);
|
log.info("Original request URL: {}", target);
|
||||||
UserDirectorySearchResult result = new UserDirectorySearchResult();
|
UserDirectorySearchResult result = new UserDirectorySearchResult();
|
||||||
|
|
||||||
URIBuilder builder = dns.transform(target);
|
if (cfg.getExclude().getHomeserver()) {
|
||||||
log.info("Querying HS at {}", builder);
|
log.info("Skipping HS directory data, disabled in config");
|
||||||
builder.setParameter("access_token", accessToken);
|
} else {
|
||||||
HttpPost req = RestClientUtils.post(
|
URIBuilder builder = dns.transform(target);
|
||||||
builder.toString(),
|
log.info("Querying HS at {}", builder);
|
||||||
new UserDirectorySearchRequest(query));
|
builder.setParameter("access_token", accessToken);
|
||||||
try (CloseableHttpResponse res = client.execute(req)) {
|
HttpPost req = RestClientUtils.post(
|
||||||
int status = res.getStatusLine().getStatusCode();
|
builder.toString(),
|
||||||
Charset charset = ContentType.getOrDefault(res.getEntity()).getCharset();
|
new UserDirectorySearchRequest(query));
|
||||||
String body = IOUtils.toString(res.getEntity().getContent(), charset);
|
try (CloseableHttpResponse res = client.execute(req)) {
|
||||||
|
int status = res.getStatusLine().getStatusCode();
|
||||||
|
Charset charset = ContentType.getOrDefault(res.getEntity()).getCharset();
|
||||||
|
String body = IOUtils.toString(res.getEntity().getContent(), charset);
|
||||||
|
|
||||||
if (status != 200) {
|
if (status != 200) {
|
||||||
MatrixErrorInfo info = gson.fromJson(body, MatrixErrorInfo.class);
|
MatrixErrorInfo info = gson.fromJson(body, MatrixErrorInfo.class);
|
||||||
if (StringUtils.equals("M_UNRECOGNIZED", info.getErrcode())) { // FIXME no hardcoding, use Enum
|
if (StringUtils.equals("M_UNRECOGNIZED", info.getErrcode())) { // FIXME no hardcoding, use Enum
|
||||||
log.warn("Homeserver does not support Directory feature, skipping");
|
log.warn("Homeserver does not support Directory feature, skipping");
|
||||||
} else {
|
} else {
|
||||||
log.error("Homeserver returned an error while performing directory search");
|
log.error("Homeserver returned an error while performing directory search");
|
||||||
throw new MatrixException(status, info.getErrcode(), info.getError());
|
throw new MatrixException(status, info.getErrcode(), info.getError());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
UserDirectorySearchResult resultHs = gson.fromJson(body, UserDirectorySearchResult.class);
|
UserDirectorySearchResult resultHs = gson.fromJson(body, UserDirectorySearchResult.class);
|
||||||
log.info("Found {} match(es) in HS for '{}'", resultHs.getResults().size(), query);
|
log.info("Found {} match(es) in HS for '{}'", resultHs.getResults().size(), query);
|
||||||
result.getResults().addAll(resultHs.getResults());
|
result.getResults().addAll(resultHs.getResults());
|
||||||
if (resultHs.isLimited()) {
|
if (resultHs.isLimited()) {
|
||||||
result.setLimited(true);
|
result.setLimited(true);
|
||||||
|
}
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
throw new InternalServerError("Invalid JSON reply from the HS: " + e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new InternalServerError("Unable to query the HS: I/O error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
} catch (JsonSyntaxException e) {
|
|
||||||
throw new InternalServerError("Invalid JSON reply from the HS: " + e.getMessage());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new InternalServerError("Unable to query the HS: I/O error: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (IDirectoryProvider provider : providers) {
|
for (IDirectoryProvider provider : providers) {
|
||||||
|
@@ -227,6 +227,10 @@ view:
|
|||||||
storage:
|
storage:
|
||||||
backend: 'sqlite'
|
backend: 'sqlite'
|
||||||
|
|
||||||
|
directory:
|
||||||
|
exclude:
|
||||||
|
homeserver: false
|
||||||
|
|
||||||
---
|
---
|
||||||
spring:
|
spring:
|
||||||
profiles: systemd
|
profiles: systemd
|
||||||
|
Reference in New Issue
Block a user