16
16
17
17
package io .grpc .xds .internal ;
18
18
19
+ import com .google .protobuf .Duration ;
20
+ import com .google .protobuf .util .Durations ;
19
21
import io .grpc .ChannelCredentials ;
20
22
import io .grpc .TlsChannelCredentials ;
23
+ import io .grpc .internal .GrpcUtil ;
21
24
import io .grpc .internal .JsonUtil ;
22
25
import io .grpc .util .AdvancedTlsX509KeyManager ;
23
26
import io .grpc .util .AdvancedTlsX509TrustManager ;
24
27
import io .grpc .xds .XdsCredentialsProvider ;
25
28
import java .io .File ;
29
+ import java .text .ParseException ;
26
30
import java .util .Map ;
31
+ import java .util .concurrent .Executors ;
32
+ import java .util .concurrent .ScheduledExecutorService ;
33
+ import java .util .concurrent .TimeUnit ;
27
34
import java .util .logging .Level ;
28
35
import java .util .logging .Logger ;
29
36
@@ -37,6 +44,10 @@ public final class TlsXdsCredentialsProvider extends XdsCredentialsProvider {
37
44
private static final String CERT_FILE_KEY = "certificate_file" ;
38
45
private static final String KEY_FILE_KEY = "private_key_file" ;
39
46
private static final String ROOT_FILE_KEY = "ca_certificate_file" ;
47
+ private static final String REFRESH_INTERVAL_KEY = "refresh_interval" ;
48
+ private static final long REFRESH_INTERVAL_DEFAULT = 600L ;
49
+ private static final ScheduledExecutorServiceFactory scheduledExecutorServiceFactory =
50
+ ScheduledExecutorServiceFactory .DEFAULT_INSTANCE ;
40
51
41
52
@ Override
42
53
protected ChannelCredentials newChannelCredentials (Map <String , ?> jsonConfig ) {
@@ -46,12 +57,29 @@ protected ChannelCredentials newChannelCredentials(Map<String, ?> jsonConfig) {
46
57
return builder .build ();
47
58
}
48
59
60
+ // use refresh interval from bootstrap config if provided; else defaults to 600s
61
+ long refreshIntervalSeconds = REFRESH_INTERVAL_DEFAULT ;
62
+ String refreshIntervalFromConfig = JsonUtil .getString (jsonConfig , REFRESH_INTERVAL_KEY );
63
+ if (refreshIntervalFromConfig != null ) {
64
+ try {
65
+ Duration duration = Durations .parse (refreshIntervalFromConfig );
66
+ refreshIntervalSeconds = Durations .toSeconds (duration );
67
+ } catch (ParseException e ) {
68
+ logger .log (Level .WARNING , "Unable to parse refresh interval" , e );
69
+ return null ;
70
+ }
71
+ }
72
+
49
73
// use trust certificate file path from bootstrap config if provided; else use system default
50
74
String rootCertPath = JsonUtil .getString (jsonConfig , ROOT_FILE_KEY );
51
75
if (rootCertPath != null ) {
52
76
try {
53
77
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager .newBuilder ().build ();
54
- trustManager .updateTrustCredentials (new File (rootCertPath ));
78
+ trustManager .updateTrustCredentials (
79
+ new File (rootCertPath ),
80
+ refreshIntervalSeconds ,
81
+ TimeUnit .SECONDS ,
82
+ scheduledExecutorServiceFactory .create ());
55
83
builder .trustManager (trustManager );
56
84
} catch (Exception e ) {
57
85
logger .log (Level .WARNING , "Unable to read root certificates" , e );
@@ -66,7 +94,12 @@ protected ChannelCredentials newChannelCredentials(Map<String, ?> jsonConfig) {
66
94
if (certChainPath != null && privateKeyPath != null ) {
67
95
try {
68
96
AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager ();
69
- keyManager .updateIdentityCredentials (new File (certChainPath ), new File (privateKeyPath ));
97
+ keyManager .updateIdentityCredentials (
98
+ new File (certChainPath ),
99
+ new File (privateKeyPath ),
100
+ refreshIntervalSeconds ,
101
+ TimeUnit .SECONDS ,
102
+ scheduledExecutorServiceFactory .create ());
70
103
builder .keyManager (keyManager );
71
104
} catch (Exception e ) {
72
105
logger .log (Level .WARNING , "Unable to read certificate chain or private key" , e );
@@ -95,4 +128,18 @@ public int priority() {
95
128
return 5 ;
96
129
}
97
130
131
+ abstract static class ScheduledExecutorServiceFactory {
132
+
133
+ private static final ScheduledExecutorServiceFactory DEFAULT_INSTANCE =
134
+ new ScheduledExecutorServiceFactory () {
135
+
136
+ @ Override
137
+ ScheduledExecutorService create () {
138
+ return Executors .newSingleThreadScheduledExecutor (
139
+ GrpcUtil .getThreadFactory ("grpc-certificate-files-%d" , true ));
140
+ }
141
+ };
142
+
143
+ abstract ScheduledExecutorService create ();
144
+ }
98
145
}
0 commit comments