mirror of
https://github.com/certd/certd.git
synced 2026-04-23 03:27:25 +08:00
feat: 自动化流程
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import fs from 'fs'
|
||||
import logger from '../utils/util.log.js'
|
||||
export class AbstractPlugin {
|
||||
constructor () {
|
||||
this.logger = logger
|
||||
}
|
||||
|
||||
async executeFromContextFile (options = {}) {
|
||||
const { contextPath } = options
|
||||
const contextJson = fs.readFileSync(contextPath)
|
||||
@@ -13,5 +18,10 @@ export class AbstractPlugin {
|
||||
return context
|
||||
}
|
||||
|
||||
|
||||
getAccessProvider (accessProvider, accessProviders) {
|
||||
if (typeof accessProvider === 'string' && accessProviders) {
|
||||
accessProvider = accessProviders[accessProvider]
|
||||
}
|
||||
return accessProvider
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
import { AbstractPlugin } from '../abstract-plugin.js'
|
||||
import { AbstractPlugin } from '../abstract-plugin/index.js'
|
||||
|
||||
export class AbstractAliyunPlugin extends AbstractPlugin {
|
||||
format (pem) {
|
||||
pem = pem.replace(/\r/g, '')
|
||||
pem = pem.replace(/\n\n/g, '')
|
||||
pem = pem.replace(/\n\n/g, '\n')
|
||||
pem = pem.replace(/\n$/g, '')
|
||||
return pem
|
||||
}
|
||||
|
||||
getAccessProvider (accessProvider, accessProviders) {
|
||||
if (typeof accessProvider === 'string' && accessProviders) {
|
||||
accessProvider = accessProviders[accessProvider]
|
||||
}
|
||||
return accessProvider
|
||||
}
|
||||
|
||||
checkRet (ret) {
|
||||
if (ret.code != null) {
|
||||
throw new Error('执行失败:', ret.Message)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AbstractPlugin } from '../../abstract-plugin/index.js'
|
||||
import { AbstractAliyunPlugin } from '../../aliyun/abstract-aliyun.js'
|
||||
import Core from '@alicloud/pop-core'
|
||||
import dayjs from 'dayjs'
|
||||
export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
export class DeployCertToAliyunCDN extends AbstractAliyunPlugin {
|
||||
/**
|
||||
* 插件定义
|
||||
* 名称
|
||||
@@ -10,7 +10,7 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
*/
|
||||
static define () {
|
||||
return {
|
||||
name: 'deployToCdn',
|
||||
name: 'deployCertToAliyunCDN',
|
||||
label: '部署到阿里云CDN',
|
||||
input: {
|
||||
domainName: {
|
||||
@@ -21,10 +21,11 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
label: '证书名称'
|
||||
},
|
||||
certType: {
|
||||
value: 'upload',
|
||||
label: '证书来源',
|
||||
options: [
|
||||
{ value: 'upload', label: '直接上传' },
|
||||
{ value: 'cas', label: '从证书库(需要uploadCertToAliyun插件作为前置任务)' }
|
||||
{ value: 'cas', label: '从证书库', desc: '需要uploadCertToAliyun作为前置任务' }
|
||||
],
|
||||
required: true
|
||||
},
|
||||
@@ -76,12 +77,8 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
ServerCertificateStatus: 'on',
|
||||
CertName: CertName,
|
||||
CertType: certType,
|
||||
ServerCertificate: context.aliyunCertId
|
||||
}
|
||||
if (certType === 'upload') {
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
params.ServerCertificate = this.format(cert.crt.toString()),
|
||||
params.PrivateKey = this.format(cert.key.toString())
|
||||
ServerCertificate: super.format(cert.crt.toString()),
|
||||
PrivateKey: super.format(cert.key.toString())
|
||||
}
|
||||
return params
|
||||
}
|
||||
@@ -92,6 +89,6 @@ export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
}
|
||||
const ret = await client.request('SetDomainServerCertificate', params, requestOption)
|
||||
this.checkRet(ret)
|
||||
console.log('设置cdn证书成功', ret)
|
||||
this.logger.info('设置cdn证书成功:', ret.RequestId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { AbstractPlugin } from '../../abstract-plugin/index.js'
|
||||
import Core from '@alicloud/pop-core'
|
||||
import dayjs from 'dayjs'
|
||||
import { AbstractAliyunPlugin } from '../abstract-aliyun.js'
|
||||
export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
|
||||
export class UploadCertToAliyun extends AbstractAliyunPlugin {
|
||||
/**
|
||||
* 插件定义
|
||||
* 名称
|
||||
@@ -11,7 +10,7 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
|
||||
*/
|
||||
static define () {
|
||||
return {
|
||||
name: 'updateToAliyun',
|
||||
name: 'uploadCertToAliyun',
|
||||
label: '上传证书到阿里云',
|
||||
input: {
|
||||
name: {
|
||||
@@ -42,8 +41,8 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
|
||||
})
|
||||
}
|
||||
|
||||
async execute ({ accessProviders, cert, args, context }) {
|
||||
const { name, provider } = args
|
||||
async execute ({ accessProviders, cert, args, context, logger }) {
|
||||
const { name, accessProvider } = args
|
||||
const certName = name + '-' + dayjs().format('YYYYMMDDHHmmss')
|
||||
const params = {
|
||||
RegionId: 'cn-hangzhou',
|
||||
@@ -56,10 +55,11 @@ export class UploadToAliyunPlugin extends AbstractAliyunPlugin {
|
||||
method: 'POST'
|
||||
}
|
||||
|
||||
const accesseProvider = this.getAccessProvider(provider, accessProviders)
|
||||
const client = this.getClient(accesseProvider)
|
||||
const provider = super.getAccessProvider(accessProvider, accessProviders)
|
||||
const client = this.getClient(provider)
|
||||
const ret = await client.request('CreateUserCertificate', params, requestOption)
|
||||
this.checkRet(ret)
|
||||
this.logger.info('证书上传成功:certId=', ret.CertId)
|
||||
context.aliyunCertId = ret.CertId
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { UploadCertToAliyun } from './upload/upload-cert-to-aliyun/index.js'
|
||||
import { UploadCertToAliyun } from './aliyun/upload-to-aliyun/index.js'
|
||||
import { DeployCertToAliyunCDN } from './aliyun/deploy-to-cdn/index.js'
|
||||
export default {
|
||||
UploadCertToAliyun
|
||||
UploadCertToAliyun, DeployCertToAliyunCDN
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user