diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Protocol.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Protocol.java index 20653d2dc030710770bcb3869bc27861e0870067..df822dd07a44e9c8f16863685275f7dac4686a81 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Protocol.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/Protocol.java @@ -36,4 +36,6 @@ public interface Protocol { String TYPE_MEMBERSHIP = "Membership"; String TYPE_TRANSACTION = "Transaction"; + + String BMA_API = "BASIC_MERKLED_API"; } diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/elasticsearch/Protocol.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/elasticsearch/Protocol.java new file mode 100644 index 0000000000000000000000000000000000000000..1fdb62aee7a26a295ad3028d9cb2262a1f84e480 --- /dev/null +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/elasticsearch/Protocol.java @@ -0,0 +1,35 @@ +package org.duniter.core.client.model.elasticsearch; + +/* + * #%L + * Duniter4j :: Core Client API + * %% + * Copyright (C) 2014 - 2016 EIS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created by blavenie on 31/03/16. + */ +public interface Protocol { + + String VERSION = "1"; + + String ES_API = "ELASTICSEARCH_API"; + + String CESIUM_PLUS_API = "CESIUM_PLUS_API"; +} diff --git a/duniter4j-es-assembly/src/test/es-home/config/logging.yml b/duniter4j-es-assembly/src/test/es-home/config/logging.yml index 5b11ce00392d57693f5af433ffb128a21424cdb1..077a8d5749a47e97ca2297b2cf3ce4dbe7653306 100644 --- a/duniter4j-es-assembly/src/test/es-home/config/logging.yml +++ b/duniter4j-es-assembly/src/test/es-home/config/logging.yml @@ -21,6 +21,7 @@ logger: org.duniter.elasticsearch: DEBUG duniter : DEBUG + duniter.security : ERROR duniter.user.event : INFO duniter.network.p2p: TRACE diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityController.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityController.java index 69e7ee7e877a3dbb8a1953ace4153a0f0ee54c9b..52c63a783a1fda29db8b765e488b7b23b0fed3d9 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityController.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityController.java @@ -37,7 +37,7 @@ import java.util.*; */ public class RestSecurityController extends AbstractLifecycleComponent<RestSecurityController> { - private static final ESLogger log = ESLoggerFactory.getLogger("security"); + private static final ESLogger log = ESLoggerFactory.getLogger("duniter.security"); private boolean enable; diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityFilter.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityFilter.java index 9b6557d0486e8344a118dcd4abb755c0ac7a917f..64319346cd3f3c68c4453aae10bd6931f6420a6d 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityFilter.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/rest/security/RestSecurityFilter.java @@ -32,9 +32,10 @@ import static org.elasticsearch.rest.RestStatus.FORBIDDEN; public class RestSecurityFilter extends RestFilter { - private static final ESLogger log = ESLoggerFactory.getLogger("security"); + private static final ESLogger log = ESLoggerFactory.getLogger("duniter.security"); private RestSecurityController securityController; + private final boolean debug; @Inject public RestSecurityFilter(PluginSettings pluginSettings, RestController controller, RestSecurityController securityController) { @@ -44,13 +45,14 @@ public class RestSecurityFilter extends RestFilter { controller.registerFilter(this); } this.securityController = securityController; + this.debug = log.isDebugEnabled(); } @Override public void process(RestRequest request, RestChannel channel, RestFilterChain filterChain) throws Exception { if (securityController.isAllow(request)) { - if (log.isDebugEnabled()) { + if (debug) { log.debug(String.format("Allow %s request [%s]", request.method().name(), request.path())); } filterChain.continueProcessing(request, channel); diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/model/Protocol.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/model/Protocol.java new file mode 100644 index 0000000000000000000000000000000000000000..3d5539e649b4b87f468be8a1bcdb9276b2a8613f --- /dev/null +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/model/Protocol.java @@ -0,0 +1,33 @@ +package org.duniter.elasticsearch.gchange.model; + +/* + * #%L + * Duniter4j :: Core Client API + * %% + * Copyright (C) 2014 - 2016 EIS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +/** + * Created by blavenie on 31/03/16. + */ +public interface Protocol { + + String VERSION = "1"; + + String GCHANGE_API = "GCHANGE_API"; +} diff --git a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java index c91568582a394cb915dfbc28738deaf77ed84f6d..a023c322aa579d60dd9417aa2e906eb4cf1a9792 100644 --- a/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java +++ b/duniter4j-es-gchange/src/main/java/org/duniter/elasticsearch/gchange/service/SynchroService.java @@ -25,6 +25,7 @@ package org.duniter.elasticsearch.gchange.service; import org.duniter.core.client.model.local.Peer; import org.duniter.core.service.CryptoService; import org.duniter.elasticsearch.gchange.PluginSettings; +import org.duniter.elasticsearch.gchange.model.Protocol; import org.duniter.elasticsearch.service.AbstractSynchroService; import org.duniter.elasticsearch.service.ServiceLocator; import org.duniter.elasticsearch.threadpool.ThreadPool; @@ -44,7 +45,7 @@ public class SynchroService extends AbstractSynchroService { public void synchronize() { logger.info("Synchronizing data..."); - Peer peer = getPeerFromAPI("GCHANGE API"); + Peer peer = getPeerFromAPI(Protocol.GCHANGE_API); synchronize(peer); } diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java index 2f6e10774d1140209e129e3cc85742a8b921e763..9669ad286ddcde720d4b7f2398a458c025093de7 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/PluginSettings.java @@ -58,18 +58,6 @@ public class PluginSettings extends org.duniter.elasticsearch.PluginSettings { return settings.get("duniter.keyring.sec"); } - public boolean enableDataSync() { - return settings.getAsBoolean("duniter.user.sync.enable", false); - } - - public String getDataSyncHost() { - return settings.get("duniter.user.sync.host", "data.duniter.fr"); - } - - public int getDataSyncPort() { - return settings.getAsInt("duniter.user.sync.port", 80); - } - public String getMailSmtpHost() { return settings.get("duniter.mail.smtp.host", "localhost"); } diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/RestModule.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/RestModule.java index a1945c0f8e9d1d3e839504615bfc21c8b2f73dce..b0eacbd1a99d71ae9f19064eaa5f3a208d3ca147 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/RestModule.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/RestModule.java @@ -40,6 +40,7 @@ public class RestModule extends AbstractModule implements Module { bind(RestUserSettingsIndexAction.class).asEagerSingleton(); bind(RestUserSettingsUpdateAction.class).asEagerSingleton(); bind(RestUserEventMarkAsReadAction.class).asEagerSingleton(); + bind(RestUserEventSearchAction.class).asEagerSingleton(); // History bind(RestHistoryDeleteIndexAction.class).asEagerSingleton(); diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/user/RestUserEventSearchAction.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/user/RestUserEventSearchAction.java new file mode 100644 index 0000000000000000000000000000000000000000..57dc1e7c48f130e4e3c153b02e806efe689807f3 --- /dev/null +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/rest/user/RestUserEventSearchAction.java @@ -0,0 +1,20 @@ +package org.duniter.elasticsearch.user.rest.user; + +import org.duniter.elasticsearch.rest.security.RestSecurityController; +import org.duniter.elasticsearch.user.service.UserEventService; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.rest.RestRequest; + +/** + * Created by blavenie on 13/12/16. + */ +public class RestUserEventSearchAction { + + @Inject + public RestUserEventSearchAction(RestSecurityController securityController) { + securityController.allow(RestRequest.Method.GET, String.format("/%s/%s/_search", UserEventService.INDEX, UserEventService.EVENT_TYPE)); + securityController.allow(RestRequest.Method.POST, String.format("/%s/%s/_search", UserEventService.INDEX, UserEventService.EVENT_TYPE)); + securityController.allow(RestRequest.Method.GET, String.format("/%s/%s/_count", UserEventService.INDEX, UserEventService.EVENT_TYPE)); + securityController.allow(RestRequest.Method.POST, String.format("/%s/%s/_count", UserEventService.INDEX, UserEventService.EVENT_TYPE)); + } +} diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java index 30a9d2ba2fda03f5cdf305e842746674b11ec466..56674f082e126c232db31f0d3d07f9491062b094 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java @@ -204,14 +204,15 @@ public class BlockchainUserEventService extends AbstractService implements Chang Set<String> senders = ImmutableSet.copyOf(tx.getIssuers()); // Received - String sendersString = joinPubkeys(senders, true); + String senderNames = getNamesFromPubkeys(senders, true); + String sendersPubkeys = joinPubkeys(senders, false); Set<String> receivers = new HashSet<>(); for (String output : tx.getOutputs()) { String[] parts = output.split(":"); if (parts.length >= 3 && parts[2].startsWith("SIG(")) { String receiver = parts[2].substring(4, parts[2].length() - 1); if (!senders.contains(receiver) && !receivers.contains(receiver)) { - notifyUserEvent(block, receiver, UserEventCodes.TX_RECEIVED, I18n.n("duniter.user.event.tx.received"), sendersString); + notifyUserEvent(block, receiver, UserEventCodes.TX_RECEIVED, I18n.n("duniter.user.event.tx.received"), senderNames, sendersPubkeys); receivers.add(receiver); } } @@ -219,9 +220,10 @@ public class BlockchainUserEventService extends AbstractService implements Chang // Sent if (CollectionUtils.isNotEmpty(receivers)) { - String receiverStr = joinPubkeys(receivers, true); + String receiverNames = getNamesFromPubkeys(receivers, true); + String receiverPubkeys = joinPubkeys(receivers, false); for (String sender : senders) { - notifyUserEvent(block, sender, UserEventCodes.TX_SENT, I18n.n("duniter.user.event.tx.sent"), receiverStr); + notifyUserEvent(block, sender, UserEventCodes.TX_SENT, I18n.n("duniter.user.event.tx.sent"), receiverNames, receiverPubkeys); } } @@ -247,7 +249,7 @@ public class BlockchainUserEventService extends AbstractService implements Chang userEventService.deleteEventsByReference(new UserEvent.Reference(change.getIndex(), change.getType(), change.getId())); } - private String joinPubkeys(Set<String> pubkeys, boolean minify) { + private String getNamesFromPubkeys(Set<String> pubkeys, boolean minify) { Preconditions.checkNotNull(pubkeys); Preconditions.checkArgument(pubkeys.size()>0); if (pubkeys.size() == 1) { @@ -268,4 +270,21 @@ public class BlockchainUserEventService extends AbstractService implements Chang return sb.substring(DEFAULT_PUBKEYS_SEPARATOR.length()); } + + private String joinPubkeys(Set<String> pubkeys, boolean minify) { + Preconditions.checkNotNull(pubkeys); + Preconditions.checkArgument(pubkeys.size()>0); + if (pubkeys.size() == 1) { + String pubkey = pubkeys.iterator().next(); + return (minify ? ModelUtils.minifyPubkey(pubkey) : pubkey); + } + + StringBuilder sb = new StringBuilder(); + pubkeys.stream().forEach((pubkey)-> { + sb.append(DEFAULT_PUBKEYS_SEPARATOR); + sb.append(minify ? ModelUtils.minifyPubkey(pubkey) : pubkey); + }); + + return sb.substring(DEFAULT_PUBKEYS_SEPARATOR.length()); + } } diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java index 155e9c56fcc07bd7f036badf27405bde2d49f60a..263f0af0f450be0ad831852d78fb0bf16df6ebbd 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/SynchroService.java @@ -22,6 +22,7 @@ package org.duniter.elasticsearch.user.service; * #L% */ +import org.duniter.core.client.model.elasticsearch.Protocol; import org.duniter.core.client.model.local.Peer; import org.duniter.core.service.CryptoService; import org.duniter.elasticsearch.PluginSettings; @@ -45,7 +46,7 @@ public class SynchroService extends AbstractSynchroService { public void synchronize() { logger.info("Synchronizing user data..."); - Peer peer = getPeerFromAPI("ES API"); + Peer peer = getPeerFromAPI(Protocol.ES_API); synchronize(peer); } @@ -71,5 +72,6 @@ public class SynchroService extends AbstractSynchroService { protected void importMessageChanges(Peer peer, long sinceTime) { importChanges(peer, MessageService.INDEX, MessageService.RECORD_TYPE, sinceTime); + importChanges(peer, MessageService.INDEX, MessageService.OUTBOX_TYPE, sinceTime); } }