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

grpc / grpc-java / #19597

19 Dec 2024 04:13PM UTC coverage: 88.556% (-0.006%) from 88.562%
#19597

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

33368 of 37680 relevant lines covered (88.56%)

0.89 hits per line

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

96.27
/../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
  // Client features.
45
  @VisibleForTesting
46
  public static final String CLIENT_FEATURE_DISABLE_OVERPROVISIONING =
47
      "envoy.lb.does_not_support_overprovisioning";
48
  @VisibleForTesting
49
  public static final String CLIENT_FEATURE_RESOURCE_IN_SOTW = "xds.config.resource-in-sotw";
50

51
  // Server features.
52
  private static final String SERVER_FEATURE_IGNORE_RESOURCE_DELETION = "ignore_resource_deletion";
53
  private static final String SERVER_FEATURE_TRUSTED_XDS_SERVER = "trusted_xds_server";
54

55
  protected final XdsLogger logger;
56

57
  protected FileReader reader = LocalFileReader.INSTANCE;
1✔
58

59
  protected BootstrapperImpl() {
1✔
60
    logger = XdsLogger.withLogId(InternalLogId.allocate("bootstrapper", null));
1✔
61
  }
1✔
62

63
  protected abstract String getJsonContent() throws IOException, XdsInitializationException;
64

65
  protected abstract Object getImplSpecificConfig(Map<String, ?> serverConfig, String serverUri)
66
      throws XdsInitializationException;
67

68
  /**
69
   * Reads and parses bootstrap config. The config is expected to be in JSON format.
70
   */
71
  @SuppressWarnings("unchecked")
72
  @Override
73
  public BootstrapInfo bootstrap() throws XdsInitializationException {
74
    String jsonContent;
75
    try {
76
      jsonContent = getJsonContent();
1✔
77
    } catch (IOException e) {
×
78
      throw new XdsInitializationException("Fail to read bootstrap file", e);
×
79
    }
1✔
80

81
    Map<String, ?> rawBootstrap;
82
    try {
83
      rawBootstrap = (Map<String, ?>) JsonParser.parse(jsonContent);
1✔
84
    } catch (IOException e) {
×
85
      throw new XdsInitializationException("Failed to parse JSON", e);
×
86
    }
1✔
87

88
    logger.log(XdsLogLevel.DEBUG, "Bootstrap configuration:\n{0}", rawBootstrap);
1✔
89
    return bootstrap(rawBootstrap);
1✔
90
  }
91

92
  @Override
93
  public BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationException {
94
    return bootstrapBuilder(rawData).build();
1✔
95
  }
96

97
  protected BootstrapInfo.Builder bootstrapBuilder(Map<String, ?> rawData)
98
      throws XdsInitializationException {
99
    BootstrapInfo.Builder builder = BootstrapInfo.builder();
1✔
100

101
    List<?> rawServerConfigs = JsonUtil.getList(rawData, "xds_servers");
1✔
102
    if (rawServerConfigs == null) {
1✔
103
      throw new XdsInitializationException("Invalid bootstrap: 'xds_servers' does not exist.");
1✔
104
    }
105
    List<ServerInfo> servers = parseServerInfos(rawServerConfigs, logger);
1✔
106
    builder.servers(servers);
1✔
107

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

154
    Map<String, ?> certProvidersBlob = JsonUtil.getObject(rawData, "certificate_providers");
1✔
155
    if (certProvidersBlob != null) {
1✔
156
      logger.log(XdsLogLevel.INFO, "Configured with {0} cert providers", certProvidersBlob.size());
1✔
157
      Map<String, CertificateProviderInfo> certProviders = new HashMap<>(certProvidersBlob.size());
1✔
158
      for (String name : certProvidersBlob.keySet()) {
1✔
159
        Map<String, ?> valueMap = JsonUtil.getObject(certProvidersBlob, name);
1✔
160
        String pluginName =
1✔
161
            checkForNull(JsonUtil.getString(valueMap, "plugin_name"), "plugin_name");
1✔
162
        logger.log(XdsLogLevel.INFO, "cert provider: {0}, plugin name: {1}", name, pluginName);
1✔
163
        Map<String, ?> config = checkForNull(JsonUtil.getObject(valueMap, "config"), "config");
1✔
164
        CertificateProviderInfo certificateProviderInfo =
1✔
165
            CertificateProviderInfo.create(pluginName, config);
1✔
166
        certProviders.put(name, certificateProviderInfo);
1✔
167
      }
1✔
168
      builder.certProviders(certProviders);
1✔
169
    }
170

171
    String serverResourceId =
1✔
172
        JsonUtil.getString(rawData, "server_listener_resource_name_template");
1✔
173
    logger.log(
1✔
174
        XdsLogLevel.INFO, "server_listener_resource_name_template: {0}", serverResourceId);
175
    builder.serverListenerResourceNameTemplate(serverResourceId);
1✔
176

177
    String clientDefaultListener =
1✔
178
        JsonUtil.getString(rawData, "client_default_listener_resource_name_template");
1✔
179
    logger.log(
1✔
180
        XdsLogLevel.INFO, "client_default_listener_resource_name_template: {0}",
181
        clientDefaultListener);
182
    if (clientDefaultListener != null) {
1✔
183
      builder.clientDefaultListenerResourceNameTemplate(clientDefaultListener);
1✔
184
    }
185

186
    Map<String, ?> rawAuthoritiesMap =
1✔
187
        JsonUtil.getObject(rawData, "authorities");
1✔
188
    ImmutableMap.Builder<String, AuthorityInfo> authorityInfoMapBuilder = ImmutableMap.builder();
1✔
189
    if (rawAuthoritiesMap != null) {
1✔
190
      logger.log(
1✔
191
          XdsLogLevel.INFO, "Configured with {0} xDS server authorities", rawAuthoritiesMap.size());
1✔
192
      for (String authorityName : rawAuthoritiesMap.keySet()) {
1✔
193
        logger.log(XdsLogLevel.INFO, "xDS server authority: {0}", authorityName);
1✔
194
        Map<String, ?> rawAuthority = JsonUtil.getObject(rawAuthoritiesMap, authorityName);
1✔
195
        String clientListnerTemplate =
1✔
196
            JsonUtil.getString(rawAuthority, "client_listener_resource_name_template");
1✔
197
        logger.log(
1✔
198
            XdsLogLevel.INFO, "client_listener_resource_name_template: {0}", clientListnerTemplate);
199
        String prefix = XDSTP_SCHEME + "//" + authorityName + "/";
1✔
200
        if (clientListnerTemplate == null) {
1✔
201
          clientListnerTemplate = prefix + "envoy.config.listener.v3.Listener/%s";
1✔
202
        } else if (!clientListnerTemplate.startsWith(prefix)) {
1✔
203
          throw new XdsInitializationException(
1✔
204
              "client_listener_resource_name_template: '" + clientListnerTemplate
205
                  + "' does not start with " + prefix);
206
        }
207
        List<?> rawAuthorityServers = JsonUtil.getList(rawAuthority, "xds_servers");
1✔
208
        List<ServerInfo> authorityServers;
209
        if (rawAuthorityServers == null || rawAuthorityServers.isEmpty()) {
1✔
210
          authorityServers = servers;
1✔
211
        } else {
212
          authorityServers = parseServerInfos(rawAuthorityServers, logger);
1✔
213
        }
214
        authorityInfoMapBuilder.put(
1✔
215
            authorityName, AuthorityInfo.create(clientListnerTemplate, authorityServers));
1✔
216
      }
1✔
217
      builder.authorities(authorityInfoMapBuilder.buildOrThrow());
1✔
218
    }
219

220
    return builder;
1✔
221
  }
222

223
  private List<ServerInfo> parseServerInfos(List<?> rawServerConfigs, XdsLogger logger)
224
      throws XdsInitializationException {
225
    logger.log(XdsLogLevel.INFO, "Configured with {0} xDS servers", rawServerConfigs.size());
1✔
226
    ImmutableList.Builder<ServerInfo> servers = ImmutableList.builder();
1✔
227
    List<Map<String, ?>> serverConfigList = JsonUtil.checkObjectList(rawServerConfigs);
1✔
228
    for (Map<String, ?> serverConfig : serverConfigList) {
1✔
229
      String serverUri = JsonUtil.getString(serverConfig, "server_uri");
1✔
230
      if (serverUri == null) {
1✔
231
        throw new XdsInitializationException("Invalid bootstrap: missing 'server_uri'");
1✔
232
      }
233
      logger.log(XdsLogLevel.INFO, "xDS server URI: {0}", serverUri);
1✔
234

235
      Object implSpecificConfig = getImplSpecificConfig(serverConfig, serverUri);
1✔
236

237
      boolean ignoreResourceDeletion = false;
1✔
238
      // "For forward compatibility reasons, the client will ignore any entry in the list that it
239
      // does not understand, regardless of type."
240
      List<?> serverFeatures = JsonUtil.getList(serverConfig, "server_features");
1✔
241
      if (serverFeatures != null) {
1✔
242
        logger.log(XdsLogLevel.INFO, "Server features: {0}", serverFeatures);
1✔
243
        ignoreResourceDeletion = serverFeatures.contains(SERVER_FEATURE_IGNORE_RESOURCE_DELETION);
1✔
244
      }
245
      servers.add(
1✔
246
          ServerInfo.create(serverUri, implSpecificConfig, ignoreResourceDeletion,
1✔
247
              serverFeatures != null
248
                  && serverFeatures.contains(SERVER_FEATURE_TRUSTED_XDS_SERVER)));
1✔
249
    }
1✔
250
    return servers.build();
1✔
251
  }
252

253
  @VisibleForTesting
254
  public void setFileReader(FileReader reader) {
255
    this.reader = reader;
1✔
256
  }
1✔
257

258
  /**
259
   * Reads the content of the file with the given path in the file system.
260
   */
261
  public interface FileReader {
262
    String readFile(String path) throws IOException;
263
  }
264

265
  protected enum LocalFileReader implements FileReader {
1✔
266
    INSTANCE;
1✔
267

268
    @Override
269
    public String readFile(String path) throws IOException {
270
      return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
×
271
    }
272
  }
273

274
  private static <T> T checkForNull(T value, String fieldName) throws XdsInitializationException {
275
    if (value == null) {
1✔
276
      throw new XdsInitializationException(
1✔
277
          "Invalid bootstrap: '" + fieldName + "' does not exist.");
278
    }
279
    return value;
1✔
280
  }
281

282
}
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

© 2025 Coveralls, Inc