2424import io .grpc .xds .client .Bootstrapper .CertificateProviderInfo ;
2525import io .grpc .xds .internal .security .CommonTlsContextUtil ;
2626import io .grpc .xds .internal .security .DynamicSslContextProvider ;
27+ import java .io .Closeable ;
2728import java .security .PrivateKey ;
2829import java .security .cert .X509Certificate ;
2930import java .util .List ;
3435abstract class CertProviderSslContextProvider extends DynamicSslContextProvider implements
3536 CertificateProvider .Watcher {
3637
37- @ Nullable private final CertificateProviderStore . Handle certHandle ;
38- @ Nullable private final CertificateProviderStore . Handle rootCertHandle ;
38+ @ Nullable private final NoExceptionCloseable certHandle ;
39+ @ Nullable private final NoExceptionCloseable rootCertHandle ;
3940 @ Nullable private final CertificateProviderInstance certInstance ;
4041 @ Nullable protected final CertificateProviderInstance rootCertInstance ;
4142 @ Nullable protected PrivateKey savedKey ;
@@ -55,38 +56,50 @@ protected CertProviderSslContextProvider(
5556 super (tlsContext , staticCertValidationContext );
5657 this .certInstance = certInstance ;
5758 this .rootCertInstance = rootCertInstance ;
58- String certInstanceName = null ;
59- if (certInstance != null && certInstance .isInitialized ()) {
60- certInstanceName = certInstance .getInstanceName ();
59+ this .isUsingSystemRootCerts = rootCertInstance == null
60+ && CommonTlsContextUtil .isUsingSystemRootCerts (tlsContext .getCommonTlsContext ());
61+ boolean createCertInstance = certInstance != null && certInstance .isInitialized ();
62+ boolean createRootCertInstance = rootCertInstance != null && rootCertInstance .isInitialized ();
63+ boolean sharedCertInstance = createCertInstance && createRootCertInstance
64+ && rootCertInstance .getInstanceName ().equals (certInstance .getInstanceName ());
65+ if (createCertInstance ) {
6166 CertificateProviderInfo certProviderInstanceConfig =
62- getCertProviderConfig (certProviders , certInstanceName );
67+ getCertProviderConfig (certProviders , certInstance .getInstanceName ());
68+ CertificateProvider .Watcher watcher = this ;
69+ if (!sharedCertInstance && !isUsingSystemRootCerts ) {
70+ watcher = new IgnoreUpdatesWatcher (watcher , /* ignoreRootCertUpdates= */ true );
71+ }
72+ // TODO: Previously we'd hang if certProviderInstanceConfig were null or
73+ // certInstance.isInitialized() == false. Now we'll proceed. Those should be errors, or are
74+ // they impossible and should be assertions?
6375 certHandle = certProviderInstanceConfig == null ? null
6476 : certificateProviderStore .createOrGetProvider (
6577 certInstance .getCertificateName (),
6678 certProviderInstanceConfig .pluginName (),
6779 certProviderInstanceConfig .config (),
68- this ,
69- true );
80+ watcher ,
81+ true ):: close ;
7082 } else {
7183 certHandle = null ;
7284 }
73- if (rootCertInstance != null
74- && rootCertInstance .isInitialized ()
75- && !rootCertInstance .getInstanceName ().equals (certInstanceName )) {
85+ if (createRootCertInstance && !sharedCertInstance ) {
7686 CertificateProviderInfo certProviderInstanceConfig =
7787 getCertProviderConfig (certProviders , rootCertInstance .getInstanceName ());
7888 rootCertHandle = certProviderInstanceConfig == null ? null
7989 : certificateProviderStore .createOrGetProvider (
8090 rootCertInstance .getCertificateName (),
8191 certProviderInstanceConfig .pluginName (),
8292 certProviderInstanceConfig .config (),
83- this ,
84- true );
93+ new IgnoreUpdatesWatcher (this , /* ignoreRootCertUpdates= */ false ),
94+ false )::close ;
95+ } else if (rootCertInstance == null
96+ && CommonTlsContextUtil .isUsingSystemRootCerts (tlsContext .getCommonTlsContext ())) {
97+ SystemRootCertificateProvider systemRootProvider = new SystemRootCertificateProvider (this );
98+ systemRootProvider .start ();
99+ rootCertHandle = systemRootProvider ::close ;
85100 } else {
86101 rootCertHandle = null ;
87102 }
88- this .isUsingSystemRootCerts = rootCertInstance == null
89- && CommonTlsContextUtil .isUsingSystemRootCerts (tlsContext .getCommonTlsContext ());
90103 }
91104
92105 private static CertificateProviderInfo getCertProviderConfig (
@@ -150,17 +163,16 @@ public final void updateSpiffeTrustMap(Map<String, List<X509Certificate>> spiffe
150163
151164 private void updateSslContextWhenReady () {
152165 if (isMtls ()) {
153- if (savedKey != null
154- && (savedTrustedRoots != null || isUsingSystemRootCerts || savedSpiffeTrustMap != null )) {
166+ if (savedKey != null && (savedTrustedRoots != null || savedSpiffeTrustMap != null )) {
155167 updateSslContext ();
156168 clearKeysAndCerts ();
157169 }
158- } else if (isClientSideTls ()) {
170+ } else if (isRegularTlsAndClientSide ()) {
159171 if (savedTrustedRoots != null || savedSpiffeTrustMap != null ) {
160172 updateSslContext ();
161173 clearKeysAndCerts ();
162174 }
163- } else if (isServerSideTls ()) {
175+ } else if (isRegularTlsAndServerSide ()) {
164176 if (savedKey != null ) {
165177 updateSslContext ();
166178 clearKeysAndCerts ();
@@ -170,20 +182,22 @@ private void updateSslContextWhenReady() {
170182
171183 private void clearKeysAndCerts () {
172184 savedKey = null ;
173- savedTrustedRoots = null ;
174- savedSpiffeTrustMap = null ;
185+ if (!isUsingSystemRootCerts ) {
186+ savedTrustedRoots = null ;
187+ savedSpiffeTrustMap = null ;
188+ }
175189 savedCertChain = null ;
176190 }
177191
178192 protected final boolean isMtls () {
179193 return certInstance != null && (rootCertInstance != null || isUsingSystemRootCerts );
180194 }
181195
182- protected final boolean isClientSideTls () {
183- return rootCertInstance != null && certInstance == null ;
196+ protected final boolean isRegularTlsAndClientSide () {
197+ return ( rootCertInstance != null || isUsingSystemRootCerts ) && certInstance == null ;
184198 }
185199
186- protected final boolean isServerSideTls () {
200+ protected final boolean isRegularTlsAndServerSide () {
187201 return certInstance != null && rootCertInstance == null ;
188202 }
189203
@@ -201,4 +215,9 @@ public final void close() {
201215 rootCertHandle .close ();
202216 }
203217 }
218+
219+ interface NoExceptionCloseable extends Closeable {
220+ @ Override
221+ void close ();
222+ }
204223}
0 commit comments