207 lines
8.7 KiB
Java
207 lines
8.7 KiB
Java
package com.cygnus.installer;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
import java.net.URI;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.time.OffsetDateTime;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
|
|
class DeploymentWriterTest {
|
|
|
|
@TempDir
|
|
Path temporaryDirectory;
|
|
|
|
@Test
|
|
void writesPortableProductDeploymentAndKeepsSecretsLocal() throws Exception {
|
|
UUID tenantId = UUID.randomUUID();
|
|
UUID installationId = UUID.randomUUID();
|
|
UUID installationUuid = UUID.randomUUID();
|
|
var validation = new ActivationDtos.ValidationResponse(
|
|
"activation-token",
|
|
OffsetDateTime.now().plusMinutes(5),
|
|
tenantId,
|
|
"matrix-client",
|
|
"FULL",
|
|
2);
|
|
var registration = new ActivationDtos.RegistrationResponse(
|
|
installationId,
|
|
installationUuid,
|
|
tenantId,
|
|
null,
|
|
"primary",
|
|
1,
|
|
"ACTIVE");
|
|
var profile = new ProductProfile(
|
|
"matrix",
|
|
"Matrix",
|
|
"matrix-onprem",
|
|
"MATRIX_IMAGE",
|
|
"matrix",
|
|
"installation.yml",
|
|
"MATRIX_INSTALLATION_CONFIG",
|
|
"/srv/matrix/config/installation.yml",
|
|
"8080:8080",
|
|
"/matrix/",
|
|
"/oauth2/token",
|
|
temporaryDirectory.resolve("unused.pem"),
|
|
temporaryDirectory.resolve("login-public.pem"),
|
|
"cygnus-login-2026-01",
|
|
List.of("region"));
|
|
Files.writeString(profile.loginEncryptionPublicKey(), "login-public-key");
|
|
var settings = new InstallerSettings(
|
|
"matrix",
|
|
URI.create("https://cloud.example.com"),
|
|
URI.create("http://host.docker.internal:8090"),
|
|
"/api/v1/installations",
|
|
"production",
|
|
temporaryDirectory,
|
|
temporaryDirectory,
|
|
"1",
|
|
new IniDocument(Map.of()));
|
|
|
|
Path output = new DeploymentWriter().write(
|
|
temporaryDirectory.resolve("output"),
|
|
installationUuid,
|
|
"primary",
|
|
"matrix-client",
|
|
validation,
|
|
registration,
|
|
new InstallationKeyService().generate(),
|
|
"encrypted.assertion.value",
|
|
settings,
|
|
profile,
|
|
"registry.example.com/matrix:1.0.0",
|
|
Map.of("region", "north"),
|
|
new RuntimeConfiguration(
|
|
"http://host.docker.internal:8090",
|
|
"jdbc:postgresql://db:5432/matrix",
|
|
"postgres",
|
|
"db-secret",
|
|
"redis",
|
|
"7901",
|
|
"redis-secret",
|
|
false,
|
|
"PT10S",
|
|
"PT30S"));
|
|
|
|
String config = Files.readString(output.resolve("config/installation.yml"));
|
|
String compose = Files.readString(output.resolve("compose.yml"));
|
|
assertThat(config)
|
|
.contains("client-id: \"matrix-client\"")
|
|
.contains("installation-id: \"" + installationId + "\"")
|
|
.contains("cloud-url: \"http://host.docker.internal:8090\"")
|
|
.contains("token-url: \"http://host.docker.internal:8090/oauth2/token\"")
|
|
.contains("region: \"north\"")
|
|
.doesNotContain("activation-token");
|
|
assertThat(compose)
|
|
.contains("matrix-onprem:")
|
|
.contains("- \"8080:8080\"")
|
|
.contains("./config:/srv/matrix/config:ro")
|
|
.contains("MATRIX_INSTALLATION_CONFIG");
|
|
assertThat(Files.readString(output.resolve(".env")))
|
|
.contains("MATRIX_IMAGE=\"registry.example.com/matrix:1.0.0\"")
|
|
.contains("MATRIX_DB_URL=\"jdbc:postgresql://db:5432/matrix\"")
|
|
.contains("REDIS_HOST=\"redis\"")
|
|
.contains("REDIS_DATABASE=\"1\"")
|
|
.contains("CYGNUS_CLOUD_BASE_URL=\"http://host.docker.internal:8090\"")
|
|
.contains("CYGNUS_TOKEN_URL=\"http://host.docker.internal:8090/oauth2/token\"")
|
|
.contains("CYGNUS_CLIENT_ID=\"matrix-client\"")
|
|
.contains("CYGNUS_INSTALLATION_ID=\"primary\"")
|
|
.contains("CYGNUS_CLIENT_ASSERTION=\"file:/srv/matrix/config/machine-assertion.jwt\"")
|
|
.contains("CYGNUS_LOGIN_PUBLIC_KEY=\"file:/srv/matrix/config/keys/login-public.pem\"");
|
|
assertThat(output.resolve("config/keys/login-public.pem"))
|
|
.hasContent("login-public-key");
|
|
assertThat(output.resolve("config/machine-assertion.jwt")).hasContent(
|
|
"encrypted.assertion.value");
|
|
assertThat(output.resolve("config/keys/client-signing-private.pem"))
|
|
.isRegularFile();
|
|
}
|
|
|
|
@Test
|
|
void refusesToOverwriteCloudServiceConfiguration() throws Exception {
|
|
Path cloudConfig = temporaryDirectory.resolve("config");
|
|
Path cloudKeys = cloudConfig.resolve("keys");
|
|
Files.createDirectories(cloudKeys);
|
|
Path assertionPublic = cloudKeys.resolve("assertion-decryption-public.pem");
|
|
Path loginPublic = cloudKeys.resolve("login-public.pem");
|
|
Files.writeString(assertionPublic, "assertion-public-key");
|
|
Files.writeString(loginPublic, "login-public-key");
|
|
|
|
var profile = new ProductProfile(
|
|
"matrix",
|
|
"Matrix",
|
|
"matrix-onprem",
|
|
"MATRIX_IMAGE",
|
|
"matrix",
|
|
"installation.yml",
|
|
"MATRIX_INSTALLATION_CONFIG",
|
|
"/srv/matrix/config/installation.yml",
|
|
"8080:8080",
|
|
"/matrix/",
|
|
"/oauth2/token",
|
|
assertionPublic,
|
|
loginPublic,
|
|
"cygnus-login-2026-01",
|
|
List.of());
|
|
|
|
assertThatThrownBy(() -> new DeploymentWriter().write(
|
|
temporaryDirectory,
|
|
UUID.randomUUID(),
|
|
"primary",
|
|
"matrix-client",
|
|
new ActivationDtos.ValidationResponse(
|
|
"activation-token",
|
|
OffsetDateTime.now().plusMinutes(5),
|
|
UUID.randomUUID(),
|
|
"matrix-client",
|
|
"FULL",
|
|
2),
|
|
new ActivationDtos.RegistrationResponse(
|
|
UUID.randomUUID(),
|
|
UUID.randomUUID(),
|
|
UUID.randomUUID(),
|
|
null,
|
|
"primary",
|
|
1,
|
|
"ACTIVE"),
|
|
new InstallationKeyService().generate(),
|
|
"assertion",
|
|
new InstallerSettings(
|
|
"matrix",
|
|
URI.create("https://cloud.example.com"),
|
|
URI.create("https://cloud.example.com"),
|
|
"/api/v1/installations",
|
|
"production",
|
|
temporaryDirectory,
|
|
temporaryDirectory,
|
|
"1",
|
|
new IniDocument(Map.of())),
|
|
profile,
|
|
"registry.example.com/matrix:1.0.0",
|
|
Map.of(),
|
|
new RuntimeConfiguration(
|
|
"https://cloud.example.com",
|
|
"jdbc:postgresql://db/matrix",
|
|
"postgres",
|
|
"secret",
|
|
"redis",
|
|
"7901",
|
|
"secret",
|
|
false,
|
|
"PT10S",
|
|
"PT30S")))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.hasMessageContaining("cloud-service configuration");
|
|
|
|
assertThat(assertionPublic).hasContent("assertion-public-key");
|
|
assertThat(loginPublic).hasContent("login-public-key");
|
|
}
|
|
}
|