PromucFlow_constructor/deploy/docker/fs/opt/appsmith/caddy-reconfigure.mjs

245 lines
6.6 KiB
JavaScript
Raw Normal View History

import * as fs from "fs"
import {dirname} from "path"
import {spawnSync} from "child_process"
import {X509Certificate} from "crypto"
// The custom domain is expected to only have the domain. So if it has a protocol, we ignore the whole value.
// This was the effective behaviour before Caddy.
const CUSTOM_DOMAIN = (process.env.APPSMITH_CUSTOM_DOMAIN || "").replace(/^https?:\/\/.+$/, "")
const CaddyfilePath = process.env.TMP + "/Caddyfile"
const AppsmithCaddy = process.env._APPSMITH_CADDY
// Rate limit environment.
const isRateLimitingEnabled = process.env.APPSMITH_RATE_LIMIT !== "disabled"
const RATE_LIMIT = parseInt(process.env.APPSMITH_RATE_LIMIT || 100, 10)
let certLocation = null
if (CUSTOM_DOMAIN !== "") {
try {
fs.accessSync("/appsmith-stacks/ssl/fullchain.pem", fs.constants.R_OK)
certLocation = "/appsmith-stacks/ssl"
} catch {
// no custom certs, see if old certbot certs are there.
const letsEncryptCertLocation = "/appsmith-stacks/letsencrypt/live/" + CUSTOM_DOMAIN
const fullChainPath = letsEncryptCertLocation + `/fullchain.pem`
try {
fs.accessSync(fullChainPath, fs.constants.R_OK)
console.log("Old Let's Encrypt cert file exists, now checking if it's expired.")
if (!isCertExpired(fullChainPath)) {
certLocation = letsEncryptCertLocation
}
} catch {
// no certs there either, ignore.
}
}
}
const frameAncestorsPolicy = (process.env.APPSMITH_ALLOWED_FRAME_ANCESTORS || "'self'")
.replace(/;.*$/, "")
const parts = []
parts.push(`
{
admin 0.0.0.0:2019
persist_config off
acme_ca_root /etc/ssl/certs/ca-certificates.crt
servers {
chore: add HTTP/3 support (#40064) ## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/14240272119> > Commit: 11254f8eb5b8e4c96404f9d833d73e29e12993e6 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=14240272119&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Thu, 03 Apr 2025 10:41:27 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved server capabilities by enabling support for HTTP/1, HTTP/2, and HTTP/3, enhancing performance and compatibility with various client protocols. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-04-03 14:09:23 +00:00
protocols h1 h2 h3
trusted_proxies static 0.0.0.0/0
metrics
}
${isRateLimitingEnabled ? "order rate_limit before basicauth" : ""}
}
(file_server) {
file_server {
precompressed br gzip
disable_canonical_uris
}
}
(reverse_proxy) {
reverse_proxy {
to 127.0.0.1:{args[0]}
header_up -Forwarded
header_up X-Appsmith-Request-Id {http.request.uuid}
}
}
(all-config) {
log {
output stdout
}
chore: caddy: enable logging of static file requests (#36500) ## Description - Enable logs of static file requests in caddy. - Skip logging for CSS and JS source map files. These files are inconsequential for page rendering and therefore not worth tracking, as they only pollute the log files. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11016033178> > Commit: 97cfe307355f159a9cb80295fb566c406ffc056a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11016033178&attempt=3" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Wed, 25 Sep 2024 05:08:51 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced logging configurations to improve performance by skipping logs for health check requests and JavaScript map files. - **Bug Fixes** - Adjusted existing logging behavior for file handling to ensure more accurate log processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-09-25 05:31:18 +00:00
# skip logs for health check
log_skip /api/v1/health
chore: caddy: enable logging of static file requests (#36500) ## Description - Enable logs of static file requests in caddy. - Skip logging for CSS and JS source map files. These files are inconsequential for page rendering and therefore not worth tracking, as they only pollute the log files. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11016033178> > Commit: 97cfe307355f159a9cb80295fb566c406ffc056a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11016033178&attempt=3" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Wed, 25 Sep 2024 05:08:51 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced logging configurations to improve performance by skipping logs for health check requests and JavaScript map files. - **Bug Fixes** - Adjusted existing logging behavior for file handling to ensure more accurate log processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-09-25 05:31:18 +00:00
# skip logs for sourcemap files
@source-map-files {
path_regexp ^.*\.(js|css)\.map$
}
log_skip @source-map-files
chore: caddy: enable logging of static file requests (#36500) ## Description - Enable logs of static file requests in caddy. - Skip logging for CSS and JS source map files. These files are inconsequential for page rendering and therefore not worth tracking, as they only pollute the log files. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.Sanity" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11016033178> > Commit: 97cfe307355f159a9cb80295fb566c406ffc056a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11016033178&attempt=3" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Wed, 25 Sep 2024 05:08:51 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced logging configurations to improve performance by skipping logs for health check requests and JavaScript map files. - **Bug Fixes** - Adjusted existing logging behavior for file handling to ensure more accurate log processing. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-09-25 05:31:18 +00:00
# The internal request ID header should never be accepted from an incoming request.
request_header -X-Appsmith-Request-Id
# Ref: https://stackoverflow.com/a/38191078/151048
# We're only accepting v4 UUIDs today, in order to not make it too lax unless needed.
@valid-request-id expression {header.X-Request-Id}.matches("(?i)^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$")
header @valid-request-id X-Request-Id {header.X-Request-Id}
@invalid-request-id expression !{header.X-Request-Id}.matches("(?i)^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$")
header @invalid-request-id X-Request-Id invalid_request_id
request_header @invalid-request-id X-Request-Id invalid_request_id
header {
-Server
Content-Security-Policy "frame-ancestors ${frameAncestorsPolicy}"
X-Content-Type-Options "nosniff"
X-Appsmith-Request-Id {http.request.uuid}
}
header /static/* {
Cache-Control "public, max-age=31536000, immutable"
}
request_body {
max_size ${process.env.APPSMITH_CODEC_SIZE || 150}MB
}
handle {
root * {$WWW_PATH}
try_files /loading.html /index.html
import file_server
}
root * /opt/appsmith/editor
@file file
handle @file {
import file_server
}
handle /static/* {
error 404
}
handle /info {
root * /opt/appsmith
rewrite * /info.json
import file_server
}
@backend path /api/* /oauth2/* /login/*
handle @backend {
import reverse_proxy 8080
}
handle /rts/* {
import reverse_proxy 8091
}
redir /supervisor /supervisor/
handle_path /supervisor/* {
import reverse_proxy 9001
}
${isRateLimitingEnabled ? `rate_limit {
2024-03-07 10:52:29 +00:00
zone dynamic_zone {
fix: Rate limiting key should respect load balancers (#37409) This PR changes the `key` used for rate limiting so that it includes any `Forwarded` or `X-Forwarded-For` headers, so that rate-limiting counter respects any load balancers that are running on top of Appsmith container. /test sanity ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11855493733> > Commit: af2d760c6e5f3ea61ae0bfb476cb4e023648cecc > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11855493733&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Fri, 15 Nov 2024 12:04:04 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced rate limiting configuration for improved performance with load balancers. - Adjusted handling of custom domains for better certificate management. - **Bug Fixes** - Improved error handling by removing unnecessary headers to enhance security and response consistency. - **Documentation** - Updated internal logic for generating server configurations, ensuring clarity in server setup. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-11-20 05:10:02 +00:00
# This key is designed to work irrespective of any load balancers running on the Appsmith container.
# We use "+" as the separator here since we don't expect it in any of the placeholder values here, and has no
# significance in header value syntax.
key {header.Forwarded}+{header.X-Forwarded-For}+{remote_host}
2024-03-07 10:52:29 +00:00
events ${RATE_LIMIT}
window 1s
}
}`: ""}
2024-03-07 10:52:29 +00:00
handle_errors {
respond "{err.status_code} {err.status_text}" {err.status_code}
header {
# Remove the Server header from the response.
-Server
# Remove Cache-Control header from the response.
-Cache-Control
}
}
}
# We bind to http on 80, so that localhost requests don't get redirected to https.
:${process.env.PORT || 80} {
import all-config
}
`)
if (CUSTOM_DOMAIN !== "") {
if (certLocation) {
// There's a custom certificate, don't bind to any exact domain.
parts.push(`
https:// {
import all-config
tls ${certLocation}/fullchain.pem ${certLocation}/privkey.pem
}
`)
} else {
// No custom certificate, bind to the custom domain explicitly, so Caddy can auto-provision the cert.
parts.push(`
https://${CUSTOM_DOMAIN} {
import all-config
}
`)
}
// We have to own the http-to-https redirect, since we need to remove the `Server` header from the response.
parts.push(`
http://${CUSTOM_DOMAIN} {
redir https://{host}{uri}
header -Server
header Connection close
}
`)
}
if (!process.argv.includes("--no-finalize-index-html")) {
chore: Fix env varibles in 404.html (#37672) Environment variables in `404.html` page aren't getting replaced with their values. This PR fixes that. ## Automation /test sanity ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!WARNING] > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12005727044> > Commit: 56b10fddf2ee7ed180ed59845b6f0223cd26b06a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12005727044&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: @tag.Sanity > Spec: > It seems like **no tests ran** 😔. We are not able to recognize it, please check <a href="https://github.com/appsmithorg/appsmith/actions/runs/12005727044" target="_blank">workflow here</a>. > <hr>Mon, 25 Nov 2024 08:48:10 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved dynamic retrieval of environment variables for better integration with environment-specific settings in the 404 error page. - Enhanced handling of HTML files, including the 404 page, during the configuration process. - **Bug Fixes** - Improved error handling for missing SSL certificates and custom domains. - **Documentation** - Updated logic for processing HTML files to ensure correct paths and configurations are applied. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-11-26 06:11:01 +00:00
finalizeHtmlFiles()
}
fs.mkdirSync(dirname(CaddyfilePath), { recursive: true })
fs.writeFileSync(CaddyfilePath, parts.join("\n"))
spawnSync(AppsmithCaddy, ["fmt", "--overwrite", CaddyfilePath])
spawnSync(AppsmithCaddy, ["reload", "--config", CaddyfilePath])
chore: Fix env varibles in 404.html (#37672) Environment variables in `404.html` page aren't getting replaced with their values. This PR fixes that. ## Automation /test sanity ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!WARNING] > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12005727044> > Commit: 56b10fddf2ee7ed180ed59845b6f0223cd26b06a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12005727044&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: @tag.Sanity > Spec: > It seems like **no tests ran** 😔. We are not able to recognize it, please check <a href="https://github.com/appsmithorg/appsmith/actions/runs/12005727044" target="_blank">workflow here</a>. > <hr>Mon, 25 Nov 2024 08:48:10 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved dynamic retrieval of environment variables for better integration with environment-specific settings in the 404 error page. - Enhanced handling of HTML files, including the 404 page, during the configuration process. - **Bug Fixes** - Improved error handling for missing SSL certificates and custom domains. - **Documentation** - Updated logic for processing HTML files to ensure correct paths and configurations are applied. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-11-26 06:11:01 +00:00
function finalizeHtmlFiles() {
let info = null;
try {
info = JSON.parse(fs.readFileSync("/opt/appsmith/info.json", "utf8"))
} catch(e) {
// info will be empty, that's okay.
console.error("Error reading info.json", e.message)
}
const extraEnv = {
APPSMITH_VERSION_ID: info?.version ?? "",
APPSMITH_VERSION_SHA: info?.commitSha ?? "",
APPSMITH_VERSION_RELEASE_DATE: info?.imageBuiltAt ?? "",
chore: add grafana faro sdk (CE) (#38301) ## Description - Remove new relic browser agent - Add faro sdk to capture frontend perf metrics and traces. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.All" ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12490844984> > Commit: c9d4264027467bf33e1de519eb69c7762b6e7f75 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12490844984&attempt=2" target="_blank">Cypress dashboard</a>. > Tags: `@tag.All` > Spec: > <hr>Wed, 25 Dec 2024 09:33:26 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Introduced new environment variable `APPSMITH_HOSTNAME` for dynamic hostname configuration in HTML files. - Enhanced telemetry capabilities with new imports and updated types for better observability. - Added `tracingUrl` under the observability section in configuration files for improved telemetry tracking. - **Bug Fixes** - Adjusted telemetry data handling to utilize new `Attributes` type for improved consistency. - **Documentation** - Updated import paths for various telemetry-related components to reflect new module organization. - **Chores** - Removed deprecated telemetry configurations and streamlined build processes. - Updated Nginx configuration to reflect new telemetry parameters. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-12-26 05:07:41 +00:00
APPSMITH_HOSTNAME: process.env.HOSTNAME ?? "appsmith-0"
}
chore: Fix env varibles in 404.html (#37672) Environment variables in `404.html` page aren't getting replaced with their values. This PR fixes that. ## Automation /test sanity ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!WARNING] > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12005727044> > Commit: 56b10fddf2ee7ed180ed59845b6f0223cd26b06a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12005727044&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: @tag.Sanity > Spec: > It seems like **no tests ran** 😔. We are not able to recognize it, please check <a href="https://github.com/appsmithorg/appsmith/actions/runs/12005727044" target="_blank">workflow here</a>. > <hr>Mon, 25 Nov 2024 08:48:10 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved dynamic retrieval of environment variables for better integration with environment-specific settings in the 404 error page. - Enhanced handling of HTML files, including the 404 page, during the configuration process. - **Bug Fixes** - Improved error handling for missing SSL certificates and custom domains. - **Documentation** - Updated logic for processing HTML files to ensure correct paths and configurations are applied. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-11-26 06:11:01 +00:00
for (const file of ["index.html", "404.html"]) {
const content = fs.readFileSync("/opt/appsmith/editor/" + file, "utf8").replaceAll(
/\{\{env\s+"(APPSMITH_[A-Z0-9_]+)"}}/g,
(_, name) => (process.env[name] || extraEnv[name] || "")
)
chore: Fix env varibles in 404.html (#37672) Environment variables in `404.html` page aren't getting replaced with their values. This PR fixes that. ## Automation /test sanity ### :mag: Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!WARNING] > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/12005727044> > Commit: 56b10fddf2ee7ed180ed59845b6f0223cd26b06a > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12005727044&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: @tag.Sanity > Spec: > It seems like **no tests ran** 😔. We are not able to recognize it, please check <a href="https://github.com/appsmithorg/appsmith/actions/runs/12005727044" target="_blank">workflow here</a>. > <hr>Mon, 25 Nov 2024 08:48:10 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved dynamic retrieval of environment variables for better integration with environment-specific settings in the 404 error page. - Enhanced handling of HTML files, including the 404 page, during the configuration process. - **Bug Fixes** - Improved error handling for missing SSL certificates and custom domains. - **Documentation** - Updated logic for processing HTML files to ensure correct paths and configurations are applied. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-11-26 06:11:01 +00:00
fs.writeFileSync(process.env.WWW_PATH + "/" + file, content)
}
}
function isCertExpired(path) {
const cert = new X509Certificate(fs.readFileSync(path, "utf-8"))
console.log(path, cert)
return new Date(cert.validTo) < new Date()
}