@nodeart/cloudflare-provisioning 1.0.8 → 1.0.10

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 +68 -18
  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)}`)
@@ -269,8 +284,13 @@ class CloudFlare {
269
284
  async rewriteFirewallRules (firewallRules) {
270
285
  const { id: rulesetId, rules: currentFirewallRules } = await this.getFirewallRules()
271
286
 
287
+ if (!rulesetId) {
288
+ console.error(`Could not update firewall rules for domain ${this.domain}: custom firewall ruleset id is not found`)
289
+ throw new Error('Custom firewall ruleset id is not found')
290
+ }
291
+
272
292
  for (const firewallRule of firewallRules) {
273
- const currentFirewallRule = currentFirewallRules.find(
293
+ const currentFirewallRule = currentFirewallRules?.find(
274
294
  rule => rule.description === firewallRule.description
275
295
  )
276
296
 
@@ -297,7 +317,12 @@ class CloudFlare {
297
317
  }
298
318
  })
299
319
 
300
- const response = await body.json()
320
+ let response
321
+ try {
322
+ response = await body.json()
323
+ } catch (e) {
324
+ response = await body.text()
325
+ }
301
326
 
302
327
  if (statusCode === 404) {
303
328
  // Create http_request_dynamic_redirect ruleset if one doesn't exist
@@ -357,7 +382,12 @@ class CloudFlare {
357
382
  body: JSON.stringify(redirectRule)
358
383
  })
359
384
 
360
- const response = await body.json()
385
+ let response
386
+ try {
387
+ response = await body.json()
388
+ } catch (e) {
389
+ response = await body.text()
390
+ }
361
391
 
362
392
  if (statusCode !== 200) {
363
393
  throw new Error(`Could not create a redirect rule: ${statusCode}, error: ${JSON.stringify(response)}`)
@@ -378,7 +408,12 @@ class CloudFlare {
378
408
  body: JSON.stringify(redirectRule)
379
409
  })
380
410
 
381
- const response = await body.json()
411
+ let response
412
+ try {
413
+ response = await body.json()
414
+ } catch (e) {
415
+ response = await body.text()
416
+ }
382
417
 
383
418
  if (statusCode !== 200) {
384
419
  throw new Error(`Could not update a redirect rule: ${statusCode}, error: ${JSON.stringify(response)}`)
@@ -390,8 +425,13 @@ class CloudFlare {
390
425
  async rewriteRedirectRules (redirectRules) {
391
426
  const { id: rulesetId, rules: currentRedirectRules } = await this.getRedirectRules()
392
427
 
428
+ if (!rulesetId) {
429
+ console.error(`Could not update redirect rules for domain ${this.domain}: custom firewall ruleset id is not found`)
430
+ throw new Error('Custom redirect ruleset id is not found')
431
+ }
432
+
393
433
  for (const redirectRule of redirectRules) {
394
- const currentRedirectRule = currentRedirectRules.find(
434
+ const currentRedirectRule = currentRedirectRules?.find(
395
435
  rule => rule.description === redirectRule.description
396
436
  )
397
437
 
@@ -829,18 +869,18 @@ class CloudFlare {
829
869
  }
830
870
 
831
871
  async clearCustomCerts () {
832
- console.log('Initiating certificate clear...')
872
+ console.log(`Initiating certificate clear for domain ${this.domain}...`, new Date().toISOString())
833
873
  const clientCertIds = await this.getClientCerts()
834
874
  const caCertIds = await this.getCaCerts()
835
875
 
836
- console.log(`Client certificates found: ${clientCertIds?.join(', ')}`)
837
- console.log(`CA certificates found: ${caCertIds?.join(', ')}`)
876
+ console.log(`Client certificates found for domain ${this.domain}: ${clientCertIds?.join(', ')}`, new Date().toISOString())
877
+ console.log(`CA certificates found for domain ${this.domain}: ${caCertIds?.join(', ')}`, new Date().toISOString())
838
878
 
839
879
  for (const cert of clientCertIds) {
840
880
  try {
841
881
  await this.deleteClientCert(cert)
842
882
  } catch (e) {
843
- console.error(`Failed to delete Client cert: ${e?.message}`)
883
+ console.error(`Failed to delete Client cert for domain ${this.domain}: ${e?.message}`, new Date().toISOString())
844
884
  }
845
885
  }
846
886
 
@@ -848,7 +888,7 @@ class CloudFlare {
848
888
  try {
849
889
  await this.deleteCaCert(cert)
850
890
  } catch (e) {
851
- console.error(`Failed to delete Client cert: ${e?.message}`)
891
+ console.error(`Failed to delete CA cert for domain ${this.domain}: ${e?.message}`, new Date().toISOString())
852
892
  }
853
893
  }
854
894
  }
@@ -865,7 +905,8 @@ class CloudFlare {
865
905
  const response = await body.json()
866
906
 
867
907
  if (statusCode !== 200) {
868
- throw new Error(`Could not get client certificate IDs: ${statusCode}, error: ${JSON.stringify(response)}`)
908
+ console.error(`Could not get client certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
909
+ throw new Error(`Could not get client certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`)
869
910
  }
870
911
 
871
912
  return response?.result?.map((cert) => cert?.id) ?? []
@@ -883,7 +924,8 @@ class CloudFlare {
883
924
  const response = await body.json()
884
925
 
885
926
  if (statusCode !== 200) {
886
- throw new Error(`Could not get CA certificate IDs: ${statusCode}, error: ${JSON.stringify(response)}`)
927
+ console.error(`Could not get CA certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
928
+ throw new Error(`Could not get CA certificate IDs for domain ${this.domain}: ${statusCode}, error: ${JSON.stringify(response)}`)
887
929
  }
888
930
 
889
931
  return response?.result?.map((cert) => cert?.id) ?? []
@@ -904,7 +946,7 @@ class CloudFlare {
904
946
  throw new Error(`Could not delete client certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
905
947
  }
906
948
 
907
- console.log(`Deleted client certificate ID ${certId}`)
949
+ console.log(`Deleted client certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
908
950
  }
909
951
 
910
952
  async deleteCaCert (certId) {
@@ -919,10 +961,11 @@ class CloudFlare {
919
961
  const response = await body.json()
920
962
 
921
963
  if (statusCode !== 200) {
964
+ console.error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
922
965
  throw new Error(`Could not delete CA certificate ID ${certId}: ${statusCode}, error: ${JSON.stringify(response)}`)
923
966
  }
924
967
 
925
- console.log(`Deleted CA certificate ID ${certId}`)
968
+ console.log(`Deleted CA certificate ID ${certId} for ${this.domain}`, new Date().toISOString())
926
969
  }
927
970
 
928
971
  async uploadCertAndKey (clientCert, clientKey) {
@@ -946,11 +989,14 @@ class CloudFlare {
946
989
  if (statusCode !== 200 && statusCode !== 201) {
947
990
  const errors = response?.errors ?? []
948
991
  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...`)
992
+ console.log(`This certificate already exists for domain ${this.domain}. Continuing...`, new Date().toISOString())
950
993
  } else {
994
+ console.error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`, new Date().toISOString())
951
995
  throw new Error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`)
952
996
  }
953
997
  }
998
+
999
+ console.log(`Client certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
954
1000
  }
955
1001
 
956
1002
  async uploadCaCert (caCert) {
@@ -978,6 +1024,8 @@ class CloudFlare {
978
1024
  throw new Error(`Could not upload CA certificate: ${statusCode}, error: ${JSON.stringify(response)}`)
979
1025
  }
980
1026
  }
1027
+
1028
+ console.log(`CA certificate uploaded for domain ${this.domain}, cert id: ${response?.result?.id}`, new Date().toISOString())
981
1029
  }
982
1030
 
983
1031
  async enableTLSClientAuth () {
@@ -996,6 +1044,8 @@ class CloudFlare {
996
1044
  if (statusCode !== 200) {
997
1045
  throw new Error(`Could not enable TSL Client Auth setting: ${statusCode}, error: ${JSON.stringify(response)}`)
998
1046
  }
1047
+
1048
+ console.log(`Enabled TSL Client Auth setting for domain ${this.domain}`, new Date().toISOString())
999
1049
  }
1000
1050
  }
1001
1051
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {