@nodeart/cloudflare-provisioning 1.0.7 → 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.
Files changed (2) hide show
  1. package/cloudflare.js +59 -11
  2. package/package.json +1 -1
package/cloudflare.js CHANGED
@@ -202,7 +202,12 @@ class CloudFlare {
202
202
  }
203
203
  })
204
204
 
205
- const response = await body.json()
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
- const response = await body.json()
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
- const response = await body.json()
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
- const response = await body.json()
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
- const response = await body.json()
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
- const response = await body.json()
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,14 +859,18 @@ class CloudFlare {
829
859
  }
830
860
 
831
861
  async clearCustomCerts () {
862
+ console.log(`Initiating certificate clear for domain ${this.domain}...`, new Date().toISOString())
832
863
  const clientCertIds = await this.getClientCerts()
833
864
  const caCertIds = await this.getCaCerts()
834
865
 
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())
868
+
835
869
  for (const cert of clientCertIds) {
836
870
  try {
837
871
  await this.deleteClientCert(cert)
838
872
  } catch (e) {
839
- 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())
840
874
  }
841
875
  }
842
876
 
@@ -844,7 +878,7 @@ class CloudFlare {
844
878
  try {
845
879
  await this.deleteCaCert(cert)
846
880
  } catch (e) {
847
- console.error(`Failed to delete Client cert: ${e?.message}`)
881
+ console.error(`Failed to delete CA cert for domain ${this.domain}: ${e?.message}`, new Date().toISOString())
848
882
  }
849
883
  }
850
884
  }
@@ -861,7 +895,8 @@ class CloudFlare {
861
895
  const response = await body.json()
862
896
 
863
897
  if (statusCode !== 200) {
864
- throw new Error(`Could not get client certificate IDs: ${statusCode}, error: ${JSON.stringify(response)}`)
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)}`)
865
900
  }
866
901
 
867
902
  return response?.result?.map((cert) => cert?.id) ?? []
@@ -879,7 +914,8 @@ class CloudFlare {
879
914
  const response = await body.json()
880
915
 
881
916
  if (statusCode !== 200) {
882
- throw new Error(`Could not get CA certificate IDs: ${statusCode}, error: ${JSON.stringify(response)}`)
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)}`)
883
919
  }
884
920
 
885
921
  return response?.result?.map((cert) => cert?.id) ?? []
@@ -899,6 +935,8 @@ class CloudFlare {
899
935
  if (statusCode !== 200) {
900
936
  throw new Error(`Could not delete client certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
901
937
  }
938
+
939
+ console.log(`Deleted client certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
902
940
  }
903
941
 
904
942
  async deleteCaCert (certId) {
@@ -913,8 +951,11 @@ class CloudFlare {
913
951
  const response = await body.json()
914
952
 
915
953
  if (statusCode !== 200) {
954
+ console.error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
916
955
  throw new Error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
917
956
  }
957
+
958
+ console.log(`Deleted CA certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
918
959
  }
919
960
 
920
961
  async uploadCertAndKey (clientCert, clientKey) {
@@ -938,11 +979,14 @@ class CloudFlare {
938
979
  if (statusCode !== 200 && statusCode !== 201) {
939
980
  const errors = response?.errors ?? []
940
981
  if (errors.find((error) => error.code === 1406 && error.message === 'This certificate already exists for this zone.')) {
941
- 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())
942
983
  } else {
984
+ console.error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
943
985
  throw new Error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`)
944
986
  }
945
987
  }
988
+
989
+ console.log(`Client certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
946
990
  }
947
991
 
948
992
  async uploadCaCert (caCert) {
@@ -970,6 +1014,8 @@ class CloudFlare {
970
1014
  throw new Error(`Could not upload CA certificate: ${statusCode}, error: ${JSON.stringify(response)}`)
971
1015
  }
972
1016
  }
1017
+
1018
+ console.log(`CA certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
973
1019
  }
974
1020
 
975
1021
  async enableTLSClientAuth () {
@@ -988,6 +1034,8 @@ class CloudFlare {
988
1034
  if (statusCode !== 200) {
989
1035
  throw new Error(`Could not enable TSL Client Auth setting: ${statusCode}, error: ${JSON.stringify(response)}`)
990
1036
  }
1037
+
1038
+ console.log(`Enabled TSL Client Auth setting for domain ${this.domain}`, new Date().toISOString())
991
1039
  }
992
1040
  }
993
1041
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {