mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
refactor: ```
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { AbstractPlugin } from '../abstract-plugin.js'
|
||||
|
||||
export class AbstractAliyunPlugin extends AbstractPlugin {
|
||||
format (pem) {
|
||||
pem = pem.replace(/\r/g, '')
|
||||
pem = pem.replace(/\n\n/g, '')
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import { AbstractPlugin } from '../../abstract-plugin/index.js'
|
||||
import Core from '@alicloud/pop-core'
|
||||
import dayjs from 'dayjs'
|
||||
export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
/**
|
||||
* 插件定义
|
||||
* 名称
|
||||
* 入参
|
||||
* 出参
|
||||
*/
|
||||
static define () {
|
||||
return {
|
||||
name: 'deployToCdn',
|
||||
label: '部署到阿里云CDN',
|
||||
input: {
|
||||
domainName: {
|
||||
label: 'cdn加速域名',
|
||||
required: true
|
||||
},
|
||||
certName: {
|
||||
label: '证书名称'
|
||||
},
|
||||
certType: {
|
||||
label: '证书来源',
|
||||
options: [
|
||||
{ value: 'upload', label: '直接上传' },
|
||||
{ value: 'cas', label: '从证书库(需要uploadCertToAliyun插件作为前置任务)' }
|
||||
],
|
||||
required: true
|
||||
},
|
||||
// serverCertificateStatus: {
|
||||
// label: '启用https',
|
||||
// options: [
|
||||
// { value: 'on', label: '开启HTTPS,并更新证书' },
|
||||
// { value: 'auto', label: '若HTTPS开启则更新,未开启不更新' }
|
||||
// ],
|
||||
// required:true
|
||||
// },
|
||||
accessProvider: {
|
||||
label: 'Access提供者',
|
||||
type: [String, Object],
|
||||
desc: 'AccessProviders的key 或 一个包含accessKeyId与accessKeySecret的对象',
|
||||
options: 'accessProviders[type=aliyun]',
|
||||
required: true
|
||||
}
|
||||
},
|
||||
output: {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getClient (aliyunProvider) {
|
||||
return new Core({
|
||||
accessKeyId: aliyunProvider.accessKeyId,
|
||||
accessKeySecret: aliyunProvider.accessKeySecret,
|
||||
endpoint: 'https://cdn.aliyuncs.com',
|
||||
apiVersion: '2018-05-10'
|
||||
})
|
||||
}
|
||||
|
||||
async execute ({ accessProviders, cert, args, context }) {
|
||||
let { accessProvider } = args
|
||||
if (typeof accessProvider === 'string' && accessProviders) {
|
||||
accessProvider = accessProviders[accessProvider]
|
||||
}
|
||||
const client = this.getClient(accessProvider)
|
||||
|
||||
const { certName, certType, domainName } = args
|
||||
const CertName = certName + '-' + dayjs().format('YYYYMMDDHHmmss')
|
||||
|
||||
const params = {
|
||||
RegionId: 'cn-hangzhou',
|
||||
DomainName: domainName,
|
||||
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())
|
||||
}
|
||||
|
||||
const requestOption = {
|
||||
method: 'POST'
|
||||
}
|
||||
const ret = await client.request('SetDomainServerCertificate', params, requestOption)
|
||||
checkRet(ret)
|
||||
console.log('设置cdn证书成功', ret)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
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 {
|
||||
/**
|
||||
* 插件定义
|
||||
* 名称
|
||||
* 入参
|
||||
* 出参
|
||||
*/
|
||||
static define () {
|
||||
return {
|
||||
name: 'updateToAliyun',
|
||||
label: '上传证书到阿里云',
|
||||
input: {
|
||||
name: {
|
||||
label: '证书名称'
|
||||
},
|
||||
accessProvider: {
|
||||
label: 'Access提供者',
|
||||
type: [String, Object],
|
||||
desc: 'AccessProviders的key 或 一个包含accessKeyId与accessKeySecret的对象',
|
||||
options: 'accessProviders[type=aliyun]'
|
||||
}
|
||||
},
|
||||
output: {
|
||||
aliyunCertId: {
|
||||
type: String,
|
||||
desc: '上传成功后的阿里云CertId'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getClient (aliyunProvider) {
|
||||
return new Core({
|
||||
accessKeyId: aliyunProvider.accessKeyId,
|
||||
accessKeySecret: aliyunProvider.accessKeySecret,
|
||||
endpoint: 'https://cas.aliyuncs.com',
|
||||
apiVersion: '2018-07-13'
|
||||
})
|
||||
}
|
||||
|
||||
async execute ({ accessProviders, cert, args, context }) {
|
||||
const { name, provider } = args
|
||||
const certName = name + '-' + dayjs().format('YYYYMMDDHHmmss')
|
||||
const params = {
|
||||
RegionId: 'cn-hangzhou',
|
||||
Name: certName,
|
||||
Cert: this.format(cert.crt.toString()),
|
||||
Key: this.format(cert.key.toString())
|
||||
}
|
||||
|
||||
const requestOption = {
|
||||
method: 'POST'
|
||||
}
|
||||
|
||||
const accesseProvider = this.getAccessProvider(provider, accessProviders)
|
||||
const client = this.getClient(accesseProvider)
|
||||
const ret = await client.request('CreateUserCertificate', params, requestOption)
|
||||
|
||||
context.aliyunCertId = ret.CertId
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { AbstractPlugin } from '../../abstract-plugin/index.js'
|
||||
import Core from '@alicloud/pop-core'
|
||||
import dayjs from 'dayjs'
|
||||
export class UploadCertToAliyunPlugin extends AbstractPlugin {
|
||||
getClient (aliyunProvider) {
|
||||
this.client = new Core({
|
||||
accessKeyId: aliyunProvider.accessKeyId,
|
||||
accessKeySecret: aliyunProvider.accessKeySecret,
|
||||
endpoint: 'https://alidns.aliyuncs.com',
|
||||
apiVersion: '2015-01-09'
|
||||
})
|
||||
}
|
||||
|
||||
async execute ({ providers, cert, args, context }) {
|
||||
const { name, provider } = args
|
||||
const certName = name + '-' + dayjs().format('YYYYMMDDHHmmss')
|
||||
const params = {
|
||||
RegionId: 'cn-hangzhou',
|
||||
Name: certName,
|
||||
Cert: cert.crt.toString(),
|
||||
Key: cert.key.toString()
|
||||
}
|
||||
|
||||
const requestOption = {
|
||||
method: 'POST'
|
||||
}
|
||||
|
||||
const client = this.getClient(providers[provider])
|
||||
const ret = await client.request('CreateUserCertificate', params, requestOption)
|
||||
|
||||
context.AliyunCertId = ret.CertId
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,7 @@
|
||||
import util from './util.js'
|
||||
import log4js from 'log4js'
|
||||
import path from 'path'
|
||||
const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
|
||||
const filename = path.join(util.getUserBasePath(), '/logs/certd.log')
|
||||
log4js.configure({
|
||||
appenders: { std: { type: 'stdout' }, file: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename } },
|
||||
categories: { default: { appenders: ['file', 'std'], level: level } }
|
||||
appenders: { std: { type: 'stdout' } },
|
||||
categories: { default: { appenders: ['std'], level: 'info' } }
|
||||
})
|
||||
const logger = log4js.getLogger('certd')
|
||||
export default logger
|
||||
|
||||
Reference in New Issue
Block a user