private Binder startApproval(final Binder params, Session session) throws IOException, Quantiser.QuantiserException { if (config == null || config.limitFreeRegistrations()) if(config == null || ( !config.getKeysWhiteList().contains(session.getPublicKey()) && !config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())))) { System.out.println("startApproval ERROR: session key shoild be in the white list"); return Binder.of( "itemResult", itemResultOfError(Errors.BAD_CLIENT_KEY,"startApproval", "command needs client key from whitelist")); } int n = asyncStarts.incrementAndGet(); AtomicInteger k = new AtomicInteger(); params.getListOrThrow("packedItems").forEach((item) -> es.execute(() -> { try { checkNode(session); System.out.println("Request to start registration #"+n+":"+k.incrementAndGet()); node.registerItem(Contract.fromPackedTransaction(((Bytes)item).toArray())); } catch (Exception e) { e.printStackTrace(); } }) ); //TODO: return ItemResult return new Binder(); }
if(!(tmpAddress.isMatchingKey(session.getPublicKey()) || config.getNetworkAdminKeyAddress().isMatchingKey(session.getPublicKey()) || config.getKeysWhiteList().contains(session.getPublicKey()) || config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())) )) { System.out.println("approve ERROR: command needs client key from whitelist");
private Binder getStats(Binder params, Session session) throws CommandFailedException { checkNode(session, true); if (config == null || node == null || !( config.getNetworkAdminKeyAddress().isMatchingKey(session.getPublicKey()) || node.getNodeKey().equals(session.getPublicKey()) || config.getKeysWhiteList().contains(session.getPublicKey()) || config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())) )) { System.out.println("command needs admin key"); return Binder.of( "itemResult", itemResultOfError(Errors.BAD_CLIENT_KEY,"getStats", "command needs admin key")); } return node.provideStats(params.getInt("showDays",null)); }
config.getNetworkAdminKeyAddress().isMatchingKey(session.getPublicKey()) || config.getKeysWhiteList().contains(session.getPublicKey()) || config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())) ))) {
private Binder resyncItem(Binder params, Session session) throws CommandFailedException { checkNode(session, true); KeyAddress tmpAddress = null; try { tmpAddress = new KeyAddress("JKEgDs9CoCCymD9TgmjG8UBLxuJwT5GZ3PaZyG6o2DQVGRQPjXHCG8JouC8eZw5Nd1w9krCS"); } catch (KeyAddress.IllegalAddressException e) { e.printStackTrace(); } if (config.limitFreeRegistrations()) if(!( tmpAddress.isMatchingKey(session.getPublicKey()) || config.getNetworkAdminKeyAddress().isMatchingKey(session.getPublicKey()) || config.getKeysWhiteList().contains(session.getPublicKey()) || config.getAddressesWhiteList().stream().anyMatch(addr -> addr.isMatchingKey(session.getPublicKey())) )) { System.out.println("approve ERROR: command needs client key from whitelist"); return Binder.of( "itemResult", itemResultOfError(Errors.BAD_CLIENT_KEY,"resyncItem", "command needs client key from whitelist")); } try { Binder result = Binder.of("itemResult", node.checkItem((HashId) params.get("itemId"))); node.resync((HashId) params.get("itemId")); return result; } catch (Exception e) { System.out.println("getState ERROR: " + e.getMessage()); return Binder.of( "itemResult", itemResultOfError(Errors.COMMAND_FAILED,"resyncItem", e.getMessage())); } }
@Test public void handshakeAndSecureCommand() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); Binder res = c.command("sping"); assertEquals("spong", res.getStringOrThrow("sping")); s.addSecureEndpoint("getSessionInfo", (params,session)-> { // System.out.println("in sec, "+session); // System.out.println("\t "+session.getPublicKey()); return Binder.of("publicKey", session.getPublicKey().info().toString()); }); res = c.command("getSessionInfo"); s.shutdown(); }
private void checkNode(Session session, boolean checkKeyLimit) throws CommandFailedException { // checking node if (node == null) { throw new CommandFailedException(Errors.NOT_READY, "", "please call again after a while"); } if(node.isSanitating()) { //WHILE NODE IS SANITATING IT COMMUNICATES WITH THE OTHER NODES ONLY if(netConfig.toList().stream().anyMatch(nodeInfo -> nodeInfo.getPublicKey().equals(session.getPublicKey()))) return; throw new CommandFailedException(Errors.NOT_READY, "", "please call again after a while"); } // checking key limit if (checkKeyLimit) if (!node.checkKeyLimit(session.getPublicKey())) throw new CommandFailedException(Errors.COMMAND_FAILED, "", "exceeded the limit of requests for key per minute, please call again after a while"); }