@nodeart/cloudflare-provisioning 1.0.5 → 1.0.6

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 +21 -11
  2. package/package.json +1 -1
package/cloudflare.js CHANGED
@@ -208,7 +208,7 @@ class CloudFlare {
208
208
  throw new Error(`Could not get firewall rules: ${statusCode}, error: ${JSON.stringify(response)}`)
209
209
  }
210
210
 
211
- const { id, rules } = response
211
+ const { id, rules } = response?.result ?? {}
212
212
  if (!id) {
213
213
  throw new Error(`Could not get firewall rules ruleset ID: got ${id}, received value: ${JSON.stringify(response)}`)
214
214
  }
@@ -325,7 +325,7 @@ class CloudFlare {
325
325
  throw new Error(`Could not create redirect ruleset: ${statusCode}, error: ${JSON.stringify(createResponse)}`)
326
326
  }
327
327
 
328
- const { id, rules } = createResponse
328
+ const { id, rules } = createResponse?.result ?? {}
329
329
  if (!id) {
330
330
  throw new Error(`Could not get redirect rules ruleset ID: got ${id}, received value: ${JSON.stringify(response)}`)
331
331
  }
@@ -336,7 +336,7 @@ class CloudFlare {
336
336
  throw new Error(`Could not get redirect rules: ${statusCode}, error: ${JSON.stringify(response)}`)
337
337
  }
338
338
 
339
- const { id, rules } = response
339
+ const { id, rules } = response?.result ?? {}
340
340
  if (!id) {
341
341
  throw new Error(`Could not get redirect rules ruleset ID: got ${id}, received value: ${JSON.stringify(response)}`)
342
342
  }
@@ -810,7 +810,7 @@ class CloudFlare {
810
810
  await fs.access(clientCert, fs.constants.R_OK)
811
811
  await fs.access(caCert, fs.constants.R_OK)
812
812
  } catch (e) {
813
- throw new Error(`Cannot access file: ${e?.message}`)
813
+ throw new Error(`Cancelling cert upload for domain ${this.domain}. Cannot access file: ${e?.message}`)
814
814
  }
815
815
 
816
816
  const clientKeyContents = await fs.readFile(clientKey, 'utf8')
@@ -825,8 +825,8 @@ class CloudFlare {
825
825
  async uploadCertAndKey (clientCert, clientKey) {
826
826
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/origin_tls_client_auth`
827
827
  const payload = {
828
- certificate: clientCert,
829
- private_key: clientKey
828
+ certificate: clientCert.replace(/\r?\n/g, '\n'),
829
+ private_key: clientKey.replace(/\r?\n/g, '\n')
830
830
  }
831
831
 
832
832
  const { statusCode, body } = await request(url, {
@@ -840,15 +840,20 @@ class CloudFlare {
840
840
 
841
841
  const response = await body.json()
842
842
 
843
- if (statusCode !== 200) {
844
- throw new Error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`)
843
+ if (statusCode !== 200 && statusCode !== 201) {
844
+ const errors = response?.errors ?? []
845
+ if (errors.find((error) => error.code === 1406 && error.message === 'This certificate already exists for this zone.')) {
846
+ console.log(`This certificate already exists for domain ${this.domain}. Continuing...`)
847
+ } else {
848
+ throw new Error(`Could not upload certificate and private key: ${statusCode}, error: ${JSON.stringify(response)}`)
849
+ }
845
850
  }
846
851
  }
847
852
 
848
853
  async uploadCaCert (caCert) {
849
854
  const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/acm/custom_trust_store`
850
855
  const payload = {
851
- certificate: caCert
856
+ certificate: caCert.replace(/\r?\n/g, '\n')
852
857
  }
853
858
 
854
859
  const { statusCode, body } = await request(url, {
@@ -862,8 +867,13 @@ class CloudFlare {
862
867
 
863
868
  const response = await body.json()
864
869
 
865
- if (statusCode !== 200) {
866
- throw new Error(`Could not upload CA certificate: ${statusCode}, error: ${JSON.stringify(response)}`)
870
+ if (statusCode !== 200 && statusCode !== 201) {
871
+ const errors = response?.errors ?? []
872
+ if (errors.find((error) => error.code === 1406 && error.message === 'This certificate already exists for this zone.')) {
873
+ console.log(`This CA certificate already exists for domain ${this.domain}. Continuing...`)
874
+ } else {
875
+ throw new Error(`Could not upload CA certificate: ${statusCode}, error: ${JSON.stringify(response)}`)
876
+ }
867
877
  }
868
878
  }
869
879
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodeart/cloudflare-provisioning",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {