mirror of
https://github.com/tiennm99/tjahzi.git
synced 2026-10-20 02:18:25 +00:00
Re #87: Fixes in useSSL configuration; Added tests using nginx reverse proxy (testing basic auth, https and log endpoint change).
This commit is contained in:
1 parent
c6f96e0011
commit
846eacb492
12 files changed
+242
-49
No files matched your search
@@ -5,7 +5,7 @@ public class ClientConfiguration {
|
||||
private final String logEndpoint;
|
||||
private final String host;
|
||||
private final int port;
|
||||
private boolean useSSL;
|
||||
private final boolean useSSL;
|
||||
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
@@ -148,6 +148,6 @@ public class ClientConfigurationBuilder {
|
||||
}
|
||||
|
||||
private void validateAndConfigureConnectionParametersNoUrl() {
|
||||
useSSL = port == HTTPS_PORT;
|
||||
useSSL |= port == HTTPS_PORT;
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,12 @@
|
||||
<version>1.14.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>nginx</artifactId>
|
||||
<version>1.14.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
+30
@@ -16,6 +16,12 @@ public abstract class LokiAppenderConfigurator extends UnsynchronizedAppenderBas
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
private boolean useSSL;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private int connectTimeoutMillis = 5000;
|
||||
private int readTimeoutMillis = 60_000;
|
||||
private int maxRetries = 3;
|
||||
@@ -68,6 +74,30 @@ public abstract class LokiAppenderConfigurator extends UnsynchronizedAppenderBas
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isUseSSL() {
|
||||
return useSSL;
|
||||
}
|
||||
|
||||
public void setUseSSL(boolean useSSL) {
|
||||
this.useSSL = useSSL;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getConnectTimeoutMillis() {
|
||||
return connectTimeoutMillis;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ public class LokiAppenderFactory {
|
||||
.withLogEndpoint(configurator.getLogEndpoint())
|
||||
.withHost(configurator.getHost())
|
||||
.withPort(configurator.getPort())
|
||||
.withUseSSL(configurator.isUseSSL())
|
||||
.withUsername(configurator.getUsername())
|
||||
.withPassword(configurator.getPassword())
|
||||
.withConnectionTimeoutMillis(configurator.getConnectTimeoutMillis())
|
||||
.withMaxRetries(configurator.getMaxRetries())
|
||||
.withRequestTimeoutMillis(configurator.getReadTimeoutMillis())
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.testcontainers.containers.BindMode;
|
||||
import org.testcontainers.containers.NginxContainer;
|
||||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.IntegrationTest;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
class LokiAppenderHttpsCustomEndpointTest extends IntegrationTest {
|
||||
|
||||
@Container
|
||||
public NginxContainer<?> nginx = new NginxContainer<>("nginx:latest")
|
||||
.withNetwork(network)
|
||||
.withClasspathResourceMapping("loki.reverse.nginx.conf",
|
||||
"/etc/nginx/conf.d/loki.reverse.conf",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/nginx-selfsigned.crt",
|
||||
"/etc/nginx/nginx-selfsigned.crt",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/nginx-selfsigned.key",
|
||||
"/etc/nginx/nginx-selfsigned.key",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/passwords",
|
||||
"/etc/nginx/passwords",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.waitingFor(new HttpWaitStrategy())
|
||||
.withExposedPorts(81);
|
||||
|
||||
@Override
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
System.setProperty("loki.host", nginx.getHost());
|
||||
System.setProperty("loki.port", nginx.getFirstMappedPort().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendData() {
|
||||
// Given
|
||||
LoggerContext context = loadConfig("appender-test-custom-endpoint-configuration.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderHttpsCustomEndpointTest.class);
|
||||
|
||||
String expectedLogLine = "Hello World";
|
||||
|
||||
// When
|
||||
logger.info(expectedLogLine);
|
||||
|
||||
// Then
|
||||
assertThat(loki)
|
||||
.returns(response -> response
|
||||
.body("data.result.size()", equalTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body(
|
||||
"data.result.values",
|
||||
hasItem(
|
||||
hasItems(
|
||||
hasItems(
|
||||
containsString("p.t.t.l.LokiAppenderHttpsCustomEndpointTest - " + expectedLogLine)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.testcontainers.containers.BindMode;
|
||||
import org.testcontainers.containers.NginxContainer;
|
||||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.IntegrationTest;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
class LokiAppenderHttpsUrlConnectionReverseProxyTest extends IntegrationTest {
|
||||
|
||||
@Container
|
||||
public NginxContainer<?> nginx = new NginxContainer<>("nginx:latest")
|
||||
.withNetwork(network)
|
||||
.withClasspathResourceMapping("loki.reverse.nginx.conf",
|
||||
"/etc/nginx/conf.d/loki.reverse.conf",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/nginx-selfsigned.crt",
|
||||
"/etc/nginx/nginx-selfsigned.crt",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/nginx-selfsigned.key",
|
||||
"/etc/nginx/nginx-selfsigned.key",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.withClasspathResourceMapping("nginx/passwords",
|
||||
"/etc/nginx/passwords",
|
||||
BindMode.READ_ONLY
|
||||
)
|
||||
.waitingFor(new HttpWaitStrategy())
|
||||
.withExposedPorts(81);
|
||||
|
||||
@Override
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
System.setProperty("loki.host", nginx.getHost());
|
||||
System.setProperty("loki.port", nginx.getFirstMappedPort().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSendData() {
|
||||
// Given
|
||||
LoggerContext context = loadConfig("appender-test-url-configuration.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderHttpsUrlConnectionReverseProxyTest.class);
|
||||
|
||||
String expectedLogLine = "Hello World";
|
||||
|
||||
// When
|
||||
logger.info(expectedLogLine);
|
||||
|
||||
// Then
|
||||
assertThat(loki)
|
||||
.returns(response -> response
|
||||
.body("data.result.size()", equalTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body(
|
||||
"data.result.values",
|
||||
hasItem(
|
||||
hasItems(
|
||||
hasItems(
|
||||
containsString("p.t.t.l.LokiAppenderHttpsUrlConnectionReverseProxyTest - " + expectedLogLine)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
package pl.tkowalcz.tjahzi.logback;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.testcontainers.containers.BindMode;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.containers.wait.strategy.Wait;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import pl.tkowalcz.tjahzi.logback.infra.IntegrationTest;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static pl.tkowalcz.tjahzi.logback.infra.LokiAssert.assertThat;
|
||||
|
||||
class LokiAppenderUrlConnectionTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldSendData() {
|
||||
// Given
|
||||
LoggerContext context = loadConfig("appender-test-url-configuration.xml");
|
||||
Logger logger = context.getLogger(LokiAppenderUrlConnectionTest.class);
|
||||
|
||||
String expectedLogLine = "Hello World";
|
||||
|
||||
// When
|
||||
logger.info(expectedLogLine);
|
||||
|
||||
// Then
|
||||
assertThat(loki)
|
||||
.returns(response -> response
|
||||
.body("data.result.size()", equalTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body(
|
||||
"data.result.values",
|
||||
hasItems(
|
||||
hasItems(
|
||||
hasItems(
|
||||
containsString("p.t.tjahzi.logback.LokiAppenderTest - " + expectedLogLine)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -8,6 +8,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testcontainers.containers.BindMode;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.containers.Network;
|
||||
import org.testcontainers.containers.wait.strategy.Wait;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
@@ -19,8 +20,12 @@ import java.net.URISyntaxException;
|
||||
@Testcontainers
|
||||
public class IntegrationTest {
|
||||
|
||||
public static Network network = Network.newNetwork();
|
||||
|
||||
@Container
|
||||
public GenericContainer loki = new GenericContainer("grafana/loki:2.3.0")
|
||||
.withNetwork(network)
|
||||
.withNetworkAliases("loki")
|
||||
.withCommand("-config.file=/etc/loki-config.yaml")
|
||||
.withClasspathResourceMapping("loki-config.yaml",
|
||||
"/etc/loki-config.yaml",
|
||||
@@ -33,7 +38,7 @@ public class IntegrationTest {
|
||||
.withExposedPorts(3100);
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
public void setUp() {
|
||||
System.setProperty("loki.host", loki.getHost());
|
||||
System.setProperty("loki.port", loki.getFirstMappedPort().toString());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<configuration debug="true">
|
||||
<appender name="Loki" class="pl.tkowalcz.tjahzi.logback.LokiAppender">
|
||||
<host>${loki.host}</host>
|
||||
<port>${loki.port}</port>
|
||||
|
||||
<useSSL>true</useSSL>
|
||||
<logEndpoint>/monitoring/loki/api/v1/push</logEndpoint>
|
||||
|
||||
<username>tjahzi</username>
|
||||
<password>tjahzi</password>
|
||||
|
||||
<efficientLayout>
|
||||
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
|
||||
</efficientLayout>
|
||||
|
||||
<header>
|
||||
<name>X-Org-Id</name>
|
||||
<value>Circus</value>
|
||||
</header>
|
||||
|
||||
<label>
|
||||
<name>server</name>
|
||||
<value>127.0.0.1</value>
|
||||
</label>
|
||||
</appender>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="Loki"/>
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -2,6 +2,9 @@
|
||||
<appender name="Loki" class="pl.tkowalcz.tjahzi.logback.LokiAppender">
|
||||
<url>https://${loki.host}:${loki.port}/monitoring/loki/api/v1/push</url>
|
||||
|
||||
<username>tjahzi</username>
|
||||
<password>tjahzi</password>
|
||||
|
||||
<efficientLayout>
|
||||
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
|
||||
</efficientLayout>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
server {
|
||||
listen 81 ssl;
|
||||
ssl_certificate nginx-selfsigned.crt;
|
||||
ssl_certificate_key nginx-selfsigned.key;
|
||||
|
||||
location /monitoring/ {
|
||||
proxy_pass http://loki:3100/;
|
||||
}
|
||||
|
||||
auth_basic "loki auth";
|
||||
auth_basic_user_file /etc/nginx/passwords;
|
||||
}
|
||||
Reference in new issue
Block a user