channel-credentials.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. "use strict";
  2. /*
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.createCertificateProviderChannelCredentials = exports.ChannelCredentials = void 0;
  20. const tls_1 = require("tls");
  21. const call_credentials_1 = require("./call-credentials");
  22. const tls_helpers_1 = require("./tls-helpers");
  23. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  24. function verifyIsBufferOrNull(obj, friendlyName) {
  25. if (obj && !(obj instanceof Buffer)) {
  26. throw new TypeError(`${friendlyName}, if provided, must be a Buffer.`);
  27. }
  28. }
  29. /**
  30. * A class that contains credentials for communicating over a channel, as well
  31. * as a set of per-call credentials, which are applied to every method call made
  32. * over a channel initialized with an instance of this class.
  33. */
  34. class ChannelCredentials {
  35. constructor(callCredentials) {
  36. this.callCredentials = callCredentials || call_credentials_1.CallCredentials.createEmpty();
  37. }
  38. /**
  39. * Gets the set of per-call credentials associated with this instance.
  40. */
  41. _getCallCredentials() {
  42. return this.callCredentials;
  43. }
  44. _ref() {
  45. // Do nothing by default
  46. }
  47. _unref() {
  48. // Do nothing by default
  49. }
  50. /**
  51. * Return a new ChannelCredentials instance with a given set of credentials.
  52. * The resulting instance can be used to construct a Channel that communicates
  53. * over TLS.
  54. * @param rootCerts The root certificate data.
  55. * @param privateKey The client certificate private key, if available.
  56. * @param certChain The client certificate key chain, if available.
  57. * @param verifyOptions Additional options to modify certificate verification
  58. */
  59. static createSsl(rootCerts, privateKey, certChain, verifyOptions) {
  60. var _a;
  61. verifyIsBufferOrNull(rootCerts, 'Root certificate');
  62. verifyIsBufferOrNull(privateKey, 'Private key');
  63. verifyIsBufferOrNull(certChain, 'Certificate chain');
  64. if (privateKey && !certChain) {
  65. throw new Error('Private key must be given with accompanying certificate chain');
  66. }
  67. if (!privateKey && certChain) {
  68. throw new Error('Certificate chain must be given with accompanying private key');
  69. }
  70. const secureContext = (0, tls_1.createSecureContext)({
  71. ca: (_a = rootCerts !== null && rootCerts !== void 0 ? rootCerts : (0, tls_helpers_1.getDefaultRootsData)()) !== null && _a !== void 0 ? _a : undefined,
  72. key: privateKey !== null && privateKey !== void 0 ? privateKey : undefined,
  73. cert: certChain !== null && certChain !== void 0 ? certChain : undefined,
  74. ciphers: tls_helpers_1.CIPHER_SUITES,
  75. });
  76. return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
  77. }
  78. /**
  79. * Return a new ChannelCredentials instance with credentials created using
  80. * the provided secureContext. The resulting instances can be used to
  81. * construct a Channel that communicates over TLS. gRPC will not override
  82. * anything in the provided secureContext, so the environment variables
  83. * GRPC_SSL_CIPHER_SUITES and GRPC_DEFAULT_SSL_ROOTS_FILE_PATH will
  84. * not be applied.
  85. * @param secureContext The return value of tls.createSecureContext()
  86. * @param verifyOptions Additional options to modify certificate verification
  87. */
  88. static createFromSecureContext(secureContext, verifyOptions) {
  89. return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
  90. }
  91. /**
  92. * Return a new ChannelCredentials instance with no credentials.
  93. */
  94. static createInsecure() {
  95. return new InsecureChannelCredentialsImpl();
  96. }
  97. }
  98. exports.ChannelCredentials = ChannelCredentials;
  99. class InsecureChannelCredentialsImpl extends ChannelCredentials {
  100. constructor() {
  101. super();
  102. }
  103. compose(callCredentials) {
  104. throw new Error('Cannot compose insecure credentials');
  105. }
  106. _getConnectionOptions() {
  107. return {};
  108. }
  109. _isSecure() {
  110. return false;
  111. }
  112. _equals(other) {
  113. return other instanceof InsecureChannelCredentialsImpl;
  114. }
  115. }
  116. class SecureChannelCredentialsImpl extends ChannelCredentials {
  117. constructor(secureContext, verifyOptions) {
  118. super();
  119. this.secureContext = secureContext;
  120. this.verifyOptions = verifyOptions;
  121. this.connectionOptions = {
  122. secureContext,
  123. };
  124. // Node asserts that this option is a function, so we cannot pass undefined
  125. if (verifyOptions === null || verifyOptions === void 0 ? void 0 : verifyOptions.checkServerIdentity) {
  126. this.connectionOptions.checkServerIdentity =
  127. verifyOptions.checkServerIdentity;
  128. }
  129. if ((verifyOptions === null || verifyOptions === void 0 ? void 0 : verifyOptions.rejectUnauthorized) !== undefined) {
  130. this.connectionOptions.rejectUnauthorized =
  131. verifyOptions.rejectUnauthorized;
  132. }
  133. }
  134. compose(callCredentials) {
  135. const combinedCallCredentials = this.callCredentials.compose(callCredentials);
  136. return new ComposedChannelCredentialsImpl(this, combinedCallCredentials);
  137. }
  138. _getConnectionOptions() {
  139. // Copy to prevent callers from mutating this.connectionOptions
  140. return Object.assign({}, this.connectionOptions);
  141. }
  142. _isSecure() {
  143. return true;
  144. }
  145. _equals(other) {
  146. if (this === other) {
  147. return true;
  148. }
  149. if (other instanceof SecureChannelCredentialsImpl) {
  150. return (this.secureContext === other.secureContext &&
  151. this.verifyOptions.checkServerIdentity ===
  152. other.verifyOptions.checkServerIdentity);
  153. }
  154. else {
  155. return false;
  156. }
  157. }
  158. }
  159. class CertificateProviderChannelCredentialsImpl extends ChannelCredentials {
  160. constructor(caCertificateProvider, identityCertificateProvider, verifyOptions) {
  161. super();
  162. this.caCertificateProvider = caCertificateProvider;
  163. this.identityCertificateProvider = identityCertificateProvider;
  164. this.verifyOptions = verifyOptions;
  165. this.refcount = 0;
  166. this.latestCaUpdate = null;
  167. this.latestIdentityUpdate = null;
  168. this.caCertificateUpdateListener = this.handleCaCertificateUpdate.bind(this);
  169. this.identityCertificateUpdateListener = this.handleIdentityCertitificateUpdate.bind(this);
  170. }
  171. compose(callCredentials) {
  172. const combinedCallCredentials = this.callCredentials.compose(callCredentials);
  173. return new ComposedChannelCredentialsImpl(this, combinedCallCredentials);
  174. }
  175. _getConnectionOptions() {
  176. var _a, _b, _c;
  177. if (this.latestCaUpdate === null) {
  178. return null;
  179. }
  180. if (this.identityCertificateProvider !== null && this.latestIdentityUpdate === null) {
  181. return null;
  182. }
  183. const secureContext = (0, tls_1.createSecureContext)({
  184. ca: this.latestCaUpdate.caCertificate,
  185. key: (_a = this.latestIdentityUpdate) === null || _a === void 0 ? void 0 : _a.privateKey,
  186. cert: (_b = this.latestIdentityUpdate) === null || _b === void 0 ? void 0 : _b.certificate,
  187. ciphers: tls_helpers_1.CIPHER_SUITES
  188. });
  189. const options = {
  190. secureContext: secureContext
  191. };
  192. if ((_c = this.verifyOptions) === null || _c === void 0 ? void 0 : _c.checkServerIdentity) {
  193. options.checkServerIdentity = this.verifyOptions.checkServerIdentity;
  194. }
  195. return options;
  196. }
  197. _isSecure() {
  198. return true;
  199. }
  200. _equals(other) {
  201. var _a, _b;
  202. if (this === other) {
  203. return true;
  204. }
  205. if (other instanceof CertificateProviderChannelCredentialsImpl) {
  206. return this.caCertificateProvider === other.caCertificateProvider &&
  207. this.identityCertificateProvider === other.identityCertificateProvider &&
  208. ((_a = this.verifyOptions) === null || _a === void 0 ? void 0 : _a.checkServerIdentity) === ((_b = other.verifyOptions) === null || _b === void 0 ? void 0 : _b.checkServerIdentity);
  209. }
  210. else {
  211. return false;
  212. }
  213. }
  214. _ref() {
  215. var _a;
  216. if (this.refcount === 0) {
  217. this.caCertificateProvider.addCaCertificateListener(this.caCertificateUpdateListener);
  218. (_a = this.identityCertificateProvider) === null || _a === void 0 ? void 0 : _a.addIdentityCertificateListener(this.identityCertificateUpdateListener);
  219. }
  220. this.refcount += 1;
  221. }
  222. _unref() {
  223. var _a;
  224. this.refcount -= 1;
  225. if (this.refcount === 0) {
  226. this.caCertificateProvider.removeCaCertificateListener(this.caCertificateUpdateListener);
  227. (_a = this.identityCertificateProvider) === null || _a === void 0 ? void 0 : _a.removeIdentityCertificateListener(this.identityCertificateUpdateListener);
  228. }
  229. }
  230. handleCaCertificateUpdate(update) {
  231. this.latestCaUpdate = update;
  232. }
  233. handleIdentityCertitificateUpdate(update) {
  234. this.latestIdentityUpdate = update;
  235. }
  236. }
  237. function createCertificateProviderChannelCredentials(caCertificateProvider, identityCertificateProvider, verifyOptions) {
  238. return new CertificateProviderChannelCredentialsImpl(caCertificateProvider, identityCertificateProvider, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : null);
  239. }
  240. exports.createCertificateProviderChannelCredentials = createCertificateProviderChannelCredentials;
  241. class ComposedChannelCredentialsImpl extends ChannelCredentials {
  242. constructor(channelCredentials, callCreds) {
  243. super(callCreds);
  244. this.channelCredentials = channelCredentials;
  245. if (!channelCredentials._isSecure()) {
  246. throw new Error('Cannot compose insecure credentials');
  247. }
  248. }
  249. compose(callCredentials) {
  250. const combinedCallCredentials = this.callCredentials.compose(callCredentials);
  251. return new ComposedChannelCredentialsImpl(this.channelCredentials, combinedCallCredentials);
  252. }
  253. _getConnectionOptions() {
  254. return this.channelCredentials._getConnectionOptions();
  255. }
  256. _isSecure() {
  257. return true;
  258. }
  259. _equals(other) {
  260. if (this === other) {
  261. return true;
  262. }
  263. if (other instanceof ComposedChannelCredentialsImpl) {
  264. return (this.channelCredentials._equals(other.channelCredentials) &&
  265. this.callCredentials._equals(other.callCredentials));
  266. }
  267. else {
  268. return false;
  269. }
  270. }
  271. }
  272. //# sourceMappingURL=channel-credentials.js.map