Skip to content

fix for registration conflicts with BouncyCastleProvider #864

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-

### Fixed
-
- Fixed bouncy castle registration issue by using local instance instead of global registration with java security API.

---
*Note: When making changes, please add your change under the appropriate section with a brief description.*
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.Security;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.sql.Timestamp;
Expand Down Expand Up @@ -118,7 +117,8 @@ public JwtPrivateKeyClientCredentials build() {
}
}

private final String BOUNCY_CASTLE_PROVIDER = "BC";
private final BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();

private IDatabricksHttpClient hc;
private String clientId;
private String tokenUrl;
Expand Down Expand Up @@ -235,13 +235,10 @@ JWSAlgorithm determineSignatureAlgorithm(String jwtAlgorithm) {
}

private PrivateKey getPrivateKey() {
try {
Security.addProvider(new BouncyCastleProvider());
try (Reader reader = new FileReader(jwtKeyFile);
PEMParser pemParser = new PEMParser(reader)) {
Object object = pemParser.readObject();
return convertPrivateKey(object);
}
try (Reader reader = new FileReader(jwtKeyFile);
PEMParser pemParser = new PEMParser(reader)) {
Object object = pemParser.readObject();
return convertPrivateKey(object);
} catch (DatabricksSQLException | IOException e) {
String errorMessage = "Failed to parse private key: " + e.getMessage();
LOGGER.error(errorMessage);
Expand All @@ -257,7 +254,7 @@ PrivateKey convertPrivateKey(Object pemObject) throws DatabricksParsingException
PKCS8EncryptedPrivateKeyInfo encryptedKeyInfo = (PKCS8EncryptedPrivateKeyInfo) pemObject;
JceOpenSSLPKCS8DecryptorProviderBuilder decryptorProviderBuilder =
new JceOpenSSLPKCS8DecryptorProviderBuilder();
decryptorProviderBuilder.setProvider(BOUNCY_CASTLE_PROVIDER);
decryptorProviderBuilder.setProvider(bouncyCastleProvider);
InputDecryptorProvider decryptorProvider =
decryptorProviderBuilder.build(jwtKeyPassphrase.toCharArray());
privateKeyInfo = encryptedKeyInfo.decryptPrivateKeyInfo(decryptorProvider);
Expand All @@ -269,8 +266,7 @@ PrivateKey convertPrivateKey(Object pemObject) throws DatabricksParsingException
privateKeyInfo = (PrivateKeyInfo) pemObject;
}
}
JcaPEMKeyConverter keyConverter =
new JcaPEMKeyConverter().setProvider(BOUNCY_CASTLE_PROVIDER);
JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter().setProvider(bouncyCastleProvider);
return keyConverter.getPrivateKey(privateKeyInfo);
} catch (OperatorCreationException | PKCSException | PEMException e) {
String errorMessage = "Cannot decrypt private JWT key " + e.getMessage();
Expand Down
Loading