Skip to content

Commit

Permalink
improve host recreation
Browse files Browse the repository at this point in the history
try to catch beautifier error
  • Loading branch information
Zoey2936 committed Jan 7, 2025
1 parent bbe0eb8 commit 7994460
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 202 deletions.
2 changes: 1 addition & 1 deletion Caddy.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.21.0
FROM alpine:3.21.1
RUN apk add --no-cache ca-certificates tzdata
COPY --from=caddy:2.8.4 /usr/bin/caddy /usr/bin/caddy
COPY Caddyfile /etc/caddy/Caddyfile
Expand Down
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:labs
FROM --platform="$BUILDPLATFORM" alpine:3.21.0 AS frontend
FROM --platform="$BUILDPLATFORM" alpine:3.21.1 AS frontend
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
ARG NODE_ENV=production \
NODE_OPTIONS=--openssl-legacy-provider
Expand All @@ -19,7 +19,7 @@ COPY darkmode.css /app/dist/css/darkmode.css
COPY security.txt /app/dist/.well-known/security.txt


FROM --platform="$BUILDPLATFORM" alpine:3.21.0 AS build-backend
FROM --platform="$BUILDPLATFORM" alpine:3.21.1 AS build-backend
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
ARG NODE_ENV=production \
TARGETARCH
Expand All @@ -38,15 +38,15 @@ RUN apk upgrade --no-cache -a && \
fi && \
yarn cache clean --all && \
clean-modules --yes
FROM alpine:3.21.0 AS strip-backend
FROM alpine:3.21.1 AS strip-backend
COPY --from=build-backend /app /app
RUN apk upgrade --no-cache -a && \
apk add --no-cache ca-certificates binutils file && \
find /app/node_modules -name "*.node" -type f -exec strip -s {} \; && \
find /app/node_modules -name "*.node" -type f -exec file {} \;


FROM --platform="$BUILDPLATFORM" alpine:3.21.0 AS crowdsec
FROM --platform="$BUILDPLATFORM" alpine:3.21.1 AS crowdsec
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
ARG CSNB_VER=v1.0.8
WORKDIR /src
Expand All @@ -72,7 +72,7 @@ RUN apk upgrade --no-cache -a && \
sed -i "s|APPSEC_PROCESS_TIMEOUT=.*|APPSEC_PROCESS_TIMEOUT=10000|g" /src/crowdsec-nginx-bouncer/lua-mod/config_example.conf


FROM zoeyvid/nginx-quic:371-python
FROM zoeyvid/nginx-quic:372-python
SHELL ["/bin/ash", "-eo", "pipefail", "-c"]
ARG CRS_VER=v4.10.0
COPY rootfs /
Expand Down Expand Up @@ -117,7 +117,7 @@ COPY --from=frontend /app/dist
LABEL com.centurylinklabs.watchtower.monitor-only="true"

ENV NODE_ENV=production \

Check warning on line 119 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "ACME_KEY_TYPE") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/
TV=1a \
TV=1b \
ACME_SERVER="https://acme-v02.api.letsencrypt.org/directory" \
ACME_MUST_STAPLE=false \
ACME_OCSP_STAPLING=true \
Expand Down
14 changes: 7 additions & 7 deletions backend/internal/access-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const internalAccessList = {
.build(row)
.then(() => {
if (row.proxy_host_count) {
return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
return internalNginx.bulkGenerateConfigs(proxyHostModel, 'proxy_host', row.proxy_hosts);
}
})
.then(() => {
Expand Down Expand Up @@ -163,7 +163,7 @@ const internalAccessList = {

return query.then(() => {
// Add new items
if (promises.length) {
if (promises.length > 0) {
return Promise.all(promises);
}
});
Expand All @@ -190,7 +190,7 @@ const internalAccessList = {

return query.then(() => {
// Add new items
if (promises.length) {
if (promises.length > 0) {
return Promise.all(promises);
}
});
Expand Down Expand Up @@ -221,7 +221,7 @@ const internalAccessList = {
.build(row)
.then(() => {
if (row.proxy_host_count) {
return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
return internalNginx.bulkGenerateConfigs(proxyHostModel, 'proxy_host', row.proxy_hosts);
}
})
.then(internalNginx.reload)
Expand Down Expand Up @@ -320,7 +320,7 @@ const internalAccessList = {
row.proxy_hosts[idx].access_list_id = 0;
});

return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
return internalNginx.bulkGenerateConfigs(proxyHostModel, 'proxy_host', row.proxy_hosts);
})
.then(() => {
return internalNginx.reload();
Expand Down Expand Up @@ -476,12 +476,12 @@ const internalAccessList = {
}
}).then((htpasswd_file) => {
// 3. generate password for each user
if (list.items.length) {
if (list.items.length > 0) {
return new Promise((resolve, reject) => {
batchflow(list.items)
.sequential()
.each((i, item, next) => {
if (typeof item.password !== 'undefined' && item.password.length) {
if (typeof item.password !== 'undefined' && item.password.length > 0) {
logger.info('Adding: ' + item.username);

try {
Expand Down
4 changes: 2 additions & 2 deletions backend/internal/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const internalCertificate = {
.where('is_deleted', 0)
.andWhere('provider', 'letsencrypt')
.then((certificates) => {
if (certificates && certificates.length) {
if (certificates && certificates.length > 0) {
const promises = [];

certificates.map(function (certificate) {
Expand Down Expand Up @@ -782,7 +782,7 @@ const internalCertificate = {
await certbot.installPlugin(certificate.meta.dns_provider);
logger.info(`Requesting Certbot certificates via ${dnsPlugin.name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);

const credentialsLocation = '/data/tls/certbot/credentials/credentials-' + certificate.id;
const credentialsLocation = '/tmp/certbot-credentials/credentials-' + certificate.id;
fs.writeFileSync(credentialsLocation, certificate.meta.dns_provider_credentials, { mode: 0o600 });

try {
Expand Down
4 changes: 2 additions & 2 deletions backend/internal/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const internalHost = {
_checkHostnameRecordsTaken: function (hostname, existing_rows, ignore_id) {
let is_taken = false;

if (existing_rows && existing_rows.length) {
if (existing_rows && existing_rows.length > 0) {
existing_rows.map(function (existing_row) {
existing_row.domain_names.map(function (existing_hostname) {
// Does this domain match?
Expand All @@ -186,7 +186,7 @@ const internalHost = {
_getHostsWithDomains: function (hosts, domain_names) {
const response = [];

if (hosts && hosts.length) {
if (hosts && hosts.length > 0) {
hosts.map(function (host) {
let host_matches = false;

Expand Down
35 changes: 13 additions & 22 deletions backend/internal/nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,12 @@ const internalNginx = {
reload: () => {
if (process.env.ACME_OCSP_STAPLING === 'true') {
return utils.execFile('certbot-ocsp-fetcher.sh', ['-c', '/data/tls/certbot', '-o', '/data/tls/certbot/live', '--no-reload-webserver', '--quiet']).finally(() => {
if (fs.existsSync('/usr/local/nginx/logs/nginx.pid') && fs.readFileSync('/usr/local/nginx/logs/nginx.pid', 'utf8').trim().length > 0) {
logger.info('Reloading Nginx');
return utils.execFile('nginx', ['-s', 'reload']);
} else {
logger.info('Starting Nginx');
utils.execfg('nginx', ['-e', 'stderr']);
}
});
} else {
if (fs.existsSync('/usr/local/nginx/logs/nginx.pid') && fs.readFileSync('/usr/local/nginx/logs/nginx.pid', 'utf8').trim().length > 0) {
logger.info('Reloading Nginx');
return utils.execFile('nginx', ['-s', 'reload']);
} else {
logger.info('Starting Nginx');
utils.execfg('nginx', ['-e', 'stderr']);
}
});
} else {
logger.info('Reloading Nginx');
return utils.execFile('nginx', ['-s', 'reload']);
}
},

Expand Down Expand Up @@ -219,7 +209,11 @@ const internalNginx = {
})
.then(() => {
if (process.env.DISABLE_NGINX_BEAUTIFIER === 'false') {
utils.execFile('nginxbeautifier', ['-s', '4', filename]);
try {
utils.execFile('nginxbeautifier', ['-s', '4', filename]);
} catch {
logger.error("nginxbeautifier failed");
}
}
});
});
Expand Down Expand Up @@ -276,13 +270,10 @@ const internalNginx = {
* @param {Array} hosts
* @returns {Promise}
*/
bulkGenerateConfigs: (host_type, hosts) => {
const promises = [];
hosts.map(function (host) {
promises.push(internalNginx.generateConfig(host_type, host));
});

return Promise.all(promises);
bulkGenerateConfigs: (model, host_type, hosts) => {
return hosts.reduce((promise, host) => {
return promise.then(() => internalNginx.configure(model, host_type, host));
}, Promise.resolve());
},

/**
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"author": "Jamie Curnow <[email protected]> and ZoeyVid <[email protected]>",
"license": "MIT",
"devDependencies": {
"@apidevtools/swagger-parser": "10.1.0",
"@apidevtools/swagger-parser": "10.1.1",
"@eslint/js": "9.17.0",
"eslint": "9.17.0",
"eslint-config-prettier": "9.1.0",
Expand Down
133 changes: 61 additions & 72 deletions backend/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ const setupDefaultSettings = () => {
logger.info('Default settings added');
});
}
})
.then(() => {
settingModel
.query()
.where('id', 'default-site')
.first()
.then((row) => {
internalNginx.generateConfig('default', row);
});
});
};

Expand All @@ -115,7 +124,7 @@ const setupCertbotPlugins = () => {
.where('is_deleted', 0)
.andWhere('provider', 'letsencrypt')
.then((certificates) => {
if (certificates && certificates.length) {
if (certificates && certificates.length > 0) {
const plugins = [];
const promises = [];

Expand All @@ -129,7 +138,7 @@ const setupCertbotPlugins = () => {
});

return certbot.installPlugins(plugins).then(() => {
if (promises.length) {
if (promises.length > 0) {
return Promise.all(promises).then(() => {
logger.info('Added Certbot plugins ' + plugins.join(', '));
});
Expand All @@ -145,76 +154,56 @@ const setupCertbotPlugins = () => {
* @returns {Promise}
*/
const regenerateAllHosts = () => {
settingModel
.query()
.where('id', 'default-site')
.first()
.then((row) => {
internalNginx.generateConfig('default', row);
})
.then(() => {
if (process.env.REGENERATE_ALL === 'true') {
const promises = [];

promises.push(
proxyModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[access_list.[clients, items], certificate]')
.then((rows) => {
if (rows && rows.length) {
internalNginx.bulkGenerateConfigs('proxy_host', rows);
}
}),
);

promises.push(
redirectModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[certificate]')
.then((rows) => {
if (rows && rows.length) {
internalNginx.bulkGenerateConfigs('redirection_host', rows);
}
}),
);

promises.push(
deadModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[certificate]')
.then((rows) => {
if (rows && rows.length) {
internalNginx.bulkGenerateConfigs('dead_host', rows);
}
}),
);

promises.push(
streamModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.then((rows) => {
if (rows && rows.length) {
internalNginx.bulkGenerateConfigs('stream', rows);
}
}),
);

// Execute all promises and then write the hash
return Promise.all(promises).then(() => {
utils.writeHash();
});
}
});

return Promise.resolve(); // Return resolved promise if REGENERATE_ALL is not true
if (process.env.REGENERATE_ALL === 'true') {
return proxyModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[access_list.[clients, items], certificate]')
.then((rows) => {
if (rows && rows.length > 0) {
internalNginx.bulkGenerateConfigs(proxyModel, 'proxy_host', rows);
}
})
.then(() => {
return redirectModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[certificate]')
.then((rows) => {
if (rows && rows.length > 0) {
internalNginx.bulkGenerateConfigs(redirectModel, 'redirection_host', rows);
}
});
})
.then(() => {
return deadModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.withGraphFetched('[certificate]')
.then((rows) => {
if (rows && rows.length > 0) {
internalNginx.bulkGenerateConfigs(deadModel, 'dead_host', rows);
}
});
})
.then(() => {
return streamModel
.query()
.where('is_deleted', 0)
.andWhere('enabled', 1)
.then((rows) => {
if (rows && rows.length > 0) {
internalNginx.bulkGenerateConfigs(streamModel, 'stream', rows);
}
});
})
.then(() => {
utils.writeHash();
});
}
};

module.exports = function () {
Expand Down
2 changes: 0 additions & 2 deletions backend/templates/_certificates.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% if certificate and certificate_id > 0 %}
{% if certificate.provider == "letsencrypt" %}
# Certbot TLS
include conf.d/include/tls-ciphers.conf;
ssl_certificate /data/tls/certbot/live/npm-{{ certificate_id }}/fullchain.pem;
ssl_certificate_key /data/tls/certbot/live/npm-{{ certificate_id }}/privkey.pem;
{% if env.ACME_OCSP_STAPLING == "true" %}
Expand All @@ -11,7 +10,6 @@
{% endif %}
{% else %}
# Custom TLS
include conf.d/include/tls-ciphers-no-stapling.conf;
ssl_certificate /data/tls/custom/npm-{{ certificate_id }}/fullchain.pem;
ssl_certificate_key /data/tls/custom/npm-{{ certificate_id }}/privkey.pem;
{% endif %}
Expand Down
Loading

0 comments on commit 7994460

Please sign in to comment.