Make configuration enums in lowercase. Wrap create hashes by try-catch. Add initial part of the documentation.

This commit is contained in:
Anatoly Sablin
2019-11-15 23:39:45 +03:00
parent 9e4cabb69b
commit f9daf4d58a
8 changed files with 83 additions and 20 deletions

47
docs/MSC2140_MSC2134.md Normal file
View File

@@ -0,0 +1,47 @@
# MSC2140
## V1 vs V2
In the [MSC2140](https://github.com/matrix-org/matrix-doc/pull/2140) the v2 prefix was introduced.
Default values:
```.yaml
matrix:
v1: true # deprecated
v2: true
```
To disable change value to `false`.
NOTE: the v1 is deprecated, therefore recommend to use only v2 and disable v1 (default value can be ommited):
```.yaml
matrix:
v1: false
```
## Terms
Example:
```.yaml
policy:
policies:
term_name: # term name
version: 1.0 # version
terms:
en: # lang
name: term name en # localized name
url: https://ma1sd.host.tld/term_en.html # localized url
fe: # lang
name: term name fr # localized name
url: https://ma1sd.host.tld/term_fr.html # localized url
regexp:
- '/_matrix/identity/v2/account.*'
- '/_matrix/identity/v2/hash_lookup'
```
Where:
- `term_name` -- name of the terms.
- `regexp` -- regexp patterns for API.
## Hash lookup

View File

@@ -48,6 +48,9 @@ Create a list under the label `myOtherServers` containing two Identity servers:
## Unbind (MSC1915) ## Unbind (MSC1915)
- `session.policy.unbind.enabled`: Enable or disable unbind functionality (MSC1915). (Defaults to true). - `session.policy.unbind.enabled`: Enable or disable unbind functionality (MSC1915). (Defaults to true).
## Hash lookups, Term and others (MSC2140, MSC2134)
See the [dedicated document](MSC2140_MSC2134.md) for configuration.
*Warning*: Unbind check incoming request by two ways: *Warning*: Unbind check incoming request by two ways:
- session validation. - session validation.
- request signature via `X-Matrix` header and uses `server.publicUrl` property to construct the signing json; - request signature via `X-Matrix` header and uses `server.publicUrl` property to construct the signing json;

View File

@@ -23,27 +23,28 @@ public class HashingConfig {
LOGGER.info(" Pepper length: {}", getPepperLength()); LOGGER.info(" Pepper length: {}", getPepperLength());
LOGGER.info(" Rotation policy: {}", getRotationPolicy()); LOGGER.info(" Rotation policy: {}", getRotationPolicy());
LOGGER.info(" Hash storage type: {}", getHashStorageType()); LOGGER.info(" Hash storage type: {}", getHashStorageType());
if (RotationPolicyEnum.PER_SECONDS == rotationPolicy) { if (RotationPolicyEnum.per_seconds == rotationPolicy) {
LOGGER.info(" Rotation delay: {}", delay); LOGGER.info(" Rotation delay: {}", delay);
} }
LOGGER.info(" Algorithms: {}", algorithms);
} else { } else {
LOGGER.info("Hash configuration disabled, used only `none` pepper."); LOGGER.info("Hash configuration disabled, used only `none` pepper.");
} }
} }
public enum Algorithm { public enum Algorithm {
NONE, none,
SHA256 sha256
} }
public enum RotationPolicyEnum { public enum RotationPolicyEnum {
PER_REQUESTS, per_requests,
PER_SECONDS per_seconds
} }
public enum HashStorageEnum { public enum HashStorageEnum {
IN_MEMORY, in_memory,
SQL sql
} }
public boolean isEnabled() { public boolean isEnabled() {

View File

@@ -100,10 +100,12 @@ public class PolicyConfig {
policyObjectItem.getValue().getPatterns().add(Pattern.compile(regexp)); policyObjectItem.getValue().getPatterns().add(Pattern.compile(regexp));
} }
sb.append(" terms:\n"); sb.append(" terms:\n");
for (Map.Entry<String, TermObject> termItem : policyObject.getTerms().entrySet()) { if (policyObject.getTerms() != null) {
sb.append(" - lang: ").append(termItem.getKey()).append("\n"); for (Map.Entry<String, TermObject> termItem : policyObject.getTerms().entrySet()) {
sb.append(" name: ").append(termItem.getValue().getName()).append("\n"); sb.append(" - lang: ").append(termItem.getKey()).append("\n");
sb.append(" url: ").append(termItem.getValue().getUrl()).append("\n"); sb.append(" name: ").append(termItem.getValue().getName()).append("\n");
sb.append(" url: ").append(termItem.getValue().getUrl()).append("\n");
}
} }
LOGGER.info(sb.toString()); LOGGER.info(sb.toString());
} }

View File

@@ -125,7 +125,7 @@ public abstract class SqlConfig {
} }
public static class Lookup { public static class Lookup {
private String query; private String query = "SELECT user_id AS mxid, medium, address from user_threepids";
public String getQuery() { public String getQuery() {
return query; return query;

View File

@@ -6,11 +6,15 @@ import io.kamax.mxisd.hash.storage.HashStorage;
import io.kamax.mxisd.lookup.ThreePidMapping; import io.kamax.mxisd.lookup.ThreePidMapping;
import io.kamax.mxisd.lookup.provider.IThreePidProvider; import io.kamax.mxisd.lookup.provider.IThreePidProvider;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
public class HashEngine { public class HashEngine {
private static final Logger LOGGER = LoggerFactory.getLogger(HashEngine.class);
private final List<? extends IThreePidProvider> providers; private final List<? extends IThreePidProvider> providers;
private final HashStorage hashStorage; private final HashStorage hashStorage;
private final MxSha256 sha256 = new MxSha256(); private final MxSha256 sha256 = new MxSha256();
@@ -24,15 +28,21 @@ public class HashEngine {
} }
public void updateHashes() { public void updateHashes() {
LOGGER.info("Start update hashes.");
synchronized (hashStorage) { synchronized (hashStorage) {
this.pepper = newPepper(); this.pepper = newPepper();
hashStorage.clear(); hashStorage.clear();
for (IThreePidProvider provider : providers) { for (IThreePidProvider provider : providers) {
for (ThreePidMapping pidMapping : provider.populateHashes()) { try {
hashStorage.add(pidMapping, hash(pidMapping)); for (ThreePidMapping pidMapping : provider.populateHashes()) {
hashStorage.add(pidMapping, hash(pidMapping));
}
} catch (Exception e) {
LOGGER.error("Unable to update hashes of the provider: " + provider.toString(), e);
} }
} }
} }
LOGGER.info("Finish update hashes.");
} }
public String getPepper() { public String getPepper() {

View File

@@ -40,10 +40,10 @@ public class HashManager {
private void initStorage() { private void initStorage() {
if (config.isEnabled()) { if (config.isEnabled()) {
switch (config.getHashStorageType()) { switch (config.getHashStorageType()) {
case IN_MEMORY: case in_memory:
this.hashStorage = new InMemoryHashStorage(); this.hashStorage = new InMemoryHashStorage();
break; break;
case SQL: case sql:
this.hashStorage = new SqlHashStorage(storage); this.hashStorage = new SqlHashStorage(storage);
break; break;
default: default:
@@ -57,10 +57,10 @@ public class HashManager {
private void initRotationStrategy() { private void initRotationStrategy() {
if (config.isEnabled()) { if (config.isEnabled()) {
switch (config.getRotationPolicy()) { switch (config.getRotationPolicy()) {
case PER_REQUESTS: case per_requests:
this.rotationStrategy = new RotationPerRequests(); this.rotationStrategy = new RotationPerRequests();
break; break;
case PER_SECONDS: case per_seconds:
this.rotationStrategy = new TimeBasedRotation(config.getDelay()); this.rotationStrategy = new TimeBasedRotation(config.getDelay());
break; break;
default: default:

View File

@@ -84,7 +84,7 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
} }
private void noneAlgorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) throws Exception { private void noneAlgorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) throws Exception {
if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.NONE)) { if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.none)) {
throw new InvalidParamException(); throw new InvalidParamException();
} }
@@ -110,7 +110,7 @@ public class HashLookupHandler extends LookupHandler implements ApiHandler {
} }
private void sha256Algorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) { private void sha256Algorithm(HttpServerExchange exchange, HashLookupRequest request, ClientHashLookupRequest input) {
if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.SHA256)) { if (!hashManager.getConfig().getAlgorithms().contains(HashingConfig.Algorithm.sha256)) {
throw new InvalidParamException(); throw new InvalidParamException();
} }