|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + |
| 18 | +import * as fs from 'fs/promises'; |
| 19 | +import * as logging from './logging'; |
| 20 | +import { LogVerbosity } from './constants'; |
| 21 | + |
| 22 | +const TRACER_NAME = 'certificate_provider'; |
| 23 | + |
| 24 | +function trace(text: string) { |
| 25 | + logging.trace(LogVerbosity.DEBUG, TRACER_NAME, text); |
| 26 | +} |
| 27 | + |
| 28 | +export interface CaCertificateUpdate { |
| 29 | + caCertificate: Buffer; |
| 30 | +} |
| 31 | + |
| 32 | +export interface IdentityCertificateUpdate { |
| 33 | + certificate: Buffer; |
| 34 | + privateKey: Buffer; |
| 35 | +} |
| 36 | + |
| 37 | +export interface CaCertificateUpdateListener { |
| 38 | + (update: CaCertificateUpdate | null): void; |
| 39 | +} |
| 40 | + |
| 41 | +export interface IdentityCertificateUpdateListener { |
| 42 | + (update: IdentityCertificateUpdate | null) : void; |
| 43 | +} |
| 44 | + |
| 45 | +export interface CertificateProvider { |
| 46 | + addCaCertificateListener(listener: CaCertificateUpdateListener): void; |
| 47 | + removeCaCertificateListener(listener: CaCertificateUpdateListener): void; |
| 48 | + addIdentityCertificateListener(listener: IdentityCertificateUpdateListener): void; |
| 49 | + removeIdentityCertificateListener(listener: IdentityCertificateUpdateListener): void; |
| 50 | +} |
| 51 | + |
| 52 | +export interface CertificateProviderProvider<Provider> { |
| 53 | + getInstance(): Provider; |
| 54 | +} |
| 55 | + |
| 56 | +export interface FileWatcherCertificateProviderConfig { |
| 57 | + certificateFile?: string | undefined; |
| 58 | + privateKeyFile?: string | undefined; |
| 59 | + caCertificateFile?: string | undefined; |
| 60 | + refreshIntervalMs: number; |
| 61 | +} |
| 62 | + |
| 63 | +export class FileWatcherCertificateProvider implements CertificateProvider { |
| 64 | + private refreshTimer: NodeJS.Timeout | null = null; |
| 65 | + private fileResultPromise: Promise<[PromiseSettledResult<Buffer>, PromiseSettledResult<Buffer>, PromiseSettledResult<Buffer>]> | null = null; |
| 66 | + private latestCaUpdate: CaCertificateUpdate | null = null; |
| 67 | + private caListeners: Set<CaCertificateUpdateListener> = new Set(); |
| 68 | + private latestIdentityUpdate: IdentityCertificateUpdate | null = null; |
| 69 | + private identityListeners: Set<IdentityCertificateUpdateListener> = new Set(); |
| 70 | + private lastUpdateTime: Date | null = null; |
| 71 | + |
| 72 | + constructor( |
| 73 | + private config: FileWatcherCertificateProviderConfig |
| 74 | + ) { |
| 75 | + if ((config.certificateFile === undefined) !== (config.privateKeyFile === undefined)) { |
| 76 | + throw new Error('certificateFile and privateKeyFile must be set or unset together'); |
| 77 | + } |
| 78 | + if (config.certificateFile === undefined && config.caCertificateFile === undefined) { |
| 79 | + throw new Error('At least one of certificateFile and caCertificateFile must be set'); |
| 80 | + } |
| 81 | + trace('File watcher constructed with config ' + JSON.stringify(config)); |
| 82 | + } |
| 83 | + |
| 84 | + private updateCertificates() { |
| 85 | + if (this.fileResultPromise) { |
| 86 | + return; |
| 87 | + } |
| 88 | + this.fileResultPromise = Promise.allSettled([ |
| 89 | + this.config.certificateFile ? fs.readFile(this.config.certificateFile) : Promise.reject<Buffer>(), |
| 90 | + this.config.privateKeyFile ? fs.readFile(this.config.privateKeyFile) : Promise.reject<Buffer>(), |
| 91 | + this.config.caCertificateFile ? fs.readFile(this.config.caCertificateFile) : Promise.reject<Buffer>() |
| 92 | + ]); |
| 93 | + this.fileResultPromise.then(([certificateResult, privateKeyResult, caCertificateResult]) => { |
| 94 | + if (!this.refreshTimer) { |
| 95 | + return; |
| 96 | + } |
| 97 | + trace('File watcher read certificates certificate' + (certificateResult ? '!=' : '==') + 'null, privateKey' + (privateKeyResult ? '!=' : '==') + 'null, CA certificate' + (caCertificateResult ? '!=' : '==') + 'null'); |
| 98 | + this.lastUpdateTime = new Date(); |
| 99 | + this.fileResultPromise = null; |
| 100 | + if (certificateResult.status === 'fulfilled' && privateKeyResult.status === 'fulfilled') { |
| 101 | + this.latestIdentityUpdate = { |
| 102 | + certificate: certificateResult.value, |
| 103 | + privateKey: privateKeyResult.value |
| 104 | + }; |
| 105 | + } else { |
| 106 | + this.latestIdentityUpdate = null; |
| 107 | + } |
| 108 | + if (caCertificateResult.status === 'fulfilled') { |
| 109 | + this.latestCaUpdate = { |
| 110 | + caCertificate: caCertificateResult.value |
| 111 | + }; |
| 112 | + } |
| 113 | + for (const listener of this.identityListeners) { |
| 114 | + listener(this.latestIdentityUpdate); |
| 115 | + } |
| 116 | + for (const listener of this.caListeners) { |
| 117 | + listener(this.latestCaUpdate); |
| 118 | + } |
| 119 | + }); |
| 120 | + trace('File watcher initiated certificate update'); |
| 121 | + } |
| 122 | + |
| 123 | + private maybeStartWatchingFiles() { |
| 124 | + if (!this.refreshTimer) { |
| 125 | + /* Perform the first read immediately, but only if there was not already |
| 126 | + * a recent read, to avoid reading from the filesystem significantly more |
| 127 | + * frequently than configured if the provider quickly switches between |
| 128 | + * used and unused. */ |
| 129 | + const timeSinceLastUpdate = this.lastUpdateTime ? (new Date()).getTime() - this.lastUpdateTime.getTime() : Infinity; |
| 130 | + if (timeSinceLastUpdate > this.config.refreshIntervalMs) { |
| 131 | + this.updateCertificates(); |
| 132 | + } |
| 133 | + if (timeSinceLastUpdate > this.config.refreshIntervalMs * 2) { |
| 134 | + // Clear out old updates if they are definitely stale |
| 135 | + this.latestCaUpdate = null; |
| 136 | + this.latestIdentityUpdate = null; |
| 137 | + } |
| 138 | + this.refreshTimer = setInterval(() => this.updateCertificates(), this.config.refreshIntervalMs); |
| 139 | + trace('File watcher started watching'); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private maybeStopWatchingFiles() { |
| 144 | + if (this.caListeners.size === 0 && this.identityListeners.size === 0) { |
| 145 | + this.fileResultPromise = null; |
| 146 | + if (this.refreshTimer) { |
| 147 | + clearInterval(this.refreshTimer); |
| 148 | + this.refreshTimer = null; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + addCaCertificateListener(listener: CaCertificateUpdateListener): void { |
| 154 | + this.caListeners.add(listener); |
| 155 | + this.maybeStartWatchingFiles(); |
| 156 | + process.nextTick(listener, this.latestCaUpdate); |
| 157 | + } |
| 158 | + removeCaCertificateListener(listener: CaCertificateUpdateListener): void { |
| 159 | + this.caListeners.delete(listener); |
| 160 | + this.maybeStopWatchingFiles(); |
| 161 | + } |
| 162 | + addIdentityCertificateListener(listener: IdentityCertificateUpdateListener): void { |
| 163 | + this.identityListeners.add(listener); |
| 164 | + this.maybeStartWatchingFiles(); |
| 165 | + process.nextTick(listener, this.latestIdentityUpdate); |
| 166 | + } |
| 167 | + removeIdentityCertificateListener(listener: IdentityCertificateUpdateListener): void { |
| 168 | + this.identityListeners.delete(listener); |
| 169 | + this.maybeStopWatchingFiles(); |
| 170 | + } |
| 171 | +} |
0 commit comments