Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAMES-2886 Allow to load extensions in WebAdmin #2701

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ https.enabled=false

# Defaults to false
#cors.enable=true
#cors.origin
#cors.origin

# List of fully qualified class names that should be exposed over webadmin
# in addition to your product default routes. Routes needs to be located
# within the classpath or in the ./extensions-jars folder.
#extensions.routes=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ https.enabled=false

# Defaults to false
#cors.enable=true
#cors.origin
#cors.origin

# List of fully qualified class names that should be exposed over webadmin
# in addition to your product default routes. Routes needs to be located
# within the classpath or in the ./extensions-jars folder.
chibenwa marked this conversation as resolved.
Show resolved Hide resolved
#extensions.routes=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ https.enabled=false

# Defaults to false
#cors.enable=true
#cors.origin
#cors.origin

# List of fully qualified class names that should be exposed over webadmin
# in addition to your product default routes. Routes needs to be located
# within the classpath or in the ./extensions-jars folder.
#extensions.routes=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ https.enabled=false

# Defaults to false
#cors.enable=true
#cors.origin
#cors.origin

# List of fully qualified class names that should be exposed over webadmin
# in addition to your product default routes. Routes needs to be located
# within the classpath or in the ./extensions-jars folder.
#extensions.routes=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

# Upon injections, the user can reference additional guice modules, that are going to be used only upon extensions instantiation.

#List of fully qualified class names of additional guice modules to be used to instanciate extensions
#List of coma separated (',') fully qualified class names of additional guice modules to be used to instantiate extensions
#guice.extension.module=
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ https.enabled=false

# Defaults to false
#cors.enable=true
#cors.origin
#cors.origin


# List of fully qualified class names that should be exposed over webadmin
# in addition to your product default routes. Routes needs to be located
# within the classpath or in the ./extensions-jars folder.
#extensions.routes=
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.apache.commons.configuration2.Configuration;

Expand All @@ -31,7 +32,9 @@ public class ExtensionConfiguration {
public static final ExtensionConfiguration DEFAULT = new ExtensionConfiguration(ImmutableList.of());

public static ExtensionConfiguration from(Configuration configuration) {
List<String> list = Arrays.asList(configuration.getStringArray("guice.extension.module"));
List<String> list = Optional.ofNullable(configuration.getStringArray("guice.extension.module"))
.map(Arrays::asList)
.orElse(ImmutableList.of());

return new ExtensionConfiguration(list.stream()
.map(ClassName::new)
Expand Down
6 changes: 6 additions & 0 deletions server/container/guice/memory-guice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
<artifactId>james-server-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>james-server-webadmin-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>testing-base</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james;

import org.apache.james.webadmin.Routes;

import spark.Service;

public class MyRoute implements Routes {
static final String ENDPOINT = "/myRoutes";
static final String SHABANG = "SHABANG";

@Override
public String getBasePath() {
return ENDPOINT;
}

@Override
public void define(Service service) {
service.get(ENDPOINT, (req, res) -> SHABANG);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james;

import static io.restassured.RestAssured.when;
import static org.apache.james.MyRoute.SHABANG;
import static org.hamcrest.CoreMatchers.is;

import org.apache.james.modules.TestJMAPServerModule;
import org.apache.james.utils.WebAdminGuiceProbe;
import org.apache.james.webadmin.RandomPortSupplier;
import org.apache.james.webadmin.WebAdminConfiguration;
import org.apache.james.webadmin.WebAdminUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.restassured.RestAssured;

class WebAdminRoutesExtensionTest {
private static final int LIMIT_TO_10_MESSAGES = 10;

@RegisterExtension
static JamesServerExtension jamesServerExtension = new JamesServerBuilder()
.server(configuration -> GuiceJamesServer.forConfiguration(configuration)
.combineWith(MemoryJamesServerMain.IN_MEMORY_SERVER_AGGREGATE_MODULE)
.overrideWith(new TestJMAPServerModule(LIMIT_TO_10_MESSAGES))
.overrideWith(binder -> binder.bind(WebAdminConfiguration.class)
.toInstance(WebAdminConfiguration.builder()
.additionalRoute(MyRoute.class.getCanonicalName())
.enabled()
.port(new RandomPortSupplier())
.corsDisabled()
.build())))
.build();


@Test
void customRoutesShouldBeBound(GuiceJamesServer jamesServer) {
WebAdminGuiceProbe probe = jamesServer.getProbe(WebAdminGuiceProbe.class);
RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(probe.getWebAdminPort()).build();

when()
.get(MyRoute.ENDPOINT)
.then()
.body(is(SHABANG));
}
}
4 changes: 4 additions & 0 deletions server/container/guice/protocols/webadmin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<groupId>${james.groupId}</groupId>
<artifactId>james-server-guice-configuration</artifactId>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>james-server-guice-utils</artifactId>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>james-server-webadmin-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.apache.james.modules.server;

import org.apache.james.core.healthcheck.HealthCheck;
import org.apache.james.webadmin.PublicRoutes;
import org.apache.james.webadmin.Routes;
import org.apache.james.webadmin.routes.HealthCheckRoutes;

import com.google.inject.AbstractModule;
Expand All @@ -32,7 +32,7 @@ public class HealthCheckRoutesModule extends AbstractModule {
protected void configure() {
bind(HealthCheckRoutes.class).in(Scopes.SINGLETON);

Multibinder<PublicRoutes> routesMultibinder = Multibinder.newSetBinder(binder(), PublicRoutes.class);
Multibinder<Routes> routesMultibinder = Multibinder.newSetBinder(binder(), Routes.class);
routesMultibinder.addBinding().to(HealthCheckRoutes.class);

Multibinder.newSetBinder(binder(), HealthCheck.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@
import static org.apache.james.webadmin.WebAdminConfiguration.DISABLED_CONFIGURATION;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.inject.Named;

import org.apache.commons.configuration2.Configuration;
import org.apache.james.jwt.JwtTokenVerifier;
import org.apache.james.lifecycle.api.Startable;
import org.apache.james.utils.ClassName;
import org.apache.james.utils.GuiceGenericLoader;
import org.apache.james.utils.GuiceProbe;
import org.apache.james.utils.InitialisationOperation;
import org.apache.james.utils.PropertiesProvider;
import org.apache.james.utils.WebAdminGuiceProbe;
import org.apache.james.webadmin.FixedPortSupplier;
import org.apache.james.webadmin.Routes;
import org.apache.james.webadmin.TlsConfiguration;
import org.apache.james.webadmin.WebAdminConfiguration;
import org.apache.james.webadmin.WebAdminServer;
Expand All @@ -43,6 +50,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.fge.lambdas.Throwing;
import com.github.steveash.guavate.Guavate;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
Expand Down Expand Up @@ -76,18 +86,41 @@ protected void configure() {
Multibinder.newSetBinder(binder(), JsonTransformerModule.class);
}

@Provides
@Singleton
@Named("webAdminRoutes")
public List<Routes> provideRoutes(GuiceGenericLoader loader, WebAdminConfiguration configuration, Set<Routes> routesList) {
List<Routes> customRoutes = configuration.getAdditionalRoutes()
.stream()
.map(ClassName::new)
.map(Throwing.function(loader::<Routes>instanciate))
.peek(routes -> LOGGER.info("Loading WebAdmin route extension {}", routes.getClass().getCanonicalName()))
.collect(Guavate.toImmutableList());

return ImmutableList.<Routes>builder()
.addAll(routesList)
.addAll(customRoutes)
.build();
}

@Provides
@Singleton
public WebAdminConfiguration provideWebAdminConfiguration(PropertiesProvider propertiesProvider) throws Exception {
try {
Configuration configurationFile = propertiesProvider.getConfiguration("webadmin");

List<String> additionalRoutes = Optional.ofNullable(configurationFile.getStringArray("extensions.routes"))
.map(ImmutableList::copyOf)
.orElse(ImmutableList.of());

return WebAdminConfiguration.builder()
.enable(configurationFile.getBoolean("enabled", DEFAULT_DISABLED))
.port(new FixedPortSupplier(configurationFile.getInt("port", WebAdminServer.DEFAULT_PORT)))
.tls(readHttpsConfiguration(configurationFile))
.enableCORS(configurationFile.getBoolean("cors.enable", DEFAULT_CORS_DISABLED))
.urlCORSOrigin(configurationFile.getString("cors.origin", DEFAULT_NO_CORS_ORIGIN))
.host(configurationFile.getString("host", WebAdminConfiguration.DEFAULT_HOST))
.additionalRoutes(additionalRoutes)
.build();
} catch (FileNotFoundException e) {
LOGGER.info("No webadmin.properties file. Disabling WebAdmin interface.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.james.utils.WebAdminGuiceProbe;
import org.apache.james.webadmin.WebAdminUtils;
import org.apache.james.webadmin.routes.HealthCheckRoutes;
import org.apache.james.webadmin.swagger.routes.SwaggerRoutes;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -68,4 +69,12 @@ public void getHealthchecksShouldNotNeedAuthentication() {
.then()
.statusCode(not(HttpStatus.UNAUTHORIZED_401));
}

@Test
public void getSwaggerShouldNotNeedAuthentication() {
when()
.get(SwaggerRoutes.SWAGGER_ENDPOINT)
.then()
.statusCode(not(HttpStatus.UNAUTHORIZED_401));
}
}