-
Notifications
You must be signed in to change notification settings - Fork 226
Remove Bouncy Castle dependency usage from PemUtils #1318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
exceptionfactory
wants to merge
4
commits into
apache:main
Choose a base branch
from
exceptionfactory:remove-bouncycastle-pem-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−10
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b4d63fb
Removed Bouncy Castle dependency usage from PemUtils
exceptionfactory d092337
Reverted LICENSE and NOTICE changes to retain Bouncy Castle
exceptionfactory c1aa0bb
Added tests for empty file and multiple PEM objects
exceptionfactory cf06a5e
Applied spotless formatting
exceptionfactory File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
160 changes: 160 additions & 0 deletions
160
service/common/src/test/java/org/apache/polaris/service/auth/PemUtilsTest.java
This file contains hidden or 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,160 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.polaris.service.auth; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.util.Base64; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class PemUtilsTest { | ||
private static final String RSA_ALGORITHM = "RSA"; | ||
|
||
private static final String PUBLIC_KEY_HEADER = "-----BEGIN PUBLIC KEY-----"; | ||
|
||
private static final String PUBLIC_KEY_FOOTER = "-----END PUBLIC KEY-----"; | ||
|
||
private static final String PRIVATE_KEY_HEADER = "-----BEGIN PRIVATE KEY-----"; | ||
|
||
private static final String PRIVATE_KEY_FOOTER = "-----END PRIVATE KEY-----"; | ||
|
||
private static final String LINE_SEPARATOR = System.lineSeparator(); | ||
|
||
private static final String RSA_PUBLIC_KEY_FILE = "rsa-public-key.pem"; | ||
|
||
private static final String RSA_PRIVATE_KEY_FILE = "rsa-private-key.pem"; | ||
|
||
private static final String RSA_PUBLIC_KEY_AND_PRIVATE_KEY_FILE = "rsa-public-key-pair.pem"; | ||
|
||
private static final String EMPTY_FILE = "empty.pem"; | ||
|
||
private static final Base64.Encoder encoder = Base64.getMimeEncoder(); | ||
|
||
@TempDir private static Path tempDir; | ||
|
||
private static Path rsaRublicKeyPath; | ||
|
||
private static PublicKey rsaPublicKey; | ||
|
||
private static Path rsaPrivateKeyPath; | ||
|
||
private static PrivateKey rsaPrivateKey; | ||
|
||
private static Path rsaPublicKeyAndPrivateKeyPath; | ||
|
||
private static Path emptyFilePath; | ||
|
||
@BeforeAll | ||
public static void setKeyPair() throws NoSuchAlgorithmException, IOException { | ||
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM); | ||
final KeyPair keyPair = keyPairGenerator.generateKeyPair(); | ||
rsaPublicKey = keyPair.getPublic(); | ||
rsaPrivateKey = keyPair.getPrivate(); | ||
|
||
final String publicKeyEncoded = getPublicKeyEncoded(rsaPublicKey); | ||
final String privateKeyEncoded = getPrivateKeyEncoded(rsaPrivateKey); | ||
|
||
rsaRublicKeyPath = tempDir.resolve(RSA_PUBLIC_KEY_FILE); | ||
Files.writeString(rsaRublicKeyPath, publicKeyEncoded); | ||
|
||
rsaPrivateKeyPath = tempDir.resolve(RSA_PRIVATE_KEY_FILE); | ||
Files.writeString(rsaPrivateKeyPath, privateKeyEncoded); | ||
|
||
rsaPublicKeyAndPrivateKeyPath = tempDir.resolve(RSA_PUBLIC_KEY_AND_PRIVATE_KEY_FILE); | ||
final String rsaPublicKeyAndPrivateKey = publicKeyEncoded + LINE_SEPARATOR + privateKeyEncoded; | ||
Files.writeString(rsaPublicKeyAndPrivateKeyPath, rsaPublicKeyAndPrivateKey); | ||
|
||
emptyFilePath = tempDir.resolve(EMPTY_FILE); | ||
Files.write(emptyFilePath, new byte[0]); | ||
} | ||
|
||
@Test | ||
public void testReadPublicKeyFromFileRSA() throws IOException { | ||
final PublicKey publicKeyRead = PemUtils.readPublicKeyFromFile(rsaRublicKeyPath, RSA_ALGORITHM); | ||
|
||
assertEquals(rsaPublicKey, publicKeyRead); | ||
} | ||
|
||
@Test | ||
public void testReadPrivateKeyFromFileRSA() throws IOException { | ||
final PrivateKey privateKeyRead = | ||
PemUtils.readPrivateKeyFromFile(rsaPrivateKeyPath, RSA_ALGORITHM); | ||
|
||
assertEquals(rsaPrivateKey, privateKeyRead); | ||
} | ||
|
||
@Test | ||
public void testReadPublicKeyFromFileRSAWithPrivateKeyIgnored() throws IOException { | ||
final PublicKey publicKeyRead = | ||
PemUtils.readPublicKeyFromFile(rsaPublicKeyAndPrivateKeyPath, RSA_ALGORITHM); | ||
|
||
assertEquals(rsaPublicKey, publicKeyRead); | ||
} | ||
|
||
@Test | ||
public void testReadEmptyFIle() { | ||
assertThrows( | ||
IOException.class, () -> PemUtils.readPublicKeyFromFile(emptyFilePath, RSA_ALGORITHM)); | ||
} | ||
|
||
private static String getPublicKeyEncoded(final PublicKey publicKey) { | ||
final StringBuilder builder = new StringBuilder(); | ||
|
||
builder.append(PUBLIC_KEY_HEADER); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
final byte[] publicKeyEncoded = publicKey.getEncoded(); | ||
final String encoded = encoder.encodeToString(publicKeyEncoded); | ||
builder.append(encoded); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
builder.append(PUBLIC_KEY_FOOTER); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
return builder.toString(); | ||
} | ||
|
||
private static String getPrivateKeyEncoded(final PrivateKey privateKey) { | ||
final StringBuilder builder = new StringBuilder(); | ||
|
||
builder.append(PRIVATE_KEY_HEADER); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
final byte[] privateKeyEncoded = privateKey.getEncoded(); | ||
final String encoded = encoder.encodeToString(privateKeyEncoded); | ||
builder.append(encoded); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
builder.append(PRIVATE_KEY_FOOTER); | ||
builder.append(LINE_SEPARATOR); | ||
|
||
return builder.toString(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One general remark: the original
readPemObject()
method, in principle, only reads the first object in the PEM file:https://downloads.bouncycastle.org/java/docs/bcprov-jdk18on-javadoc/org/bouncycastle/util/io/pem/PemReader.html#readPemObject()
With your changes, what happens when the PEM file contains more than one object? And what if it doesn't contain any? It might be good to add tests for these cases as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback and noting the behavior of
readPemObject()
. I adjusted the new implementation to stop reading when finding a footer line, and added unit tests for that scenario, as well as an empty file, which throws anIOException
.