-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
160 lines (137 loc) · 4 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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"),
timeout: parseInt(core.getInput("timeout")) || 10000,
retry: parseInt(core.getInput("retry")) || 3,
};
/**
* @param {string} endpoint
* @param {string} apiVersion
* @param {string} action
* @param {Record<string, unknown>} params
*/
function callAliyunApi(endpoint, apiVersion, action, params) {
return new Promise((resolve, reject) => {
let retryTimes = 0;
const client = new AliyunClient({
...input.accessKeyId && input.accessKeySecret ? {
accessKeyId: input.accessKeyId,
accessKeySecret: input.accessKeySecret
} : {},
...input.securityToken ? {
securityToken: input.securityToken
} : {},
endpoint,
apiVersion
});
const request = () => client
.request(action, params, { method: "POST", timeout: input.timeout })
.then(resolve)
.catch(error => {
console.log(`Aliyun Client Error ${++retryTimes}/${input.retry}`, error)
if (retryTimes >= input.retry) reject(error);
request();
});
request();
});
}
async function deletePreviouslyDeployedCertificate() {
/**
* @typedef CertificateListItem
* @prop {number} id
* @prop {string} name
*
* @typedef DescribeUserCertificateListResponse
* @prop {number} TotalCount
* @prop {CertificateListItem[]} CertificateList
*/
/**
* @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",
"DescribeUserCertificateList",
{
ShowSize: 50,
CurrentPage: i
}
);
for (const item of response.CertificateList)
await callback(item);
currentItems += response.CertificateList.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);
});