• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19590

16 Dec 2024 03:29PM UTC coverage: 88.605%. Remained the same
#19590

push

github

ejona86
xds: Unexpected types in server_features should be ignored

It was clearly defined in gRFC A30. The relevant text was copied as a
comment in the code.

As discovered due to grpc/grpc-go#7932

33481 of 37787 relevant lines covered (88.6%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.96
/../xds/src/main/java/io/grpc/xds/client/BootstrapperImpl.java
1
/*
2
 * Copyright 2021 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.xds.client;
18

19
import com.google.common.annotations.VisibleForTesting;
20
import com.google.common.collect.ImmutableList;
21
import com.google.common.collect.ImmutableMap;
22
import io.grpc.Internal;
23
import io.grpc.InternalLogId;
24
import io.grpc.internal.GrpcUtil;
25
import io.grpc.internal.GrpcUtil.GrpcBuildVersion;
26
import io.grpc.internal.JsonParser;
27
import io.grpc.internal.JsonUtil;
28
import io.grpc.xds.client.EnvoyProtoData.Node;
29
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
30
import java.io.IOException;
31
import java.nio.charset.StandardCharsets;
32
import java.nio.file.Files;
33
import java.nio.file.Paths;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37

38
/**
39
 * A {@link Bootstrapper} implementation that reads xDS configurations from local file system.
40
 */
41
@Internal
42
public abstract class BootstrapperImpl extends Bootstrapper {
43

44
  public static final String GRPC_EXPERIMENTAL_XDS_FALLBACK =
45
      "GRPC_EXPERIMENTAL_XDS_FALLBACK";
46

47
  // Client features.
48
  @VisibleForTesting
49
  public static final String CLIENT_FEATURE_DISABLE_OVERPROVISIONING =
50
      "envoy.lb.does_not_support_overprovisioning";
51
  @VisibleForTesting
52
  public static final String CLIENT_FEATURE_RESOURCE_IN_SOTW = "xds.config.resource-in-sotw";
53

54
  // Server features.
55
  private static final String SERVER_FEATURE_IGNORE_RESOURCE_DELETION = "ignore_resource_deletion";
56
  private static final String SERVER_FEATURE_TRUSTED_XDS_SERVER = "trusted_xds_server";
57

58
  @VisibleForTesting
59
  static boolean enableXdsFallback = GrpcUtil.getFlag(GRPC_EXPERIMENTAL_XDS_FALLBACK, false);
1✔
60

61
  protected final XdsLogger logger;
62

63
  protected FileReader reader = LocalFileReader.INSTANCE;
1✔
64

65
  protected BootstrapperImpl() {
1✔
66
    logger = XdsLogger.withLogId(InternalLogId.allocate("bootstrapper", null));
1✔
67
  }
1✔
68

69
  protected abstract String getJsonContent() throws IOException, XdsInitializationException;
70

71
  protected abstract Object getImplSpecificConfig(Map<String, ?> serverConfig, String serverUri)
72
      throws XdsInitializationException;
73

74

75
  /**
76
   * Reads and parses bootstrap config. The config is expected to be in JSON format.
77
   */
78
  @SuppressWarnings("unchecked")
79
  @Override
80
  public BootstrapInfo bootstrap() throws XdsInitializationException {
81
    String jsonContent;
82
    try {
83
      jsonContent = getJsonContent();
1✔
84
    } catch (IOException e) {
×
85
      throw new XdsInitializationException("Fail to read bootstrap file", e);
×
86
    }
1✔
87

88
    Map<String, ?> rawBootstrap;
89
    try {
90
      rawBootstrap = (Map<String, ?>) JsonParser.parse(jsonContent);
1✔
91
    } catch (IOException e) {
×
92
      throw new XdsInitializationException("Failed to parse JSON", e);
×
93
    }
1✔
94

95
    logger.log(XdsLogLevel.DEBUG, "Bootstrap configuration:\n{0}", rawBootstrap);
1✔
96
    return bootstrap(rawBootstrap);
1✔
97
  }
98

99
  @Override
100
  public BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationException {
101
    return bootstrapBuilder(rawData).build();
1✔
102
  }
103

104
  protected BootstrapInfo.Builder bootstrapBuilder(Map<String, ?> rawData)
105
      throws XdsInitializationException {
106
    BootstrapInfo.Builder builder = BootstrapInfo.builder();
1✔
107

108
    List<?> rawServerConfigs = JsonUtil.getList(rawData, "xds_servers");
1✔
109
    if (rawServerConfigs == null) {
1✔
110
      throw new XdsInitializationException("Invalid bootstrap: 'xds_servers' does not exist.");
1✔
111
    }
112
    List<ServerInfo> servers = parseServerInfos(rawServerConfigs, logger);
1✔
113
    if (servers.size() > 1 && !enableXdsFallback) {
1✔
114
      servers = ImmutableList.of(servers.get(0));
×
115
    }
116
    builder.servers(servers);
1✔
117

118
    Node.Builder nodeBuilder = Node.newBuilder();
1✔
119
    Map<String, ?> rawNode = JsonUtil.getObject(rawData, "node");
1✔
120
    if (rawNode != null) {
1✔
121
      String id = JsonUtil.getString(rawNode, "id");
1✔
122
      if (id != null) {
1✔
123
        logger.log(XdsLogLevel.INFO, "Node id: {0}", id);
1✔
124
        nodeBuilder.setId(id);
1✔
125
      }
126
      String cluster = JsonUtil.getString(rawNode, "cluster");
1✔
127
      if (cluster != null) {
1✔
128
        logger.log(XdsLogLevel.INFO, "Node cluster: {0}", cluster);
1✔
129
        nodeBuilder.setCluster(cluster);
1✔
130
      }
131
      Map<String, ?> metadata = JsonUtil.getObject(rawNode, "metadata");
1✔
132
      if (metadata != null) {
1✔
133
        nodeBuilder.setMetadata(metadata);
1✔
134
      }
135
      Map<String, ?> rawLocality = JsonUtil.getObject(rawNode, "locality");
1✔
136
      if (rawLocality != null) {
1✔
137
        String region = "";
1✔
138
        String zone = "";
1✔
139
        String subZone = "";
1✔
140
        if (rawLocality.containsKey("region")) {
1✔
141
          region = JsonUtil.getString(rawLocality, "region");
1✔
142
        }
143
        if (rawLocality.containsKey("zone")) {
1✔
144
          zone = JsonUtil.getString(rawLocality, "zone");
1✔
145
        }
146
        if (rawLocality.containsKey("sub_zone")) {
1✔
147
          subZone = JsonUtil.getString(rawLocality, "sub_zone");
1✔
148
        }
149
        logger.log(XdsLogLevel.INFO, "Locality region: {0}, zone: {1}, subZone: {2}",
1✔
150
            region, zone, subZone);
151
        Locality locality = Locality.create(region, zone, subZone);
1✔
152
        nodeBuilder.setLocality(locality);
1✔
153
      }
154
    }
155
    GrpcBuildVersion buildVersion = GrpcUtil.getGrpcBuildVersion();
1✔
156
    logger.log(XdsLogLevel.INFO, "Build version: {0}", buildVersion);
1✔
157
    nodeBuilder.setBuildVersion(buildVersion.toString());
1✔
158
    nodeBuilder.setUserAgentName(buildVersion.getUserAgent());
1✔
159
    nodeBuilder.setUserAgentVersion(buildVersion.getImplementationVersion());
1✔
160
    nodeBuilder.addClientFeatures(CLIENT_FEATURE_DISABLE_OVERPROVISIONING);
1✔
161
    nodeBuilder.addClientFeatures(CLIENT_FEATURE_RESOURCE_IN_SOTW);
1✔
162
    builder.node(nodeBuilder.build());
1✔
163

164
    Map<String, ?> certProvidersBlob = JsonUtil.getObject(rawData, "certificate_providers");
1✔
165
    if (certProvidersBlob != null) {
1✔
166
      logger.log(XdsLogLevel.INFO, "Configured with {0} cert providers", certProvidersBlob.size());
1✔
167
      Map<String, CertificateProviderInfo> certProviders = new HashMap<>(certProvidersBlob.size());
1✔
168
      for (String name : certProvidersBlob.keySet()) {
1✔
169
        Map<String, ?> valueMap = JsonUtil.getObject(certProvidersBlob, name);
1✔
170
        String pluginName =
1✔
171
            checkForNull(JsonUtil.getString(valueMap, "plugin_name"), "plugin_name");
1✔
172
        logger.log(XdsLogLevel.INFO, "cert provider: {0}, plugin name: {1}", name, pluginName);
1✔
173
        Map<String, ?> config = checkForNull(JsonUtil.getObject(valueMap, "config"), "config");
1✔
174
        CertificateProviderInfo certificateProviderInfo =
1✔
175
            CertificateProviderInfo.create(pluginName, config);
1✔
176
        certProviders.put(name, certificateProviderInfo);
1✔
177
      }
1✔
178
      builder.certProviders(certProviders);
1✔
179
    }
180

181
    String serverResourceId =
1✔
182
        JsonUtil.getString(rawData, "server_listener_resource_name_template");
1✔
183
    logger.log(
1✔
184
        XdsLogLevel.INFO, "server_listener_resource_name_template: {0}", serverResourceId);
185
    builder.serverListenerResourceNameTemplate(serverResourceId);
1✔
186

187
    String clientDefaultListener =
1✔
188
        JsonUtil.getString(rawData, "client_default_listener_resource_name_template");
1✔
189
    logger.log(
1✔
190
        XdsLogLevel.INFO, "client_default_listener_resource_name_template: {0}",
191
        clientDefaultListener);
192
    if (clientDefaultListener != null) {
1✔
193
      builder.clientDefaultListenerResourceNameTemplate(clientDefaultListener);
1✔
194
    }
195

196
    Map<String, ?> rawAuthoritiesMap =
1✔
197
        JsonUtil.getObject(rawData, "authorities");
1✔
198
    ImmutableMap.Builder<String, AuthorityInfo> authorityInfoMapBuilder = ImmutableMap.builder();
1✔
199
    if (rawAuthoritiesMap != null) {
1✔
200
      logger.log(
1✔
201
          XdsLogLevel.INFO, "Configured with {0} xDS server authorities", rawAuthoritiesMap.size());
1✔
202
      for (String authorityName : rawAuthoritiesMap.keySet()) {
1✔
203
        logger.log(XdsLogLevel.INFO, "xDS server authority: {0}", authorityName);
1✔
204
        Map<String, ?> rawAuthority = JsonUtil.getObject(rawAuthoritiesMap, authorityName);
1✔
205
        String clientListnerTemplate =
1✔
206
            JsonUtil.getString(rawAuthority, "client_listener_resource_name_template");
1✔
207
        logger.log(
1✔
208
            XdsLogLevel.INFO, "client_listener_resource_name_template: {0}", clientListnerTemplate);
209
        String prefix = XDSTP_SCHEME + "//" + authorityName + "/";
1✔
210
        if (clientListnerTemplate == null) {
1✔
211
          clientListnerTemplate = prefix + "envoy.config.listener.v3.Listener/%s";
1✔
212
        } else if (!clientListnerTemplate.startsWith(prefix)) {
1✔
213
          throw new XdsInitializationException(
1✔
214
              "client_listener_resource_name_template: '" + clientListnerTemplate
215
                  + "' does not start with " + prefix);
216
        }
217
        List<?> rawAuthorityServers = JsonUtil.getList(rawAuthority, "xds_servers");
1✔
218
        List<ServerInfo> authorityServers;
219
        if (rawAuthorityServers == null || rawAuthorityServers.isEmpty()) {
1✔
220
          authorityServers = servers;
1✔
221
        } else {
222
          if (rawAuthorityServers.size() > 1 && !enableXdsFallback) {
1✔
223
            rawAuthorityServers = ImmutableList.of(rawAuthorityServers.get(0));
×
224
          }
225
          authorityServers = parseServerInfos(rawAuthorityServers, logger);
1✔
226
        }
227
        authorityInfoMapBuilder.put(
1✔
228
            authorityName, AuthorityInfo.create(clientListnerTemplate, authorityServers));
1✔
229
      }
1✔
230
      builder.authorities(authorityInfoMapBuilder.buildOrThrow());
1✔
231
    }
232

233
    return builder;
1✔
234
  }
235

236
  private List<ServerInfo> parseServerInfos(List<?> rawServerConfigs, XdsLogger logger)
237
      throws XdsInitializationException {
238
    logger.log(XdsLogLevel.INFO, "Configured with {0} xDS servers", rawServerConfigs.size());
1✔
239
    ImmutableList.Builder<ServerInfo> servers = ImmutableList.builder();
1✔
240
    List<Map<String, ?>> serverConfigList = JsonUtil.checkObjectList(rawServerConfigs);
1✔
241
    for (Map<String, ?> serverConfig : serverConfigList) {
1✔
242
      String serverUri = JsonUtil.getString(serverConfig, "server_uri");
1✔
243
      if (serverUri == null) {
1✔
244
        throw new XdsInitializationException("Invalid bootstrap: missing 'server_uri'");
1✔
245
      }
246
      logger.log(XdsLogLevel.INFO, "xDS server URI: {0}", serverUri);
1✔
247

248
      Object implSpecificConfig = getImplSpecificConfig(serverConfig, serverUri);
1✔
249

250
      boolean ignoreResourceDeletion = false;
1✔
251
      // "For forward compatibility reasons, the client will ignore any entry in the list that it
252
      // does not understand, regardless of type."
253
      List<?> serverFeatures = JsonUtil.getList(serverConfig, "server_features");
1✔
254
      if (serverFeatures != null) {
1✔
255
        logger.log(XdsLogLevel.INFO, "Server features: {0}", serverFeatures);
1✔
256
        ignoreResourceDeletion = serverFeatures.contains(SERVER_FEATURE_IGNORE_RESOURCE_DELETION);
1✔
257
      }
258
      servers.add(
1✔
259
          ServerInfo.create(serverUri, implSpecificConfig, ignoreResourceDeletion,
1✔
260
              serverFeatures != null
261
                  && serverFeatures.contains(SERVER_FEATURE_TRUSTED_XDS_SERVER)));
1✔
262
    }
1✔
263
    return servers.build();
1✔
264
  }
265

266
  @VisibleForTesting
267
  public void setFileReader(FileReader reader) {
268
    this.reader = reader;
1✔
269
  }
1✔
270

271
  /**
272
   * Reads the content of the file with the given path in the file system.
273
   */
274
  public interface FileReader {
275
    String readFile(String path) throws IOException;
276
  }
277

278
  protected enum LocalFileReader implements FileReader {
1✔
279
    INSTANCE;
1✔
280

281
    @Override
282
    public String readFile(String path) throws IOException {
283
      return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
×
284
    }
285
  }
286

287
  private static <T> T checkForNull(T value, String fieldName) throws XdsInitializationException {
288
    if (value == null) {
1✔
289
      throw new XdsInitializationException(
1✔
290
          "Invalid bootstrap: '" + fieldName + "' does not exist.");
291
    }
292
    return value;
1✔
293
  }
294

295
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc