-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
447 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ds-storage/src/test/java/com/github/bsideup/liiklus/pulsar/PulsarAbstractStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.bsideup.liiklus.pulsar; | ||
|
||
import com.github.bsideup.liiklus.records.RecordStorageTests; | ||
import lombok.Getter; | ||
import org.apache.pulsar.client.impl.Murmur3_32Hash; | ||
import org.apache.pulsar.client.util.MathUtils; | ||
import org.springframework.context.ApplicationContext; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public abstract class PulsarAbstractStorageTest implements RecordStorageTests { | ||
|
||
static final String VERSION = "2.4.0"; | ||
|
||
private static final int NUM_OF_PARTITIONS = 4; | ||
|
||
// Generate a set of keys where each key goes to unique partition | ||
private static Map<Integer, String> PARTITION_KEYS = Mono.fromCallable(() -> UUID.randomUUID().toString()) | ||
.repeat() | ||
.scanWith( | ||
() -> new HashMap<Integer, String>(), | ||
(acc, it) -> { | ||
acc.put(MathUtils.signSafeMod(Murmur3_32Hash.getInstance().makeHash(it), NUM_OF_PARTITIONS), it); | ||
return acc; | ||
} | ||
) | ||
.filter(it -> it.size() == NUM_OF_PARTITIONS) | ||
.blockFirst(Duration.ofSeconds(10)); | ||
|
||
static ApplicationContext applicationContext; | ||
|
||
@Getter | ||
String topic = UUID.randomUUID().toString(); | ||
|
||
@Override | ||
public String keyByPartition(int partition) { | ||
return PARTITION_KEYS.get(partition); | ||
} | ||
|
||
@Override | ||
public int getNumberOfPartitions() { | ||
return NUM_OF_PARTITIONS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/test/java/com/github/bsideup/liiklus/pulsar/PulsarWithTlsAuthRecordsStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.bsideup.liiklus.pulsar; | ||
|
||
import com.github.bsideup.liiklus.ApplicationRunner; | ||
import com.github.bsideup.liiklus.pulsar.container.PulsarTlsContainer; | ||
import com.github.bsideup.liiklus.records.RecordStorageTests; | ||
import com.github.bsideup.liiklus.records.RecordsStorage; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.client.impl.auth.AuthenticationTls; | ||
import org.junit.jupiter.api.AfterAll; | ||
|
||
import java.util.Map; | ||
|
||
public class PulsarWithTlsAuthRecordsStorageTest extends PulsarAbstractStorageTest implements RecordStorageTests { | ||
|
||
private static final PulsarTlsContainer pulsar = new PulsarTlsContainer(VERSION).withTlsAuthentication(); | ||
|
||
static { | ||
pulsar.start(); | ||
|
||
System.getProperties().putAll(Map.of( | ||
"pulsar.serviceUrl", pulsar.getPulsarTlsBrokerUrl(), | ||
"pulsar.tlsTrustCertsFilePath", pulsar.getCaCert().toAbsolutePath().toString(), | ||
"pulsar.authPluginClassName", "org.apache.pulsar.broker.authentication.AuthenticationProviderTls", | ||
"pulsar.authPluginParams", | ||
String.format( | ||
"tlsCertFile:%s,tlsKeyFile:%s", | ||
pulsar.getUserCert().toAbsolutePath().toString(), | ||
pulsar.getUserKey().toAbsolutePath().toString() | ||
) | ||
)); | ||
|
||
applicationContext = new ApplicationRunner("PULSAR", "MEMORY").run(); | ||
} | ||
|
||
@Getter | ||
RecordsStorage target = applicationContext.getBean(RecordsStorage.class); | ||
|
||
@SneakyThrows | ||
public PulsarWithTlsAuthRecordsStorageTest() { | ||
PulsarAdmin pulsarAdmin = PulsarAdmin.builder() | ||
.serviceHttpUrl(pulsar.getHttpsServiceUrl()) | ||
.tlsTrustCertsFilePath(pulsar.getCaCert().toAbsolutePath().toString()) | ||
.authentication(new AuthenticationTls( | ||
pulsar.getUserCert().toAbsolutePath().toString(), | ||
pulsar.getUserKey().toAbsolutePath().toString() | ||
)) | ||
.build(); | ||
|
||
pulsarAdmin.topics().createPartitionedTopic(topic, getNumberOfPartitions()); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
pulsar.stop(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...rage/src/test/java/com/github/bsideup/liiklus/pulsar/PulsarWithTlsRecordsStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.github.bsideup.liiklus.pulsar; | ||
|
||
import com.github.bsideup.liiklus.ApplicationRunner; | ||
import com.github.bsideup.liiklus.pulsar.container.PulsarTlsContainer; | ||
import com.github.bsideup.liiklus.records.RecordStorageTests; | ||
import com.github.bsideup.liiklus.records.RecordsStorage; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.client.impl.auth.AuthenticationTls; | ||
import org.junit.jupiter.api.AfterAll; | ||
|
||
import java.util.Map; | ||
|
||
public class PulsarWithTlsRecordsStorageTest extends PulsarAbstractStorageTest implements RecordStorageTests { | ||
|
||
private static final PulsarTlsContainer pulsar = new PulsarTlsContainer(VERSION).withTlsAuthentication(); | ||
|
||
static { | ||
pulsar.start(); | ||
|
||
System.getProperties().putAll(Map.of( | ||
"pulsar.serviceUrl", pulsar.getPulsarTlsBrokerUrl(), | ||
"pulsar.tlsTrustCertsFilePath", pulsar.getCaCert().toAbsolutePath().toString() | ||
)); | ||
applicationContext = new ApplicationRunner("PULSAR", "MEMORY").run(); | ||
} | ||
|
||
@Getter | ||
RecordsStorage target = applicationContext.getBean(RecordsStorage.class); | ||
|
||
@SneakyThrows | ||
public PulsarWithTlsRecordsStorageTest() { | ||
PulsarAdmin pulsarAdmin = PulsarAdmin.builder() | ||
.serviceHttpUrl(pulsar.getHttpsServiceUrl()) | ||
.tlsTrustCertsFilePath(pulsar.getCaCert().toAbsolutePath().toString()) | ||
.authentication(new AuthenticationTls( | ||
pulsar.getUserCert().toAbsolutePath().toString(), | ||
pulsar.getUserKey().toAbsolutePath().toString() | ||
)) | ||
.build(); | ||
|
||
pulsarAdmin.topics().createPartitionedTopic(topic, getNumberOfPartitions()); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
pulsar.stop(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...storage/src/test/java/com/github/bsideup/liiklus/pulsar/container/PulsarTlsContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.bsideup.liiklus.pulsar.container; | ||
|
||
import org.testcontainers.containers.PulsarContainer; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
|
||
public class PulsarTlsContainer extends PulsarContainer { | ||
|
||
public PulsarTlsContainer(String pulsarVersion) { | ||
super(pulsarVersion); | ||
withExposedPorts(BROKER_HTTP_PORT, BROKER_PORT, 6651, 8443); | ||
withEnv("PULSAR_PREFIX_brokerServicePortTls", "6651"); | ||
withEnv("PULSAR_PREFIX_webServicePortTls", "8443"); | ||
withEnv("PULSAR_PREFIX_tlsEnabled", "true"); | ||
withEnv("PULSAR_PREFIX_tlsCertificateFilePath", "/pulsar/broker.cert.pem"); | ||
withEnv("PULSAR_PREFIX_tlsKeyFilePath", "/pulsar/broker.key-pk8.pem"); | ||
withEnv("PULSAR_PREFIX_tlsTrustCertsFilePath", "/pulsar/ca.cert.pem"); | ||
|
||
Path resourceDirectory = getTestCertsFolder(); | ||
Arrays.stream(resourceDirectory.toFile().listFiles()) | ||
.forEach(certFile -> { | ||
var certFilePath = certFile.toPath().toAbsolutePath(); | ||
var fileName = certFilePath.getFileName().toString(); | ||
withCopyFileToContainer( | ||
MountableFile.forHostPath(certFilePath), | ||
"/pulsar/" + fileName | ||
); | ||
}); | ||
|
||
setCommand( | ||
"/bin/bash", | ||
"-c", | ||
"bin/apply-config-from-env.py conf/standalone.conf && bin/pulsar standalone --no-functions-worker -nss" | ||
); | ||
} | ||
|
||
private Path getTestCertsFolder() { | ||
return Paths.get("src", "test", "resources", "certs"); | ||
} | ||
|
||
public Path getCaCert() { | ||
return getTestCertsFolder().resolve("ca.cert.pem"); | ||
} | ||
|
||
public Path getUserCert() { | ||
return getTestCertsFolder().resolve("user1.cert.pem"); | ||
} | ||
|
||
public Path getUserKey() { | ||
return getTestCertsFolder().resolve("user1.key-pk8.pem"); | ||
} | ||
|
||
public PulsarTlsContainer withTlsAuthentication() { | ||
withEnv("PULSAR_PREFIX_authenticationEnabled", "true"); | ||
withEnv("PULSAR_PREFIX_authenticationProviders", "org.apache.pulsar.broker.authentication.AuthenticationProviderTls"); | ||
return this; | ||
} | ||
|
||
public String getPulsarTlsBrokerUrl() { | ||
return String.format("pulsar+ssl://%s:%s", getContainerIpAddress(), getMappedPort(6651)); | ||
} | ||
|
||
public String getHttpsServiceUrl() { | ||
return String.format("https://%s:%s", getContainerIpAddress(), getMappedPort(8443)); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
plugins/pulsar-records-storage/src/test/resources/certs/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Files are taken from Pulsar's example: | ||
https://github.com/apache/pulsar/tree/master/tests/docker-images/latest-version-image/ssl |
Oops, something went wrong.