Skip to content

Commit

Permalink
fix getting pem file from jar
Browse files Browse the repository at this point in the history
  • Loading branch information
rodesai committed Nov 19, 2024
1 parent d5056d5 commit 8259dd0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public synchronized void start() {
E2ETestUtils.maybeCreateKeyspace(properties);
// build topology after creating keyspace because we use keyspace retry
// to wait for scylla to resolve
properties.put(ResponsiveConfig.PLATFORM_API_KEY_CONFIG, "test-api-key");
//properties.put(ResponsiveConfig.PLATFORM_API_KEY_CONFIG, "test-api-key");
properties.put(ResponsiveConfig.RESPONSIVE_LICENSE_CONFIG, "ewogICJ2ZXJzaW9uIiA6ICIxIiwKICAiaW5mbyIgOiAiZXlKMGVYQmxJam9pZEdsdFpXUmZkSEpwWVd4ZmRqRWlMQ0psYldGcGJDSTZJblJsYzNSQWNtVnpjRzl1YzJsMlpTNWtaWFlpTENKcGMzTjFaV1JCZENJNk1UY3pNakEwTlRVNU1pd2laWGh3YVhKbGMwRjBJam96TVRVMU5qZzRPVGcyTkRRd016RTVPWDA9IiwKICAic2lnbmF0dXJlIiA6ICJnVnQ1bWZ1eFhKK2k2YkNqSTJrMkh4ZFozUzJwamN4T2ZxcVh5elM0WjcvUUZUNmJzNWt1MWhtSzZicmZaZHIxSVZLM0NmeXZyWWI0YVNVS2dRZ1ZLaDdBZzVmVEZvSE4ybGRRSklUUFdVQ3dxTFZaQVpwdy80cVQrbmdZSWFNZVhTVUFQL1hueTNTVm1pTFpvMlJpK2FlN05BYXNXR05aNEVwODhPREwzSEh0SkJjcGFYWTlheGZKUmtoYzlvbEx0T0Y2cWhtWlRzcmozZjdpOU9IWmsvQVFYU2JuS2RVMGprSkxMNTIwVUNqeFJxek8rc25sL28zWGM2eDg1ZlBTbFFxTHBLL043Sk9ZSVpwemR1Q1hEQkRFZUZaZTVvZ1h6dkpvN1VJTkkrbUFKTzM1Z2F0WWF2K0VqaERMR2x3TzlXZzBVRHFPNkJaRzhmVVlKeEt0UXhYSUFyTUhrVHpRM3VFODJWMUw2dkdDZVBiTHFxMGtFdkpBQ1N5WnFUOE56VjVjM3B3cEhIVXRVdmE4NnpTMllRTkJXRy9TMkJsdTVvY2JKQ3hDQ0lybUJWYnROTmdtTFVEblpUUzg3ejR0ZFRrZklsWkZOZjZpUTBMdVBqRlUxMm9yRWhkQmFkaU1Cc20reU5HS3pJQXZXb24zV0V2ZGE0RGxYcm5pa2dlcDlFV0RIcWxFdUVVWXkxWEFaRGJqM0pEbzZ0OHN5YUpxR01IUktPbTc0R21mMllmVE5NZzdaUElmWFE1VksvTVBXcEdyamZtcmhITEEvQmhBQzh5ZUJQTTFUNXFKM1NQRTJ4dFZNWnlHb1g0cFh0Y2hqZzFYSXgxNXo1UWFpdzA4RExGa3gydkdJWXNXb3NDblNMTCs4Z1l4dXFPbkU3R2ovdXhEeHA2M2pFMD0iLAogICJrZXkiIDogImxpY2Vuc2Utc2lnbmluZy1rZXktMCIsCiAgImFsZ28iIDogIlJTQVNTQV9QU1NfU0hBXzI1NiIKfQo=");
E2ETestUtils.retryFor(
() -> kafkaStreams = buildTopology(properties),
Duration.ofMinutes(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import dev.responsive.kafka.internal.license.model.LicenseDocumentV1;
import dev.responsive.kafka.internal.license.model.LicenseInfo;
import dev.responsive.kafka.internal.license.model.SigningKeys;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -87,13 +85,7 @@ private byte[] verifyLicenseV1Signature(final LicenseDocumentV1 license) {
}

private PublicKey loadPublicKey(final SigningKeys.SigningKey signingKey) {
final File file;
try {
file = new File(this.getClass().getResource(signingKey.path()).toURI());
} catch (final URISyntaxException e) {
throw new RuntimeException(e);
}
final byte[] publicKeyBytes = PublicKeyPemFileParser.parsePemFile(file);
final byte[] publicKeyBytes = PublicKeyPemFileParser.parsePemFileInResource(signingKey.path());
final KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("RSA");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package dev.responsive.kafka.internal.license;

import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

public class PublicKeyPemFileParser {
private static final String HEADER_PREFIX = "-----";
Expand All @@ -31,13 +33,19 @@ public class PublicKeyPemFileParser {
private static final String END_PUBLIC_KEY_HEADER
= HEADER_PREFIX + END_PUBLIC_KEY + HEADER_PREFIX;

public static byte[] parsePemFile(final File file) {
final List<String> lines;
try {
lines = Files.readAllLines(file.toPath());
} catch (IOException e) {
public static byte[] parsePemFileInResource(final String path) {
try (final InputStream inputStream = PublicKeyPemFileParser.class.getResourceAsStream(path)) {
return parsePemFile(inputStream);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

private static byte[] parsePemFile(final InputStream inputStream) throws IOException {
final List<String> lines;
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
lines = reader.lines().collect(Collectors.toList());
}
final StringBuilder keyB64Builder = new StringBuilder();
boolean foundBegin = false;
for (final String l : lines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test;

class PublicKeyPemFileParserTest {

@Test
public void shouldParseValidPemFile() {
// given:
final File file = getPemFile("valid.pem");
final String file = getPemFile("valid.pem");

// when:
final byte[] key = PublicKeyPemFileParser.parsePemFile(file);
final byte[] key = PublicKeyPemFileParser.parsePemFileInResource(file);

// then:
assertThat(new String(key), is("foobarbaz"));
Expand All @@ -41,10 +39,10 @@ public void shouldParseValidPemFile() {
@Test
public void shouldParsePemFileWithRealKey() {
// given:
final File file = getPemFile("valid-real.pem");
final String file = getPemFile("valid-real.pem");

// when:
final byte[] key = PublicKeyPemFileParser.parsePemFile(file);
final byte[] key = PublicKeyPemFileParser.parsePemFileInResource(file);

// then:
assertThat(key.length, is(550));
Expand All @@ -53,10 +51,10 @@ public void shouldParsePemFileWithRealKey() {
@Test
public void shouldParseValidPemFileWithComment() {
// given:
final File file = getPemFile("valid-with-comment.pem");
final String file = getPemFile("valid-with-comment.pem");

// when:
final byte[] key = PublicKeyPemFileParser.parsePemFile(file);
final byte[] key = PublicKeyPemFileParser.parsePemFileInResource(file);

// then:
assertThat(new String(key), is("foobarbaz"));
Expand All @@ -65,29 +63,24 @@ public void shouldParseValidPemFileWithComment() {
@Test
public void shouldFailToParseInvalidPemFileWithMissingFooter() {
// given:
final File file = getPemFile("invalid-missing-footer.pem");
final String file = getPemFile("invalid-missing-footer.pem");

// when/then:
assertThrows(IllegalArgumentException.class, () -> PublicKeyPemFileParser.parsePemFile(file));
assertThrows(
IllegalArgumentException.class, () -> PublicKeyPemFileParser.parsePemFileInResource(file));
}

@Test
public void shouldFailToParseInvalidPemFileWithMissingHeader() {
// given:
final File file = getPemFile("invalid-missing-header.pem");
final String file = getPemFile("invalid-missing-header.pem");

// when/then:
assertThrows(IllegalArgumentException.class, () -> PublicKeyPemFileParser.parsePemFile(file));
assertThrows(
IllegalArgumentException.class, () -> PublicKeyPemFileParser.parsePemFileInResource(file));
}

private File getPemFile(final String filename) {
final String path = "public-key-pem-file-parser-test/" + filename;
try {
return new File(
PublicKeyPemFileParserTest.class.getClassLoader().getResource(path).toURI()
);
} catch (final URISyntaxException e) {
throw new RuntimeException(e);
}
private String getPemFile(final String filename) {
return "/public-key-pem-file-parser-test/" + filename;
}
}

0 comments on commit 8259dd0

Please sign in to comment.