Skip to content

Commit

Permalink
feat: use crud.getHost and crud.getorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Jan 6, 2024
1 parent 49e8e83 commit 574718b
Showing 1 changed file with 29 additions and 49 deletions.
78 changes: 29 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,61 +199,41 @@ class CoCreateAcme {
const hostKeyPath = keyPath + host + '/';

if (!organization_id) {
let org = await this.crud.send({
method: 'object.read',
array: 'organizations',
$filter: {
query: [
{ key: "host", value: [host], operator: "$in" }
]
},
organization_id: process.env.organization_id
})

if (!org || !org.object || !org.object[0]) {
console.log('Organization could not be found');
let org = await this.crud.getHost(host)
if (org.error)
// console.log('Organization could not be found');
return false
} else {
organization_id = org.object[0]._id
}

else
organization_id = org._id
}

let organization = await this.crud.send({
method: 'object.read',
array: 'organizations',
object: {
_id: organization_id
},
organization_id,
});

if (organization && organization.object && organization.object[0]) {
let organization = await this.crud.getOrganization(organization_id, false);
if (organization.error)
return false

if (!organization.object[0].host || !organization.object[0].host.includes(host))
return false
if (!organization.host || !organization.host.includes(host))
return false

let safeKey = host.replace(/\./g, '_');
if (organization.object[0].ssl && organization.object[0].ssl[safeKey]) {
let cert = organization.object[0].ssl[safeKey].cert
let key = organization.object[0].ssl[safeKey].key
if (cert && key) {
let expires = await forge.readCertificateInfo(cert);
expires = expires.notAfter;
if (this.isValid(expires)) {
this.setCertificate(host, expires, organization_id)
if (!fs.existsSync(hostKeyPath)) {
fs.mkdirSync(hostKeyPath, { recursive: true });
}

fs.writeFileSync(hostKeyPath + 'fullchain.pem', cert);
// fs.chmodSync(keyPath + 'fullchain.pem', '444')
fs.writeFileSync(hostKeyPath + 'private-key.pem', key);
// fs.chmodSync(keyPath + 'private-key.pem', '400')

this.proxy.createServer(host)
return true
let safeKey = host.replace(/\./g, '_');
if (organization.ssl && organization.ssl[safeKey]) {
let cert = organization.ssl[safeKey].cert
let key = organization.ssl[safeKey].key
if (cert && key) {
let expires = await forge.readCertificateInfo(cert);
expires = expires.notAfter;
if (this.isValid(expires)) {
this.setCertificate(host, expires, organization_id)
if (!fs.existsSync(hostKeyPath)) {
fs.mkdirSync(hostKeyPath, { recursive: true });
}

fs.writeFileSync(hostKeyPath + 'fullchain.pem', cert);
// fs.chmodSync(keyPath + 'fullchain.pem', '444')
fs.writeFileSync(hostKeyPath + 'private-key.pem', key);
// fs.chmodSync(keyPath + 'private-key.pem', '400')

this.proxy.createServer(host)
return true
}
}
}
Expand Down

0 comments on commit 574718b

Please sign in to comment.