feat: 支持中间证书

pull/189/head
xiaojunnuo 2024-09-22 23:19:10 +08:00
parent bdc0227c08
commit e86756e4c6
6 changed files with 102 additions and 20 deletions

View File

@ -21,17 +21,17 @@ gcloud beta publicca external-account-keys create
```shell ```shell
Created an external account key Created an external account key
[b64MacKey: xxxxxxxxxxxxx [b64MacKey: xxxxxxxxxxxxxxxx
keyId: xxxxxxxxx] keyId: xxxxxxxxxxxxx]
``` ```
记录以上信息备用 记录以上信息备用注意keyId是不带中括号的
## 3、 创建证书流水线 ## 3、 创建证书流水线
选择证书提供商为google 开启使用代理 选择证书提供商为google 开启使用代理
## 4、 将key信息作为EAB授权信息 ## 4、 将key信息作为EAB授权信息
google证书需要EAB授权 使用第二步中的 keyId 和 b64MacKey 信息创建一条EAB授权记录 google证书需要EAB授权 使用第二步中的 keyId 和 b64MacKey 信息创建一条EAB授权记录
注意keyId没有`]`结尾,不要把`]`也复制了
## 5、 其他就跟正常申请证书一样了 ## 5、 其他就跟正常申请证书一样了

View File

@ -12,6 +12,7 @@ export type CertInfo = {
crt: string; crt: string;
key: string; key: string;
csr: string; csr: string;
ic?: string;
pfx?: string; pfx?: string;
der?: string; der?: string;
}; };
@ -276,8 +277,9 @@ export class AcmeService {
signal: this.options.signal, signal: this.options.signal,
}); });
const crtString = crt.toString();
const cert: CertInfo = { const cert: CertInfo = {
crt: crt.toString(), crt: crtString,
key: key.toString(), key: key.toString(),
csr: csr.toString(), csr: csr.toString(),
}; };

View File

@ -175,6 +175,7 @@ export abstract class CertApplyBasePlugin extends AbstractTaskPlugin {
const zip = new JSZip(); const zip = new JSZip();
zip.file("cert.crt", cert.crt); zip.file("cert.crt", cert.crt);
zip.file("cert.key", cert.key); zip.file("cert.key", cert.key);
zip.file("intermediate.crt", cert.ic);
if (cert.pfx) { if (cert.pfx) {
zip.file("cert.pfx", Buffer.from(cert.pfx, "base64")); zip.file("cert.pfx", Buffer.from(cert.pfx, "base64"));
} }

View File

@ -6,7 +6,14 @@ import { crypto } from "@certd/acme-client";
import { ILogger } from "@certd/pipeline"; import { ILogger } from "@certd/pipeline";
import dayjs from "dayjs"; import dayjs from "dayjs";
export type CertReaderHandleContext = { reader: CertReader; tmpCrtPath: string; tmpKeyPath: string; tmpPfxPath?: string; tmpDerPath?: string }; export type CertReaderHandleContext = {
reader: CertReader;
tmpCrtPath: string;
tmpKeyPath: string;
tmpPfxPath?: string;
tmpDerPath?: string;
tmpIcPath?: string;
};
export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>; export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>;
export type HandleOpts = { logger: ILogger; handle: CertReaderHandle }; export type HandleOpts = { logger: ILogger; handle: CertReaderHandle };
export class CertReader { export class CertReader {
@ -14,6 +21,7 @@ export class CertReader {
crt: string; crt: string;
key: string; key: string;
csr: string; csr: string;
ic: string; //中间证书
detail: any; detail: any;
expires: number; expires: number;
@ -23,11 +31,30 @@ export class CertReader {
this.key = certInfo.key; this.key = certInfo.key;
this.csr = certInfo.csr; this.csr = certInfo.csr;
this.ic = certInfo.ic;
if (!this.ic) {
this.ic = this.getIc();
this.cert.ic = this.ic;
}
const { detail, expires } = this.getCrtDetail(this.cert.crt); const { detail, expires } = this.getCrtDetail(this.cert.crt);
this.detail = detail; this.detail = detail;
this.expires = expires.getTime(); this.expires = expires.getTime();
} }
getIc() {
//中间证书ic 就是crt的第一个 -----END CERTIFICATE----- 之后的内容
const endStr = "-----END CERTIFICATE-----";
const firstBlockEndIndex = this.crt.indexOf(endStr);
const start = firstBlockEndIndex + endStr.length + 1;
if (this.crt.length <= start) {
return "";
}
const ic = this.crt.substring(start);
return ic.trim();
}
toCertInfo(): CertInfo { toCertInfo(): CertInfo {
return this.cert; return this.cert;
} }
@ -38,7 +65,7 @@ export class CertReader {
return { detail, expires }; return { detail, expires };
} }
saveToFile(type: "crt" | "key" | "pfx" | "der", filepath?: string) { saveToFile(type: "crt" | "key" | "pfx" | "der" | "ic", filepath?: string) {
if (!this.cert[type]) { if (!this.cert[type]) {
return; return;
} }
@ -52,7 +79,7 @@ export class CertReader {
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true }); fs.mkdirSync(dir, { recursive: true });
} }
if (type === "crt" || type === "key") { if (type === "crt" || type === "key" || type === "ic") {
fs.writeFileSync(filepath, this.cert[type]); fs.writeFileSync(filepath, this.cert[type]);
} else { } else {
fs.writeFileSync(filepath, Buffer.from(this.cert[type], "base64")); fs.writeFileSync(filepath, Buffer.from(this.cert[type], "base64"));
@ -66,8 +93,9 @@ export class CertReader {
const tmpCrtPath = this.saveToFile("crt"); const tmpCrtPath = this.saveToFile("crt");
const tmpKeyPath = this.saveToFile("key"); const tmpKeyPath = this.saveToFile("key");
const tmpPfxPath = this.saveToFile("pfx"); const tmpPfxPath = this.saveToFile("pfx");
const tmpDerPath = this.saveToFile("der"); const tmpIcPath = this.saveToFile("ic");
logger.info("本地文件写入成功"); logger.info("本地文件写入成功");
const tmpDerPath = this.saveToFile("der");
try { try {
return await opts.handle({ return await opts.handle({
reader: this, reader: this,
@ -75,6 +103,7 @@ export class CertReader {
tmpKeyPath: tmpKeyPath, tmpKeyPath: tmpKeyPath,
tmpPfxPath: tmpPfxPath, tmpPfxPath: tmpPfxPath,
tmpDerPath: tmpDerPath, tmpDerPath: tmpDerPath,
tmpIcPath: tmpIcPath,
}); });
} catch (err) { } catch (err) {
throw err; throw err;
@ -90,6 +119,7 @@ export class CertReader {
removeFile(tmpKeyPath); removeFile(tmpKeyPath);
removeFile(tmpPfxPath); removeFile(tmpPfxPath);
removeFile(tmpDerPath); removeFile(tmpDerPath);
removeFile(tmpIcPath);
} }
} }

View File

@ -1,6 +1,6 @@
<template> <template>
<fs-page class="cd-page-account"> <fs-page class="cd-page-account">
<iframe ref="iframeRef" class="account-iframe" src="http://localhost:1017/#/home?appKey=z4nXOeTeSnnpUpnmsV"> </iframe> <iframe ref="iframeRef" class="account-iframe" src="http://localhost:1017/#/?appKey=z4nXOeTeSnnpUpnmsV"> </iframe>
</fs-page> </fs-page>
</template> </template>

View File

@ -9,7 +9,7 @@ import { SshAccess } from '../../access/index.js';
title: '上传证书到主机', title: '上传证书到主机',
icon: 'line-md:uploading-loop', icon: 'line-md:uploading-loop',
group: pluginGroups.host.key, group: pluginGroups.host.key,
desc: '也支持复制证书到本机', desc: '支持上传完成后执行脚本命令',
default: { default: {
strategy: { strategy: {
runStrategy: RunStrategy.SkipWhenSucceed, runStrategy: RunStrategy.SkipWhenSucceed,
@ -34,6 +34,15 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
}) })
keyPath!: string; keyPath!: string;
@TaskInput({
title: '中间证书保存路径',
helper: '需要有写入权限,路径要包含私钥文件名,文件名不能用*?!等特殊符号,例如:/tmp/intermediate.crt',
component: {
placeholder: '/root/deploy/nginx/intermediate.crt',
},
})
icPath!: string;
@TaskInput({ @TaskInput({
title: 'PFX证书保存路径', title: 'PFX证书保存路径',
helper: '用于IIS证书部署需要有写入权限路径要包含私钥文件名文件名不能用*?!等特殊符号,例如:/tmp/cert.pfx', helper: '用于IIS证书部署需要有写入权限路径要包含私钥文件名文件名不能用*?!等特殊符号,例如:/tmp/cert.pfx',
@ -85,10 +94,24 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
}) })
mkdirs = true; mkdirs = true;
@TaskInput({
title: 'shell脚本命令',
component: {
name: 'a-textarea',
vModel: 'value',
rows: 6,
},
helper: '上传后执行脚本命令,不填则不执行\n注意如果目标主机是windows且终端是cmd系统会自动将多行命令通过“&&”连接成一行',
required: false,
})
script!: string;
@TaskInput({ @TaskInput({
title: '仅复制到当前主机', title: '仅复制到当前主机',
helper: helper:
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path', '注意:本配置即将废弃\n' +
'开启后将直接复制到当前主机某个目录不上传到主机由于是docker启动实际上是复制到docker容器内的“证书保存路径”\n' +
'你需要事先在docker-compose.yaml中配置主机目录映射 volumes: /your_target_path:/your_target_path',
value: false, value: false,
component: { component: {
name: 'a-switch', name: 'a-switch',
@ -107,6 +130,10 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
}) })
hostKeyPath!: string; hostKeyPath!: string;
@TaskOutput({
title: '中间证书保存路径',
})
hostIcPath!: string;
@TaskOutput({ @TaskOutput({
title: 'PFX保存路径', title: 'PFX保存路径',
}) })
@ -129,63 +156,76 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
fs.mkdirSync(dir, { recursive: true }); fs.mkdirSync(dir, { recursive: true });
} }
fs.copyFileSync(srcFile, destFile); fs.copyFileSync(srcFile, destFile);
this.logger.info(`复制文件:${srcFile} => ${destFile}`);
} }
async execute(): Promise<void> { async execute(): Promise<void> {
const { crtPath, keyPath, cert, accessId } = this; const { crtPath, keyPath, cert, accessId } = this;
const certReader = new CertReader(cert); const certReader = new CertReader(cert);
const connectConf: SshAccess = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const handle = async (opts: CertReaderHandleContext) => { const handle = async (opts: CertReaderHandleContext) => {
const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath } = opts; const { tmpCrtPath, tmpKeyPath, tmpDerPath, tmpPfxPath, tmpIcPath } = opts;
if (this.copyToThisHost) { if (this.copyToThisHost) {
this.logger.info('复制到目标路径'); this.logger.info('复制到目标路径');
this.copyFile(tmpCrtPath, crtPath); this.copyFile(tmpCrtPath, crtPath);
this.copyFile(tmpKeyPath, keyPath); this.copyFile(tmpKeyPath, keyPath);
this.copyFile(tmpIcPath, this.icPath);
this.copyFile(tmpPfxPath, this.pfxPath); this.copyFile(tmpPfxPath, this.pfxPath);
this.copyFile(tmpDerPath, this.derPath); this.copyFile(tmpDerPath, this.derPath);
this.logger.info(`证书复制成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`);
} else { } else {
if (!accessId) { if (!accessId) {
throw new Error('主机登录授权配置不能为空'); throw new Error('主机登录授权配置不能为空');
} }
this.logger.info('准备上传文件到服务器'); this.logger.info('准备上传文件到服务器');
const connectConf: SshAccess = await this.accessService.getById(accessId);
const sshClient = new SshClient(this.logger);
const transports: any = []; const transports: any = [];
if (crtPath) { if (crtPath) {
transports.push({ transports.push({
localPath: tmpCrtPath, localPath: tmpCrtPath,
remotePath: crtPath, remotePath: crtPath,
}); });
this.logger.info(`上传证书到主机:${crtPath}`);
} }
if (keyPath) { if (keyPath) {
transports.push({ transports.push({
localPath: tmpKeyPath, localPath: tmpKeyPath,
remotePath: keyPath, remotePath: keyPath,
}); });
this.logger.info(`上传私钥到主机:${keyPath}`);
}
if (this.icPath) {
transports.push({
localPath: tmpIcPath,
remotePath: this.icPath,
});
this.logger.info(`上传中间证书到主机:${this.icPath}`);
} }
if (this.pfxPath) { if (this.pfxPath) {
transports.push({ transports.push({
localPath: tmpPfxPath, localPath: tmpPfxPath,
remotePath: this.pfxPath, remotePath: this.pfxPath,
}); });
this.logger.info(`上传PFX证书到主机${this.pfxPath}`);
} }
if (this.derPath) { if (this.derPath) {
transports.push({ transports.push({
localPath: tmpDerPath, localPath: tmpDerPath,
remotePath: this.derPath, remotePath: this.derPath,
}); });
this.logger.info(`上传DER证书到主机${this.derPath}`);
} }
this.logger.info('开始上传文件到服务器');
await sshClient.uploadFiles({ await sshClient.uploadFiles({
connectConf, connectConf,
transports, transports,
mkdirs: this.mkdirs, mkdirs: this.mkdirs,
}); });
this.logger.info(`证书上传成功crtPath=${crtPath},keyPath=${keyPath},pfxPath=${this.pfxPath},derPath=${this.derPath}`); this.logger.info('上传文件到服务器成功');
//输出 //输出
this.hostCrtPath = crtPath; this.hostCrtPath = crtPath;
this.hostKeyPath = keyPath; this.hostKeyPath = keyPath;
this.hostIcPath = this.icPath;
this.hostPfxPath = this.pfxPath; this.hostPfxPath = this.pfxPath;
this.hostDerPath = this.derPath; this.hostDerPath = this.derPath;
} }
@ -194,6 +234,15 @@ export class UploadCertToHostPlugin extends AbstractTaskPlugin {
logger: this.logger, logger: this.logger,
handle, handle,
}); });
if (this.script.trim()) {
this.logger.info('执行脚本命令');
const scripts = this.script.split('\n');
await sshClient.exec({
connectConf,
script: scripts,
});
}
} }
} }