1919import com .google .common .annotations .VisibleForTesting ;
2020import com .google .common .collect .ImmutableMap ;
2121import com .google .errorprone .annotations .concurrent .GuardedBy ;
22+ import io .grpc .CallCredentials ;
2223import io .grpc .ChannelCredentials ;
2324import io .grpc .internal .JsonUtil ;
2425import io .grpc .xds .client .BootstrapperImpl ;
2526import io .grpc .xds .client .XdsInitializationException ;
2627import io .grpc .xds .client .XdsLogger ;
28+ import io .grpc .xds .internal .grpcservice .ChannelCredsConfig ;
29+ import io .grpc .xds .internal .grpcservice .ConfiguredChannelCredentials ;
30+ import io .grpc .xds .internal .grpcservice .GrpcServiceXdsContext ;
2731import java .io .IOException ;
2832import java .util .List ;
2933import java .util .Map ;
34+ import java .util .Optional ;
3035import javax .annotation .Nullable ;
3136
3237class GrpcBootstrapperImpl extends BootstrapperImpl {
@@ -97,7 +102,8 @@ protected String getJsonContent() throws XdsInitializationException, IOException
97102 @ Override
98103 protected Object getImplSpecificConfig (Map <String , ?> serverConfig , String serverUri )
99104 throws XdsInitializationException {
100- return getChannelCredentials (serverConfig , serverUri );
105+ ConfiguredChannelCredentials configuredChannel = getChannelCredentials (serverConfig , serverUri );
106+ return configuredChannel != null ? configuredChannel .channelCredentials () : null ;
101107 }
102108
103109 @ GuardedBy ("GrpcBootstrapperImpl.class" )
@@ -120,26 +126,26 @@ static synchronized BootstrapInfo defaultBootstrap() throws XdsInitializationExc
120126 return defaultBootstrap ;
121127 }
122128
123- private static ChannelCredentials getChannelCredentials (Map <String , ?> serverConfig ,
124- String serverUri )
129+ private static ConfiguredChannelCredentials getChannelCredentials (Map <String , ?> serverConfig ,
130+ String serverUri )
125131 throws XdsInitializationException {
126132 List <?> rawChannelCredsList = JsonUtil .getList (serverConfig , "channel_creds" );
127133 if (rawChannelCredsList == null || rawChannelCredsList .isEmpty ()) {
128134 throw new XdsInitializationException (
129135 "Invalid bootstrap: server " + serverUri + " 'channel_creds' required" );
130136 }
131- ChannelCredentials channelCredentials =
137+ ConfiguredChannelCredentials credentials =
132138 parseChannelCredentials (JsonUtil .checkObjectList (rawChannelCredsList ), serverUri );
133- if (channelCredentials == null ) {
139+ if (credentials == null ) {
134140 throw new XdsInitializationException (
135141 "Server " + serverUri + ": no supported channel credentials found" );
136142 }
137- return channelCredentials ;
143+ return credentials ;
138144 }
139145
140146 @ Nullable
141- private static ChannelCredentials parseChannelCredentials (List <Map <String , ?>> jsonList ,
142- String serverUri )
147+ private static ConfiguredChannelCredentials parseChannelCredentials (List <Map <String , ?>> jsonList ,
148+ String serverUri )
143149 throws XdsInitializationException {
144150 for (Map <String , ?> channelCreds : jsonList ) {
145151 String type = JsonUtil .getString (channelCreds , "type" );
@@ -155,9 +161,90 @@ private static ChannelCredentials parseChannelCredentials(List<Map<String, ?>> j
155161 config = ImmutableMap .of ();
156162 }
157163
158- return provider .newChannelCredentials (config );
164+ ChannelCredentials creds = provider .newChannelCredentials (config );
165+ if (creds == null ) {
166+ continue ;
167+ }
168+ return ConfiguredChannelCredentials .create (creds , new JsonChannelCredsConfig (type , config ));
159169 }
160170 }
161171 return null ;
162172 }
173+
174+ @ Override
175+ protected Optional <Object > parseAllowedGrpcServices (
176+ Map <String , ?> rawAllowedGrpcServices )
177+ throws XdsInitializationException {
178+ ImmutableMap .Builder <String , GrpcServiceXdsContext .AllowedGrpcService > builder =
179+ ImmutableMap .builder ();
180+ for (String targetUri : rawAllowedGrpcServices .keySet ()) {
181+ Map <String , ?> serviceConfig = JsonUtil .getObject (rawAllowedGrpcServices , targetUri );
182+ if (serviceConfig == null ) {
183+ throw new XdsInitializationException (
184+ "Invalid allowed_grpc_services config for " + targetUri );
185+ }
186+ ConfiguredChannelCredentials configuredChannel =
187+ getChannelCredentials (serviceConfig , targetUri );
188+
189+ Optional <CallCredentials > callCredentials = Optional .empty ();
190+ List <?> rawCallCredsList = JsonUtil .getList (serviceConfig , "call_creds" );
191+ if (rawCallCredsList != null && !rawCallCredsList .isEmpty ()) {
192+ callCredentials =
193+ parseCallCredentials (JsonUtil .checkObjectList (rawCallCredsList ), targetUri );
194+ }
195+
196+ GrpcServiceXdsContext .AllowedGrpcService .Builder b = GrpcServiceXdsContext .AllowedGrpcService
197+ .builder ().configuredChannelCredentials (configuredChannel );
198+ callCredentials .ifPresent (b ::callCredentials );
199+ builder .put (targetUri , b .build ());
200+ }
201+ ImmutableMap <String , GrpcServiceXdsContext .AllowedGrpcService > parsed = builder .buildOrThrow ();
202+ return parsed .isEmpty () ? Optional .empty () : Optional .of (parsed );
203+ }
204+
205+ @ SuppressWarnings ("unused" )
206+ private static Optional <CallCredentials > parseCallCredentials (List <Map <String , ?>> jsonList ,
207+ String targetUri )
208+ throws XdsInitializationException {
209+ // TODO(sauravzg): Currently no xDS call credentials providers are implemented (no
210+ // XdsCallCredentialsRegistry).
211+ // As per A102/A97, we should just ignore unsupported call credentials types
212+ // without throwing an exception.
213+ return Optional .empty ();
214+ }
215+
216+ private static final class JsonChannelCredsConfig implements ChannelCredsConfig {
217+ private final String type ;
218+ private final Map <String , ?> config ;
219+
220+ JsonChannelCredsConfig (String type , Map <String , ?> config ) {
221+ this .type = type ;
222+ this .config = config ;
223+ }
224+
225+ @ Override
226+ public String type () {
227+ return type ;
228+ }
229+
230+ @ Override
231+ public boolean equals (Object o ) {
232+ if (this == o ) {
233+ return true ;
234+ }
235+ if (o == null || getClass () != o .getClass ()) {
236+ return false ;
237+ }
238+ JsonChannelCredsConfig that = (JsonChannelCredsConfig ) o ;
239+ return java .util .Objects .equals (type , that .type )
240+ && java .util .Objects .equals (config , that .config );
241+ }
242+
243+ @ Override
244+ public int hashCode () {
245+ return java .util .Objects .hash (type , config );
246+ }
247+ }
248+
163249}
250+
0 commit comments