@cocreate/acme 1.1.3 → 1.2.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-acme/compare/v1.1.3...v1.2.0) (2024-01-08)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * applied host to define environment/branch ([6931a9b](https://github.com/CoCreate-app/CoCreate-acme/commit/6931a9b81bfdf5b095534f7f42c6b9df1ea99141))
7
+ * handle ssl from host array ([fd45091](https://github.com/CoCreate-app/CoCreate-acme/commit/fd450914804b389647af52a1c40f6569e44066cf))
8
+ * setCertificate handle writeFileSync and hostPosition defined to save to host object ([29e794d](https://github.com/CoCreate-app/CoCreate-acme/commit/29e794de6d38584462ea926f3ef957d42b97c0bf))
9
+
10
+
11
+ ### Features
12
+
13
+ * bumped CoCreate dependencies to their latest versions ([c25e34d](https://github.com/CoCreate-app/CoCreate-acme/commit/c25e34de1e47f660c773c7ea242e4ff2b060c936))
14
+ * host contains sert and key ([11380ac](https://github.com/CoCreate-app/CoCreate-acme/commit/11380acb7463e13d14a3e6ea856c288cef1d26c4))
15
+ * use crud.getHost and crud.getorganization ([574718b](https://github.com/CoCreate-app/CoCreate-acme/commit/574718b0ae035b63f6e70f4423ea1fd484bc80cc))
16
+
1
17
  ## [1.1.3](https://github.com/CoCreate-app/CoCreate-acme/compare/v1.1.2...v1.1.3) (2024-01-03)
2
18
 
3
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/acme",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "An intergration with ACME and CoCreateJS.",
5
5
  "keywords": [
6
6
  "acme",
package/src/index.js CHANGED
@@ -76,7 +76,7 @@ class CoCreateAcme {
76
76
  }
77
77
  }
78
78
 
79
- async requestCertificate(host, organization_id, wildcard = false) {
79
+ async requestCertificate(host, hostPosition, organization_id, wildcard = false) {
80
80
  try {
81
81
 
82
82
  const self = this
@@ -102,9 +102,12 @@ class CoCreateAcme {
102
102
  email: [email], // Replace with your email
103
103
  termsOfServiceAgreed: true,
104
104
  challengeCreateFn: async (authz, challenge, keyAuthorization) => {
105
+ console.log(`Challenge added for host: ${host} token: ${challenge.token}`);
106
+
105
107
  if (challenge.type === 'http-01') {
106
108
  const httpChallenge = await self.crud.send({
107
109
  method: 'object.create',
110
+ host,
108
111
  array: 'files',
109
112
  object: {
110
113
  "content-type": "text/plain",
@@ -148,6 +151,7 @@ class CoCreateAcme {
148
151
  if (challenge.type === 'http-01') {
149
152
  self.crud.send({
150
153
  method: 'object.delete',
154
+ host,
151
155
  array: 'files',
152
156
  object: {
153
157
  _id: challenge_id
@@ -163,32 +167,23 @@ class CoCreateAcme {
163
167
 
164
168
  let expires = await forge.readCertificateInfo(cert);
165
169
  expires = expires.notAfter;
166
- this.setCertificate(host, expires, organization_id)
170
+ this.setCertificate(host, expires, organization_id, hostKeyPath, cert, key)
167
171
 
168
- /* Save the certificate and key */
169
- fs.writeFileSync(hostKeyPath + 'fullchain.pem', cert);
170
- // fs.chmodSync(keyPath + 'fullchain.pem', '444')
171
-
172
- fs.writeFileSync(hostKeyPath + 'private-key.pem', key);
173
- // fs.chmodSync(keyPath + 'private-key.pem', '400')
174
-
175
- // process.emit('certificateCreated', host)
176
- this.proxy.createServer(host)
177
-
178
- let safeKey = host.replace(/\./g, '_');
179
- let organization = await this.crud.send({
172
+ this.crud.send({
180
173
  method: 'object.update',
174
+ host,
181
175
  array: 'organizations',
182
176
  object: {
183
177
  _id: organization_id,
184
- ['ssl.' + safeKey]: { cert, key: key.toString('utf-8') }
178
+ [`host[${hostPosition}]`]: { name: host, cert, key, expires },
185
179
  },
186
- organization_id,
180
+ organization_id
187
181
  });
188
182
 
189
- console.log('Successfully created certificate!');
183
+ console.log(`Certificate successfully created for ${host}!'`);
190
184
  return true
191
185
  } catch (error) {
186
+ console.log(`Certificate failed to create for ${host}!`);
192
187
  delete hosts[host]
193
188
  return false
194
189
  }
@@ -199,66 +194,40 @@ class CoCreateAcme {
199
194
  const hostKeyPath = keyPath + host + '/';
200
195
 
201
196
  if (!organization_id) {
202
- let org = await this.crud.send({
203
- method: 'object.read',
204
- array: 'organizations',
205
- $filter: {
206
- query: [
207
- { key: "host", value: [host], operator: "$in" }
208
- ]
209
- },
210
- organization_id: process.env.organization_id
211
- })
212
-
213
- if (!org || !org.object || !org.object[0]) {
214
- console.log('Organization could not be found');
197
+ let org = await this.crud.getHost(host)
198
+ if (org.error)
199
+ // console.log('Organization could not be found');
215
200
  return false
216
- } else {
217
- organization_id = org.object[0]._id
218
- }
219
-
201
+ else
202
+ organization_id = org._id
220
203
  }
221
204
 
222
- let organization = await this.crud.send({
223
- method: 'object.read',
224
- array: 'organizations',
225
- object: {
226
- _id: organization_id
227
- },
228
- organization_id,
229
- });
230
-
231
- if (organization && organization.object && organization.object[0]) {
232
-
233
- if (!organization.object[0].host || !organization.object[0].host.includes(host))
234
- return false
205
+ let organization = await this.crud.getOrganization(organization_id, false);
206
+ if (organization.error)
207
+ return false
235
208
 
236
- let safeKey = host.replace(/\./g, '_');
237
- if (organization.object[0].ssl && organization.object[0].ssl[safeKey]) {
238
- let cert = organization.object[0].ssl[safeKey].cert
239
- let key = organization.object[0].ssl[safeKey].key
240
- if (cert && key) {
241
- let expires = await forge.readCertificateInfo(cert);
242
- expires = expires.notAfter;
243
- if (this.isValid(expires)) {
244
- this.setCertificate(host, expires, organization_id)
245
- if (!fs.existsSync(hostKeyPath)) {
246
- fs.mkdirSync(hostKeyPath, { recursive: true });
209
+ if (organization.host) {
210
+ let hostPosition
211
+ for (let i = 0; i < organization.host.length; i++) {
212
+ if (organization.host[i].name === host) {
213
+ hostPosition = i
214
+ if (organization.host[i].cert && organization.host[i].key) {
215
+ let expires = await forge.readCertificateInfo(organization.host[i].cert);
216
+ expires = expires.notAfter;
217
+ if (this.isValid(expires)) {
218
+ this.setCertificate(host, expires, organization_id, hostKeyPath, organization.host[i].cert, organization.host[i].key)
219
+ return true
247
220
  }
248
-
249
- fs.writeFileSync(hostKeyPath + 'fullchain.pem', cert);
250
- // fs.chmodSync(keyPath + 'fullchain.pem', '444')
251
- fs.writeFileSync(hostKeyPath + 'private-key.pem', key);
252
- // fs.chmodSync(keyPath + 'private-key.pem', '400')
253
-
254
- this.proxy.createServer(host)
255
- return true
256
221
  }
222
+ break
257
223
  }
258
224
  }
225
+
226
+ if (hostPosition >= 0)
227
+ return false
259
228
  }
260
229
 
261
- return await this.requestCertificate(host, organization_id, false)
230
+ return await this.requestCertificate(host, hostPosition, organization_id, false)
262
231
  }
263
232
 
264
233
  async checkCertificate(host, organization_id, pathname = '') {
@@ -297,7 +266,7 @@ class CoCreateAcme {
297
266
  }
298
267
  }
299
268
 
300
- setCertificate(host, expires, organization_id) {
269
+ setCertificate(host, expires, organization_id, hostKeyPath, cert, key) {
301
270
  // let expireDate = new Date(expires);
302
271
  // let currentDate = new Date();
303
272
 
@@ -323,6 +292,19 @@ class CoCreateAcme {
323
292
  // }, timeoutDuration);
324
293
 
325
294
  // Store the timeout and organization_id for later reference or cancellation
295
+
296
+ if (hostKeyPath) {
297
+ if (!fs.existsSync(hostKeyPath)) {
298
+ fs.mkdirSync(hostKeyPath, { recursive: true });
299
+ }
300
+ fs.writeFileSync(hostKeyPath + 'fullchain.pem', cert);
301
+ // fs.chmodSync(keyPath + 'fullchain.pem', '444')
302
+ fs.writeFileSync(hostKeyPath + 'private-key.pem', key);
303
+ // fs.chmodSync(keyPath + 'private-key.pem', '400')
304
+ }
305
+
306
+ this.proxy.createServer(host)
307
+
326
308
  certificates[host] = { expires, organization_id }
327
309
 
328
310
  }