public UiNgResourceForwarding(String sourcePath, String targetPath, boolean redirect) { this.sourcePath = sourcePath; this.targetPath = targetPath; this.redirect = redirect; setSelfLink(sourcePath); this.options.add(ServiceOption.URI_NAMESPACE_OWNER); }
@Override public void handleGet(Operation get) { if (isEmbedded == null) { // ConfigurationUtil.getConfigProperty(this, ConfigurationUtil.EMBEDDED_MODE_PROPERTY, // (embedded) -> { // isEmbedded = Boolean.valueOf(embedded); // handleGet(get); // }); // return; // TODO - the code above should be used instead but for some reason the UI components // may get initialized before the configuration service is fully initialized. isEmbedded = ConfigurationUtil.isEmbedded(); } if (!isEmbedded) { // Avoid clickjacking attacks, by ensuring that content is not embedded. get.addResponseHeader(ConfigurationUtil.UI_FRAME_OPTIONS_HEADER, "DENY"); } if (redirectToLoginOrIndex(getHost(), get)) { return; } super.handleGet(get); }
protected void startForwardingService(String sourcePath, String targetPath) { Operation post = Operation .createPost(UriUtils.buildUri(getHost(), sourcePath)); UiNgResourceForwarding rf = new UiNgResourceForwarding(sourcePath, targetPath); getHost().startService(post, rf); }
@Test public void testRedirectToBase() { UiService service = new UiService(); service.setSelfLink("/"); VerificationHost vh = new VerificationHost(); vh.setAuthorizationEnabled(true); vh.setAuthorizationContext(createAuthorizationContext(true)); service.setHost(vh); AtomicBoolean completionCalled = new AtomicBoolean(); service.handleGet(new Operation().setUri(UriUtils.buildUri("http://localhost/login/")) .setCompletion((o, e) -> { assertEquals("/", o.getResponseHeader(Operation.LOCATION_HEADER)); completionCalled.set(true); })); assertTrue(completionCalled.get()); }
private void startExternalContainerImageIconResourceServices(String resourcesPath) { Path iconResourcePath = Paths.get(resourcesPath, ManagementUriParts.CONTAINER_ICONS_RESOURCE_PATH); if (!iconResourcePath.toFile().exists()) { logInfo("Skip loading container icons from user resource path"); return; } else { logInfo("Loading container icons from user resource path"); } List<File> files = FileUtils.findFiles(iconResourcePath, new HashSet<>(), false); for (File f : files) { String subPath = f.getAbsolutePath().replace( iconResourcePath.toAbsolutePath().toString(), ""); Path servicePath = Paths .get(ManagementUriParts.CONTAINER_ICONS_RESOURCE_PATH.substring(1), subPath); String servicePathString = FileUtil.getForwardSlashesPathString(servicePath); Operation post = Operation .createPost(UriUtils.buildUri(getHost(), servicePathString)); RestrictiveFileContentService fcs = new RestrictiveFileContentService(f); getHost().startService(post, fcs); } } }
protected Map<Path, String> discoverUiResources(Path path, Service s) throws Throwable { Map<Path, String> pathToURIPath = new HashMap<>(); Path baseUriPath = Paths.get(getSelfLink()); String prefix = path.toString().replace('\\', '/'); if (getHost().getState().resourceSandboxFileReference != null) { discoverFileResources(s, pathToURIPath, baseUriPath, prefix); } if (pathToURIPath.isEmpty()) { discoverJarResources(path, s, pathToURIPath, baseUriPath, prefix); } return pathToURIPath; }
@Test public void testDiscoverFileResources() throws Throwable { UiService service = new UiService(); service.setSelfLink("/"); Arguments args = new Arguments(); args.resourceSandbox = Paths .get("src/main/resources/ui/com/vmware/admiral/UiService/container-identicons"); VerificationHost vh = VerificationHost.create(args); service.setHost(vh); Map<Path, String> discoverUiResources = service.discoverUiResources( Paths.get("./"), service); assertFalse(discoverUiResources.isEmpty()); }
@Test public void testDiscoverFileResourcesOnStart() throws Throwable { UiService service = new UiService(); service.setSelfLink("/"); Arguments args = new Arguments(); args.resourceSandbox = Paths.get("src/main/resources/"); VerificationHost vh = VerificationHost.create(args); service.setHost(vh); Operation start = new Operation().setUri(UriUtils.buildUri("/")); service.handleStart(start); assertEquals(Operation.STATUS_CODE_OK, start.getStatusCode()); }
@Test public void testRedirect() { UiNgResourceForwarding service = new UiNgResourceForwarding("/sample", "/sample-new", true); service.setHost(VerificationHost.create()); AtomicBoolean completionCalled = new AtomicBoolean(); service.handleGet(new Operation().setUri(UriUtils.buildUri("http://localhost/sample")) .setCompletion((o, e) -> { assertEquals("/sample-new", o.getResponseHeader(Operation.LOCATION_HEADER)); completionCalled.set(true); })); assertTrue(completionCalled.get()); }
@Override public void handleStart(Operation startPost) { super.handleStart(startPost); startForwardingService(UriUtils.buildUriPath(SELF_LINK, "assets/i18n/base"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "messages/admiral")); startForwardingService(UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH, "assets/i18n/base"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "messages/admiral")); startForwardingService(UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH, "assets/i18n"), UriUtils.buildUriPath(SELF_LINK, "assets/i18n")); String loginServicePath = UriUtils.buildUriPath(SELF_LINK, LOGIN_PATH); if (!loginServicePath.endsWith(UriUtils.URI_PATH_CHAR)) { loginServicePath += UriUtils.URI_PATH_CHAR; } String uiServicePath = ManagementUriParts.UI_SERVICE; if (!uiServicePath.endsWith(UriUtils.URI_PATH_CHAR)) { uiServicePath += UriUtils.URI_PATH_CHAR; } startForwardingService(loginServicePath, uiServicePath); // TODO: remove after new UI is active in CS startRedirectService(UriUtils.buildUriPath(SELF_LINK, "/index-embedded.html"), UriUtils.buildUriPath(ManagementUriParts.UI_OG_SERVICE, "/index-embedded.html")); }
@Override public void handleStart(Operation startOp) { sendRequest(Operation .createGet(getHost(), UriUtils.buildUriPath(ManagementUriParts.CONFIG_PROPS, FileUtil.USER_RESOURCES_PATH_VARIABLE)) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY) .setCompletion((res, ex) -> { if (ex == null && res.hasBody()) { ConfigurationState body = res.getBody(ConfigurationState.class); if (body.value != null && !body.value.isEmpty()) { String userResourcesPath = body.value; startExternalContainerImageIconResourceServices(userResourcesPath); } } startOp.complete(); })); }
@Override public void handleGet(Operation get) { if (redirectToLoginOrIndex(getHost(), get)) { return; operation.setUri(UriUtils.buildUri(getHost(), uriPath)) .setCompletion((o, e) -> { get.setBody(o.getBodyRaw()) sendRequest(operation);
@Test public void testAuthorizeRequest() { UiNgResourceForwarding service = new UiNgResourceForwarding("/sample", "/sample-new"); AtomicBoolean completionCalled = new AtomicBoolean(); service.authorizeRequest(new Operation().setCompletion((o, e) -> { completionCalled.set(true); })); assertTrue(completionCalled.get()); }
@Test public void testNgRedirect() { UiNgService service = new UiNgService(); AtomicBoolean completionCalled = new AtomicBoolean(); service.handleGet(new Operation().setUri(UriUtils.buildUri("http://localhost/sample")) .setCompletion((o, e) -> { assertEquals("../", o.getResponseHeader(Operation.LOCATION_HEADER)); completionCalled.set(true); })); assertTrue(completionCalled.get()); }
@Override public void handleStart(Operation startPost) { try { startUiFileContentServices(); super.handleStart(startPost); } catch (Throwable e) { startPost.fail(e); } }
private void discoverJarResources(Path path, Service s, Map<Path, String> pathToURIPath, Path baseUriPath, String prefix) throws URISyntaxException, IOException { for (ResourceEntry entry : FileUtils.findResources(s.getClass(), prefix)) { Path resourcePath = path.resolve(entry.suffix); Path uriPath = baseUriPath.resolve(entry.suffix); Path outputPath = getHost().copyResourceToSandbox(entry.url, resourcePath); if (outputPath == null) { // Failed to copy one resource, disable user interface for this service. s.toggleOption(ServiceOption.HTML_USER_INTERFACE, false); } else { pathToURIPath.put(outputPath, uriPath.toString().replace('\\', '/')); } } }
private void getIcon(String path, Operation get, Runnable notFoundHandler) { Operation getOp = Operation.createGet(this, path) .setCompletion((op, ex) -> { if (op.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND && notFoundHandler != null) { notFoundHandler.run(); } else if (ex != null) { get.fail(ex); } else { get.transferResponseHeadersFrom(op); get.getResponseHeaders().put(Operation.CONTENT_TYPE_HEADER, op.getContentType()); get.getResponseHeaders().put(CACHE_CONTROL_HEADER, CACHE_CONTROL_VALUE); get.setBody(op.getBodyRaw()); get.setStatusCode(op.getStatusCode()); get.complete(); } }); String originalUiProxyHeader = get.getRequestHeader(ConfigurationUtil.UI_PROXY_FORWARD_HEADER); if (originalUiProxyHeader != null) { getOp.addRequestHeader(ConfigurationUtil.UI_PROXY_FORWARD_HEADER, originalUiProxyHeader); } sendRequest(getOp); }
@Test public void testRedirectToLogin() { UiService service = new UiService(); service.setSelfLink("/"); VerificationHost vh = new VerificationHost(); vh.setAuthorizationEnabled(true); vh.setAuthorizationContext(createAuthorizationContext(false)); service.setHost(vh); AtomicBoolean completionCalled = new AtomicBoolean(); service.handleGet(new Operation().setUri(UriUtils.buildUri("http://localhost/sample")) .setCompletion((o, e) -> { assertEquals("/login/", o.getResponseHeader(Operation.LOCATION_HEADER)); completionCalled.set(true); })); assertTrue(completionCalled.get()); }
protected void startRedirectService(String sourcePath, String targetPath) { Operation post = Operation .createPost(UriUtils.buildUri(getHost(), sourcePath)); UiNgResourceForwarding rf = new UiNgResourceForwarding(sourcePath, targetPath, true); getHost().startService(post, rf); }
@Test public void testRedirect() { UiService service = new UiService(); service.setSelfLink("/sample"); service.setHost(new VerificationHost()); AtomicBoolean completionCalled = new AtomicBoolean(); service.handleGet(new Operation().setUri(UriUtils.buildUri("http://localhost/sample")) .setCompletion((o, e) -> { assertEquals("/sample/", o.getResponseHeader(Operation.LOCATION_HEADER)); completionCalled.set(true); })); assertTrue(completionCalled.get()); }