@nodeart/cloudflare-provisioning 1.0.13 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cloudflare.js +37 -1
- package/package.json +1 -1
package/cloudflare.js
CHANGED
|
@@ -210,7 +210,43 @@ class CloudFlare {
|
|
|
210
210
|
response = await body.text()
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
if (statusCode
|
|
213
|
+
if (statusCode === 404) {
|
|
214
|
+
console.log('Firewall ruleset was not found. Initializing firewall ruleset creation...')
|
|
215
|
+
const createRulesetUrl = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets`
|
|
216
|
+
const payload = {
|
|
217
|
+
name: 'Custom firewall ruleset',
|
|
218
|
+
kind: 'zone',
|
|
219
|
+
phase: 'http_request_firewall_custom',
|
|
220
|
+
rules: []
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const { statusCode: createStatusCode, body: createBody } = await this.requestWithDelay(createRulesetUrl, {
|
|
224
|
+
method: 'POST',
|
|
225
|
+
headers: {
|
|
226
|
+
...this.authorizationHeaders,
|
|
227
|
+
'Content-Type': 'application/json'
|
|
228
|
+
},
|
|
229
|
+
body: JSON.stringify(payload)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
let createResponse
|
|
233
|
+
try {
|
|
234
|
+
createResponse = await createBody.json()
|
|
235
|
+
} catch (e) {
|
|
236
|
+
createResponse = await createBody.text()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (createStatusCode !== 200) {
|
|
240
|
+
throw new Error(`Could not create firewall ruleset: ${createStatusCode}, error: ${JSON.stringify(createResponse)}`)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const { id, rules } = createResponse?.result ?? {}
|
|
244
|
+
if (!id) {
|
|
245
|
+
throw new Error(`Could not get firewall rules ruleset ID: got ${id}, received value: ${JSON.stringify(createResponse)}`)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { id, rules: rules ?? [] }
|
|
249
|
+
} else if (statusCode !== 200) {
|
|
214
250
|
throw new Error(`Could not get firewall rules: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
215
251
|
}
|
|
216
252
|
|