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

grpc / grpc-java / #19593

17 Dec 2024 04:54AM UTC coverage: 84.599% (-0.03%) from 84.627%
#19593

push

github

web-flow
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

33844 of 40005 relevant lines covered (84.6%)

0.85 hits per line

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

96.24
/../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

54
  protected final XdsLogger logger;
55

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

219
    return builder;
1✔
220
  }
221

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

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

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

250
  @VisibleForTesting
251
  public void setFileReader(FileReader reader) {
252
    this.reader = reader;
1✔
253
  }
1✔
254

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

262
  protected enum LocalFileReader implements FileReader {
1✔
263
    INSTANCE;
1✔
264

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

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

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