From fe1b3d147eb08aed8423ab77ab801c9d534717cc Mon Sep 17 00:00:00 2001 From: Manuel Gutierrez <1380243+xr09@users.noreply.github.com> Date: Thu, 22 Oct 2020 14:32:18 +0200 Subject: [PATCH] Add support to create custom API users --- wazuh-opendistro/Dockerfile | 5 +- wazuh-opendistro/config/create_user.py | 59 +++++++++++++++++++ .../config/etc/cont-init.d/2-manager | 18 ++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 wazuh-opendistro/config/create_user.py diff --git a/wazuh-opendistro/Dockerfile b/wazuh-opendistro/Dockerfile index 6df889e6..06f3d99f 100644 --- a/wazuh-opendistro/Dockerfile +++ b/wazuh-opendistro/Dockerfile @@ -6,10 +6,6 @@ ARG WAZUH_VERSION=4.0.0-1 ARG TEMPLATE_VERSION="master" ARG WAZUH_FILEBEAT_MODULE="wazuh-filebeat-0.2.tar.gz" -ENV API_USER="foo" \ - API_PASS="bar" - - # Set repositories. RUN rpm --import https://packages.wazuh.com/key/GPG-KEY-WAZUH @@ -40,6 +36,7 @@ ADD https://raw.githubusercontent.com/wazuh/wazuh/$TEMPLATE_VERSION/extensions/e RUN chmod go-w /etc/filebeat/wazuh-template.json COPY config/etc/ /etc/ +COPY --chown=root:ossec config/create_user.py /var/ossec/framework/scripts/create_user.py # Prepare permanent data # Sync calls are due to https://github.com/docker/docker/issues/9547 diff --git a/wazuh-opendistro/config/create_user.py b/wazuh-opendistro/config/create_user.py new file mode 100644 index 00000000..c7ea3efe --- /dev/null +++ b/wazuh-opendistro/config/create_user.py @@ -0,0 +1,59 @@ +import logging +import sys +import json +import os + +# Set framework path +sys.path.append(os.path.dirname(sys.argv[0]) + "/../framework") + +USER_FILE_PATH = "/var/ossec/api/configuration/admin.json" + + +try: + from wazuh.security import ( + create_user, + get_users, + get_roles, + set_user_role, + update_user, + ) +except Exception as e: + logging.error("No module 'wazuh' found.") + sys.exit(1) + + +def read_user_file(path=USER_FILE_PATH): + with open(path) as user_file: + data = json.load(user_file) + return data["username"], data["password"] + + +def db_users(): + users_result = get_users() + return {user["username"]: user["id"] for user in users_result.affected_items} + + +def db_roles(): + roles_result = get_roles() + return {role["name"]: role["id"] for role in roles_result.affected_items} + + +if __name__ == "__main__": + if not os.path.exists(USER_FILE_PATH): + # abort if no user file detected + sys.exit(0) + username, password = read_user_file() + if username not in db_users(): + create_user(username=username, password=password) + users = db_users() + uid = users[username] + roles = db_roles() + rid = roles["administrator"] + set_user_role( + user_id=[ + str(uid), + ], + role_ids=[ + str(rid), + ], + ) diff --git a/wazuh-opendistro/config/etc/cont-init.d/2-manager b/wazuh-opendistro/config/etc/cont-init.d/2-manager index da8cffa8..cbeeec0f 100644 --- a/wazuh-opendistro/config/etc/cont-init.d/2-manager +++ b/wazuh-opendistro/config/etc/cont-init.d/2-manager @@ -75,8 +75,26 @@ function_wazuh_migration(){ fi } +function_create_custom_user() { + if [[ ! -z $API_USERNAME ]] && [[ ! -z $API_PASSWORD ]]; then + cat << EOF > /var/ossec/api/configuration/admin.json +{ + "username": "$API_USERNAME", + "password": "$API_PASSWORD" +} +EOF + + # insert user into API database + /var/ossec/framework/python/bin/python3 /var/ossec/framework/scripts/create_user.py + fi +} + + # Migrate data from /wazuh-migration volume function_wazuh_migration +# create API custom user +function_create_custom_user + # Start Wazuh /var/ossec/bin/ossec-control start