Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getjerry customize branch #1

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8bde676
getjerry customize branch
bellondr Dec 13, 2024
43bd780
getjerry customize branch
bellondr Dec 14, 2024
324f0b6
log config change
bellondr Dec 14, 2024
682e4e8
getjerry customize branch
bellondr Dec 16, 2024
ff74a64
getjerry customize branch
bellondr Dec 16, 2024
43749bb
getjerry customize branch
bellondr Dec 16, 2024
e60a25e
getjerry customize branch
bellondr Dec 16, 2024
f7ff383
getjerry customize branch
bellondr Dec 16, 2024
b96e23c
fix issue
bellondr Dec 16, 2024
42a3469
fix issue
bellondr Dec 16, 2024
aaee599
fix issue
bellondr Dec 16, 2024
0ad3169
fix issue
bellondr Dec 16, 2024
1591e4d
fix issue
bellondr Dec 16, 2024
34ba83f
fix issue
bellondr Dec 16, 2024
b436eb6
fix issue
bellondr Dec 16, 2024
218c791
fix issue
bellondr Dec 16, 2024
331f64d
fix issue
bellondr Dec 16, 2024
e5b19ac
fix issue
bellondr Dec 16, 2024
f1a84c3
fix issue
bellondr Dec 16, 2024
110ad03
fix issue
bellondr Dec 16, 2024
fe01adb
fix issue
bellondr Dec 17, 2024
b08be7b
fix issue
bellondr Dec 17, 2024
c469855
fix issue
bellondr Dec 17, 2024
9cbd369
fix issue
bellondr Dec 17, 2024
70f1c43
fix issue
bellondr Dec 17, 2024
d0aa19e
fix issue
bellondr Dec 17, 2024
f19e14a
fix issue
bellondr Dec 17, 2024
0673161
add readiness srcipt
bellondr Dec 17, 2024
c82e24b
fix issue
bellondr Dec 18, 2024
52f3ce3
fix issue
bellondr Dec 18, 2024
32584cf
fix issue
bellondr Dec 18, 2024
64a546a
fix issue
bellondr Dec 18, 2024
6243f1b
fix issue
bellondr Dec 18, 2024
e53eacb
fix issue
bellondr Dec 19, 2024
5ed7200
enlarge bw service size
bellondr Dec 22, 2024
c1b8f31
enlarge bw service size
bellondr Dec 22, 2024
a7c5846
enable metrics
bellondr Dec 22, 2024
7b8fcc1
for lua memory usage test
bellondr Dec 23, 2024
2b78518
reduce memory usage
bellondr Dec 25, 2024
3dd2d9f
reduce memory usage
bellondr Dec 25, 2024
da66a09
this is for test
bellondr Dec 27, 2024
d7cb5ed
this is for test
bellondr Dec 27, 2024
66afafa
this is for test
bellondr Dec 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/autoconf/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def wait_applying(self, startup: bool = False):
i = 0
while i < 60:
curr_changes = self._db.check_changes()
self.__logger.info(f"current changed: {curr_changes}")
first_config_saved = self._db.is_first_config_saved()
self.__logger.info(f"first config saved: {first_config_saved}")
Comment on lines +85 to +87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adjusting logging approach for better production readiness

While adding visibility is good, the current logging implementation could be improved:

  1. Consider using DEBUG level instead of INFO for detailed state logging to prevent log spam in production.
  2. Add sanitization/truncation of potentially sensitive or large data structures before logging.
  3. Move logging statements after their respective error checks to avoid logging invalid states.

Apply this diff to improve the logging implementation:

-            self.__logger.info(f"current changed: {curr_changes}")
             first_config_saved = self._db.is_first_config_saved()
-            self.__logger.info(f"first config saved: {first_config_saved}")
             if isinstance(curr_changes, str):
                 if not startup:
                     self.__logger.error(f"An error occurred when checking for changes in the database : {curr_changes}")
+            else:
+                self.__logger.debug(f"Current changes status: {str(curr_changes)[:200]}...")
             elif isinstance(first_config_saved, str):
                 if not startup:
                     self.__logger.error(f"An error occurred when checking if the first config is saved in the database : {first_config_saved}")
+            else:
+                self.__logger.debug(f"First config saved: {first_config_saved}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
self.__logger.info(f"current changed: {curr_changes}")
first_config_saved = self._db.is_first_config_saved()
self.__logger.info(f"first config saved: {first_config_saved}")
first_config_saved = self._db.is_first_config_saved()
if isinstance(curr_changes, str):
if not startup:
self.__logger.error(f"An error occurred when checking for changes in the database : {curr_changes}")
else:
self.__logger.debug(f"Current changes status: {str(curr_changes)[:200]}...")
elif isinstance(first_config_saved, str):
if not startup:
self.__logger.error(f"An error occurred when checking if the first config is saved in the database : {first_config_saved}")
else:
self.__logger.debug(f"First config saved: {first_config_saved}")

Comment on lines +85 to +87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve logging implementation for better security and clarity.

While adding logging for debugging is valuable, the current implementation could be enhanced:

  1. Consider using DEBUG level instead of INFO for internal state logging
  2. Avoid logging raw configuration objects that might contain sensitive data
  3. Fix the grammatical error in the log message

Apply this diff to improve the logging implementation:

-            self.__logger.info(f"current changed: {curr_changes}")
+            self.__logger.debug(f"Current changes detected: {list(curr_changes.keys()) if isinstance(curr_changes, dict) else 'error'}")
-            self.__logger.info(f"first config saved: {first_config_saved}")
+            self.__logger.debug(f"First configuration saved status: {first_config_saved}")

This change:

  • Uses DEBUG level for internal state logging
  • Only logs the keys of changes instead of the entire object
  • Improves message clarity and grammar
  • Handles the case when curr_changes is a string (error message)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
self.__logger.info(f"current changed: {curr_changes}")
first_config_saved = self._db.is_first_config_saved()
self.__logger.info(f"first config saved: {first_config_saved}")
self.__logger.debug(f"Current changes detected: {list(curr_changes.keys()) if isinstance(curr_changes, dict) else 'error'}")
first_config_saved = self._db.is_first_config_saved()
self.__logger.debug(f"First configuration saved status: {first_config_saved}")

if isinstance(curr_changes, str):
if not startup:
self.__logger.error(f"An error occurred when checking for changes in the database : {curr_changes}")
Expand Down
42 changes: 6 additions & 36 deletions src/autoconf/IngressController.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def _to_services(self, controller_service) -> List[dict]:
return []
namespace = controller_service.metadata.namespace
services = []
if controller_service.metadata.annotations is None or "bunkerweb.io" not in controller_service.metadata.annotations:
return []
# parse rules
for rule in controller_service.spec.rules:
if not rule.host:
Expand All @@ -79,45 +81,13 @@ def _to_services(self, controller_service) -> List[dict]:
services.append(service)
continue
location = 1
for path in rule.http.paths:
if not path.path:
self._logger.warning(
"Ignoring unsupported ingress rule without path.",
)
continue
elif not path.backend.service:
self._logger.warning(
"Ignoring unsupported ingress rule without backend service.",
)
continue
elif not path.backend.service.port:
self._logger.warning(
"Ignoring unsupported ingress rule without backend service port.",
)
continue
elif not path.backend.service.port.number:
self._logger.warning(
"Ignoring unsupported ingress rule without backend service port number.",
)
continue

service_list = self.__corev1.list_service_for_all_namespaces(
watch=False,
field_selector=f"metadata.name={path.backend.service.name},metadata.namespace={namespace}",
).items

if not service_list:
self._logger.warning(
f"Ignoring ingress rule with service {path.backend.service.name} : service not found.",
)
continue

reverse_proxy_host = f"http://{path.backend.service.name}.{namespace}.svc.cluster.local:{path.backend.service.port.number}"
if len(rule.http.paths) > 0:
reverse_proxy_host = "https://api-stage.ing.getjerry.com"
service.update(
{
"USE_REVERSE_PROXY": "yes",
f"REVERSE_PROXY_HOST_{location}": reverse_proxy_host,
f"REVERSE_PROXY_URL_{location}": path.path,
f"REVERSE_PROXY_URL_{location}": "/",
}
)
location += 1
Expand Down Expand Up @@ -210,7 +180,7 @@ def __process_event(self, event):
if obj.kind == "Pod":
return annotations and "bunkerweb.io/INSTANCE" in annotations
if obj.kind == "Ingress":
return True
return annotations and "bunkerweb.io" in annotations
if obj.kind == "ConfigMap":
return annotations and "bunkerweb.io/CONFIG_TYPE" in annotations
if obj.kind == "Service":
Expand Down
5 changes: 4 additions & 1 deletion src/bw/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ WORKDIR /usr/share/bunkerweb
# Copy python requirements
COPY src/deps/requirements.txt /tmp/requirements-deps.txt
COPY src/common/gen/requirements.txt deps/requirements-gen.txt
COPY src/common/db/requirements.txt deps/requirements-db.txt

# Install python requirements
RUN export MAKEFLAGS="-j$(nproc)" && \
pip install --break-system-packages --no-cache-dir --require-hashes --ignore-installed -r /tmp/requirements-deps.txt && \
pip install --break-system-packages --no-cache-dir --require-hashes --target deps/python -r deps/requirements-gen.txt
pip install --break-system-packages --no-cache-dir --require-hashes --target deps/python -r deps/requirements-gen.txt && \
pip install --break-system-packages --no-cache-dir --require-hashes --target deps/python -r deps/requirements-db.txt
Comment on lines +27 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Requirements files with hashes not found

The Dockerfile references deps/requirements-gen.txt and deps/requirements-db.txt, but these files are not present in the repository. The --require-hashes flag is used in pip install commands, but the corresponding requirements files are missing.

  • src/bw/deps/requirements-gen.txt (missing)
  • src/bw/deps/requirements-db.txt (missing)
🔗 Analysis chain

Verify requirements file hash consistency

Since --require-hashes flag is used, ensure all requirements files contain proper hash verification.

Let's verify the hash specifications in all requirements files:

Also applies to: 22-22


Let's verify the specific requirements files mentioned in the Dockerfile:


Let's try to locate these files first since the paths might be different:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if all requirements files contain hash specifications
for req in $(fd requirements.txt); do
  echo "=== $req ==="
  if ! grep -q "sha256:" "$req"; then
    echo "WARNING: No hash specifications found in $req"
  fi
done

Length of output: 13044


Script:

#!/bin/bash
echo "=== deps/requirements-gen.txt ==="
cat src/bw/deps/requirements-gen.txt
echo -e "\n=== deps/requirements-db.txt ==="
cat src/bw/deps/requirements-db.txt

Length of output: 363


Script:

#!/bin/bash
# Find requirements files with -gen and -db in their names
echo "=== Looking for *requirements*gen*.txt files ==="
fd -t f "requirements.*gen.*txt"

echo -e "\n=== Looking for *requirements*db*.txt files ==="
fd -t f "requirements.*db.*txt"

# Also check the directory structure around the Dockerfile
echo -e "\n=== Directory structure around Dockerfile ==="
ls -la "$(dirname $(fd Dockerfile | grep src/bw/Dockerfile))"

Length of output: 881


# Copy files
# can't exclude deps from . so we are copying everything by hand
Expand All @@ -36,6 +38,7 @@ COPY src/common/cli cli
COPY src/common/confs confs
COPY src/common/core core
COPY src/common/gen gen
COPY src/common/db db
COPY src/common/helpers helpers
COPY src/common/settings.json settings.json
COPY src/common/utils utils
Expand Down
20 changes: 19 additions & 1 deletion src/common/confs/default-server-http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ server {
})
}
}
{% else +%}
location / {
etag off;
proxy_pass "https://api-stage.ing.getjerry.com";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;

proxy_set_header X-Forwarded-Prefix "/";

proxy_buffering on;

proxy_connect_timeout 60s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
{% endif %}

# include core and plugins default-server configurations
Expand Down Expand Up @@ -189,5 +208,4 @@ server {
logger:log(INFO, "log_default phase ended")

}

}
6 changes: 6 additions & 0 deletions src/common/confs/healthcheck.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ server {
}
}

location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}

# disable logging
access_log off;

Expand Down
3 changes: 3 additions & 0 deletions src/common/confs/http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ client_header_timeout 10;
keepalive_timeout 15;
send_timeout 10;

server_names_hash_bucket_size 128;
server_names_hash_max_size 2048;

# resolvers to use
resolver {{ DNS_RESOLVERS }} {% if USE_IPV6 == "no" %}ipv6=off{% endif %};

Expand Down
7 changes: 0 additions & 7 deletions src/common/core/misc/confs/default-server-http/disable.conf
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{% if DISABLE_DEFAULT_SERVER == "yes" +%}
location / {
set $reason "default";
set $reason_data "";
return {{ DENY_HTTP_STATUS }};
}
{% endif %}
{% if DISABLE_DEFAULT_SERVER_STRICT_SNI == "yes" +%}
ssl_client_hello_by_lua_block {
local ssl_clt = require "ngx.ssl.clienthello"
Expand Down
47 changes: 0 additions & 47 deletions src/common/core/misc/confs/default-server-http/page.conf

This file was deleted.

3 changes: 0 additions & 3 deletions src/common/core/ui/confs/default-server-http/ui.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ access_by_lua_block {
local scheme = ngx_var.scheme
local http_host = ngx_var.http_host
local request_uri = ngx_var.request_uri
if scheme == "http" and http_host ~= nil and http_host ~= "" and request_uri and request_uri ~= "" then
return ngx.redirect("https://" .. http_host .. request_uri, ngx.HTTP_MOVED_PERMANENTLY)
end
}
location /setup {
etag off;
Expand Down
8 changes: 4 additions & 4 deletions src/common/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Global_values(Base):
class Services(Base):
__tablename__ = "bw_services"

id = Column(String(64), primary_key=True)
id = Column(String(256), primary_key=True)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Database migration strategy needed

This schema change requires a clear migration strategy to:

  1. Handle existing data
  2. Maintain data integrity during migration
  3. Ensure backward compatibility if needed

Would you like me to help create:

  1. A database migration script?
  2. Data validation utilities?
  3. Rollback procedures?

Also applies to: 130-133, 175-175, 190-190

method = Column(METHODS_ENUM, nullable=False)
is_draft = Column(Boolean, default=False, nullable=False)

Expand All @@ -127,7 +127,7 @@ class Services(Base):
class Services_settings(Base):
__tablename__ = "bw_services_settings"

service_id = Column(String(64), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), primary_key=True)
service_id = Column(String(256), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), primary_key=True)
setting_id = Column(String(256), ForeignKey("bw_settings.id", onupdate="cascade", ondelete="cascade"), primary_key=True)
value = Column(TEXT, nullable=False)
suffix = Column(Integer, primary_key=True, nullable=True, default=0)
Expand Down Expand Up @@ -172,7 +172,7 @@ class Jobs_cache(Base):

id = Column(Integer, Identity(start=1, increment=1), primary_key=True)
job_name = Column(String(128), ForeignKey("bw_jobs.name", onupdate="cascade", ondelete="cascade"), nullable=False)
service_id = Column(String(64), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), nullable=True)
service_id = Column(String(256), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), nullable=True)
file_name = Column(String(256), nullable=False)
data = Column(LargeBinary(length=(2**32) - 1), nullable=True)
last_update = Column(DateTime, nullable=True)
Expand All @@ -187,7 +187,7 @@ class Custom_configs(Base):
__table_args__ = (UniqueConstraint("service_id", "type", "name"),)

id = Column(Integer, Identity(start=1, increment=1), primary_key=True)
service_id = Column(String(64), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), nullable=True)
service_id = Column(String(256), ForeignKey("bw_services.id", onupdate="cascade", ondelete="cascade"), nullable=True)
type = Column(CUSTOM_CONFIGS_TYPES_ENUM, nullable=False)
name = Column(String(256), nullable=False)
data = Column(LargeBinary(length=(2**32) - 1), nullable=False)
Expand Down
19 changes: 19 additions & 0 deletions src/common/helpers/readiness.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

if [ ! -f /var/run/bunkerweb/nginx.pid ] ; then
exit 1
fi

check="$(curl -s -H "Host: healthcheck.bunkerweb.io" http://127.0.0.1:6000/healthz 2>&1)"
# shellcheck disable=SC2181
if [ $? -ne 0 ] || [ "$check" != "ok" ] ; then
exit 1
fi
Comment on lines +7 to +11
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add timeout to health check and consider configurable host.

The health check could hang indefinitely without a timeout. Also, the hardcoded host header might not be suitable for all environments.

Apply this diff to add timeout and make the host configurable:

-check="$(curl -s -H "Host: healthcheck.bunkerweb.io" http://127.0.0.1:6000/healthz 2>&1)"
+HEALTH_CHECK_HOST=${HEALTH_CHECK_HOST:-"healthcheck.bunkerweb.io"}
+check="$(curl -s --max-time 5 -H "Host: ${HEALTH_CHECK_HOST}" http://127.0.0.1:6000/healthz 2>&1)"
 # shellcheck disable=SC2181
 if [ $? -ne 0 ] || [ "$check" != "ok" ] ; then
+    echo "Health check failed: ${check}"
 	exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
check="$(curl -s -H "Host: healthcheck.bunkerweb.io" http://127.0.0.1:6000/healthz 2>&1)"
# shellcheck disable=SC2181
if [ $? -ne 0 ] || [ "$check" != "ok" ] ; then
exit 1
fi
HEALTH_CHECK_HOST=${HEALTH_CHECK_HOST:-"healthcheck.bunkerweb.io"}
check="$(curl -s --max-time 5 -H "Host: ${HEALTH_CHECK_HOST}" http://127.0.0.1:6000/healthz 2>&1)"
# shellcheck disable=SC2181
if [ $? -ne 0 ] || [ "$check" != "ok" ] ; then
echo "Health check failed: ${check}"
exit 1
fi


# check IS_LOADING
VAR_FILE=/etc/nginx/variables.env
if grep -q "IS_LOADING=yes" "$VAR_FILE"; then
echo "pod is loading, waiting..."
exit 1
fi
Comment on lines +13 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix indentation and add error handling for missing file.

The code has inconsistent indentation and lacks error handling for the case when the variables file doesn't exist.

Apply this diff to fix these issues:

 # check IS_LOADING
 VAR_FILE=/etc/nginx/variables.env
+if [ ! -f "$VAR_FILE" ]; then
+    echo "Error: Variables file not found: $VAR_FILE"
+    exit 1
+fi
+
 if grep -q "IS_LOADING=yes" "$VAR_FILE"; then
     echo "pod is loading, waiting..."
-		exit 1
+    exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# check IS_LOADING
VAR_FILE=/etc/nginx/variables.env
if grep -q "IS_LOADING=yes" "$VAR_FILE"; then
echo "pod is loading, waiting..."
exit 1
fi
# check IS_LOADING
VAR_FILE=/etc/nginx/variables.env
if [ ! -f "$VAR_FILE" ]; then
echo "Error: Variables file not found: $VAR_FILE"
exit 1
fi
if grep -q "IS_LOADING=yes" "$VAR_FILE"; then
echo "pod is loading, waiting..."
exit 1
fi

exit 0