Skip to content

Commit

Permalink
fix: ssl syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Jan 8, 2024
1 parent def1ca5 commit 65b226a
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,62 @@ class CoCreateAcme {

const hostKeyPath = keyPath + host + '/';
if (fs.existsSync(hostKeyPath + 'fullchain.pem')) {
let expires = fs.readFileSync(hostKeyPath + 'fullchain.pem', 'utf8');
expires = await forge.readCertificateInfo(expires);
let cert = fs.readFileSync(hostKeyPath + 'fullchain.pem', 'utf8');
let expires = await forge.readCertificateInfo(cert);
expires = expires.notAfter;
if (this.isValid(expires)) {
this.setCertificate(host, expires, organization_id)

// TODO: remove after certs are synced
let key = fs.readFileSync(hostKeyPath + 'private-key.pem', 'utf8');
let hostPosition, org

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

let organization = await this.crud.getOrganization(organization_id, false);
if (organization.error)
return false

if (organization.host) {
for (let i = 0; i < organization.host.length; i++) {
if (organization.host[i].name === host) {
hostPosition = i
console.log(`Found host: ${host} at postion: ${hostPosition}`)

if (organization.host[i].cert && organization.host[i].key) {
let expires = await forge.readCertificateInfo(organization.host[i].cert);
expires = expires.notAfter;
if (this.isValid(expires)) {
this.setCertificate(host, expires, organization_id, hostKeyPath, organization.host[i].cert, organization.host[i].key)
return true
}
}
break
}
}

if (!hostPosition && hostPosition !== 0)
return false
}

this.crud.send({
method: 'object.update',
host,
array: 'organizations',
object: {
_id: organization_id,
[`host[${hostPosition}]`]: { name: host, cert, key, expires },
},
organization_id
});

return true
}
}
Expand Down

0 comments on commit 65b226a

Please sign in to comment.