use of org.infinispan.rest.framework.impl.RestDispatcherImpl in project infinispan by infinispan.
the class RestDispatcherTest method testDispatchWithAuthz.
@Test
public void testDispatchWithAuthz() {
final Subject ADMIN = TestingUtil.makeSubject("admin");
final Subject LIFECYCLE = TestingUtil.makeSubject("lifecycle");
ResourceManagerImpl manager = new ResourceManagerImpl();
manager.registerResource("ctx", new SecureResource());
CustomAuditLoggerTest.TestAuditLogger auditLogger = new CustomAuditLoggerTest.TestAuditLogger();
GlobalConfiguration globalConfiguration = new GlobalConfigurationBuilder().security().authorization().enable().auditLogger(auditLogger).principalRoleMapper(new IdentityRoleMapper()).build();
RestDispatcherImpl restDispatcher = new RestDispatcherImpl(manager, new Authorizer(globalConfiguration.security(), AuditContext.SERVER, "test", null));
// Anonymous
RestRequest restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/secure").build();
CompletionStage<RestResponse> response = restDispatcher.dispatch(restRequest);
Exceptions.expectCompletionException(SecurityException.class, response);
assertEquals("Permission to ADMIN is DENY for user null", auditLogger.getLastRecord());
// Wrong user
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/secure").setSubject(LIFECYCLE).build();
response = restDispatcher.dispatch(restRequest);
Exceptions.expectCompletionException(SecurityException.class, response);
assertEquals("Permission to ADMIN is DENY for user Subject:\n\tPrincipal: TestPrincipal [name=lifecycle]\n", auditLogger.getLastRecord());
// Correct user
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/secure").setSubject(ADMIN).build();
response = restDispatcher.dispatch(restRequest);
assertEquals("Subject:\n\tPrincipal: TestPrincipal [name=admin]\n", join(response).getEntity().toString());
assertEquals("Permission to ADMIN is ALLOW for user Subject:\n\tPrincipal: TestPrincipal [name=admin]\n", auditLogger.getLastRecord());
}
use of org.infinispan.rest.framework.impl.RestDispatcherImpl in project infinispan by infinispan.
the class RestDispatcherTest method testDispatch.
@Test
public void testDispatch() {
ResourceManagerImpl manager = new ResourceManagerImpl();
manager.registerResource("/", new RootResource());
manager.registerResource("ctx", new CounterResource());
manager.registerResource("ctx", new MemoryResource());
manager.registerResource("ctx", new EchoResource());
manager.registerResource("ctx", new FileResource());
GlobalConfiguration globalConfiguration = new GlobalConfigurationBuilder().build();
RestDispatcherImpl restDispatcher = new RestDispatcherImpl(manager, new Authorizer(globalConfiguration.security(), AuditContext.SERVER, "test", null));
RestRequest restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/").build();
CompletionStage<RestResponse> response = restDispatcher.dispatch(restRequest);
assertEquals("Hello World!", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/image.gif").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("Hello World!", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(POST).setPath("//ctx/counters/counter1").build();
response = restDispatcher.dispatch(restRequest);
assertEquals(200, join(response).getStatus());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/counters/counter1?action=increment").build();
response = restDispatcher.dispatch(restRequest);
assertEquals(200, join(response).getStatus());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/counters//counter1").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("counter1->1", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(POST).setPath("/ctx/jvm").build();
assertNoResource(restDispatcher, restRequest);
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/jvm/memory").build();
response = restDispatcher.dispatch(restRequest);
assertTrue(Long.parseLong(join(response).getEntity().toString()) > 0);
restRequest = new SimpleRequest.Builder().setMethod(HEAD).setPath("/ctx/jvm/memory").build();
response = restDispatcher.dispatch(restRequest);
assertTrue(Long.parseLong(join(response).getEntity().toString()) > 0);
restRequest = new SimpleRequest.Builder().setMethod(HEAD).setPath("/ctx/v2/java-memory").build();
response = restDispatcher.dispatch(restRequest);
assertTrue(Long.parseLong(join(response).getEntity().toString()) > 0);
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/context/var1/var2").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("var1,var2", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/context/var1/var2/var3?action=triple").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("triple(var1,var2,var3)", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/context/var1/var2/var3?action=invalid").build();
assertNoResource(restDispatcher, restRequest);
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/web/").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("/ctx/web/index.html", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/web/file.txt").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("/ctx/web/file.txt", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/web/dir/file.txt").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("/ctx/web/dir/file.txt", join(response).getEntity().toString());
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/web/dir1/dir2/file.txt").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("/ctx/web/dir1/dir2/file.txt", join(response).getEntity().toString());
// Create a resource named "{c}"
restRequest = new SimpleRequest.Builder().setMethod(POST).setPath("/ctx/counters/{c}").build();
response = restDispatcher.dispatch(restRequest);
assertEquals(200, join(response).getStatus());
// Read a resource named "{c}"
restRequest = new SimpleRequest.Builder().setMethod(GET).setPath("/ctx/counters/{c}").build();
response = restDispatcher.dispatch(restRequest);
assertEquals("{c}->0", join(response).getEntity().toString());
}
use of org.infinispan.rest.framework.impl.RestDispatcherImpl in project infinispan by infinispan.
the class RestServer method startInternal.
@Override
protected void startInternal() {
this.maxContentLength = configuration.maxContentLength() + MAX_INITIAL_LINE_SIZE + MAX_HEADER_SIZE;
AuthenticationConfiguration auth = configuration.authentication();
if (auth.enabled()) {
auth.authenticator().init(this);
}
super.startInternal();
restCacheManager = new RestCacheManager<>(cacheManager, this::isCacheIgnored);
invocationHelper = new InvocationHelper(this, restCacheManager, (EmbeddedCounterManager) EmbeddedCounterManagerFactory.asCounterManager(cacheManager), configuration, server, getExecutor());
String restContext = configuration.contextPath();
String rootContext = "/";
ResourceManager resourceManager = new ResourceManagerImpl();
resourceManager.registerResource(restContext, new CacheResourceV2(invocationHelper));
resourceManager.registerResource(restContext, new CounterResource(invocationHelper));
resourceManager.registerResource(restContext, new ContainerResource(invocationHelper));
resourceManager.registerResource(restContext, new XSiteResource(invocationHelper));
resourceManager.registerResource(restContext, new SearchAdminResource(invocationHelper));
resourceManager.registerResource(restContext, new TasksResource(invocationHelper));
resourceManager.registerResource(restContext, new ProtobufResource(invocationHelper));
resourceManager.registerResource(rootContext, new MetricsResource(auth.metricsAuth(), invocationHelper));
Path staticResources = configuration.staticResources();
if (staticResources != null) {
Path console = configuration.staticResources().resolve("console");
resourceManager.registerResource(rootContext, new StaticContentResource(staticResources, "static"));
resourceManager.registerResource(rootContext, new StaticContentResource(console, "console", (path, resource) -> {
if (!path.contains("."))
return StaticContentResource.DEFAULT_RESOURCE;
return path;
}));
resourceManager.registerResource(rootContext, new RedirectResource(rootContext, rootContext + "console/welcome", true));
}
if (adminEndpoint) {
resourceManager.registerResource(restContext, new ServerResource(invocationHelper));
resourceManager.registerResource(restContext, new ClusterResource(invocationHelper));
resourceManager.registerResource(restContext, new SecurityResource(invocationHelper, rootContext + "console/", rootContext + "console/forbidden.html"));
registerLoggingResource(resourceManager, restContext);
}
this.restDispatcher = new RestDispatcherImpl(resourceManager, restCacheManager.getAuthorizer());
}
Aggregations