1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020
21- import io . grpc . ExperimentalApi ;
21+ import com . google . errorprone . annotations . InlineMe ;
2222import java .io .File ;
2323import java .io .FileInputStream ;
2424import java .io .IOException ;
4040 * AdvancedTlsX509KeyManager is an {@code X509ExtendedKeyManager} that allows users to configure
4141 * advanced TLS features, such as private key and certificate chain reloading.
4242 */
43- @ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/8024" )
4443public final class AdvancedTlsX509KeyManager extends X509ExtendedKeyManager {
4544 private static final Logger log = Logger .getLogger (AdvancedTlsX509KeyManager .class .getName ());
4645 // Minimum allowed period for refreshing files with credential information.
@@ -100,31 +99,44 @@ public String chooseEngineServerAlias(String keyType, Principal[] issuers,
10099 *
101100 * @param key the private key that is going to be used
102101 * @param certs the certificate chain that is going to be used
102+ * @deprecated Use {@link #updateIdentityCredentials(X509Certificate[], PrivateKey)}
103103 */
104+ @ Deprecated
105+ @ InlineMe (replacement = "this.updateIdentityCredentials(certs, key)" )
104106 public void updateIdentityCredentials (PrivateKey key , X509Certificate [] certs ) {
107+ updateIdentityCredentials (certs , key );
108+ }
109+
110+ /**
111+ * Updates the current cached private key and cert chains.
112+ *
113+ * @param certs the certificate chain that is going to be used
114+ * @param key the private key that is going to be used
115+ */
116+ public void updateIdentityCredentials (X509Certificate [] certs , PrivateKey key ) {
105117 this .keyInfo = new KeyInfo (checkNotNull (key , "key" ), checkNotNull (certs , "certs" ));
106118 }
107119
108120 /**
109- * Schedules a {@code ScheduledExecutorService} to read private key and certificate chains from
121+ * Schedules a {@code ScheduledExecutorService} to read certificate chains and private key from
110122 * the local file paths periodically, and update the cached identity credentials if they are both
111123 * updated. You must close the returned Closeable before calling this method again or other update
112124 * methods ({@link AdvancedTlsX509KeyManager#updateIdentityCredentials}, {@link
113- * AdvancedTlsX509KeyManager#updateIdentityCredentialsFromFile (File, File)}).
125+ * AdvancedTlsX509KeyManager#updateIdentityCredentials (File, File)}).
114126 * Before scheduling the task, the method synchronously executes {@code readAndUpdate} once. The
115127 * minimum refresh period of 1 minute is enforced.
116128 *
117- * @param keyFile the file on disk holding the private key
118129 * @param certFile the file on disk holding the certificate chain
130+ * @param keyFile the file on disk holding the private key
119131 * @param period the period between successive read-and-update executions
120132 * @param unit the time unit of the initialDelay and period parameters
121- * @param executor the execute service we use to read and update the credentials
133+ * @param executor the executor service we use to read and update the credentials
122134 * @return an object that caller should close when the file refreshes are not needed
123135 */
124- public Closeable updateIdentityCredentialsFromFile (File keyFile , File certFile ,
136+ public Closeable updateIdentityCredentials (File certFile , File keyFile ,
125137 long period , TimeUnit unit , ScheduledExecutorService executor ) throws IOException ,
126138 GeneralSecurityException {
127- UpdateResult newResult = readAndUpdate (keyFile , certFile , 0 , 0 );
139+ UpdateResult newResult = readAndUpdate (certFile , keyFile , 0 , 0 );
128140 if (!newResult .success ) {
129141 throw new GeneralSecurityException (
130142 "Files were unmodified before their initial update. Probably a bug." );
@@ -138,25 +150,66 @@ public Closeable updateIdentityCredentialsFromFile(File keyFile, File certFile,
138150 }
139151 final ScheduledFuture <?> future =
140152 checkNotNull (executor , "executor" ).scheduleWithFixedDelay (
141- new LoadFilePathExecution (keyFile , certFile ), period , period , unit );
153+ new LoadFilePathExecution (certFile , keyFile ), period , period , unit );
142154 return () -> future .cancel (false );
143155 }
144156
145157 /**
146- * Updates the private key and certificate chains from the local file paths.
158+ * Updates certificate chains and the private key from the local file paths.
147159 *
148- * @param keyFile the file on disk holding the private key
149160 * @param certFile the file on disk holding the certificate chain
161+ * @param keyFile the file on disk holding the private key
150162 */
151- public void updateIdentityCredentialsFromFile (File keyFile , File certFile ) throws IOException ,
163+ public void updateIdentityCredentials (File certFile , File keyFile ) throws IOException ,
152164 GeneralSecurityException {
153- UpdateResult newResult = readAndUpdate (keyFile , certFile , 0 , 0 );
165+ UpdateResult newResult = readAndUpdate (certFile , keyFile , 0 , 0 );
154166 if (!newResult .success ) {
155167 throw new GeneralSecurityException (
156168 "Files were unmodified before their initial update. Probably a bug." );
157169 }
158170 }
159171
172+ /**
173+ * Updates the private key and certificate chains from the local file paths.
174+ *
175+ * @param keyFile the file on disk holding the private key
176+ * @param certFile the file on disk holding the certificate chain
177+ * @deprecated Use {@link #updateIdentityCredentials(File, File)} instead.
178+ */
179+ @ Deprecated
180+ @ InlineMe (replacement = "this.updateIdentityCredentials(certFile, keyFile)" )
181+ public void updateIdentityCredentialsFromFile (File keyFile , File certFile ) throws IOException ,
182+ GeneralSecurityException {
183+ updateIdentityCredentials (certFile , keyFile );
184+ }
185+
186+ /**
187+ * Schedules a {@code ScheduledExecutorService} to read private key and certificate chains from
188+ * the local file paths periodically, and update the cached identity credentials if they are both
189+ * updated. You must close the returned Closeable before calling this method again or other update
190+ * methods ({@link AdvancedTlsX509KeyManager#updateIdentityCredentials}, {@link
191+ * AdvancedTlsX509KeyManager#updateIdentityCredentials(File, File)}).
192+ * Before scheduling the task, the method synchronously executes {@code readAndUpdate} once. The
193+ * minimum refresh period of 1 minute is enforced.
194+ *
195+ * @param keyFile the file on disk holding the private key
196+ * @param certFile the file on disk holding the certificate chain
197+ * @param period the period between successive read-and-update executions
198+ * @param unit the time unit of the initialDelay and period parameters
199+ * @param executor the executor service we use to read and update the credentials
200+ * @return an object that caller should close when the file refreshes are not needed
201+ * @deprecated Use {@link
202+ * #updateIdentityCredentials(File, File, long, TimeUnit, ScheduledExecutorService)} instead.
203+ */
204+ @ Deprecated
205+ @ InlineMe (replacement =
206+ "this.updateIdentityCredentials(certFile, keyFile, period, unit, executor)" )
207+ public Closeable updateIdentityCredentialsFromFile (File keyFile , File certFile ,
208+ long period , TimeUnit unit , ScheduledExecutorService executor ) throws IOException ,
209+ GeneralSecurityException {
210+ return updateIdentityCredentials (certFile , keyFile , period , unit , executor );
211+ }
212+
160213 private static class KeyInfo {
161214 // The private key and the cert chain we will use to send to peers to prove our identity.
162215 final PrivateKey key ;
@@ -174,26 +227,26 @@ private class LoadFilePathExecution implements Runnable {
174227 long currentKeyTime ;
175228 long currentCertTime ;
176229
177- public LoadFilePathExecution (File keyFile , File certFile ) {
178- this .keyFile = keyFile ;
230+ public LoadFilePathExecution (File certFile , File keyFile ) {
179231 this .certFile = certFile ;
232+ this .keyFile = keyFile ;
180233 this .currentKeyTime = 0 ;
181234 this .currentCertTime = 0 ;
182235 }
183236
184237 @ Override
185238 public void run () {
186239 try {
187- UpdateResult newResult = readAndUpdate (this .keyFile , this .certFile , this .currentKeyTime ,
240+ UpdateResult newResult = readAndUpdate (this .certFile , this .keyFile , this .currentKeyTime ,
188241 this .currentCertTime );
189242 if (newResult .success ) {
190243 this .currentKeyTime = newResult .keyTime ;
191244 this .currentCertTime = newResult .certTime ;
192245 }
193246 } catch (IOException | GeneralSecurityException e ) {
194247 log .log (Level .SEVERE , String .format ("Failed refreshing private key and certificate"
195- + " chain from files. Using previous ones (keyFile lastModified = %s, certFile "
196- + "lastModified = %s)" , keyFile .lastModified (), certFile .lastModified ()), e );
248+ + " chain from files. Using previous ones (certFile lastModified = %s, keyFile "
249+ + "lastModified = %s)" , certFile .lastModified (), keyFile .lastModified ()), e );
197250 }
198251 }
199252 }
@@ -214,13 +267,13 @@ public UpdateResult(boolean success, long keyTime, long certTime) {
214267 * Reads the private key and certificates specified in the path locations. Updates {@code key} and
215268 * {@code cert} if both of their modified time changed since last read.
216269 *
217- * @param keyFile the file on disk holding the private key
218270 * @param certFile the file on disk holding the certificate chain
271+ * @param keyFile the file on disk holding the private key
219272 * @param oldKeyTime the time when the private key file is modified during last execution
220273 * @param oldCertTime the time when the certificate chain file is modified during last execution
221274 * @return the result of this update execution
222275 */
223- private UpdateResult readAndUpdate (File keyFile , File certFile , long oldKeyTime , long oldCertTime )
276+ private UpdateResult readAndUpdate (File certFile , File keyFile , long oldKeyTime , long oldCertTime )
224277 throws IOException , GeneralSecurityException {
225278 long newKeyTime = checkNotNull (keyFile , "keyFile" ).lastModified ();
226279 long newCertTime = checkNotNull (certFile , "certFile" ).lastModified ();
@@ -232,7 +285,7 @@ private UpdateResult readAndUpdate(File keyFile, File certFile, long oldKeyTime,
232285 FileInputStream certInputStream = new FileInputStream (certFile );
233286 try {
234287 X509Certificate [] certs = CertificateUtils .getX509Certificates (certInputStream );
235- updateIdentityCredentials (key , certs );
288+ updateIdentityCredentials (certs , key );
236289 return new UpdateResult (true , newKeyTime , newCertTime );
237290 } finally {
238291 certInputStream .close ();
0 commit comments