From cbb9fced8d831dbf3bc5ce25551469bb506f96e8 Mon Sep 17 00:00:00 2001 From: Anatoly Sablin Date: Thu, 5 Dec 2019 23:27:13 +0300 Subject: [PATCH] Clarify the documentation. Add the hash config to the example config. Uses duration in the delay field instead of the seconds. --- docs/MSC2140_MSC2134.md | 14 ++++++- gradle/wrapper/gradle-wrapper.properties | 5 ++- ma1sd.example.yaml | 39 +++++++++++++++++++ .../mxisd/config/DurationDeserializer.java | 30 ++++++++++++++ .../io/kamax/mxisd/config/HashingConfig.java | 27 +++++++++---- .../java/io/kamax/mxisd/hash/HashManager.java | 2 +- .../test/config/DurationDeserializerTest.java | 20 ++++++++++ 7 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 src/main/java/io/kamax/mxisd/config/DurationDeserializer.java create mode 100644 src/test/java/io/kamax/mxisd/test/config/DurationDeserializerTest.java diff --git a/docs/MSC2140_MSC2134.md b/docs/MSC2140_MSC2134.md index 4f1fb7f..8d5c17e 100644 --- a/docs/MSC2140_MSC2134.md +++ b/docs/MSC2140_MSC2134.md @@ -70,21 +70,31 @@ There is only one exception: [`POST /_matrix/identity/v2/terms`](https://matrix. ## [Hash lookup](https://github.com/matrix-org/matrix-doc/blob/hs/hash-identity/proposals/2134-identity-hash-lookup.md) +Hashes and the pepper updates together according to the `rotationPolicy`. + ```.yaml hashing: - enabled: true # enable or disable the hash lookup MSC2140 (default to false) + enabled: true # enable or disable the hash lookup MSC2140 (default is false) pepperLength: 20 # length of the pepper value (default is 20) rotationPolicy: per_requests # or `per_seconds` how often the hashes will be updating hashStorageType: sql # or `in_memory` where the hashes will be stored algorithms: - none # the same as v1 bulk lookup - sha256 # hash the 3PID and pepper. - delay: 10 # how often hashes will be updated if rotation policy = per_seconds (default is 10) + delay: 2m # how often hashes will be updated if rotation policy = per_seconds (default is 10s) requests: 10 # how many lookup requests will be performed before updating hashes if rotation policy = per_requests (default is 10) ``` When enabled and client requests the `none` algorithms then hash lookups works as v1 bulk lookup. +Delay specified in the format: `2d 4h 12m 34s` - this means 2 days 4 hours 12 minutes and 34 seconds. Zero units may be omitted. For example: + +- 12s - 12 seconds +- 3m - 3 minutes +- 5m 6s - 5 minutes and 6 seconds +- 6h 3s - 6 hours and 3 seconds + + Sha256 algorithm supports only sql, memory and exec 3PID providers. For sql provider (i.e. for the `synapseSql`): ```.yaml diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 6ce793f..9851cd8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Dec 05 22:39:36 MSK 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-bin.zip -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/ma1sd.example.yaml b/ma1sd.example.yaml index 0f3be3a..05b40a0 100644 --- a/ma1sd.example.yaml +++ b/ma1sd.example.yaml @@ -21,6 +21,8 @@ # matrix: domain: '' + v1: true # deprecated + v2: true # MSC2140 API v2 ################ @@ -109,3 +111,40 @@ threepid: # Password for the account password: "ThePassword" + + +#### MSC2134 (hash lookup) + +hashing: + enabled: false # enable or disable the hash lookup MSC2140 (default is false) + pepperLength: 20 # length of the pepper value (default is 20) + rotationPolicy: per_requests # or `per_seconds` how often the hashes will be updating + hashStorageType: sql # or `in_memory` where the hashes will be stored + algorithms: + - none # the same as v1 bulk lookup + - sha256 # hash the 3PID and pepper. + delay: 2m # how often hashes will be updated if rotation policy = per_seconds (default is 10s) + requests: 10 # how many lookup requests will be performed before updating hashes if rotation policy = per_requests (default is 10) + +### hash lookup for synapseSql provider. +# synapseSql: +# lookup: +# query: 'select user_id as mxid, medium, address from user_threepids' # query for retrive 3PIDs for hashes. + +#### MSC2140 (Terms) +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_details' + - '/_matrix/identity/v2/lookup' + diff --git a/src/main/java/io/kamax/mxisd/config/DurationDeserializer.java b/src/main/java/io/kamax/mxisd/config/DurationDeserializer.java new file mode 100644 index 0000000..f27bc8a --- /dev/null +++ b/src/main/java/io/kamax/mxisd/config/DurationDeserializer.java @@ -0,0 +1,30 @@ +package io.kamax.mxisd.config; + +public class DurationDeserializer { + + public long deserialize(String argument) { + long duration = 0L; + for (String part : argument.split(" ")) { + String unit = part.substring(part.length() - 1); + long value = Long.parseLong(part.substring(0, part.length() - 1)); + switch (unit) { + case "s": + duration += value; + break; + case "m": + duration += value * 60; + break; + case "h": + duration += value * 60 * 60; + break; + case "d": + duration += value * 60 * 60 * 24; + break; + default: + throw new IllegalArgumentException(String.format("Unknown duration unit: %s", unit)); + } + } + + return duration; + } +} diff --git a/src/main/java/io/kamax/mxisd/config/HashingConfig.java b/src/main/java/io/kamax/mxisd/config/HashingConfig.java index 374e28d..4d7088b 100644 --- a/src/main/java/io/kamax/mxisd/config/HashingConfig.java +++ b/src/main/java/io/kamax/mxisd/config/HashingConfig.java @@ -14,7 +14,8 @@ public class HashingConfig { private int pepperLength = 20; private RotationPolicyEnum rotationPolicy; private HashStorageEnum hashStorageType; - private long delay = 10; + private String delay = "10s"; + private transient long delayInSeconds = 10; private int requests = 10; private List algorithms = new ArrayList<>(); @@ -24,13 +25,15 @@ public class HashingConfig { LOGGER.info(" Pepper length: {}", getPepperLength()); LOGGER.info(" Rotation policy: {}", getRotationPolicy()); LOGGER.info(" Hash storage type: {}", getHashStorageType()); - if (RotationPolicyEnum.per_seconds == rotationPolicy) { - LOGGER.info(" Rotation delay: {}", delay); + if (RotationPolicyEnum.per_seconds == getRotationPolicy()) { + setDelayInSeconds(new DurationDeserializer().deserialize(getDelay())); + LOGGER.info(" Rotation delay: {}", getDelay()); + LOGGER.info(" Rotation delay in seconds: {}", getDelayInSeconds()); } - if (RotationPolicyEnum.per_requests == rotationPolicy) { - LOGGER.info(" Rotation after requests: {}", requests); + if (RotationPolicyEnum.per_requests == getRotationPolicy()) { + LOGGER.info(" Rotation after requests: {}", getRequests()); } - LOGGER.info(" Algorithms: {}", algorithms); + LOGGER.info(" Algorithms: {}", getAlgorithms()); } else { LOGGER.info("Hash configuration disabled, used only `none` pepper."); } @@ -83,14 +86,22 @@ public class HashingConfig { this.hashStorageType = hashStorageType; } - public long getDelay() { + public String getDelay() { return delay; } - public void setDelay(long delay) { + public void setDelay(String delay) { this.delay = delay; } + public long getDelayInSeconds() { + return delayInSeconds; + } + + public void setDelayInSeconds(long delayInSeconds) { + this.delayInSeconds = delayInSeconds; + } + public int getRequests() { return requests; } diff --git a/src/main/java/io/kamax/mxisd/hash/HashManager.java b/src/main/java/io/kamax/mxisd/hash/HashManager.java index 246ffbf..e475450 100644 --- a/src/main/java/io/kamax/mxisd/hash/HashManager.java +++ b/src/main/java/io/kamax/mxisd/hash/HashManager.java @@ -61,7 +61,7 @@ public class HashManager { this.rotationStrategy = new RotationPerRequests(config.getRequests()); break; case per_seconds: - this.rotationStrategy = new TimeBasedRotation(config.getDelay()); + this.rotationStrategy = new TimeBasedRotation(config.getDelayInSeconds()); break; default: throw new IllegalArgumentException("Unknown rotation type: " + config.getHashStorageType()); diff --git a/src/test/java/io/kamax/mxisd/test/config/DurationDeserializerTest.java b/src/test/java/io/kamax/mxisd/test/config/DurationDeserializerTest.java new file mode 100644 index 0000000..4e40f6d --- /dev/null +++ b/src/test/java/io/kamax/mxisd/test/config/DurationDeserializerTest.java @@ -0,0 +1,20 @@ +package io.kamax.mxisd.test.config; + +import static org.junit.Assert.assertEquals; + +import io.kamax.mxisd.config.DurationDeserializer; +import org.junit.Test; + +public class DurationDeserializerTest { + + @Test + public void durationLoadTest() { + DurationDeserializer deserializer = new DurationDeserializer(); + + assertEquals(4, deserializer.deserialize("4s")); + assertEquals((60 * 60) + 4, deserializer.deserialize("1h 4s")); + assertEquals((2 * 60) + 4, deserializer.deserialize("2m 4s")); + assertEquals((2 * 60 * 60) + (7 * 60) + 4, deserializer.deserialize("2h 7m 4s")); + assertEquals((60 * 60 * 24) + (2 * 60 * 60) + (7 * 60) + 4, deserializer.deserialize("1d 2h 7m 4s")); + } +}