From b005e3aeb0f34efc6ac70312fdd8fbcd4edea3b7 Mon Sep 17 00:00:00 2001 From: Weimin Yu Date: Tue, 12 Nov 2019 11:14:51 -0500 Subject: [PATCH] Fix a few lint errors (#361) Replace deprecated bouncycastle class in SslInitializerTestUils. Generic array as vargs: worked around it in ProbingAction and removed unused method in CircularList. --- .../blackbox/connection/ProbingAction.java | 56 ++++++++++--------- .../handler/SslInitializerTestUtils.java | 43 ++++++++++---- .../google/registry/util/CircularList.java | 8 --- 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/prober/src/main/java/google/registry/monitoring/blackbox/connection/ProbingAction.java b/prober/src/main/java/google/registry/monitoring/blackbox/connection/ProbingAction.java index f7f96ffca..c9c9740e7 100644 --- a/prober/src/main/java/google/registry/monitoring/blackbox/connection/ProbingAction.java +++ b/prober/src/main/java/google/registry/monitoring/blackbox/connection/ProbingAction.java @@ -155,33 +155,37 @@ public abstract class ProbingAction implements Callable { // Write appropriate outboundMessage to pipeline ChannelFuture unusedFutureWriteAndFlush = channel().writeAndFlush(outboundMessage()); - channelFuture.addListeners( - future -> { - if (future.isSuccess()) { - ChannelFuture unusedFuture = finished.setSuccess(); - } else { - ChannelFuture unusedFuture = finished.setFailure(future.cause()); - } - }, - // If we don't have a persistent connection, close the connection to this - // channel - future -> { - if (!protocol().persistentConnection()) { + channelFuture + .addListener( + future -> { + if (future.isSuccess()) { + ChannelFuture unusedFuture = finished.setSuccess(); + } else { + ChannelFuture unusedFuture = finished.setFailure(future.cause()); + } + }) + .addListener( + // If we don't have a persistent connection, close the connection to + // this + // channel + future -> { + if (!protocol().persistentConnection()) { - ChannelFuture closedFuture = channel().close(); - closedFuture.addListener( - f -> { - if (f.isSuccess()) { - logger.atInfo().log( - "Closed stale channel. Moving on to next ProbingStep"); - } else { - logger.atWarning().log( - "Issue closing stale channel. Most likely already " - + "closed."); - } - }); - } - }); + ChannelFuture closedFuture = channel().close(); + closedFuture.addListener( + f -> { + if (f.isSuccess()) { + logger.atInfo().log( + "Closed stale channel. Moving on to next" + + " ProbingStep"); + } else { + logger.atWarning().log( + "Issue closing stale channel. Most likely already " + + "closed."); + } + }); + } + }); }, delay().getStandardSeconds(), TimeUnit.SECONDS); diff --git a/prober/src/test/java/google/registry/monitoring/blackbox/handler/SslInitializerTestUtils.java b/prober/src/test/java/google/registry/monitoring/blackbox/handler/SslInitializerTestUtils.java index 1242e9b0d..cb3bcb157 100644 --- a/prober/src/test/java/google/registry/monitoring/blackbox/handler/SslInitializerTestUtils.java +++ b/prober/src/test/java/google/registry/monitoring/blackbox/handler/SslInitializerTestUtils.java @@ -29,9 +29,18 @@ import java.time.Duration; import java.time.Instant; import java.util.Date; import javax.net.ssl.SSLSession; -import javax.security.auth.x500.X500Principal; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.cert.X509CertificateHolder; +import org.bouncycastle.cert.X509v3CertificateBuilder; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.bouncycastle.x509.X509V3CertificateGenerator; +import org.bouncycastle.operator.ContentSigner; +import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; +import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder; +import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder; /** Utility class that provides methods used by {@link SslClientInitializerTest} */ public class SslInitializerTestUtils { @@ -53,16 +62,26 @@ public class SslInitializerTestUtils { */ public static X509Certificate signKeyPair( SelfSignedCertificate ssc, KeyPair keyPair, String hostname) throws Exception { - X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); - X500Principal dnName = new X500Principal("CN=" + hostname); - certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); - certGen.setSubjectDN(dnName); - certGen.setIssuerDN(ssc.cert().getSubjectX500Principal()); - certGen.setNotBefore(Date.from(Instant.now().minus(Duration.ofDays(1)))); - certGen.setNotAfter(Date.from(Instant.now().plus(Duration.ofDays(1)))); - certGen.setPublicKey(keyPair.getPublic()); - certGen.setSignatureAlgorithm("SHA256WithRSAEncryption"); - return certGen.generate(ssc.key(), "BC"); + X500Name subjectDnName = new X500Name("CN=" + hostname); + BigInteger serialNumber = (BigInteger.valueOf(System.currentTimeMillis())); + X500Name issuerDnName = new X500Name(ssc.cert().getIssuerDN().getName()); + Date from = Date.from(Instant.now().minus(Duration.ofDays(1))); + Date to = Date.from(Instant.now().plus(Duration.ofDays(1))); + SubjectPublicKeyInfo subPubKeyInfo = + SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()); + AlgorithmIdentifier sigAlgId = + new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSAEncryption"); + AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); + + ContentSigner sigGen = + new BcRSAContentSignerBuilder(sigAlgId, digAlgId) + .build(PrivateKeyFactory.createKey(ssc.key().getEncoded())); + X509v3CertificateBuilder v3CertGen = + new X509v3CertificateBuilder( + issuerDnName, serialNumber, from, to, subjectDnName, subPubKeyInfo); + + X509CertificateHolder certificateHolder = v3CertGen.build(sigGen); + return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certificateHolder); } /** diff --git a/util/src/main/java/google/registry/util/CircularList.java b/util/src/main/java/google/registry/util/CircularList.java index 729a5cd23..0be8e0d76 100644 --- a/util/src/main/java/google/registry/util/CircularList.java +++ b/util/src/main/java/google/registry/util/CircularList.java @@ -92,14 +92,6 @@ public class CircularList { return this; } - /** Simply calls {@code addElement}, for each element in {@code elements}. */ - public AbstractBuilder add(T... values) { - for (T element : values) { - add(element); - } - return this; - } - /** Simply calls {@code addElement}, for each element in {@code elements}. */ public AbstractBuilder add(Iterable values) { values.forEach(this::add);