Merge pull request #342 from TheophileDiot/1.5

Fix bunkerweb container + UI custom configs with DB
This commit is contained in:
Théophile Diot 2022-11-08 18:14:47 +01:00 committed by GitHub
commit ef7fa5b4f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 153 additions and 54 deletions

View File

@ -45,6 +45,7 @@ COPY VERSION /opt/bunkerweb/VERSION
# Install runtime dependencies, pypi packages, move bwcli, create data folders and set permissions
RUN apk add --no-cache bash python3 libgcc libstdc++ openssl git && \
cp /opt/bunkerweb/helpers/bwcli /usr/local/bin && \
echo "Docker" > /opt/bunkerweb/INTEGRATION && \
for dir in $(echo "cache configs plugins www") ; do mkdir -p "/data/${dir}" && ln -s "/data/${dir}" "/opt/bunkerweb/${dir}" ; done && \
for dir in $(echo "configs/http configs/stream configs/server-http configs/server-stream configs/default-server-http configs/default-server-stream configs/modsec configs/modsec-crs cache/letsencrypt") ; do mkdir -p "/data/${dir}" ; done && \
chown -R root:nginx /data && \
@ -67,7 +68,8 @@ RUN apk add --no-cache bash python3 libgcc libstdc++ openssl git && \
ln -s /proc/1/fd/2 /var/log/nginx/modsec_audit.log && \
ln -s /proc/1/fd/1 /var/log/nginx/access.log && \
ln -s /proc/1/fd/1 /var/log/nginx/jobs.log && \
ln -s /proc/1/fd/1 /var/log/letsencrypt/letsencrypt.log
ln -s /proc/1/fd/1 /var/log/letsencrypt/letsencrypt.log && \
chmod 660 /opt/bunkerweb/INTEGRATION
# Fix CVEs
RUN apk add "freetype>=2.10.4-r3" "curl>=7.79.1-r2" "libcurl>=7.79.1-r2" "openssl>=1.1.1q-r0" "libssl1.1>=1.1.1q-r0" "libcrypto1.1>=1.1.1q-r0" "git>=2.32.3-r0" "ncurses-libs>=6.2_p20210612-r1" "ncurses-terminfo-base>=6.2_p20210612-r1" "zlib>=1.2.12-r2" "libxml2>=2.9.14-r1"

View File

@ -32,9 +32,13 @@ function trap_reload() {
}
trap "trap_reload" HUP
# generate "temp" config
echo -e "IS_LOADING=yes\nSERVER_NAME=\nAPI_HTTP_PORT=${API_HTTP_PORT:-5000}\nAPI_SERVER_NAME=${API_SERVER_NAME:-bwapi}\nAPI_WHITELIST_IP=${API_WHITELIST_IP:-127.0.0.0/8}" > /tmp/variables.env
python3 /opt/bunkerweb/gen/main.py --variables /tmp/variables.env
if [ "$SWARM_MODE" == "yes" ] ; then
echo "Swarm" > /opt/bunkerweb/INTEGRATION
elif [ "$KUBERNETES_MODE" == "yes" ] ; then
echo "Kubernetes" > /opt/bunkerweb/INTEGRATION
elif [ "$AUTOCONF_MODE" == "yes" ] ; then
echo "Autoconf" > /opt/bunkerweb/INTEGRATION
fi
# start nginx
log "ENTRYPOINT" "" "Starting nginx ..."

View File

@ -325,64 +325,157 @@ def form_plugin_gen(
def path_to_dict(
path, *, level: int = 0, is_cache: bool = False, db_configs: List[dict] = []
path,
*,
level: int = 0,
is_cache: bool = False,
db_configs: List[dict] = [],
integration: str = "Linux",
) -> dict:
d = {"name": os.path.basename(path)}
if integration == "Linux":
d = {"name": os.path.basename(path)}
if os.path.isdir(path):
d.update(
{
"type": "folder",
"path": path,
"can_create_files": level > 0 and not is_cache,
"can_create_folders": level > 0 and not is_cache,
"can_edit": level > 1 and not is_cache,
"can_delete": False,
"children": [
path_to_dict(
os.path.join(path, x),
level=level + 1,
is_cache=is_cache,
db_configs=db_configs,
)
for x in sorted(os.listdir(path))
],
}
)
if os.path.isdir(path):
d.update(
{
"type": "folder",
"path": path,
"can_create_files": level > 0 and not is_cache,
"can_create_folders": level > 0 and not is_cache,
"can_edit": level > 1 and not is_cache,
"can_delete": False,
"children": [
path_to_dict(
os.path.join(path, x),
level=level + 1,
is_cache=is_cache,
db_configs=db_configs,
)
for x in sorted(os.listdir(path))
],
}
)
if level > 1 and not is_cache and not d["children"]:
d["can_delete"] = True
if level > 1 and not is_cache and not d["children"]:
d["can_delete"] = True
else:
d.update(
{
"type": "file",
"path": path,
"can_download": is_cache,
}
)
can_edit = False
if level > 1 and not is_cache:
exploded_path = path.split("/")
for conf in db_configs:
if exploded_path[-1].replace(".conf", "") == conf["name"]:
if level > 2 and exploded_path[-2] != conf["service_id"]:
continue
can_edit = True
break
d["can_edit"] = can_edit
magic_file = magic.from_file(path, mime=True)
if (
not is_cache
or magic_file.startswith("text/")
or magic_file.startswith("application/json")
):
with open(path, "rb") as f:
d["content"] = b64encode(f.read()).decode("utf-8")
else:
d.update(
{
config_types = [
"http",
"stream",
"server-http",
"server-stream",
"default-server-http",
"modsec",
"modsec-crs",
]
d = {
"name": "configs",
"type": "folder",
"path": path,
"can_create_files": False,
"can_create_folders": False,
"can_edit": False,
"can_delete": False,
"children": [
{
"name": config,
"type": "folder",
"path": f"{path}/{config}",
"can_create_files": True,
"can_create_folders": True,
"can_edit": False,
"can_delete": False,
"children": [],
}
for config in config_types
],
}
for conf in db_configs:
file_info = {
"name": conf["name"],
"type": "file",
"path": path,
"path": f"{path}/{conf['type'].replace('_', '-')}{'/' + conf['service_id'] if conf['service_id'] else ''}/{conf['name']}.conf",
"can_edit": conf["method"] == "ui",
"can_download": is_cache,
"content": b64encode(conf["data"]).decode("utf-8"),
}
)
can_edit = False
if level > 1 and not is_cache:
exploded_path = path.split("/")
for conf in db_configs:
if exploded_path[-1].replace(".conf", "") == conf["name"]:
if level > 2 and exploded_path[-2] != conf["service_id"]:
continue
can_edit = True
break
d["can_edit"] = can_edit
magic_file = magic.from_file(path, mime=True)
if (
not is_cache
or magic_file.startswith("text/")
or magic_file.startswith("application/json")
):
with open(path, "rb") as f:
d["content"] = b64encode(f.read()).decode("utf-8")
if (
d["children"][config_types.index(conf["type"].replace("_", "-"))][
"children"
]
and conf["service_id"]
and conf["service_id"]
in [
x["name"]
for x in d["children"][
config_types.index(conf["type"].replace("_", "-"))
]["children"]
]
):
d["children"][config_types.index(conf["type"].replace("_", "-"))][
"children"
][
[
x["name"]
for x in d["children"][
config_types.index(conf["type"].replace("_", "-"))
]["children"]
].index(conf["service_id"])
][
"children"
].append(
file_info
)
else:
d["children"][config_types.index(conf["type"].replace("_", "-"))][
"children"
].append(
{
"name": conf["service_id"],
"type": "folder",
"path": f"{path}/{conf['type']}/{conf['service_id']}",
"can_create_files": True,
"can_create_folders": False,
"can_edit": True,
"can_delete": True,
"children": [file_info],
}
if conf["service_id"]
else file_info
)
return d