forked from Menci/deploy-certificate-to-aliyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (123 loc) · 3.52 KB
/
index.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
const fs = require("fs");
const core = require("@actions/core");
const AliyunClient = require('@alicloud/pop-core');
const input = {
accessKeyId: core.getInput("access-key-id"),
accessKeySecret: core.getInput("access-key-secret"),
securityToken: core.getInput("security-token"),
fullchainFile: core.getInput("fullchain-file"),
keyFile: core.getInput("key-file"),
certificateName: core.getInput("certificate-name"),
cdnDomains: core.getInput("cdn-domains")
};
/**
* @param {string} endpoint
* @param {string} apiVersion
* @param {string} action
* @param {Record<string, unknown>} params
*/
function callAliyunApi(endpoint, apiVersion, action, params) {
return new AliyunClient({
...input.accessKeyId && input.accessKeySecret ? {
accessKeyId: input.accessKeyId,
accessKeySecret: input.accessKeySecret
} : {},
...input.securityToken ? {
securityToken: input.securityToken
} : {},
endpoint,
apiVersion
}).request(action, params, { method: "POST" });
}
async function deletePreviouslyDeployedCertificate() {
/**
* @typedef CertificateListItem
* @prop {number} id
* @prop {string} name
*
* @typedef DescribeUserCertificateListResponse
* @prop {number} TotalCount
* @prop {CertificateListItem[]} CertificateOrderList
*/
/**
* @param {(item: CertificateListItem) => Promise<void>} callback
*/
async function listCertificates(callback) {
let currentItems = 0;
for (let i = 1; ; i++) {
/**
* @type {DescribeUserCertificateListResponse}
*/
const response = await callAliyunApi(
"https://cas.aliyuncs.com", "2018-07-13",
"CertificateOrderList",
{
ShowSize: 50,
CurrentPage: i
}
);
for (const item of response.CertificateOrderList)
await callback(item);
currentItems += response.CertificateOrderList.length;
if (currentItems === response.TotalCount) break;
}
}
let foundId = 0;
await listCertificates(async item => {
if (item.name === input.certificateName) {
foundId = item.id;
}
});
if (foundId === 0) {
console.log("Previously deployed certificate not found. Skipping delete.");
return;
}
console.log(`Found previously deployed certificate ${foundId}. Deleting.`);
await callAliyunApi(
"https://cas.aliyuncs.com", "2018-07-13",
"DeleteUserCertificate",
{
CertId: foundId
}
);
}
async function deployCertificate() {
const fullchain = fs.readFileSync(input.fullchainFile, "utf-8");
const key = fs.readFileSync(input.keyFile, "utf-8");
await deletePreviouslyDeployedCertificate();
await callAliyunApi(
"https://cas.aliyuncs.com", "2018-07-13",
"CreateUserCertificate",
{
Cert: fullchain,
Key: key,
Name: input.certificateName
}
);
}
async function deployCertificateToCdn() {
const domains = Array.from(new Set(input.cdnDomains.split(/\s+/).filter(x => x)));
for (const domain of domains) {
console.log(`Deploying certificate to CDN domain ${domain}.`);
await callAliyunApi(
"https://cdn.aliyuncs.com", "2018-05-10",
"SetDomainServerCertificate",
{
DomainName: domain,
CertName: input.certificateName,
CertType: "cas",
ServerCertificateStatus: "on",
ForceSet: "1"
}
);
}
}
async function main() {
await deployCertificate();
if (input.cdnDomains) await deployCertificateToCdn();
}
main().catch(error => {
console.log(error.stack);
core.setFailed(error);
process.exit(1);
});