service-config.js
9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"use strict";
/*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractAndSelectServiceConfig = exports.validateServiceConfig = void 0;
/* This file implements gRFC A2 and the service config spec:
* https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md
* https://github.com/grpc/grpc/blob/master/doc/service_config.md. Each
* function here takes an object with unknown structure and returns its
* specific object type if the input has the right structure, and throws an
* error otherwise. */
/* The any type is purposely used here. All functions validate their input at
* runtime */
/* eslint-disable @typescript-eslint/no-explicit-any */
const os = require("os");
const load_balancer_1 = require("./load-balancer");
/**
* Recognizes a number with up to 9 digits after the decimal point, followed by
* an "s", representing a number of seconds.
*/
const TIMEOUT_REGEX = /^\d+(\.\d{1,9})?s$/;
/**
* Client language name used for determining whether this client matches a
* `ServiceConfigCanaryConfig`'s `clientLanguage` list.
*/
const CLIENT_LANGUAGE_STRING = 'node';
function validateName(obj) {
if (!('service' in obj) || typeof obj.service !== 'string') {
throw new Error('Invalid method config name: invalid service');
}
const result = {
service: obj.service,
};
if ('method' in obj) {
if (typeof obj.method === 'string') {
result.method = obj.method;
}
else {
throw new Error('Invalid method config name: invalid method');
}
}
return result;
}
function validateMethodConfig(obj) {
const result = {
name: [],
};
if (!('name' in obj) || !Array.isArray(obj.name)) {
throw new Error('Invalid method config: invalid name array');
}
for (const name of obj.name) {
result.name.push(validateName(name));
}
if ('waitForReady' in obj) {
if (typeof obj.waitForReady !== 'boolean') {
throw new Error('Invalid method config: invalid waitForReady');
}
result.waitForReady = obj.waitForReady;
}
if ('timeout' in obj) {
if (!(typeof obj.timeout === 'string') ||
!TIMEOUT_REGEX.test(obj.timeout)) {
throw new Error('Invalid method config: invalid timeout');
}
result.timeout = obj.timeout;
}
if ('maxRequestBytes' in obj) {
if (typeof obj.maxRequestBytes !== 'number') {
throw new Error('Invalid method config: invalid maxRequestBytes');
}
result.maxRequestBytes = obj.maxRequestBytes;
}
if ('maxResponseBytes' in obj) {
if (typeof obj.maxResponseBytes !== 'number') {
throw new Error('Invalid method config: invalid maxRequestBytes');
}
result.maxResponseBytes = obj.maxResponseBytes;
}
return result;
}
function validateServiceConfig(obj) {
const result = {
loadBalancingConfig: [],
methodConfig: [],
};
if ('loadBalancingPolicy' in obj) {
if (typeof obj.loadBalancingPolicy === 'string') {
result.loadBalancingPolicy = obj.loadBalancingPolicy;
}
else {
throw new Error('Invalid service config: invalid loadBalancingPolicy');
}
}
if ('loadBalancingConfig' in obj) {
if (Array.isArray(obj.loadBalancingConfig)) {
for (const config of obj.loadBalancingConfig) {
result.loadBalancingConfig.push(load_balancer_1.validateLoadBalancingConfig(config));
}
}
else {
throw new Error('Invalid service config: invalid loadBalancingConfig');
}
}
if ('methodConfig' in obj) {
if (Array.isArray(obj.methodConfig)) {
for (const methodConfig of obj.methodConfig) {
result.methodConfig.push(validateMethodConfig(methodConfig));
}
}
}
// Validate method name uniqueness
const seenMethodNames = [];
for (const methodConfig of result.methodConfig) {
for (const name of methodConfig.name) {
for (const seenName of seenMethodNames) {
if (name.service === seenName.service &&
name.method === seenName.method) {
throw new Error(`Invalid service config: duplicate name ${name.service}/${name.method}`);
}
}
seenMethodNames.push(name);
}
}
return result;
}
exports.validateServiceConfig = validateServiceConfig;
function validateCanaryConfig(obj) {
if (!('serviceConfig' in obj)) {
throw new Error('Invalid service config choice: missing service config');
}
const result = {
serviceConfig: validateServiceConfig(obj.serviceConfig),
};
if ('clientLanguage' in obj) {
if (Array.isArray(obj.clientLanguage)) {
result.clientLanguage = [];
for (const lang of obj.clientLanguage) {
if (typeof lang === 'string') {
result.clientLanguage.push(lang);
}
else {
throw new Error('Invalid service config choice: invalid clientLanguage');
}
}
}
else {
throw new Error('Invalid service config choice: invalid clientLanguage');
}
}
if ('clientHostname' in obj) {
if (Array.isArray(obj.clientHostname)) {
result.clientHostname = [];
for (const lang of obj.clientHostname) {
if (typeof lang === 'string') {
result.clientHostname.push(lang);
}
else {
throw new Error('Invalid service config choice: invalid clientHostname');
}
}
}
else {
throw new Error('Invalid service config choice: invalid clientHostname');
}
}
if ('percentage' in obj) {
if (typeof obj.percentage === 'number' &&
0 <= obj.percentage &&
obj.percentage <= 100) {
result.percentage = obj.percentage;
}
else {
throw new Error('Invalid service config choice: invalid percentage');
}
}
// Validate that no unexpected fields are present
const allowedFields = [
'clientLanguage',
'percentage',
'clientHostname',
'serviceConfig',
];
for (const field in obj) {
if (!allowedFields.includes(field)) {
throw new Error(`Invalid service config choice: unexpected field ${field}`);
}
}
return result;
}
function validateAndSelectCanaryConfig(obj, percentage) {
if (!Array.isArray(obj)) {
throw new Error('Invalid service config list');
}
for (const config of obj) {
const validatedConfig = validateCanaryConfig(config);
/* For each field, we check if it is present, then only discard the
* config if the field value does not match the current client */
if (typeof validatedConfig.percentage === 'number' &&
percentage > validatedConfig.percentage) {
continue;
}
if (Array.isArray(validatedConfig.clientHostname)) {
let hostnameMatched = false;
for (const hostname of validatedConfig.clientHostname) {
if (hostname === os.hostname()) {
hostnameMatched = true;
}
}
if (!hostnameMatched) {
continue;
}
}
if (Array.isArray(validatedConfig.clientLanguage)) {
let languageMatched = false;
for (const language of validatedConfig.clientLanguage) {
if (language === CLIENT_LANGUAGE_STRING) {
languageMatched = true;
}
}
if (!languageMatched) {
continue;
}
}
return validatedConfig.serviceConfig;
}
throw new Error('No matching service config found');
}
/**
* Find the "grpc_config" record among the TXT records, parse its value as JSON, validate its contents,
* and select a service config with selection fields that all match this client. Most of these steps
* can fail with an error; the caller must handle any errors thrown this way.
* @param txtRecord The TXT record array that is output from a successful call to dns.resolveTxt
* @param percentage A number chosen from the range [0, 100) that is used to select which config to use
* @return The service configuration to use, given the percentage value, or null if the service config
* data has a valid format but none of the options match the current client.
*/
function extractAndSelectServiceConfig(txtRecord, percentage) {
for (const record of txtRecord) {
if (record.length > 0 && record[0].startsWith('grpc_config=')) {
/* Treat the list of strings in this record as a single string and remove
* "grpc_config=" from the beginning. The rest should be a JSON string */
const recordString = record.join('').substring('grpc_config='.length);
const recordJson = JSON.parse(recordString);
return validateAndSelectCanaryConfig(recordJson, percentage);
}
}
return null;
}
exports.extractAndSelectServiceConfig = extractAndSelectServiceConfig;
//# sourceMappingURL=service-config.js.map