@nodeart/cloudflare-provisioning 1.0.8 → 1.0.9
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 +56 -16
- package/package.json +1 -1
package/cloudflare.js
CHANGED
|
@@ -202,7 +202,12 @@ class CloudFlare {
|
|
|
202
202
|
}
|
|
203
203
|
})
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
let response
|
|
206
|
+
try {
|
|
207
|
+
response = await body.json()
|
|
208
|
+
} catch (e) {
|
|
209
|
+
response = await body.text()
|
|
210
|
+
}
|
|
206
211
|
|
|
207
212
|
if (statusCode !== 200) {
|
|
208
213
|
throw new Error(`Could not get firewall rules: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
@@ -232,7 +237,12 @@ class CloudFlare {
|
|
|
232
237
|
body: JSON.stringify(rule)
|
|
233
238
|
})
|
|
234
239
|
|
|
235
|
-
|
|
240
|
+
let response
|
|
241
|
+
try {
|
|
242
|
+
response = await body.json()
|
|
243
|
+
} catch (e) {
|
|
244
|
+
response = await body.text()
|
|
245
|
+
}
|
|
236
246
|
|
|
237
247
|
if (statusCode !== 200) {
|
|
238
248
|
throw new Error(`Could not create a firewall rule: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
@@ -257,7 +267,12 @@ class CloudFlare {
|
|
|
257
267
|
body: JSON.stringify(rule)
|
|
258
268
|
})
|
|
259
269
|
|
|
260
|
-
|
|
270
|
+
let response
|
|
271
|
+
try {
|
|
272
|
+
response = await body.json()
|
|
273
|
+
} catch (e) {
|
|
274
|
+
response = await body.text()
|
|
275
|
+
}
|
|
261
276
|
|
|
262
277
|
if (statusCode !== 200) {
|
|
263
278
|
throw new Error(`Could not update a firewall rule: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
@@ -297,7 +312,12 @@ class CloudFlare {
|
|
|
297
312
|
}
|
|
298
313
|
})
|
|
299
314
|
|
|
300
|
-
|
|
315
|
+
let response
|
|
316
|
+
try {
|
|
317
|
+
response = await body.json()
|
|
318
|
+
} catch (e) {
|
|
319
|
+
response = await body.text()
|
|
320
|
+
}
|
|
301
321
|
|
|
302
322
|
if (statusCode === 404) {
|
|
303
323
|
// Create http_request_dynamic_redirect ruleset if one doesn't exist
|
|
@@ -357,7 +377,12 @@ class CloudFlare {
|
|
|
357
377
|
body: JSON.stringify(redirectRule)
|
|
358
378
|
})
|
|
359
379
|
|
|
360
|
-
|
|
380
|
+
let response
|
|
381
|
+
try {
|
|
382
|
+
response = await body.json()
|
|
383
|
+
} catch (e) {
|
|
384
|
+
response = await body.text()
|
|
385
|
+
}
|
|
361
386
|
|
|
362
387
|
if (statusCode !== 200) {
|
|
363
388
|
throw new Error(`Could not create a redirect rule: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
@@ -378,7 +403,12 @@ class CloudFlare {
|
|
|
378
403
|
body: JSON.stringify(redirectRule)
|
|
379
404
|
})
|
|
380
405
|
|
|
381
|
-
|
|
406
|
+
let response
|
|
407
|
+
try {
|
|
408
|
+
response = await body.json()
|
|
409
|
+
} catch (e) {
|
|
410
|
+
response = await body.text()
|
|
411
|
+
}
|
|
382
412
|
|
|
383
413
|
if (statusCode !== 200) {
|
|
384
414
|
throw new Error(`Could not update a redirect rule: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
@@ -829,18 +859,18 @@ class CloudFlare {
|
|
|
829
859
|
}
|
|
830
860
|
|
|
831
861
|
async clearCustomCerts () {
|
|
832
|
-
console.log(
|
|
862
|
+
console.log(`Initiating certificate clear for domain ${this.domain}...`, new Date().toISOString())
|
|
833
863
|
const clientCertIds = await this.getClientCerts()
|
|
834
864
|
const caCertIds = await this.getCaCerts()
|
|
835
865
|
|
|
836
|
-
console.log(`Client certificates found: ${clientCertIds?.join(', ')}
|
|
837
|
-
console.log(`CA certificates found: ${caCertIds?.join(', ')}
|
|
866
|
+
console.log(`Client certificates found for domain ${this.domain}: ${clientCertIds?.join(', ')}`, new Date().toISOString())
|
|
867
|
+
console.log(`CA certificates found for domain ${this.domain}: ${caCertIds?.join(', ')}`, new Date().toISOString())
|
|
838
868
|
|
|
839
869
|
for (const cert of clientCertIds) {
|
|
840
870
|
try {
|
|
841
871
|
await this.deleteClientCert(cert)
|
|
842
872
|
} catch (e) {
|
|
843
|
-
console.error(`Failed to delete Client cert: ${e?.message}
|
|
873
|
+
console.error(`Failed to delete Client cert for domain ${this.domain}: ${e?.message}`, new Date().toISOString())
|
|
844
874
|
}
|
|
845
875
|
}
|
|
846
876
|
|
|
@@ -848,7 +878,7 @@ class CloudFlare {
|
|
|
848
878
|
try {
|
|
849
879
|
await this.deleteCaCert(cert)
|
|
850
880
|
} catch (e) {
|
|
851
|
-
console.error(`Failed to delete
|
|
881
|
+
console.error(`Failed to delete CA cert for domain ${this.domain}: ${e?.message}`, new Date().toISOString())
|
|
852
882
|
}
|
|
853
883
|
}
|
|
854
884
|
}
|
|
@@ -865,7 +895,8 @@ class CloudFlare {
|
|
|
865
895
|
const response = await body.json()
|
|
866
896
|
|
|
867
897
|
if (statusCode !== 200) {
|
|
868
|
-
|
|
898
|
+
console.error(`Could not get client certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
|
|
899
|
+
throw new Error(`Could not get client certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
869
900
|
}
|
|
870
901
|
|
|
871
902
|
return response?.result?.map((cert) => cert?.id) ?? []
|
|
@@ -883,7 +914,8 @@ class CloudFlare {
|
|
|
883
914
|
const response = await body.json()
|
|
884
915
|
|
|
885
916
|
if (statusCode !== 200) {
|
|
886
|
-
|
|
917
|
+
console.error(`Could not get CA certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
|
|
918
|
+
throw new Error(`Could not get CA certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
887
919
|
}
|
|
888
920
|
|
|
889
921
|
return response?.result?.map((cert) => cert?.id) ?? []
|
|
@@ -904,7 +936,7 @@ class CloudFlare {
|
|
|
904
936
|
throw new Error(`Could not delete client certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
905
937
|
}
|
|
906
938
|
|
|
907
|
-
console.log(`Deleted client certificate ID ${certId}
|
|
939
|
+
console.log(`Deleted client certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
|
|
908
940
|
}
|
|
909
941
|
|
|
910
942
|
async deleteCaCert (certId) {
|
|
@@ -919,10 +951,11 @@ class CloudFlare {
|
|
|
919
951
|
const response = await body.json()
|
|
920
952
|
|
|
921
953
|
if (statusCode !== 200) {
|
|
954
|
+
console.error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
|
|
922
955
|
throw new Error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
923
956
|
}
|
|
924
957
|
|
|
925
|
-
console.log(`Deleted CA certificate ID ${certId}
|
|
958
|
+
console.log(`Deleted CA certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
|
|
926
959
|
}
|
|
927
960
|
|
|
928
961
|
async uploadCertAndKey (clientCert, clientKey) {
|
|
@@ -946,11 +979,14 @@ class CloudFlare {
|
|
|
946
979
|
if (statusCode !== 200 && statusCode !== 201) {
|
|
947
980
|
const errors = response?.errors ?? []
|
|
948
981
|
if (errors.find((error) => error.code === 1406 && error.message === 'This certificate already exists for this zone.')) {
|
|
949
|
-
console.log(`This certificate already exists for domain ${this.domain}. Continuing
|
|
982
|
+
console.log(`This certificate already exists for domain ${this.domain}. Continuing...`, new Date().toISOString())
|
|
950
983
|
} else {
|
|
984
|
+
console.error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
|
|
951
985
|
throw new Error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
952
986
|
}
|
|
953
987
|
}
|
|
988
|
+
|
|
989
|
+
console.log(`Client certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
|
|
954
990
|
}
|
|
955
991
|
|
|
956
992
|
async uploadCaCert (caCert) {
|
|
@@ -978,6 +1014,8 @@ class CloudFlare {
|
|
|
978
1014
|
throw new Error(`Could not upload CA certificate: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
979
1015
|
}
|
|
980
1016
|
}
|
|
1017
|
+
|
|
1018
|
+
console.log(`CA certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
|
|
981
1019
|
}
|
|
982
1020
|
|
|
983
1021
|
async enableTLSClientAuth () {
|
|
@@ -996,6 +1034,8 @@ class CloudFlare {
|
|
|
996
1034
|
if (statusCode !== 200) {
|
|
997
1035
|
throw new Error(`Could not enable TSL Client Auth setting: ${statusCode}, error: ${JSON.stringify(response)}`)
|
|
998
1036
|
}
|
|
1037
|
+
|
|
1038
|
+
console.log(`Enabled TSL Client Auth setting for domain ${this.domain}`, new Date().toISOString())
|
|
999
1039
|
}
|
|
1000
1040
|
}
|
|
1001
1041
|
|