-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.js
executable file
·154 lines (135 loc) · 5.24 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const util = require('util')
const { Component } = require('@serverless/core')
const tencentAuth = require('serverless-tencent-auth-tool')
const tencentcloud = require('tencentcloud-sdk-nodejs')
const ClientProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/client_profile.js')
const HttpProfile = require('tencentcloud-sdk-nodejs/tencentcloud/common/profile/http_profile.js')
const sslClient = tencentcloud.ssl.v20191205.Client
const sslModels = tencentcloud.ssl.v20191205.Models
class TencentDomain extends Component {
getSSLClient(credentials) {
// create cam client
const secret_id = credentials.SecretId
const secret_key = credentials.SecretKey
const token = credentials.token
const cred = new tencentcloud.common.Credential(secret_id, secret_key, token)
const httpProfile = new HttpProfile()
httpProfile.reqTimeout = 30
const clientProfile = new ClientProfile('HmacSHA256', httpProfile)
return new sslClient(cred, 'ap-guangzhou', clientProfile)
}
async compareJson(source, target) {
for (const key in source) {
if (typeof source[key] == 'object') {
if (await this.compareJson(source[key], target[key])) {
delete target[key]
}
} else {
if (String(source[key]) == String(target[key])) {
delete target[key]
}
}
}
if (JSON.stringify(target) == '{}') {
return true
}
return false
}
async default(inputs = {}) {
// login
const auth = new tencentAuth()
this.context.credentials.tencent = await auth.doAuth(this.context.credentials.tencent, {
client: 'tencent-ssl',
remark: inputs.fromClientRemark,
project: this.context.instance ? this.context.instance.id : undefined,
action: 'default'
})
const ssl = this.getSSLClient(this.context.credentials.tencent)
ssl.sdkVersion = 'ServerlessComponent'
this.context.debug(`Applying Certificate ...`)
let sslOutput = {}
const CertificateId = this.state.CertificateId
delete this.state.CertificateId
const compareResult = await this.compareJson(
JSON.parse(JSON.stringify(this.state)),
JSON.parse(JSON.stringify(inputs))
)
if (!CertificateId || !compareResult) {
const applyCertificateData = {
DvAuthMethod: inputs.dvAuthMethod ? inputs.dvAuthMethod : 'DNS_AUTO',
DomainName: inputs.domain,
PackageType: '2',
ContactEmail: inputs.email,
ContactPhone: String(inputs.phone),
ValidityPeriod: String(inputs.validityPeriod ? inputs.validityPeriod : '12')
}
if (inputs.csr && inputs.csr.encryptAlgo) {
applyCertificateData.CsrEncryptAlgo = inputs.csr.encryptAlgo
}
if (inputs.csr && inputs.csr.encryptAlgo) {
applyCertificateData.CsrKeyParameter = inputs.csr.keyParameter
}
if (inputs.csr && inputs.csr.encryptAlgo) {
applyCertificateData.CsrKeyPassword = inputs.csr.keyPassword
}
const applyCertificateReq = new sslModels.ApplyCertificateRequest()
applyCertificateReq.from_json_string(JSON.stringify(applyCertificateData))
const handler = util.promisify(ssl.ApplyCertificate.bind(ssl))
sslOutput = await handler(applyCertificateReq)
} else {
sslOutput = this.state
sslOutput.CertificateId = CertificateId
}
this.context.debug(`Applyed Certificate ...`)
const output = {
CertificateId: sslOutput.CertificateId
}
if (inputs.dvAuthMethod == 'DNS') {
const descCertificateData = {
CertificateId: CertificateId
}
const descCertificateReq = new sslModels.DescribeCertificateRequest()
descCertificateReq.from_json_string(JSON.stringify(descCertificateData))
const handler = util.promisify(ssl.DescribeCertificate.bind(ssl))
const descOutput = await handler(descCertificateReq)
if (descOutput.StatusName == '审核中') {
output['Please add the resolution record'] = {
Domain: inputs.domain,
'Host record': '_dnsauth',
'Record type': 'TXT',
Value: descOutput.DvAuthDetail.DvAuthValue
}
}
}
this.state = inputs
this.state.CertificateId = sslOutput.CertificateId
await this.save()
return output
}
async remove(inputs = {}) {
this.context.debug(`Removing ...`)
// login
const auth = new tencentAuth()
this.context.credentials.tencent = await auth.doAuth(this.context.credentials.tencent, {
client: 'tencent-ssl',
remark: inputs.fromClientRemark,
project: this.context.instance ? this.context.instance.id : undefined,
action: 'default'
})
const ssl = this.getSSLClient(this.context.credentials.tencent)
ssl.sdkVersion = 'ServerlessComponent'
this.context.debug(`Removing CertificateId ${this.state.CertificateId}`)
const deleteCertificateData = {
CertificateId: this.state.CertificateId
}
const deleteCertificateReq = new sslModels.DeleteCertificateRequest()
deleteCertificateReq.from_json_string(JSON.stringify(deleteCertificateData))
const handler = util.promisify(ssl.DeleteCertificate.bind(ssl))
await handler(deleteCertificateReq)
this.state = {}
await this.save()
return {}
this.context.debug(`Removed ...`)
}
}
module.exports = TencentDomain