@gravity-ui/gateway 3.2.0 → 3.2.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/build/components/grpc.d.ts +1 -1
- package/build/components/grpc.js +35 -2
- package/build/index.js +1 -1
- package/build/models/common.d.ts +8 -0
- package/build/utils/grpc.js +14 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -102,9 +102,13 @@ interface GatewayConfig {
|
|
|
102
102
|
onRequestFailed?: (req: Request, res: Response, error: any) => any;
|
|
103
103
|
// List of paths to the necessary proto files for the gateway.
|
|
104
104
|
includeProtoRoots?: string[];
|
|
105
|
-
// Configuration of the path to the certificate in gRPC.
|
|
105
|
+
// Configuration of the path to the CA certificate in gRPC.
|
|
106
106
|
// Set to null to use system certificates by default.
|
|
107
107
|
caCertificatePath?: string | null;
|
|
108
|
+
// Configuration of the path to the client certificate for mTLS in gRPC.
|
|
109
|
+
clientCertificatePath?: string | null;
|
|
110
|
+
// Configuration of the path to the client private key for mTLS in gRPC.
|
|
111
|
+
clientKeyPath?: string | null;
|
|
108
112
|
// Telemetry sending configuration.
|
|
109
113
|
sendStats?: SendStats;
|
|
110
114
|
// Configuration of headers sent to the API.
|
|
@@ -143,6 +147,9 @@ const config = {
|
|
|
143
147
|
includeProtoRoots: ['...'],
|
|
144
148
|
timeout: 25000, // default 25 seconds
|
|
145
149
|
caCertificatePath: '...',
|
|
150
|
+
// Optional: paths for mTLS client certificate and key
|
|
151
|
+
clientCertificatePath: '...',
|
|
152
|
+
clientKeyPath: '...',
|
|
146
153
|
};
|
|
147
154
|
|
|
148
155
|
const {api: gatewayApi} = getGatewayControllers({root: Schema}, config);
|
|
@@ -19,6 +19,6 @@ export interface GrpcContext {
|
|
|
19
19
|
credentials: CredentialsMap;
|
|
20
20
|
}
|
|
21
21
|
export declare function createRoot(includeGrpcPaths?: string[]): protobufjs.Root;
|
|
22
|
-
export declare function getCredentialsMap(caCertificatePath?: string | null): CredentialsMap;
|
|
22
|
+
export declare function getCredentialsMap(caCertificatePath?: string | null, clientCertificatePath?: string | null, clientKeyPath?: string | null): CredentialsMap;
|
|
23
23
|
export default function createGrpcAction<Context extends GatewayContext>({ root, credentials }: GrpcContext, endpoints: EndpointsConfig | undefined, config: ApiServiceGrpcActionConfig<Context, any, any>, serviceKey: string, actionName: string, options: GatewayApiOptions<Context>, ErrorConstructor: AppErrorConstructor): (actionConfig: ApiActionConfig<Context, any, any>) => Promise<import("../models/common").GatewayActionClientStreamResponse<any> | import("../models/common").GatewayActionServerStreamResponse<any> | import("../models/common").GatewayActionDuplexStreamResponse<any> | import("../models/common").GatewayActionUnaryResponse<any>>;
|
|
24
24
|
export {};
|
package/build/components/grpc.js
CHANGED
|
@@ -59,13 +59,21 @@ function createRoot(includeGrpcPaths) {
|
|
|
59
59
|
return root;
|
|
60
60
|
}
|
|
61
61
|
exports.createRoot = createRoot;
|
|
62
|
-
function getCredentialsMap(caCertificatePath) {
|
|
62
|
+
function getCredentialsMap(caCertificatePath, clientCertificatePath, clientKeyPath) {
|
|
63
63
|
let certificate;
|
|
64
|
+
let clientCertificate;
|
|
65
|
+
let clientKey;
|
|
64
66
|
if (caCertificatePath && fs_1.default.existsSync(caCertificatePath)) {
|
|
65
67
|
certificate = fs_1.default.readFileSync(caCertificatePath);
|
|
66
68
|
}
|
|
69
|
+
if (clientCertificatePath && fs_1.default.existsSync(clientCertificatePath)) {
|
|
70
|
+
clientCertificate = fs_1.default.readFileSync(clientCertificatePath);
|
|
71
|
+
}
|
|
72
|
+
if (clientKeyPath && fs_1.default.existsSync(clientKeyPath)) {
|
|
73
|
+
clientKey = fs_1.default.readFileSync(clientKeyPath);
|
|
74
|
+
}
|
|
67
75
|
return {
|
|
68
|
-
secure: grpc.ChannelCredentials.createSsl(certificate),
|
|
76
|
+
secure: grpc.ChannelCredentials.createSsl(certificate, clientKey, clientCertificate),
|
|
69
77
|
secureWithoutRootCert: grpc.ChannelCredentials.createSsl(),
|
|
70
78
|
insecure: grpc.ChannelCredentials.createInsecure(),
|
|
71
79
|
};
|
|
@@ -170,12 +178,37 @@ function clearInstancesCache(service, instancesMap, cachePath, closeTimeout, ctx
|
|
|
170
178
|
function getChannelCredential(config, endpointData, credentials) {
|
|
171
179
|
let endpointInsecure;
|
|
172
180
|
let endpointSecureWithoutRootCert;
|
|
181
|
+
let endpointCaCertificatePath;
|
|
182
|
+
let endpointClientCertificatePath;
|
|
183
|
+
let endpointClientKeyPath;
|
|
173
184
|
if ((0, common_2.isExtendedGrpcActionEndpoint)(endpointData)) {
|
|
174
185
|
endpointInsecure = endpointData === null || endpointData === void 0 ? void 0 : endpointData.insecure;
|
|
175
186
|
endpointSecureWithoutRootCert = endpointData === null || endpointData === void 0 ? void 0 : endpointData.secureWithoutRootCert;
|
|
187
|
+
endpointCaCertificatePath = endpointData === null || endpointData === void 0 ? void 0 : endpointData.caCertificatePath;
|
|
188
|
+
endpointClientCertificatePath = endpointData === null || endpointData === void 0 ? void 0 : endpointData.clientCertificatePath;
|
|
189
|
+
endpointClientKeyPath = endpointData === null || endpointData === void 0 ? void 0 : endpointData.clientKeyPath;
|
|
176
190
|
}
|
|
177
191
|
const isInsecure = config.insecure || endpointInsecure;
|
|
178
192
|
const isSecureWithoutRootCert = config.secureWithoutRootCert || endpointSecureWithoutRootCert;
|
|
193
|
+
// If endpoint-specific certificates are provided, create new credentials
|
|
194
|
+
if (endpointCaCertificatePath || endpointClientCertificatePath || endpointClientKeyPath) {
|
|
195
|
+
let certificate;
|
|
196
|
+
let clientCertificate;
|
|
197
|
+
let clientKey;
|
|
198
|
+
const caCertPath = endpointCaCertificatePath || config.caCertificatePath;
|
|
199
|
+
const clientCertPath = endpointClientCertificatePath || config.clientCertificatePath;
|
|
200
|
+
const clientKeyPath = endpointClientKeyPath || config.clientKeyPath;
|
|
201
|
+
if (caCertPath && fs_1.default.existsSync(caCertPath)) {
|
|
202
|
+
certificate = fs_1.default.readFileSync(caCertPath);
|
|
203
|
+
}
|
|
204
|
+
if (clientCertPath && fs_1.default.existsSync(clientCertPath)) {
|
|
205
|
+
clientCertificate = fs_1.default.readFileSync(clientCertPath);
|
|
206
|
+
}
|
|
207
|
+
if (clientKeyPath && fs_1.default.existsSync(clientKeyPath)) {
|
|
208
|
+
clientKey = fs_1.default.readFileSync(clientKeyPath);
|
|
209
|
+
}
|
|
210
|
+
return grpc.ChannelCredentials.createSsl(certificate, clientKey, clientCertificate);
|
|
211
|
+
}
|
|
179
212
|
let creds = credentials.secure;
|
|
180
213
|
if (isInsecure) {
|
|
181
214
|
creds = credentials.insecure;
|
package/build/index.js
CHANGED
|
@@ -244,7 +244,7 @@ function getGatewayControllers(schemasByScope, config) {
|
|
|
244
244
|
console.warn('Error when parse GATEWAY_ENDPOINTS_OVERRIDES', err);
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
-
const credentials = (0, grpc_1.getCredentialsMap)(config.caCertificatePath);
|
|
247
|
+
const credentials = (0, grpc_1.getCredentialsMap)(config.caCertificatePath, config.clientCertificatePath, config.clientKeyPath);
|
|
248
248
|
for (const scope of (0, common_1.getKeys)(schemasByScope)) {
|
|
249
249
|
apiByScope[scope] = generateGatewayApi(schemasByScope[scope], config, { root: (0, grpc_1.createRoot)(config.includeProtoRoots), credentials }, apiByScope);
|
|
250
250
|
}
|
package/build/models/common.d.ts
CHANGED
|
@@ -106,6 +106,9 @@ export interface ExtendedBaseActionEndpoint {
|
|
|
106
106
|
export interface ExtendedGrpcActionEndpoint extends ExtendedBaseActionEndpoint {
|
|
107
107
|
insecure?: boolean;
|
|
108
108
|
secureWithoutRootCert?: boolean;
|
|
109
|
+
caCertificatePath?: string;
|
|
110
|
+
clientCertificatePath?: string;
|
|
111
|
+
clientKeyPath?: string;
|
|
109
112
|
grpcOptions?: object;
|
|
110
113
|
}
|
|
111
114
|
export interface ExtendedRestActionEndpoint extends ExtendedBaseActionEndpoint {
|
|
@@ -140,6 +143,9 @@ export interface ApiServiceBaseGrpcActionConfig<Context extends GatewayContext,
|
|
|
140
143
|
protoKey: string;
|
|
141
144
|
insecure?: boolean;
|
|
142
145
|
secureWithoutRootCert?: boolean;
|
|
146
|
+
caCertificatePath?: string;
|
|
147
|
+
clientCertificatePath?: string;
|
|
148
|
+
clientKeyPath?: string;
|
|
143
149
|
encodedFields?: string[];
|
|
144
150
|
type?: HandlerType;
|
|
145
151
|
decodeAnyMessageProtoLoaderOptions?: protobufjs.IConversionOptions;
|
|
@@ -255,6 +261,8 @@ export interface GatewayConfig<Context extends GatewayContext, Req extends Gatew
|
|
|
255
261
|
sendStats?: SendStats<Context>;
|
|
256
262
|
includeProtoRoots?: string[];
|
|
257
263
|
caCertificatePath: string | null;
|
|
264
|
+
clientCertificatePath?: string | null;
|
|
265
|
+
clientKeyPath?: string | null;
|
|
258
266
|
proxyHeaders: ProxyHeaders;
|
|
259
267
|
proxyDebugHeaders?: ProxyHeaders;
|
|
260
268
|
withDebugHeaders?: boolean | ((req: Req, res: Res) => boolean);
|
package/build/utils/grpc.js
CHANGED
|
@@ -24,9 +24,20 @@ function decodeAnyMessageRecursively(root, message, decodeAnyMessageProtoLoaderO
|
|
|
24
24
|
return message;
|
|
25
25
|
}
|
|
26
26
|
const typeName = message.type_url.substring(lastSlashIndex + 1);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
try {
|
|
28
|
+
const type = root.lookupType(typeName);
|
|
29
|
+
const decodedMessage = type.toObject(type.decode(message.value), Object.assign(Object.assign({}, constants_1.DEFAULT_PROTO_LOADER_OPTIONS), decodeAnyMessageProtoLoaderOptions));
|
|
30
|
+
if (typeof decodedMessage === 'object' &&
|
|
31
|
+
!Array.isArray(decodedMessage) &&
|
|
32
|
+
!decodedMessage['@type']) {
|
|
33
|
+
Object.assign(decodedMessage, { '@type': message.type_url });
|
|
34
|
+
}
|
|
35
|
+
return decodeAnyMessageRecursively(root, decodedMessage, decodeAnyMessageProtoLoaderOptions);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`Failed to lookup ${typeName}`, error);
|
|
39
|
+
return message;
|
|
40
|
+
}
|
|
30
41
|
}
|
|
31
42
|
exports.decodeAnyMessageRecursively = decodeAnyMessageRecursively;
|
|
32
43
|
function isRetryableError(error) {
|