Search in sources :

Example 1 with ResourceManagerImpl

use of org.infinispan.rest.framework.impl.ResourceManagerImpl 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());
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) IdentityRoleMapper(org.infinispan.security.mappers.IdentityRoleMapper) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) SimpleRestResponse(org.infinispan.rest.framework.impl.SimpleRestResponse) ResourceManagerImpl(org.infinispan.rest.framework.impl.ResourceManagerImpl) SimpleRequest(org.infinispan.rest.framework.impl.SimpleRequest) Subject(javax.security.auth.Subject) CustomAuditLoggerTest(org.infinispan.security.CustomAuditLoggerTest) RestDispatcherImpl(org.infinispan.rest.framework.impl.RestDispatcherImpl) Authorizer(org.infinispan.security.impl.Authorizer) Test(org.testng.annotations.Test) CustomAuditLoggerTest(org.infinispan.security.CustomAuditLoggerTest)

Example 2 with ResourceManagerImpl

use of org.infinispan.rest.framework.impl.ResourceManagerImpl 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());
}
Also used : GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalConfiguration(org.infinispan.configuration.global.GlobalConfiguration) SimpleRestResponse(org.infinispan.rest.framework.impl.SimpleRestResponse) ResourceManagerImpl(org.infinispan.rest.framework.impl.ResourceManagerImpl) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) SimpleRequest(org.infinispan.rest.framework.impl.SimpleRequest) RestDispatcherImpl(org.infinispan.rest.framework.impl.RestDispatcherImpl) Authorizer(org.infinispan.security.impl.Authorizer) Test(org.testng.annotations.Test) CustomAuditLoggerTest(org.infinispan.security.CustomAuditLoggerTest)

Example 3 with ResourceManagerImpl

use of org.infinispan.rest.framework.impl.ResourceManagerImpl 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());
}
Also used : AuthenticationConfiguration(org.infinispan.rest.configuration.AuthenticationConfiguration) Path(java.nio.file.Path) RestDispatcherImpl(org.infinispan.rest.framework.impl.RestDispatcherImpl) RedirectResource(org.infinispan.rest.resources.RedirectResource) SearchAdminResource(org.infinispan.rest.resources.SearchAdminResource) MetricsResource(org.infinispan.rest.resources.MetricsResource) ChannelInboundHandler(io.netty.channel.ChannelInboundHandler) Log(org.infinispan.server.core.logging.Log) ResourceManagerImpl(org.infinispan.rest.framework.impl.ResourceManagerImpl) ArrayList(java.util.ArrayList) SecurityResource(org.infinispan.rest.resources.SecurityResource) MAX_HEADER_SIZE(org.infinispan.rest.RestChannelInitializer.MAX_HEADER_SIZE) AbstractProtocolServer(org.infinispan.server.core.AbstractProtocolServer) RestCacheManager(org.infinispan.rest.cachemanager.RestCacheManager) XSiteResource(org.infinispan.rest.resources.XSiteResource) ChannelMatcher(io.netty.channel.group.ChannelMatcher) Path(java.nio.file.Path) ClusterResource(org.infinispan.rest.resources.ClusterResource) RestDispatcher(org.infinispan.rest.framework.RestDispatcher) CacheResourceV2(org.infinispan.rest.resources.CacheResourceV2) ServerResource(org.infinispan.rest.resources.ServerResource) StaticContentResource(org.infinispan.rest.resources.StaticContentResource) ProtobufResource(org.infinispan.rest.resources.ProtobufResource) ChannelInitializer(io.netty.channel.ChannelInitializer) LoggingResource(org.infinispan.rest.resources.LoggingResource) ChannelOutboundHandler(io.netty.channel.ChannelOutboundHandler) CorsConfig(io.netty.handler.codec.http.cors.CorsConfig) TasksResource(org.infinispan.rest.resources.TasksResource) IOException(java.io.IOException) ResourceManager(org.infinispan.rest.framework.ResourceManager) EmbeddedCounterManagerFactory(org.infinispan.counter.EmbeddedCounterManagerFactory) Channel(io.netty.channel.Channel) MAX_INITIAL_LINE_SIZE(org.infinispan.rest.RestChannelInitializer.MAX_INITIAL_LINE_SIZE) AuthenticationConfiguration(org.infinispan.rest.configuration.AuthenticationConfiguration) List(java.util.List) NettyInitializers(org.infinispan.server.core.transport.NettyInitializers) LogFactory(org.infinispan.commons.logging.LogFactory) RestServerConfiguration(org.infinispan.rest.configuration.RestServerConfiguration) EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) ContainerResource(org.infinispan.rest.resources.ContainerResource) CounterResource(org.infinispan.rest.resources.CounterResource) CounterResource(org.infinispan.rest.resources.CounterResource) MetricsResource(org.infinispan.rest.resources.MetricsResource) RedirectResource(org.infinispan.rest.resources.RedirectResource) StaticContentResource(org.infinispan.rest.resources.StaticContentResource) ResourceManagerImpl(org.infinispan.rest.framework.impl.ResourceManagerImpl) CacheResourceV2(org.infinispan.rest.resources.CacheResourceV2) ServerResource(org.infinispan.rest.resources.ServerResource) EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) ResourceManager(org.infinispan.rest.framework.ResourceManager) ClusterResource(org.infinispan.rest.resources.ClusterResource) XSiteResource(org.infinispan.rest.resources.XSiteResource) RestDispatcherImpl(org.infinispan.rest.framework.impl.RestDispatcherImpl) ContainerResource(org.infinispan.rest.resources.ContainerResource) ProtobufResource(org.infinispan.rest.resources.ProtobufResource) TasksResource(org.infinispan.rest.resources.TasksResource) SearchAdminResource(org.infinispan.rest.resources.SearchAdminResource) SecurityResource(org.infinispan.rest.resources.SecurityResource)

Aggregations

ResourceManagerImpl (org.infinispan.rest.framework.impl.ResourceManagerImpl)3 RestDispatcherImpl (org.infinispan.rest.framework.impl.RestDispatcherImpl)3 GlobalConfiguration (org.infinispan.configuration.global.GlobalConfiguration)2 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)2 SimpleRequest (org.infinispan.rest.framework.impl.SimpleRequest)2 SimpleRestResponse (org.infinispan.rest.framework.impl.SimpleRestResponse)2 Channel (io.netty.channel.Channel)1 ChannelInboundHandler (io.netty.channel.ChannelInboundHandler)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOutboundHandler (io.netty.channel.ChannelOutboundHandler)1 ChannelMatcher (io.netty.channel.group.ChannelMatcher)1 CorsConfig (io.netty.handler.codec.http.cors.CorsConfig)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Subject (javax.security.auth.Subject)1 LogFactory (org.infinispan.commons.logging.LogFactory)1 EmbeddedCounterManagerFactory (org.infinispan.counter.EmbeddedCounterManagerFactory)1 EmbeddedCounterManager (org.infinispan.counter.impl.manager.EmbeddedCounterManager)1