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

Move config stuff to own Config class, cleanup readme about logging c… #15

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ Run with
java -jar rlp_07.jar
----

Listens at default port 1601 logs received content with logback.
Listens to plain server port 1601 by default and prints received events.
Port can be changed with "-Dport=1234" command-line argument.
TLS mode can be enabled with "-Dtls=true" command-line argument.
Custom server keystore can be supplied with -DtlsKeystore=path/to/file.jks
Custom server keystore password can be supplied with -DtlsKeystorePassword=MyCustomPassword

Debug output

Expand Down
9 changes: 0 additions & 9 deletions src/main/assembly/jar-with-dependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/config</directory>
<includes>
<include>logback.xml</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
35 changes: 35 additions & 0 deletions src/main/java/com/teragrep/rlp_07/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.teragrep.rlp_07;
public class Config {
private final int port;

public int getPort() {
return port;
}

private final boolean isTls;

public boolean isTls() {
return isTls;
}

private final String keystorePassword;

public String getKeystorePassword() {
return keystorePassword;
}

private final String keystorePath;
public String getKeystorePath() {
return keystorePath;
}
public Config() {
try {
port = Integer.parseInt(System.getProperty("port", "1601"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16% of developers fix this issue

StaticAssignmentInConstructor: This assignment is to a static field. Mutating static state from a constructor is highly error-prone.

❗❗ 2 similar findings have been found in this PR

🔎 Expand here to view all instances of this finding
File Path Line Number
src/main/java/com/teragrep/rlp_07/Config.java 33
src/main/java/com/teragrep/rlp_07/Config.java 34

Visit the Lift Web Console to find more details in your report.


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

} catch (NumberFormatException e) {
throw new RuntimeException("Can't parse port: " + e.getMessage());
}
isTls = Boolean.parseBoolean(System.getProperty("tls", "false"));
keystorePassword = System.getProperty("tlsKeystorePassword", "changeit");
keystorePath = System.getProperty("tlsKeystore", null);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16% of developers fix this issue

StaticAssignmentInConstructor: This assignment is to a static field. Mutating static state from a constructor is highly error-prone.


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

}
}
40 changes: 20 additions & 20 deletions src/main/java/com/teragrep/rlp_07/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,57 @@ class Main {
LOGGER.info(message);
};
private static final FrameProcessor syslogFrameProcessor = new SyslogFrameProcessor(byteConsumer);
static Config config;

public static void main(String[] args) throws IOException, InterruptedException {
int port = Integer.parseInt(System.getProperty("port", "1601"));
boolean tlsMode = Boolean.parseBoolean(System.getProperty("tls", "false"));
config = new Config();
try {
if (tlsMode) {
tlsServer(port);
if (config.isTls()) {
tlsServer();
} else {
plainServer(port);
plainServer();
}
}
catch (Exception e) {
LOGGER.error("Failed to run: " + e.getMessage());
}
}

private static void plainServer(int port) throws IOException, InterruptedException {
LOGGER.info("Starting plain server on port " +port);
Server relpServer = new Server(port, syslogFrameProcessor);
private static void plainServer() throws IOException, InterruptedException {
LOGGER.info("Starting plain server on port " + config.getPort());
Server relpServer = new Server(config.getPort(), syslogFrameProcessor);
relpServer.start();
Thread.sleep(Long.MAX_VALUE);
}

private static void tlsServer(int port) throws IOException, InterruptedException {
LOGGER.info("Starting TLS server on port " + port);
String keystorePath = System.getProperty("tlsKeystore");
InputStream keyStoreStream;
private static void tlsServer() throws IOException, InterruptedException {
LOGGER.info("Starting TLS server on port " + config.getPort());

String keystorePath = config.getKeystorePath();
InputStream keystoreStream;
if(keystorePath != null) {
LOGGER.info("Using user supplied keystore");
Path path = Paths.get(keystorePath);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9% of developers fix this issue

PATH_TRAVERSAL_IN: This API (java/nio/file/Paths.get(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;) reads a file whose location might be specified by user input


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.

if(!path.toFile().exists()) {
throw new RuntimeException("File " + keystorePath + " doesn't exist");
}
keyStoreStream = Files.newInputStream(path);
keystoreStream = Files.newInputStream(path);
}
else {
LOGGER.info("Using default keystore");
// get server keyStore as inputstream, works on JAR packaging as well this way
keyStoreStream = Main.class.getClassLoader().getResourceAsStream("keystore-server.jks");
keystoreStream = Main.class.getClassLoader().getResourceAsStream("keystore-server.jks");
}

SSLContext sslContext;
try {
sslContext = TLSContextFactory.authenticatedContext(
keyStoreStream,
"changeit",
keystoreStream,
config.getKeystorePassword(),
"TLSv1.3"
);
} catch (GeneralSecurityException e) {
throw new RuntimeException("SSL.demoContext Error: " + e);
throw new RuntimeException("Can't create sslContext: " + e);
}

Function<SSLContext, SSLEngine> sslEngineFunction = sslCtx -> {
Expand All @@ -82,10 +84,8 @@ private static void tlsServer(int port) throws IOException, InterruptedException
return sslEngine;
};

Server relpServer = new Server(port, syslogFrameProcessor, sslContext, sslEngineFunction);

Server relpServer = new Server(config.getPort(), syslogFrameProcessor, sslContext, sslEngineFunction);
relpServer.start();

Thread.sleep(Long.MAX_VALUE);
}
}