2121
2222import com .google .common .annotations .VisibleForTesting ;
2323import com .google .common .base .Optional ;
24- import com .google .common .base .Preconditions ;
2524import com .google .errorprone .annotations .ForOverride ;
2625import io .grpc .Attributes ;
2726import io .grpc .CallCredentials ;
8483import java .security .cert .X509Certificate ;
8584import java .util .Arrays ;
8685import java .util .EnumSet ;
87- import java .util .LinkedHashMap ;
8886import java .util .List ;
89- import java .util .Map ;
9087import java .util .Set ;
9188import java .util .concurrent .Executor ;
9289import java .util .logging .Level ;
9390import java .util .logging .Logger ;
9491import javax .annotation .Nonnull ;
9592import javax .annotation .Nullable ;
96- import javax .annotation .concurrent .GuardedBy ;
9793import javax .net .ssl .SSLEngine ;
9894import javax .net .ssl .SSLException ;
9995import javax .net .ssl .SSLParameters ;
@@ -589,31 +585,27 @@ protected void userEventTriggered0(ChannelHandlerContext ctx, Object evt) throws
589585 }
590586
591587 static final class ClientTlsProtocolNegotiator implements ProtocolNegotiator {
592- private static final Logger logger = Logger .getLogger (ClientTlsProtocolNegotiator .class .getName ());
588+ private static final Logger logger =
589+ Logger .getLogger (ClientTlsProtocolNegotiator .class .getName ());
593590 private static final Method checkServerTrustedMethod ;
591+
594592 static {
595593 Method method = null ;
596594 try {
597- Class <?> x509ExtendedTrustManagerClass = Class .forName ("javax.net.ssl.X509ExtendedTrustManager" );
595+ Class <?> x509ExtendedTrustManagerClass =
596+ Class .forName ("javax.net.ssl.X509ExtendedTrustManager" );
598597 method = x509ExtendedTrustManagerClass .getMethod ("checkServerTrusted" ,
599598 X509Certificate [].class , String .class , SSLEngine .class );
600599 } catch (ClassNotFoundException e ) {
600+ // Per-rpc authority overriding via call options will be disallowed.
601601 } catch (NoSuchMethodException e ) {
602602 // Should never happen.
603- logger .log (Level .WARNING , "Method checkServerTrusted not found in " +
604- "javax.net.ssl.X509ExtendedTrustManager" , e );
603+ logger .log (Level .WARNING , "Method checkServerTrusted not found in "
604+ + "javax.net.ssl.X509ExtendedTrustManager" , e );
605605 }
606606 checkServerTrustedMethod = method ;
607607 }
608608
609- @ GuardedBy ("this" )
610- private final LinkedHashMap <String , Status > peerVerificationResults =
611- new LinkedHashMap <String , Status >() {
612- @ Override
613- protected boolean removeEldestEntry (Map .Entry <String , Status > eldest ) {
614- return size () > 100 ;
615- }
616- };
617609 private SSLEngine sslEngine ;
618610
619611 public ClientTlsProtocolNegotiator (SslContext sslContext ,
@@ -656,32 +648,25 @@ public void close() {
656648 }
657649
658650 @ Override
659- public synchronized Status verifyAuthority (@ Nonnull String authority ) {
651+ public Status verifyAuthority (@ Nonnull String authority ) {
660652 // sslEngine won't be set when creating ClientTlsHandler from InternalProtocolNegotiators
661653 // for example.
662654 if (sslEngine == null || x509ExtendedTrustManager == null ) {
663655 return Status .FAILED_PRECONDITION .withDescription (
664656 "Can't allow authority override in rpc when SslEngine or X509ExtendedTrustManager"
665657 + " is not available" );
666658 }
667- if (peerVerificationResults .containsKey (authority )) {
668- return peerVerificationResults .get (authority );
669- } else {
670- Status peerVerificationStatus ;
671- try {
672- verifyAuthorityAllowedForPeerCert (authority );
673- peerVerificationStatus = Status .OK ;
674- } catch (SSLPeerUnverifiedException | CertificateException | InvocationTargetException |
675- IllegalAccessException | IllegalStateException e ) {
676- peerVerificationStatus = Status .UNAVAILABLE .withDescription (
677- String .format ("Peer hostname verification during rpc failed for authority '%s'" ,
678- authority )).withCause (e );
679- logger .log (Level .WARNING , "Authority verification failed (this will be an error in the "
680- + "future)." , e );
681- }
682- peerVerificationResults .put (authority , peerVerificationStatus );
683- return peerVerificationStatus ;
659+ Status peerVerificationStatus ;
660+ try {
661+ verifyAuthorityAllowedForPeerCert (authority );
662+ peerVerificationStatus = Status .OK ;
663+ } catch (SSLPeerUnverifiedException | CertificateException | InvocationTargetException
664+ | IllegalAccessException | IllegalStateException e ) {
665+ peerVerificationStatus = Status .UNAVAILABLE .withDescription (
666+ String .format ("Peer hostname verification during rpc failed for authority '%s'" ,
667+ authority )).withCause (e );
684668 }
669+ return peerVerificationStatus ;
685670 }
686671
687672 public void setSslEngine (SSLEngine sslEngine ) {
@@ -692,7 +677,8 @@ private void verifyAuthorityAllowedForPeerCert(String authority)
692677 throws SSLPeerUnverifiedException , CertificateException , InvocationTargetException ,
693678 IllegalAccessException {
694679 if (checkServerTrustedMethod == null ) {
695- throw new IllegalStateException ("Method checkServerTrusted not found in javax.net.ssl.X509ExtendedTrustManager" );
680+ throw new IllegalStateException ("Method checkServerTrusted not found in "
681+ + "javax.net.ssl.X509ExtendedTrustManager" );
696682 }
697683 SSLEngine sslEngineWrapper = new SslEngineWrapper (sslEngine , authority );
698684 // The typecasting of Certificate to X509Certificate should work because this method will only
@@ -702,7 +688,8 @@ private void verifyAuthorityAllowedForPeerCert(String authority)
702688 for (int i = 0 ; i < peerCertificates .length ; i ++) {
703689 x509PeerCertificates [i ] = (X509Certificate ) peerCertificates [i ];
704690 }
705- checkServerTrustedMethod .invoke (x509ExtendedTrustManager , x509PeerCertificates , "RSA" , sslEngineWrapper );
691+ checkServerTrustedMethod .invoke (
692+ x509ExtendedTrustManager , x509PeerCertificates , "RSA" , sslEngineWrapper );
706693 }
707694
708695 @ VisibleForTesting
0 commit comments