Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from thomasgl-orange/JENKINS-47967
[JENKINS-47967] workaround JCE Providers memory leak
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
50 changes: 50 additions & 0 deletions
50
jclouds-plugin/src/main/java/jenkins/plugins/jclouds/modules/JenkinsBouncyCastleCrypto.java
This file contains 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,50 @@ | ||
package jenkins.plugins.jclouds.modules; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.security.Security; | ||
import java.security.cert.CertificateException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.inject.Singleton; | ||
|
||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.jclouds.crypto.Crypto; | ||
import org.jclouds.encryption.bouncycastle.BouncyCastleCrypto; | ||
import org.jclouds.encryption.internal.JCECrypto; | ||
|
||
/** | ||
* A JCloud <code>{@link Crypto}</code> implementation, similar to <code>{@link BouncyCastleCrypto}</code>, but which | ||
* always reuse the same <code>{@link BouncyCastleProvider}</code> instance, to avoid a memory leak. | ||
*/ | ||
@Singleton | ||
public class JenkinsBouncyCastleCrypto extends JCECrypto { | ||
|
||
/* The only instance of BouncyCastleProvider we'll ever use in JClouds contexts. | ||
* It may even be an already registered instance (Jenkins adds one on startup, | ||
* when initializing the bouncycastle-api plugin). */ | ||
private static final BouncyCastleProvider BC_PROVIDER; | ||
static { | ||
final BouncyCastleProvider myBCProvider = new BouncyCastleProvider(); | ||
final Provider installedProvider = Security.getProvider(myBCProvider.getName()); | ||
if (installedProvider != null && installedProvider.getClass().equals(BouncyCastleProvider.class)) { | ||
BC_PROVIDER = (BouncyCastleProvider) installedProvider; | ||
} else { | ||
BC_PROVIDER = myBCProvider; | ||
} | ||
} | ||
|
||
public JenkinsBouncyCastleCrypto() throws NoSuchAlgorithmException, CertificateException { | ||
super(BC_PROVIDER); | ||
} | ||
|
||
/** | ||
* @see org.jclouds.encryption.bouncycastle.BouncyCastleCrypto | ||
*/ | ||
@Override | ||
public Cipher cipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException { | ||
return super.cipher("RSA".equals(algorithm) ? "RSA/NONE/PKCS1Padding" : algorithm); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
jclouds-plugin/src/main/java/jenkins/plugins/jclouds/modules/JenkinsConfigurationModule.java
This file contains 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,25 @@ | ||
package jenkins.plugins.jclouds.modules; | ||
|
||
import org.jclouds.concurrent.config.ConfiguresExecutorService; | ||
import org.jclouds.concurrent.config.ExecutorServiceModule; | ||
import org.jclouds.crypto.Crypto; | ||
import org.jclouds.date.joda.config.JodaDateServiceModule; | ||
import org.jclouds.netty.config.NettyPayloadModule; | ||
|
||
/** | ||
* Similar to <code>{@link org.jclouds.enterprise.config.EnterpriseConfigurationModule}</code>, | ||
* but enables our own <code>JenkinsBouncyCastleCrypto</code> module instead of upstream's | ||
* <code>BouncyCastleCrypto</code>. This is to avoid memory leak with new instances of | ||
* <code>BouncyCastleProvider</code> getting registered each time a JClouds context is built. | ||
*/ | ||
@ConfiguresExecutorService | ||
public class JenkinsConfigurationModule extends ExecutorServiceModule { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(Crypto.class).to(JenkinsBouncyCastleCrypto.class); | ||
install(new JodaDateServiceModule()); | ||
install(new NettyPayloadModule()); | ||
} | ||
|
||
} |