Files
matrix/cygnus-installer/src/test/java/com/cygnus/installer/MachineAssertionServiceTest.java

61 lines
2.0 KiB
Java

package com.cygnus.installer;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyPairGenerator;
import java.util.Base64;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class MachineAssertionServiceTest {
@TempDir
Path temporaryDirectory;
@Test
void generatesEncryptedAssertionUsingOnlyCloudPublicKey() throws Exception {
var generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
var cloudKeys = generator.generateKeyPair();
Path cloudPublicKey = temporaryDirectory.resolve("cloud-public.pem");
Files.writeString(cloudPublicKey, pem(
"PUBLIC KEY", cloudKeys.getPublic().getEncoded()));
ProductProfile profile = new ProductProfile(
"matrix",
"Matrix",
"matrix-onprem",
"MATRIX_IMAGE",
"matrix",
"installation.yml",
"MATRIX_INSTALLATION_CONFIG",
"/opt/matrix/config/installation.yml",
"8080:8080",
"/matrix/",
"/oauth2/token",
cloudPublicKey,
cloudPublicKey,
"cygnus-login-2026-01",
List.of());
String assertion = new MachineAssertionService().generate(
"matrix-client",
"primary",
"https://cloud.example.com/oauth2/token",
new InstallationKeyService().generate(),
profile);
assertThat(assertion.split("\\.")).hasSize(5);
assertThat(assertion).doesNotContain("matrix-client");
}
private String pem(String type, byte[] encoded) {
return "-----BEGIN " + type + "-----\n"
+ Base64.getMimeEncoder(64, new byte[] {'\n'})
.encodeToString(encoded)
+ "\n-----END " + type + "-----\n";
}
}