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

Added docker-compose installation. Redis is not included yet. #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Empty file added .env
Empty file.
47 changes: 47 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
FROM ubuntu:24.04

WORKDIR /opt/loboguara
COPY . .
RUN mkdir -p /opt/loboguara/bin /opt/loboguara/app

# Installing all the necessary APT packages
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install -y python3 python3-pip python3-venv libpq-dev python3-dev redis-server build-essential zip wget curl sudo postgresql postgresql-contrib

# Install the chrome and chromedriver
RUN wget -O /tmp/google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i /tmp/google-chrome.deb || apt-get install -f -y
RUN ln -sf /usr/bin/google-chrome /opt/loboguara/bin/google-chrome

RUN wget -O /tmp/chromedriver.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/129.0.6668.89/linux64/chromedriver-linux64.zip
RUN unzip /tmp/chromedriver.zip -d /tmp/
RUN mv /tmp/chromedriver-linux64/ /opt/loboguara/bin/chromedriver_dir
RUN chmod +x /opt/loboguara/bin/chromedriver_dir/chromedriver
RUN ln -sf /opt/loboguara/bin/chromedriver_dir/chromedriver /opt/loboguara/bin/chromedriver

# Install subfinder
RUN wget -O /tmp/subfinder.zip https://github.com/projectdiscovery/subfinder/releases/download/v2.6.6/subfinder_2.6.6_linux_amd64.zip
RUN unzip /tmp/subfinder.zip -d /tmp/
RUN mv /tmp/subfinder /opt/loboguara/bin/
RUN chmod +x /opt/loboguara/bin/subfinder

# Install FFUF
RUN wget -O /tmp/ffuf.tar.gz https://github.com/ffuf/ffuf/releases/download/v2.0.0/ffuf_2.0.0_linux_amd64.tar.gz
RUN tar -xvzf /tmp/ffuf.tar.gz -C /tmp/
RUN mv /tmp/ffuf /opt/loboguara/bin/
RUN chmod +x /opt/loboguara/bin/ffuf

# Install the application

RUN chmod +x ./run_via_docker.sh

RUN adduser --disabled-password --gecos '' guarauser
RUN adduser guarauser sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
RUN chown guarauser:guarauser -R /opt/loboguara

EXPOSE 7405
USER guarauser

CMD [ "/opt/loboguara/run_via_docker.sh" ]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ The application installation has been approved on Ubuntu 24.04 Server and Red Ha

[Lobo Guará Implementation on Red Hat 9.4](https://loboguara.olivsec.com.br/docs/lobo_guara_installation_manual_on_RedHat_9-4.html)

There is a Dockerfile and a docker-compose version of Lobo Guará too. Just clone the repo and do:

```
docker compose up
```

Then, go to your web browser at localhost:7405.

### Dependencies

Before proceeding with the installation, ensure the following dependencies are installed:
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
postgres:
image: postgres
environment:
POSTGRES_USER: "root"
POSTGRES_PASSWORD: "YOUR_PASSWORD_HERE"
volumes:
- ./loboguara.sql:/docker-entrypoint-initdb.d/loboguara.sql
ports:
- "5432:5432"
loboguara:
build: .
ports:
- "7405:7405"
depends_on:
- postgres
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export FLASK_APP="$INSTALL_DIR/run.py"

echo "Initializing the database..."
cd "$INSTALL_DIR" || error_exit "Failed to access the installation directory!"

hostname
echo "as user $USER - `id`"
flask db init || error_exit "Failed to initialize the database!"
flask db migrate -m "Initial migration." || error_exit "Failed to create migration!"
flask db upgrade || error_exit "Failed to upgrade the database!"
Expand Down
195 changes: 195 additions & 0 deletions install_via_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/bin/bash

CONFIG_FILE="server/app/config.py"
INIT_FILE="/opt/loboguara/app/__init__.py"
INSTALL_DIR="/opt/loboguara"

error_exit() {
echo "$1"
exit 1
}

if [ ! -f "$CONFIG_FILE" ]; then
error_exit "Configuration file $CONFIG_FILE not found!"
fi

echo "Checking variables in the configuration file..."

# need to modify variables referencing services with localhost
sed -i -r "s/(SQLALCHEMY_DATABASE_URI = 'postgresql:\/\/guarauser:YOUR_PASSWORD_HERE@)(localhost)(\/guaradb\?sslmode=disable')/\1postgres\3/" "$CONFIG_FILE"

DB_URI=$(grep -oP 'SQLALCHEMY_DATABASE_URI\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
MAIL_SERVER=$(grep -oP 'MAIL_SERVER\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
MAIL_PORT=$(grep -oP 'MAIL_PORT\s*=\s*\K[0-9]+' "$CONFIG_FILE")
MAIL_USE_TLS=$(grep -oP 'MAIL_USE_TLS\s*=\s*\K(True|False)' "$CONFIG_FILE")
MAIL_USERNAME=$(grep -oP 'MAIL_USERNAME\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
MAIL_PASSWORD=$(grep -oP 'MAIL_PASSWORD\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
MAIL_DEFAULT_SENDER=$(grep -oP 'MAIL_DEFAULT_SENDER\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
API_ACCESS_TOKEN=$(grep -oP 'API_ACCESS_TOKEN\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
API_URL=$(grep -oP 'API_URL\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
CHROME_DRIVER_PATH=$(grep -oP 'CHROME_DRIVER_PATH\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
GOOGLE_CHROME_PATH=$(grep -oP 'GOOGLE_CHROME_PATH\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
FFUF_PATH=$(grep -oP 'FFUF_PATH\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")
SUBFINDER_PATH=$(grep -oP 'SUBFINDER_PATH\s*=\s*'\''\K[^'\'']+' "$CONFIG_FILE")

# SKIP, otherwise the build process will fail
#echo "Checking database connection and permissions..."
#psql -h postgres "$DB_URI" -c "\dt" > /dev/null 2>&1 || error_exit "Failed to connect to the database!"

#EXTENSION=$(psql -h postgres "$DB_URI" -c "SELECT * FROM pg_extension WHERE extname = 'pg_trgm';" | grep pg_trgm)
#if [ -z "$EXTENSION" ]; then
# error_exit "pg_trgm extension not found!"
#fi

echo "Checking email settings..."
if [ -z "$MAIL_SERVER" ] || [ -z "$MAIL_PORT" ] || [ -z "$MAIL_USERNAME" ] || [ -z "$MAIL_PASSWORD" ] || [ -z "$MAIL_DEFAULT_SENDER" ]; then
error_exit "Incomplete email configuration!"
fi

echo "Checking access to the Lobo Guará API..."
API_RESPONSE=$(curl -s -H "x-access-tokens: $API_ACCESS_TOKEN" "$API_URL/verify_token")
if [[ "$API_RESPONSE" != *"Token is valid"* ]]; then
error_exit "Invalid or expired token for the Lobo Guará API!"
fi

echo "Checking binaries..."
for path in "$CHROME_DRIVER_PATH" "$GOOGLE_CHROME_PATH" "$FFUF_PATH" "$SUBFINDER_PATH"; do
if [ ! -x "$path" ]; then
error_exit "Binary $path not found or not executable!"
fi
done

echo "Installing in the directory $INSTALL_DIR..."
sudo mkdir -p "$INSTALL_DIR" || error_exit "Failed to create directory $INSTALL_DIR!"
sudo cp -R server/* "$INSTALL_DIR" || error_exit "Failed to copy files to $INSTALL_DIR!"

echo "Creating virtual environment..."
python3.12 -m venv "$INSTALL_DIR/venv" || error_exit "Failed to create virtual environment!"
source "$INSTALL_DIR/venv/bin/activate"
pip install -r "$INSTALL_DIR/requirements.txt" || error_exit "Failed to install dependencies!"

echo "Compiling realtime.proto..."
python3.12 -m grpc_tools.protoc -I"$INSTALL_DIR" --python_out="$INSTALL_DIR" --grpc_python_out="$INSTALL_DIR" "$INSTALL_DIR/realtime.proto" || error_exit "Failed to compile realtime.proto!"

export FLASK_APP="$INSTALL_DIR/run.py"

echo "Initializing the database..."
cd "$INSTALL_DIR" || error_exit "Failed to access the installation directory!"

flask db init || error_exit "Failed to initialize the database!"
flask db migrate -m "Initial migration." || error_exit "Failed to create migration!"
flask db upgrade || error_exit "Failed to upgrade the database!"

echo "Populating timezones table..."

psql -h postgres "$DB_URI" <<EOF || error_exit "Failed to populate timezones table!"
DO
\$\$
BEGIN
-- Remove duplicate timezone if exists
DELETE FROM timezones WHERE name = '(UTC) Monróvia, Reiquiavique';

-- Insert or update timezones
INSERT INTO timezones (name, pytz_name)
VALUES
('(UTC-12:00) International Date Line West', 'Etc/GMT+12'),
('(UTC-11:00) Coordinated Universal Time-11', 'Etc/GMT+11'),
('(UTC-10:00) Hawaii', 'Pacific/Honolulu'),
('(UTC-09:00) Alaska', 'America/Anchorage'),
('(UTC-08:00) Pacific Time (US & Canada)', 'America/Los_Angeles'),
('(UTC-07:00) Mountain Time (US & Canada)', 'America/Denver'),
('(UTC-06:00) Central Time (US & Canada)', 'America/Chicago'),
('(UTC-05:00) Eastern Time (US & Canada)', 'America/New_York'),
('(UTC-04:00) Atlantic Time (Canada)', 'America/Halifax'),
('(UTC-03:00) Brasília', 'America/Sao_Paulo'),
('(UTC-02:00) Mid-Atlantic', 'Etc/GMT+2'),
('(UTC-01:00) Azores', 'Atlantic/Azores'),
('(UTC+00:00) Monróvia, Reiquiavique', 'UTC'),
('(UTC+01:00) West Central Africa', 'Africa/Lagos'),
('(UTC+02:00) Cairo', 'Africa/Cairo'),
('(UTC+03:00) Moscow, St. Petersburg, Volgograd', 'Europe/Moscow'),
('(UTC+04:00) Abu Dhabi, Muscat', 'Asia/Dubai'),
('(UTC+05:00) Islamabad, Karachi', 'Asia/Karachi'),
('(UTC+06:00) Astana, Dhaka', 'Asia/Dhaka'),
('(UTC+07:00) Bangkok, Hanoi, Jakarta', 'Asia/Bangkok'),
('(UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi', 'Asia/Shanghai'),
('(UTC+09:00) Tokyo, Osaka, Sapporo', 'Asia/Tokyo'),
('(UTC+10:00) Brisbane', 'Australia/Brisbane'),
('(UTC+11:00) Solomon Islands, New Caledonia', 'Pacific/Guadalcanal'),
('(UTC+12:00) Fiji, Marshall Islands', 'Pacific/Fiji')
ON CONFLICT (name) DO UPDATE SET pytz_name = EXCLUDED.pytz_name;
END
\$\$;
EOF


echo "Uncommenting the call to init_timezones() in the __init__.py file..."
sed -i '/# init_timezones()/s/^# //' "$INIT_FILE" || error_exit "Failed to uncomment init_timezones()"

echo "Creating symbolic link to wafw00f..."
sudo ln -sf /opt/loboguara/venv/bin/wafw00f /opt/loboguara/bin/wafw00f

echo "Creating the service user 'loboguara'..."
if id "loboguara" &>/dev/null; then
echo "User 'loboguara' already exists."
else
sudo useradd -r -m -d /home/loboguara -s /bin/false loboguara || error_exit "Failed to create the service user 'loboguara'."
fi

echo "Setting 'loboguara' as the owner of the directory /opt/loboguara/..."
sudo chown -R loboguara:loboguara /opt/loboguara || error_exit "Failed to change ownership of the directory to 'loboguara'."

echo "Adjusting permissions..."
sudo chmod -R 750 /opt/loboguara || error_exit "Failed to adjust permissions."

echo "Adding dashboard update to cron..."
cron_job="*/1 * * * * /opt/loboguara/venv/bin/python /opt/loboguara/update_dashboard_metrics.py > /dev/null 2>&1"

( sudo crontab -u loboguara -l 2>/dev/null | grep -qF "$cron_job" ) || ( sudo crontab -u loboguara -l 2>/dev/null; echo "$cron_job" ) | sudo crontab -u loboguara -

echo "Cron job added to loboguara's crontab."


sudo chmod a+x /opt/loboguara/start.sh

echo "





.
:%# .
=@@@* *+
-#@@@@@= -%@*
-#@@#@@%@%#@@@@+
-#@@#+*@%=@@@@@@@:
:#@@#+=+%@*:%@@@@@*
-#@@@*==+%@%::#%#@@%.
.:-=*%@@@@#=+#@@#-::-=:-*@%=
.-+**###@@*#@@@*-::::::::-%@-
:=+#%@@@@@#=-----+**+-:=@%-
:*%@@@@@@@%*=--------*%@%-:=%@*-
:#@@@@@@@@%*++======++---------+#@%*=:
*@@@@@@@@@@@@@@@@@%*+=-------------+*%@@# ::-:.
-+*+: #@@@@@@@@@@@@@@%*+--------------------=%@* -==---.
+@@@@@+ *@@@@@@@@@@@@%#+-------=++***********#%@%- -====-.
=@@@@@= -@@@@@@@@@@@%*==------=++#@@@**********+: .-*-
.===. #@@%+#@@@@@#=======---=*%@@@@. -@=
:#: @@*.#@@@@@*===========%@@@@@@* =@-
-@= %= +@@@@@+==========+%@@@@#@@@+ +@:
.@* - %@@@@#=*#========%@@@%=*@@@@#: %%
#%. .@@@@@+%@+=======*@@@@=-=@@@@@@*- =@-
:@+ %@@@@%@@========*@@@*---+%@@@@@@%*=:. :@#
+@= *@@@@@@@========*@@@=--+==#@@@@@@@@@@%#*++==-. .%%.
*@- :@@@@@@@*========%@@====%#++*#@@@@@@@@@%*+-: +#.
*@= =@@@*@@%========*@@+===*@@@@%%%@@@@@@@@%+-.
-%* =@@:-@@#==+++===+%#====*@@@@@@@@@@@@@@@@@@%#*+++++*#*.
.. -%. -@@#+++++++++**====+%@@@@@@@@@@@@@@@@@@@@@@@@#-
==+++++++++++++++++++++*+++*@@%*+++++++++++++==+#@@@@@@@@@@@@@@@@@@@@%*+======.
=++++++++++++++++++++++++++++++++--------------::-=+++++++++++++++++++++++++++.

Installation completed successfully!
The application can now only be started by the user 'loboguara'.
"

18 changes: 18 additions & 0 deletions loboguara.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE DATABASE guaradb;
CREATE USER guarauser WITH PASSWORD 'YOUR_PASSWORD_HERE';

GRANT ALL PRIVILEGES ON DATABASE guaradb TO guarauser;
ALTER SCHEMA public OWNER TO guarauser;
GRANT USAGE ON SCHEMA public TO guarauser;
GRANT CREATE ON SCHEMA public TO guarauser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO guarauser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO guarauser;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO guarauser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO guarauser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO guarauser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO guarauser;
CREATE EXTENSION IF NOT EXISTS pg_trgm;

ALTER DATABASE guaradb OWNER TO guarauser;

CREATE EXTENSION IF NOT EXISTS pg_trgm;
11 changes: 11 additions & 0 deletions run_via_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

ALREADY_INSTALLED=".installed"

if [ ! -e "$ALREADY_INSTALLED" ]
then
source /opt/loboguara/install_via_docker.sh
[[ $? -eq 0 ]] && sudo -u loboguara touch "$ALREADY_INSTALLED"
fi

sudo -u loboguara /opt/loboguara/start.sh
2 changes: 1 addition & 1 deletion server/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Config:
API_ACCESS_TOKEN = 'YOUR_LOBOGUARA_API_TOKEN_HERE'
API_URL = 'https://loboguara.olivsec.com.br/api'

# Path to Chrome and ChromeDriver binaries
# Path to Chrome and Chro6. meDriver binaries
CHROME_DRIVER_PATH = '/opt/loboguara/bin/chromedriver'
GOOGLE_CHROME_PATH = '/opt/loboguara/bin/google-chrome'

Expand Down
2 changes: 1 addition & 1 deletion server/start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
source /opt/loboguara/venv/bin/activate
python /opt/loboguara/run.py
python /opt/loboguara/run.py