Skip to content

Commit 5f2fa67

Browse files
0utplayderklaro
authored andcommitted
fix: invalid module injection layer handling (#43)
1 parent ae187da commit 5f2fa67

File tree

8 files changed

+47
-34
lines changed

8 files changed

+47
-34
lines changed

.github/workflows/gradle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup java
2929
uses: actions/setup-java@v4
3030
with:
31-
java-version: 21
31+
java-version: 22
3232
check-latest: true
3333
distribution: 'zulu'
3434

cloudnet-rest-module/build.gradle.kts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ plugins {
2121
alias(libs.plugins.juppiter)
2222
}
2323

24+
repositories {
25+
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
26+
}
27+
2428
dependencies {
2529
api(projects.webApi)
2630
moduleLibrary(libs.guava)
@@ -49,8 +53,8 @@ dependencies {
4953
compileOnly(libs.logbackCore)
5054
compileOnly(libs.logbackClassic)
5155

52-
compileOnly("eu.cloudnetservice.cloudnet:node:4.0.0-RC10")
53-
compileOnly("eu.cloudnetservice.cloudnet:bridge:4.0.0-RC10")
56+
compileOnly("eu.cloudnetservice.cloudnet:node:4.0.0-RC11-SNAPSHOT")
57+
compileOnly("eu.cloudnetservice.cloudnet:bridge:4.0.0-RC11-SNAPSHOT")
5458
}
5559

5660
tasks.withType<Jar> {
@@ -65,6 +69,11 @@ tasks.withType<ShadowJar> {
6569
}
6670
}
6771

72+
tasks.withType<JavaCompile> {
73+
sourceCompatibility = JavaVersion.VERSION_22.toString()
74+
targetCompatibility = JavaVersion.VERSION_22.toString()
75+
}
76+
6877
moduleJson {
6978
main = "eu.cloudnetservice.ext.modules.rest.CloudNetRestModule"
7079
name = "CloudNet-Rest"

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/CloudNetRestModule.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package eu.cloudnetservice.ext.modules.rest;
1818

19+
import dev.derklaro.aerogel.SpecifiedInjector;
1920
import dev.derklaro.aerogel.binding.BindingBuilder;
2021
import eu.cloudnetservice.common.language.I18n;
2122
import eu.cloudnetservice.driver.document.DocumentFactory;
2223
import eu.cloudnetservice.driver.event.EventManager;
2324
import eu.cloudnetservice.driver.inject.InjectionLayer;
2425
import eu.cloudnetservice.driver.module.ModuleLifeCycle;
26+
import eu.cloudnetservice.driver.module.ModuleProvider;
2527
import eu.cloudnetservice.driver.module.ModuleTask;
2628
import eu.cloudnetservice.driver.module.driver.DriverModule;
2729
import eu.cloudnetservice.ext.modules.rest.config.RestConfiguration;
@@ -46,7 +48,9 @@
4648
import eu.cloudnetservice.ext.rest.api.auth.RestUserManagementLoader;
4749
import eu.cloudnetservice.ext.rest.api.factory.HttpComponentFactoryLoader;
4850
import eu.cloudnetservice.ext.rest.validation.ValidationHandlerMethodContextDecorator;
51+
import eu.cloudnetservice.node.TickLoop;
4952
import eu.cloudnetservice.node.command.CommandProvider;
53+
import jakarta.inject.Named;
5054
import jakarta.inject.Singleton;
5155
import lombok.NonNull;
5256
import org.slf4j.Logger;
@@ -63,7 +67,7 @@ public void loadLanguageFile() {
6367
}
6468

6569
@ModuleTask(order = 127, lifecycle = ModuleLifeCycle.STARTED)
66-
public void initHttpServer(@NonNull InjectionLayer<?> injectionLayer) {
70+
public void initHttpServer(@Named("module") @NonNull InjectionLayer<?> injectionLayer) {
6771
var restConfig = this.readConfig(RestConfiguration.class, () -> RestConfiguration.DEFAULT, DocumentFactory.json());
6872

6973
// construct the http server component
@@ -124,16 +128,30 @@ public void registerRestCommand(@NonNull CommandProvider commandProvider) {
124128
commandProvider.register(RestCommand.class);
125129
}
126130

131+
@ModuleTask(lifecycle = ModuleLifeCycle.STARTED)
132+
public void scheduleBridgeInitialization(
133+
@NonNull TickLoop tickLoop,
134+
@NonNull ModuleProvider moduleProvider,
135+
@NonNull HttpServer server,
136+
@NonNull @Named("module") InjectionLayer<SpecifiedInjector> moduleLayer
137+
) {
138+
// we want to register the bridge handlers after all modules are started
139+
tickLoop.runTask(() -> CloudNetBridgeInitializer.installBridgeHandler(moduleProvider, server, moduleLayer));
140+
}
141+
127142
@ModuleTask(lifecycle = ModuleLifeCycle.STARTED)
128143
public void registerListener(@NonNull EventManager eventManager) {
129144
eventManager.registerListener(RestUserUpdateListener.class);
130-
eventManager.registerListener(CloudNetBridgeInitializer.class);
131145
}
132146

133147
@ModuleTask(lifecycle = ModuleLifeCycle.STOPPED)
134-
public void unregisterModule(@NonNull HttpServer httpServer) {
148+
public void unregisterModule(
149+
@NonNull HttpServer httpServer,
150+
@Named("module") InjectionLayer<SpecifiedInjector> layer
151+
) {
135152
try {
136153
httpServer.close();
154+
layer.injector().removeConstructedBindings();
137155
} catch (Exception exception) {
138156
LOGGER.error("Unable to close http server while disabling cloudnet rest module.", exception);
139157
}

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/dto/JsonConfigurationDto.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import jakarta.validation.constraints.NotBlank;
2323
import jakarta.validation.constraints.NotNull;
2424
import jakarta.validation.constraints.Positive;
25-
import java.util.Collection;
2625
import java.util.Map;
2726
import java.util.Set;
2827
import lombok.NonNull;
@@ -65,19 +64,12 @@ public final class JsonConfigurationDto implements Dto<JsonConfiguration> {
6564
@NotNull
6665
private final Map<String, String> ipAliases;
6766

68-
@Valid
69-
@NotNull
70-
private final Collection<HostAndPortDto> httpListeners;
71-
7267
@Valid
7368
@NotNull
7469
private final SSLConfigurationDto clientSslConfig;
7570
@Valid
7671
@NotNull
7772
private final SSLConfigurationDto serverSslConfig;
78-
@Valid
79-
@NotNull
80-
private final SSLConfigurationDto webSslConfig;
8173

8274
@NotNull
8375
private final Document properties;
@@ -97,10 +89,8 @@ public JsonConfigurationDto(
9789
String jvmCommand,
9890
String hostAddress,
9991
Map<String, String> ipAliases,
100-
Collection<HostAndPortDto> httpListeners,
10192
SSLConfigurationDto clientSslConfig,
10293
SSLConfigurationDto serverSslConfig,
103-
SSLConfigurationDto webSslConfig,
10494
Document properties
10595
) {
10696
this.language = language;
@@ -117,10 +107,8 @@ public JsonConfigurationDto(
117107
this.jvmCommand = jvmCommand;
118108
this.hostAddress = hostAddress;
119109
this.ipAliases = ipAliases;
120-
this.httpListeners = httpListeners;
121110
this.clientSslConfig = clientSslConfig;
122111
this.serverSslConfig = serverSslConfig;
123-
this.webSslConfig = webSslConfig;
124112
this.properties = properties;
125113
}
126114

@@ -141,10 +129,8 @@ public JsonConfigurationDto(
141129
config.javaCommand(this.jvmCommand);
142130
config.hostAddress(this.hostAddress);
143131
config.ipAliases(this.ipAliases);
144-
config.httpListeners(Dto.toList(this.httpListeners));
145132
config.clientSSLConfig(this.clientSslConfig.toEntity());
146133
config.serverSSLConfig(this.serverSslConfig.toEntity());
147-
config.webSSLConfig(this.webSslConfig.toEntity());
148134
config.properties(this.properties);
149135
return config;
150136
}

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/dto/SSLConfigurationDto.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ public final class SSLConfigurationDto implements Dto<SSLConfiguration> {
3131
private final Path certificatePath;
3232
@NotNull
3333
private final Path privateKeyPath;
34+
private final String privateKeyPassword;
3435

3536
public SSLConfigurationDto(
3637
boolean enabled,
3738
boolean clientAuth,
3839
Path trustCertificatePath,
3940
Path certificatePath,
40-
Path privateKeyPath
41+
Path privateKeyPath,
42+
String privateKeyPassword
4143
) {
4244
this.enabled = enabled;
4345
this.clientAuth = clientAuth;
4446
this.trustCertificatePath = trustCertificatePath;
4547
this.certificatePath = certificatePath;
4648
this.privateKeyPath = privateKeyPath;
49+
this.privateKeyPassword = privateKeyPassword;
4750
}
4851

4952
@Override
@@ -53,6 +56,7 @@ public SSLConfigurationDto(
5356
this.clientAuth,
5457
this.trustCertificatePath,
5558
this.certificatePath,
56-
this.privateKeyPath);
59+
this.privateKeyPath,
60+
this.privateKeyPassword);
5761
}
5862
}

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/listener/CloudNetBridgeInitializer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,34 @@
1616

1717
package eu.cloudnetservice.ext.modules.rest.listener;
1818

19-
import eu.cloudnetservice.driver.event.EventListener;
19+
import dev.derklaro.aerogel.SpecifiedInjector;
2020
import eu.cloudnetservice.driver.inject.InjectionLayer;
2121
import eu.cloudnetservice.driver.module.ModuleLifeCycle;
2222
import eu.cloudnetservice.driver.module.ModuleProvider;
2323
import eu.cloudnetservice.ext.modules.rest.v3.bridge.V3HttpHandlerPlayer;
2424
import eu.cloudnetservice.ext.rest.api.HttpServer;
25-
import eu.cloudnetservice.node.event.CloudNetNodePostInitializationEvent;
26-
import jakarta.inject.Singleton;
2725
import lombok.NonNull;
2826
import org.slf4j.Logger;
2927
import org.slf4j.LoggerFactory;
3028

31-
@Singleton
3229
public class CloudNetBridgeInitializer {
3330

3431
private static final String BRIDGE_MODULE_NAME = "CloudNet-Bridge";
3532

3633
private static final Logger LOGGER = LoggerFactory.getLogger(CloudNetBridgeInitializer.class);
3734

38-
@EventListener
39-
public void handleNodePostInit(
40-
@NonNull CloudNetNodePostInitializationEvent event,
35+
public static void installBridgeHandler(
4136
@NonNull ModuleProvider moduleProvider,
42-
@NonNull HttpServer server
37+
@NonNull HttpServer server,
38+
@NonNull InjectionLayer<SpecifiedInjector> moduleLayer
4339
) {
4440
var bridgeModule = moduleProvider.module(BRIDGE_MODULE_NAME);
4541
if (bridgeModule == null || bridgeModule.moduleLifeCycle() != ModuleLifeCycle.STARTED) {
4642
LOGGER.debug("Could not detect started {} module. Aborting http player initialization.", BRIDGE_MODULE_NAME);
4743
return;
4844
}
4945

50-
var layer = InjectionLayer.findLayerOf(this.getClass());
51-
server.annotationParser().parseAndRegister(layer.instance(V3HttpHandlerPlayer.class));
46+
server.annotationParser().parseAndRegister(moduleLayer.instance(V3HttpHandlerPlayer.class));
5247
LOGGER.debug("Successfully registered V3HttpHandlerPlayer as {} is present", BRIDGE_MODULE_NAME);
5348
}
5449
}

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/v3/V3HttpHandlerModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public V3HttpHandlerModule(@NonNull ModulesHolder modulesHolder, @NonNull Module
229229
// download the module
230230
var target = this.moduleProvider.moduleDirectoryPath().resolve(entry.name() + ".jar");
231231
Unirest.get(entry.url())
232-
.connectTimeout(5000)
232+
.requestTimeout(5000)
233233
.thenConsume(rawResponse -> FileUtil.copy(rawResponse.getContent(), target));
234234

235235
// validate the downloaded file

cloudnet-rest-module/src/main/java/eu/cloudnetservice/ext/modules/rest/v3/V3HttpHandlerNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import ch.qos.logback.classic.spi.ILoggingEvent;
2020
import ch.qos.logback.core.ConsoleAppender;
2121
import ch.qos.logback.core.OutputStreamAppender;
22+
import eu.cloudnetservice.common.concurrent.TaskUtil;
2223
import eu.cloudnetservice.common.util.StringUtil;
2324
import eu.cloudnetservice.driver.CloudNetVersion;
2425
import eu.cloudnetservice.driver.module.ModuleProvider;
@@ -228,7 +229,7 @@ public void handle(@NonNull WebSocketChannel channel, @NonNull WebSocketFrameTyp
228229
if (this.user.hasScope("cloudnet_rest:node_send_commands")) {
229230
var commandLine = new String(bytes, StandardCharsets.UTF_8);
230231
var commandSource = new DriverCommandSource();
231-
V3HttpHandlerNode.this.commandProvider.execute(commandSource, commandLine).getOrNull();
232+
TaskUtil.getOrDefault(V3HttpHandlerNode.this.commandProvider.execute(commandSource, commandLine), null);
232233

233234
for (var message : commandSource.messages()) {
234235
this.channel.sendWebSocketFrame(WebSocketFrameType.TEXT, message);

0 commit comments

Comments
 (0)