From 83feb6ad13187af9be55cc71408611f67d280955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 21:28:10 +0100 Subject: Fix timestamps and ValueQuery --- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 10 +++++----- .../cloudatlas/agent/modules/GossipGirlState.java | 10 +++++----- .../edu/mimuw/cloudatlas/agent/modules/Qurnik.java | 5 +++-- .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 20 +++++++++++--------- 4 files changed, 24 insertions(+), 21 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index 5199e82..b6087e6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -200,11 +200,11 @@ public class GossipGirl extends Module { System.out.println("INFO: handling Query in " + Long.toString(message.getReceiverGossipId())); state.setLastAction(); state.gotQuery(message); - Map> queries = new HashMap(); - queries.put( - message.getName(), - new SimpleImmutableEntry(message.getQuery(), state.getTheirQueryTimestamp(message.getName())) - ); + Map queries = new HashMap(); + ValueQuery vq = message.getQuery(); + ValueTime timestamp = state.getTheirQueryTimestamp(message.getName()); + vq.setTimestamp(timestamp.getValue()); + queries.put(message.getName(), vq); UpdateQueriesMessage updateMessage = new UpdateQueriesMessage("", 0, queries); System.out.println("INFO: GossipGirl sending UpdateQueriesMessage"); sendMessage(updateMessage); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java index cfaf560..4709eb1 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java @@ -47,7 +47,7 @@ public class GossipGirlState { public long timeOffest; public State state; public ZMI hierarchy; - public Map> queries; + public Map queries; public ValueTime hejkaSendTimestamp; public ValueTime hejkaReceiveTimestamp; public ValueTime noCoTamSendTimestamp; @@ -79,7 +79,7 @@ public class GossipGirlState { lastAction = ValueUtils.currentTime(); } - public void setState(ZMI hierarchy, Map> queries) { + public void setState(ZMI hierarchy, Map queries) { switch (state) { case WAIT_FOR_STATE_INITIALIZER: this.hierarchy = hierarchy; @@ -211,8 +211,8 @@ public class GossipGirlState { public Map getQueryTimestampsToSend() { Map queryTimestamps= new HashMap(); - for (Entry> query : queries.entrySet()) { - queryTimestamps.put(query.getKey(), query.getValue().getValue()); + for (Entry query : queries.entrySet()) { + queryTimestamps.put(query.getKey(), new ValueTime(query.getValue().getTimestamp())); } return queryTimestamps; @@ -259,7 +259,7 @@ public class GossipGirlState { queryList.add( new SimpleImmutableEntry( name, - queries.get(name).getKey() + queries.get(name) ) ); } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java index 2119653..9a544d9 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java @@ -59,13 +59,14 @@ public class Qurnik extends Module { private void runQueriesOnState(StateMessage message) throws InterruptedException { List queries = new LinkedList(); - for (Entry timestampedQuery : message.getQueries().values()) { - queries.add(timestampedQuery.getKey()); + for (ValueQuery timestampedQuery : message.getQueries().values()) { + queries.add(timestampedQuery); } executeAllQueries(message.getZMI(), queries, PathName.ROOT); } private void executeAllQueries(ZMI zmi, List queries, PathName currentPath) throws InterruptedException { + System.out.println("INFO: Qurnik executing all queries " + queries); if(!zmi.getSons().isEmpty()) { for(ZMI son : zmi.getSons()) { Value sonName = son.getAttributes().getOrNull("name"); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 6e7d4dc..9e2a603 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -16,7 +16,7 @@ public class Stanik extends Module { } private ZMI hierarchy; - private HashMap> queries; + private HashMap queries; private long freshnessPeriod; private Set contacts; private ValueTime contactsTimestamp; @@ -26,7 +26,7 @@ public class Stanik extends Module { super(ModuleType.STATE); this.ourPath = ourPath; hierarchy = new ZMI(); - queries = new HashMap>(); + queries = new HashMap(); hierarchy.getAttributes().add("timestamp", new ValueTime(0l)); this.freshnessPeriod = freshnessPeriod; this.contactsTimestamp = ValueUtils.currentTime(); @@ -45,7 +45,7 @@ public class Stanik extends Module { private void setDefaultQuery(String name, String query) { try { ValueQuery queryValue = new ValueQuery(query); - queries.put(new Attribute(name), new SimpleImmutableEntry(queryValue, new ValueTime(0l))); + queries.put(new Attribute(name), queryValue); } catch (Exception e) { System.out.println("ERROR: failed to compile default query"); } @@ -89,7 +89,7 @@ public class Stanik extends Module { 0, message.getRequestId(), hierarchy.clone(), - (HashMap>) queries.clone(), + (HashMap) queries.clone(), contacts ); sendMessage(response); @@ -210,11 +210,13 @@ public class Stanik extends Module { } public void handleUpdateQueries(UpdateQueriesMessage message) { - for (Entry> entry : message.getQueries().entrySet()) { + System.out.println("INFO: Stanik handles update queries"); + for (Entry entry : message.getQueries().entrySet()) { Attribute attribute = entry.getKey(); - ValueTime timestamp = entry.getValue().getValue(); - Entry currentTimestampedQuery = queries.get(attribute); - if (currentTimestampedQuery == null || ValueUtils.valueLower(currentTimestampedQuery.getValue(), timestamp)) { + ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); + ValueQuery currentTimestampedQuery = queries.get(attribute); + ValueTime currentQueryTimestamp = new ValueTime(currentTimestampedQuery.getTimestamp()); + if (currentQueryTimestamp == null || ValueUtils.valueLower(currentQueryTimestamp, timestamp)) { queries.put(entry.getKey(), entry.getValue()); } } @@ -273,7 +275,7 @@ public class Stanik extends Module { return hierarchy; } - public HashMap> getQueries() { + public HashMap getQueries() { return queries; } -- cgit v1.2.3 From c4513b4817ec623d026fa35682b4934a211e1b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 21:54:11 +0100 Subject: Some small fixes --- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 9e2a603..79728b5 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -215,8 +215,8 @@ public class Stanik extends Module { Attribute attribute = entry.getKey(); ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); ValueQuery currentTimestampedQuery = queries.get(attribute); - ValueTime currentQueryTimestamp = new ValueTime(currentTimestampedQuery.getTimestamp()); - if (currentQueryTimestamp == null || ValueUtils.valueLower(currentQueryTimestamp, timestamp)) { + if (currentTimestampedQuery == null || + ValueUtils.valueLower(new ValueTime(currentTimestampedQuery.getTimestamp()), timestamp)) { queries.put(entry.getKey(), entry.getValue()); } } -- cgit v1.2.3 From 9681d76b15cc68f2f8246972ae8266522b92fefe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 22:33:17 +0100 Subject: Add query installation check --- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java | 2 -- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index b6087e6..5137e0b 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -202,8 +202,6 @@ public class GossipGirl extends Module { state.gotQuery(message); Map queries = new HashMap(); ValueQuery vq = message.getQuery(); - ValueTime timestamp = state.getTheirQueryTimestamp(message.getName()); - vq.setTimestamp(timestamp.getValue()); queries.put(message.getName(), vq); UpdateQueriesMessage updateMessage = new UpdateQueriesMessage("", 0, queries); System.out.println("INFO: GossipGirl sending UpdateQueriesMessage"); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java index 9a544d9..c94a87d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java @@ -66,7 +66,6 @@ public class Qurnik extends Module { } private void executeAllQueries(ZMI zmi, List queries, PathName currentPath) throws InterruptedException { - System.out.println("INFO: Qurnik executing all queries " + queries); if(!zmi.getSons().isEmpty()) { for(ZMI son : zmi.getSons()) { Value sonName = son.getAttributes().getOrNull("name"); @@ -81,7 +80,7 @@ public class Qurnik extends Module { Interpreter interpreter = new Interpreter(zmi); AttributesMap newAttributes = new AttributesMap(); for (ValueQuery query : queries) { - if (query != null) { + if (query != null && query.isInstalled()) { try { List result = interpreter.interpretProgram(query.getQuery()); for(QueryResult r : result) { -- cgit v1.2.3 From 93cafafbc6fe53628be1b4ef08b6d9ca48ec6bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Tue, 14 Jan 2020 22:50:10 +0100 Subject: Add validation in Stanik --- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 1 + .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 31 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index 5137e0b..a952274 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -26,6 +26,7 @@ import pl.edu.mimuw.cloudatlas.model.ValueContact; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueTime; import pl.edu.mimuw.cloudatlas.model.ZMI; +import pl.edu.mimuw.cloudatlas.querysigner.QuerySignerApiImplementation; public class GossipGirl extends Module { private long nextGossipId = 0; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 79728b5..efc5605 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -1,12 +1,21 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import java.nio.file.Path; +import java.rmi.RemoteException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; import java.util.*; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.*; import pl.edu.mimuw.cloudatlas.model.*; +import pl.edu.mimuw.cloudatlas.querysigner.*; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; public class Stanik extends Module { private class InvalidUpdateAttributesMessage extends Exception { @@ -21,6 +30,7 @@ public class Stanik extends Module { private Set contacts; private ValueTime contactsTimestamp; private PathName ourPath; + private PublicKey publicKey; public Stanik(PathName ourPath, long freshnessPeriod) { super(ModuleType.STATE); @@ -31,6 +41,8 @@ public class Stanik extends Module { this.freshnessPeriod = freshnessPeriod; this.contactsTimestamp = ValueUtils.currentTime(); this.contacts = new HashSet<>(); + String publicKeyFile = System.getProperty("public_key_file"); + this.publicKey = KeyUtils.getPublicKey(publicKeyFile); setDefaultQueries(); } @@ -213,6 +225,25 @@ public class Stanik extends Module { System.out.println("INFO: Stanik handles update queries"); for (Entry entry : message.getQueries().entrySet()) { Attribute attribute = entry.getKey(); + ValueQuery query = entry.getValue(); + try { + if (query.isInstalled()) { + QuerySignerApiImplementation.validateInstallQuery( + attribute.getName(), + QueryUtils.constructQueryData(query), + this.publicKey); + + } else { + QuerySignerApiImplementation.validateUninstallQuery( + attribute.getName(), + QueryUtils.constructQueryData(query), + this.publicKey); + } + } catch (RemoteException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | QuerySigner.InvalidQueryException e) { + System.out.println("ERROR: Query " + attribute.getName() + " was not updated in Stanik with error message " + e.getMessage()); + e.printStackTrace(); + continue; + } ValueTime timestamp = new ValueTime(entry.getValue().getTimestamp()); ValueQuery currentTimestampedQuery = queries.get(attribute); if (currentTimestampedQuery == null || -- cgit v1.2.3