@Override public void start() throws Exception { // Create the client object ProcessorService service = new ProcessorServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("vertx.processor") .register(ProcessorService.class, service); Router router = Router.router(vertx); // Allow events for the designated addresses in/out of the event bus bridge BridgeOptions opts = new BridgeOptions() .addInboundPermitted(new PermittedOptions().setAddress("vertx.processor")) .addOutboundPermitted(new PermittedOptions().setAddress("vertx.processor")); // Create the event bus bridge and add it to the router. SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts); router.route("/eventbus/*").handler(ebHandler); router.route().handler(StaticHandler.create()); // vertx.createHttpServer().requestHandler(router).listen(8080); }
@Test public void testPermittedOptions() { PermittedOptions options = new PermittedOptions(); assertEquals(PermittedOptions.DEFAULT_ADDRESS, options.getAddress()); assertEquals(PermittedOptions.DEFAULT_ADDRESS_REGEX, options.getAddressRegex()); assertEquals(PermittedOptions.DEFAULT_REQUIRED_AUTHORITY, options.getRequiredAuthority()); assertEquals(PermittedOptions.DEFAULT_MATCH, options.getMatch()); String address = TestUtils.randomAlphaString(10); String addressRegex = TestUtils.randomAlphaString(10); String requiredAuthority = TestUtils.randomAlphaString(10); JsonObject match = new JsonObject().put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(10)); assertSame(options, options.setAddress(address)); assertSame(options, options.setAddressRegex(addressRegex)); assertSame(options, options.setRequiredAuthority(requiredAuthority)); assertSame(options, options.setMatch(match)); assertEquals(address, options.getAddress()); assertEquals(addressRegex, options.getAddressRegex()); assertEquals(requiredAuthority, options.getRequiredAuthority()); assertEquals(match, options.getMatch()); PermittedOptions copy = new PermittedOptions(options); assertEquals(address, copy.getAddress()); assertEquals(addressRegex, copy.getAddressRegex()); assertEquals(requiredAuthority, copy.getRequiredAuthority()); assertEquals(match, copy.getMatch()); assertSame(copy, copy.setAddress(TestUtils.randomAlphaString(10))); assertSame(copy, copy.setAddressRegex(TestUtils.randomAlphaString(10))); assertSame(copy, copy.setRequiredAuthority(TestUtils.randomAlphaString(10))); assertSame(copy, copy.setMatch(new JsonObject().put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(10)))); assertSame(options, options.setAddress(address)); assertSame(options, options.setAddressRegex(addressRegex));
private Match checkMatches(boolean inbound, String address, Object body) { List<PermittedOptions> matches = inbound ? inboundPermitted : outboundPermitted; for (PermittedOptions matchHolder : matches) { String matchAddress = matchHolder.getAddress(); String matchRegex; if (matchAddress == null) { matchRegex = matchHolder.getAddressRegex(); } else { matchRegex = null; } boolean addressOK; if (matchAddress == null) { addressOK = matchRegex == null || regexMatches(matchRegex, address); } else { addressOK = matchAddress.equals(address); } if (addressOK) { boolean matched = structureMatches(matchHolder.getMatch(), body); if (matched) { String requiredAuthority = matchHolder.getRequiredAuthority(); return new Match(true, requiredAuthority); } } } return new Match(false); }
@Test public void testSendRequiresAuthorityNotLoggedIn() throws Exception { sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("admin"))); testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "not_logged_in"); }
@Test public void testSendPermittedMultipleAddressRe() throws Exception { String addr1 = "allo.+"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddressRegex(addr1)). addInboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testSend("allow1", "foobar"); testSend("allow2", "foobar"); testSend("ballow1", "foobar"); testSend("ballow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedMixedAddressRe() throws Exception { String addr1 = "allow1"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddress(addr1)). addOutboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testReceive("allow1", "foobar"); testReceive("ballow1", "foobar"); testReceive("ballow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"), "access_denied"); testError(new JsonObject().put("type", "register").put("address", "allow2").put("body", "blah"), "access_denied"); }
@Test public void testSendPermittedStructureMatchWithAddress() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setMatch(match).setAddress(addr))); testSend(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testSend(addr, json1); json1.remove("fib"); testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1), "access_denied"); testError(new JsonObject().put("type", "send").put("address", "otheraddress").put("body", json1), "access_denied"); }
@Test public void testSendPermittedStructureMatch() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setMatch(match))); testSend(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testSend(addr, json1); json1.remove("fib"); testError(new JsonObject().put("type", "send").put("address", addr).put("body", json1), "access_denied"); }
if (obj instanceof JsonObject) { JsonObject jobj = (JsonObject)obj; inboundPermitted.add(new PermittedOptions(jobj)); } else { throw new IllegalArgumentException("Invalid type " + obj.getClass() + " in inboundPermitteds array"); if (obj instanceof JsonObject) { JsonObject jobj = (JsonObject)obj; outboundPermitted.add(new PermittedOptions(jobj)); } else { throw new IllegalArgumentException("Invalid type " + obj.getClass() + " in outboundPermitteds array");
private boolean checkMatches(boolean inbound, String address, Object body) { List<PermittedOptions> matches = inbound ? options.getInboundPermitteds() : options.getOutboundPermitteds(); for (PermittedOptions matchHolder : matches) { String matchAddress = matchHolder.getAddress(); String matchRegex; if (matchAddress == null) { matchRegex = matchHolder.getAddressRegex(); } else { matchRegex = null; } boolean addressOK; if (matchAddress == null) { addressOK = matchRegex == null || regexMatches(matchRegex, address); } else { addressOK = matchAddress.equals(address); } if (addressOK) { return structureMatches(matchHolder.getMatch(), body); } } return false; }
private boolean checkMatches(boolean inbound, String address, Map<String, Message<JsonObject>> replies) { // special case, when dealing with replies the addresses are not in the inbound/outbound list but on // the replies registry if (replies != null && inbound && replies.containsKey(address)) { return true; } List<PermittedOptions> matches = inbound ? options.getInboundPermitteds() : options.getOutboundPermitteds(); for (PermittedOptions matchHolder : matches) { String matchAddress = matchHolder.getAddress(); String matchRegex; if (matchAddress == null) { matchRegex = matchHolder.getAddressRegex(); } else { matchRegex = null; } boolean addressOK; if (matchAddress == null) { addressOK = matchRegex == null || regexMatches(matchRegex, address); } else { addressOK = matchAddress.equals(address); } if (addressOK) { return true; } } return false; }
@Test public void testPermittedOptionsJson() { String address = TestUtils.randomAlphaString(10); String addressRegex = TestUtils.randomAlphaString(10); String requiredAuthority = TestUtils.randomAlphaString(10); JsonObject match = new JsonObject().put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(10)); JsonObject json = new JsonObject(). put("address", address). put("addressRegex", addressRegex). put("requiredAuthority", requiredAuthority). put("match", match); PermittedOptions options = new PermittedOptions(json); assertEquals(address, options.getAddress()); assertEquals(addressRegex, options.getAddressRegex()); assertEquals(requiredAuthority, options.getRequiredAuthority()); assertEquals(match, options.getMatch()); }
@Test public void testSendRequiresAuthorityHasnotAuthority() throws Exception { sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("pick_nose"))); router.clear(); router.route().handler(CookieHandler.create()); SessionStore store = LocalSessionStore.create(vertx); router.route().handler(SessionHandler.create(store)); JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties"); AuthProvider authProvider = ShiroAuth.create(vertx, new ShiroAuthOptions().setType(ShiroAuthRealmType.PROPERTIES).setConfig(authConfig)); addLoginHandler(router, authProvider); router.route("/eventbus/*").handler(sockJSHandler); testError(new JsonObject().put("type", "send").put("address", addr).put("body", "foo"), "access_denied"); }
@Test public void testRegisterPermittedMultipleAddressRe() throws Exception { String addr1 = "allo.+"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setAddressRegex(addr1)). addOutboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testReceive("allow1", "foobar"); testReceive("allow2", "foobar"); testReceive("ballow1", "foobar"); testReceive("ballow2", "foobar"); testError(new JsonObject().put("type", "register").put("address", "hello").put("body", "blah"), "access_denied"); }
@Test public void testSendPermittedMixedAddressRe() throws Exception { String addr1 = "allow1"; String addr2 = "ballo.+"; sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr1)). addInboundPermitted(new PermittedOptions().setAddressRegex(addr2))); testSend("allow1", "foobar"); testSend("ballow1", "foobar"); testSend("ballow2", "foobar"); testError(new JsonObject().put("type", "send").put("address", "hello").put("body", "blah"), "access_denied"); testError(new JsonObject().put("type", "send").put("address", "allow2").put("body", "blah"), "access_denied"); }
@Test public void testRegisterPermittedStructureMatchWithAddress() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setMatch(match).setAddress(addr))); testReceive(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testReceive(addr, json1); JsonObject json2 = json1.copy(); json2.remove("fib"); testReceiveFail(addr, json2); }
@Test public void testRegisterPermittedStructureMatch() throws Exception { JsonObject match = new JsonObject().put("fib", "wib").put("oop", 12); sockJSHandler.bridge(defaultOptions.addOutboundPermitted(new PermittedOptions().setMatch(match))); testReceive(addr, match); JsonObject json1 = match.copy(); json1.put("blah", "foob"); testReceive(addr, json1); JsonObject json2 = json1.copy(); json2.remove("fib"); testReceiveFail(addr, json2); }
if (obj instanceof JsonObject) { JsonObject jobj = (JsonObject)obj; inboundPermitted.add(new PermittedOptions(jobj)); } else { throw new IllegalArgumentException("Invalid type " + obj.getClass() + " in inboundPermitteds array"); if (obj instanceof JsonObject) { JsonObject jobj = (JsonObject)obj; outboundPermitted.add(new PermittedOptions(jobj)); } else { throw new IllegalArgumentException("Invalid type " + obj.getClass() + " in outboundPermitteds array");
private boolean checkMatches(boolean inbound, String address, Map<String, Message<JsonObject>> replies) { // special case, when dealing with replies the addresses are not in the inbound/outbound list but on // the replies registry if (replies != null && inbound && replies.containsKey(address)) { return true; } List<PermittedOptions> matches = inbound ? options.getInboundPermitteds() : options.getOutboundPermitteds(); for (PermittedOptions matchHolder : matches) { String matchAddress = matchHolder.getAddress(); String matchRegex; if (matchAddress == null) { matchRegex = matchHolder.getAddressRegex(); } else { matchRegex = null; } boolean addressOK; if (matchAddress == null) { addressOK = matchRegex == null || regexMatches(matchRegex, address); } else { addressOK = matchAddress.equals(address); } if (addressOK) { return true; } } return false; }
@Override public void start() throws Exception { // Create the client object MyService service = new MyServiceImpl(); // Register the handler new ServiceBinder(vertx) .setAddress("proxy.example") .register(MyService.class, service); Router router = Router.router(vertx); BridgeOptions options = new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("proxy.example")); router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options)); // Serve the static resources router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); } }