From 585aa14c0b85ad4c2c4158c86817867b413aa107 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Wed, 25 Feb 2026 19:17:25 -0500 Subject: [PATCH] services --- nodejs/conf/base.js | 11 ++++++- nodejs/conf/development.js | 23 ++++++++++++++ nodejs/models/host.js | 20 ++++++++++-- nodejs/services/host_scheduler.js | 53 +++++++++++++++++-------------- nodejs/utils/model_pubsub.js | 8 ++--- 5 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 nodejs/conf/development.js diff --git a/nodejs/conf/base.js b/nodejs/conf/base.js index 2fb7363..a04d514 100644 --- a/nodejs/conf/base.js +++ b/nodejs/conf/base.js @@ -14,5 +14,14 @@ module.exports = { socketFile: '/var/run/proxy_lookup.socket', redis: { prefix: 'proxy_' - } + }, + + + service:{ + hostScheduler:{ + enabled: true, + initial: 30000, + interval: 86400000, + } + }, }; diff --git a/nodejs/conf/development.js b/nodejs/conf/development.js new file mode 100644 index 0000000..024cde1 --- /dev/null +++ b/nodejs/conf/development.js @@ -0,0 +1,23 @@ +'use strict'; +// Using https://github.com/simpleworkjs/conf to handle configuration + +module.exports = { + userModel: 'redis', // pam, redis, ldap + ldap: { + url: 'ldap://192.168.1.55:389', + bindDN: 'cn=ldapclient service,ou=people,dc=theta42,dc=com', + bindPassword: '__IN SRECREST FILE__', + searchBase: 'ou=people,dc=theta42,dc=com', + userFilter: '(objectClass=inetOrgPerson)', + userNameAttribute: 'uid' + }, + socketFile: '/var/run/proxy_lookup.socket', + redis: { + prefix: 'proxy_' + }, + service:{ + hostScheduler:{ + enabled: false, + } + }, +}; diff --git a/nodejs/models/host.js b/nodejs/models/host.js index 07205e3..87d24ab 100755 --- a/nodejs/models/host.js +++ b/nodejs/models/host.js @@ -82,8 +82,15 @@ class Host extends Table{ static async create(data, ...args){ try{ + + console.log('host create data', data.challengeType, data.challengeType === 'DNS-01-wildcard') + // Validate requested host is valid host and domain - if(data.challengeType === 'DNS-01-wildcard') await this.validateWildcardCreate(data, args); + if(data.challengeType === 'DNS-01-wildcard'){ + data.is_wildcard = true; + data.wildcard_status = "Starting" + await this.validateWildcardCreate(data, args); + } // Validate requested host has a valid wildcard parent if(data.challengeType === 'wildcardChild'){ @@ -96,24 +103,31 @@ class Host extends Table{ } } + console.log('Host create here 102') + // Create the new host entry let out = await super.create(data, ...args); + console.log('Host create here 106') // Update the lookup table to reflect new host await this.buildLookUpObj(); + console.log('Host create here 111') // Fire the request for the wild card cert // This is "back ground" job, await is intentionally missing - if(out.challengeType === 'DNS-01-wildcard') out.createWildcardCert(); + if(data.challengeType === 'DNS-01-wildcard') out.createWildcardCert(); + console.log('Host create here 111') return out; } catch(error){ + console.log(error) throw error; } } static async validateWildcardCreate(data, ...args){ + console.log('validateWildcardCreate here') try{ if(!data.host.startsWith('*.')) throw new Error('not wild card'); await Domain.get(data.host); @@ -125,9 +139,11 @@ class Host extends Table{ } async createWildcardCert(){ + console.log('createWildcardCert here') if(!this.host.startsWith('*.')) throw new Error('not wild card'); try{ + console.log('createWildcardCert here in try') let host = this; await host.update({ diff --git a/nodejs/services/host_scheduler.js b/nodejs/services/host_scheduler.js index 50e6768..465f658 100644 --- a/nodejs/services/host_scheduler.js +++ b/nodejs/services/host_scheduler.js @@ -1,32 +1,39 @@ 'use strict'; +const conf = require('@simpleworkjs/conf'); const {Host} = require('../models/host'); -/** - * Host Scheduler Service - * - * Manages scheduled tasks for host-related operations: - * - Wildcard SSL certificate renewal checks - * - * Schedule: - * - Initial check: 30 seconds after application starts - * - Recurring checks: Every 24 hours (86400000ms) - * - * The checkWildcardForRenew method: - * - Iterates through all hosts in the system - * - Checks if wildcard certificates are expiring within 30 days - * - Automatically renews certificates that are approaching expiration - */ -// Initial wildcard cert check 30 seconds after app starts -// Delay allows the system to fully initialize before checking certs -// setTimeout(Host.checkWildcardForRenew.bind(Host), 30000); +function hostSchedulerService(){ + /** + * Host Scheduler Service + * + * Manages scheduled tasks for host-related operations: + * - Wildcard SSL certificate renewal checks + * + * Schedule: + * - Initial check: 30 seconds after application starts + * - Recurring checks: Every 24 hours (86400000ms) + * + * The checkWildcardForRenew method: + * - Iterates through all hosts in the system + * - Checks if wildcard certificates are expiring within 30 days + * - Automatically renews certificates that are approaching expiration + */ -// Check wildcard certs once every 24 hours -// Ensures certificates are renewed well before expiration -setInterval(Host.checkWildcardForRenew.bind(Host), 86400000); + // Initial wildcard cert check 30 seconds after app starts + // Delay allows the system to fully initialize before checking certs + setTimeout(Host.checkWildcardForRenew.bind(Host), conf.service.hostScheduler.initial); + + // Check wildcard certs once every 24 hours + // Ensures certificates are renewed well before expiration + setInterval(Host.checkWildcardForRenew.bind(Host), conf.service.hostScheduler.interval); + + console.log('Host scheduler service initialized'); + console.log('- Wildcard cert check: 30s after start, then every 24h'); +} + +if(conf.service.hostScheduler.enabled !== false) hostSchedulerService(); -console.log('Host scheduler service initialized'); -console.log('- Wildcard cert check: 30s after start, then every 24h'); module.exports = {}; diff --git a/nodejs/utils/model_pubsub.js b/nodejs/utils/model_pubsub.js index f2ba5d1..e935ab8 100644 --- a/nodejs/utils/model_pubsub.js +++ b/nodejs/utils/model_pubsub.js @@ -38,16 +38,16 @@ function ModelPs(model){ publish(propKey, res, ...args); }).catch(function(error){ - // console.error("Error PS", model.name, propKey, error) - console.log('toDo, publish errors...'); + console.error("Error async PS", model.name, propKey, error) + // console.log('toDo, publish errors...'); }); }else{ publish(propKey, res, ...args); } return res; }catch(error){ - // console.error("Error PS", model.name, propKey, error) - console.log("toDo, publish errors..."); + console.error("Error PS", model.name, propKey, error) + // console.log("toDo, publish errors..."); } } } else {