|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { CaCertificate } from 'src/modules/certificate/models/ca-certificate'; |
| 3 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 4 | +import { CaCertificateEntity } from 'src/modules/certificate/entities/ca-certificate.entity'; |
| 5 | +import { Repository } from 'typeorm'; |
| 6 | +import { EncryptionService } from 'src/modules/encryption/encryption.service'; |
| 7 | +import { ModelEncryptor } from 'src/modules/encryption/model.encryptor'; |
| 8 | +import { ClientCertificate } from 'src/modules/certificate/models/client-certificate'; |
| 9 | +import { ClientCertificateEntity } from 'src/modules/certificate/entities/client-certificate.entity'; |
| 10 | +import { classToClass } from 'src/utils'; |
| 11 | +import { |
| 12 | + getCertNameFromFilename, |
| 13 | + getPemBodyFromFileSync, |
| 14 | + isValidPemCertificate, |
| 15 | + isValidPemPrivateKey, |
| 16 | +} from 'src/common/utils'; |
| 17 | +import { |
| 18 | + InvalidCaCertificateBodyException, InvalidCertificateNameException, |
| 19 | + InvalidClientCertificateBodyException, InvalidClientPrivateKeyException, |
| 20 | +} from 'src/modules/database-import/exceptions'; |
| 21 | + |
| 22 | +@Injectable() |
| 23 | +export class CertificateImportService { |
| 24 | + private caCertEncryptor: ModelEncryptor; |
| 25 | + |
| 26 | + private clientCertEncryptor: ModelEncryptor; |
| 27 | + |
| 28 | + constructor( |
| 29 | + @InjectRepository(CaCertificateEntity) |
| 30 | + private readonly caCertRepository: Repository<CaCertificateEntity>, |
| 31 | + @InjectRepository(ClientCertificateEntity) |
| 32 | + private readonly clientCertRepository: Repository<ClientCertificateEntity>, |
| 33 | + private readonly encryptionService: EncryptionService, |
| 34 | + ) { |
| 35 | + this.caCertEncryptor = new ModelEncryptor(encryptionService, ['certificate']); |
| 36 | + this.clientCertEncryptor = new ModelEncryptor(encryptionService, ['certificate', 'key']); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Validate data + prepare CA certificate to be imported along with new database |
| 41 | + * @param cert |
| 42 | + */ |
| 43 | + async processCaCertificate(cert: Partial<CaCertificate>): Promise<CaCertificate> { |
| 44 | + let toImport: Partial<CaCertificate> = { |
| 45 | + certificate: null, |
| 46 | + name: cert.name, |
| 47 | + }; |
| 48 | + |
| 49 | + if (isValidPemCertificate(cert.certificate)) { |
| 50 | + toImport.certificate = cert.certificate; |
| 51 | + } else { |
| 52 | + try { |
| 53 | + toImport.certificate = getPemBodyFromFileSync(cert.certificate); |
| 54 | + toImport.name = getCertNameFromFilename(cert.certificate); |
| 55 | + } catch (e) { |
| 56 | + // ignore error |
| 57 | + toImport = null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (!toImport?.certificate || !isValidPemCertificate(toImport.certificate)) { |
| 62 | + throw new InvalidCaCertificateBodyException(); |
| 63 | + } |
| 64 | + |
| 65 | + if (!toImport?.name) { |
| 66 | + throw new InvalidCertificateNameException(); |
| 67 | + } |
| 68 | + |
| 69 | + return this.prepareCaCertificateForImport(toImport); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Use existing certificate if found |
| 74 | + * Generate unique name for new certificate |
| 75 | + * @param cert |
| 76 | + * @private |
| 77 | + */ |
| 78 | + private async prepareCaCertificateForImport(cert: Partial<CaCertificate>): Promise<CaCertificate> { |
| 79 | + const encryptedModel = await this.caCertEncryptor.encryptEntity(cert as CaCertificate); |
| 80 | + const existing = await this.caCertRepository.createQueryBuilder('c') |
| 81 | + .select('c.id') |
| 82 | + .where({ certificate: cert.certificate }) |
| 83 | + .orWhere({ certificate: encryptedModel.certificate }) |
| 84 | + .getOne(); |
| 85 | + |
| 86 | + if (existing) { |
| 87 | + return existing; |
| 88 | + } |
| 89 | + |
| 90 | + const name = await CertificateImportService.determineAvailableName( |
| 91 | + cert.name, |
| 92 | + this.caCertRepository, |
| 93 | + ); |
| 94 | + |
| 95 | + return classToClass(CaCertificate, { |
| 96 | + ...cert, |
| 97 | + name, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Validate data + prepare CA certificate to be imported along with new database |
| 103 | + * @param cert |
| 104 | + */ |
| 105 | + async processClientCertificate(cert: Partial<ClientCertificateEntity>): Promise<ClientCertificate> { |
| 106 | + const toImport: Partial<ClientCertificate> = { |
| 107 | + certificate: null, |
| 108 | + key: null, |
| 109 | + name: cert.name, |
| 110 | + }; |
| 111 | + |
| 112 | + if (isValidPemCertificate(cert.certificate)) { |
| 113 | + toImport.certificate = cert.certificate; |
| 114 | + } else { |
| 115 | + try { |
| 116 | + toImport.certificate = getPemBodyFromFileSync(cert.certificate); |
| 117 | + toImport.name = getCertNameFromFilename(cert.certificate); |
| 118 | + } catch (e) { |
| 119 | + // ignore error |
| 120 | + toImport.certificate = null; |
| 121 | + toImport.name = null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (isValidPemPrivateKey(cert.key)) { |
| 126 | + toImport.key = cert.key; |
| 127 | + } else { |
| 128 | + try { |
| 129 | + toImport.key = getPemBodyFromFileSync(cert.key); |
| 130 | + } catch (e) { |
| 131 | + // ignore error |
| 132 | + toImport.key = null; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (!toImport?.certificate || !isValidPemCertificate(toImport.certificate)) { |
| 137 | + throw new InvalidClientCertificateBodyException(); |
| 138 | + } |
| 139 | + |
| 140 | + if (!toImport?.key || !isValidPemPrivateKey(toImport.key)) { |
| 141 | + throw new InvalidClientPrivateKeyException(); |
| 142 | + } |
| 143 | + |
| 144 | + if (!toImport?.name) { |
| 145 | + throw new InvalidCertificateNameException(); |
| 146 | + } |
| 147 | + |
| 148 | + return this.prepareClientCertificateForImport(toImport); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Use existing certificate if found |
| 153 | + * Generate unique name for new certificate |
| 154 | + * @param cert |
| 155 | + * @private |
| 156 | + */ |
| 157 | + private async prepareClientCertificateForImport(cert: Partial<ClientCertificate>): Promise<ClientCertificate> { |
| 158 | + const encryptedModel = await this.clientCertEncryptor.encryptEntity(cert as ClientCertificate); |
| 159 | + const existing = await this.clientCertRepository.createQueryBuilder('c') |
| 160 | + .select('c.id') |
| 161 | + .where({ |
| 162 | + certificate: cert.certificate, |
| 163 | + key: cert.key, |
| 164 | + }) |
| 165 | + .orWhere({ |
| 166 | + certificate: encryptedModel.certificate, |
| 167 | + key: encryptedModel.key, |
| 168 | + }) |
| 169 | + .getOne(); |
| 170 | + |
| 171 | + if (existing) { |
| 172 | + return existing; |
| 173 | + } |
| 174 | + |
| 175 | + const name = await CertificateImportService.determineAvailableName( |
| 176 | + cert.name, |
| 177 | + this.clientCertRepository, |
| 178 | + ); |
| 179 | + |
| 180 | + return classToClass(ClientCertificate, { |
| 181 | + ...cert, |
| 182 | + name, |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Find available name for certificate using such pattern "{N}_{name}" |
| 188 | + * @param originalName |
| 189 | + * @param repository |
| 190 | + */ |
| 191 | + static async determineAvailableName(originalName: string, repository: Repository<any>): Promise<string> { |
| 192 | + let index = 0; |
| 193 | + |
| 194 | + // temporary solution |
| 195 | + // investigate how to make working "regexp" for sqlite |
| 196 | + // https://github.com/kriasoft/node-sqlite/issues/55 |
| 197 | + // https://www.sqlite.org/c3ref/create_function.html |
| 198 | + while (true) { |
| 199 | + let name = originalName; |
| 200 | + |
| 201 | + if (index) { |
| 202 | + name = `${index}_${name}`; |
| 203 | + } |
| 204 | + |
| 205 | + if (!await repository |
| 206 | + .createQueryBuilder('c') |
| 207 | + .where({ name }) |
| 208 | + .select(['c.id']) |
| 209 | + .getOne()) { |
| 210 | + return name; |
| 211 | + } |
| 212 | + |
| 213 | + index += 1; |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments