Skip to content

Add Default Container Environment Variables + Wazuh Agent Installation#250

Merged
runleveldev merged 10 commits intomainfrom
cmyers_wazuh-int
Mar 23, 2026
Merged

Add Default Container Environment Variables + Wazuh Agent Installation#250
runleveldev merged 10 commits intomainfrom
cmyers_wazuh-int

Conversation

@cmyers-mieweb
Copy link
Collaborator

This pull request introduces a new system for managing default environment variables for containers, with a focus on supporting Wazuh agent enrollment out-of-the-box. It adds a UI for editing default environment variables, updates the backend and database schema to store them as structured data, and seeds initial Wazuh-related variables. Additionally, it integrates a first-boot Wazuh agent enrollment service into the base container image.

Default Container Environment Variables Management:

  • Adds a new UI section in settings/index.ejs for managing default environment variables, allowing users to add, edit, and remove variables with descriptions. These are injected into every new container. [1] [2]
  • Updates backend logic in settings.js to support storing and retrieving these variables as an array of {key, value, description} objects, with migration support for the old flat-object format. [1] [2] [3]
  • Changes the Settings.value database column type from STRING to TEXT to accommodate larger/structured data and updates the model accordingly. [1] [2]

Wazuh Agent Enrollment Integration:

  • Adds a database seeder to pre-populate default_container_env_vars with Wazuh agent variables (WAZUH_MANAGER, WAZUH_REGISTRATION_PASSWORD), including migration logic to merge with existing values.
  • Integrates a first-boot Wazuh agent enrollment system into the base container image:
    • Adds wazuh-enroll.sh script and wazuh-enroll.service systemd unit, which automatically enrolls the agent if the relevant environment variables are set, and securely cleans up credentials after enrollment. [1] [2] [3]

These changes make it easier to centrally manage environment variables for containers and enable secure, automated Wazuh agent enrollment for improved monitoring and security.

Introduce Wazuh agent first-boot enrollment and system-wide default container environment variables.

- create-a-container: Use Setting in container creation to load a JSON 'default_container_env_vars' and inject WAZUH_MANAGER (hostname from wazuh_api_url) and WAZUH_REGISTRATION_PASSWORD (if set). Merge priority: image defaults < system defaults < per-container values.
- UI: Add settings UI to manage default container env vars and Wazuh settings (API URL + enrollment password). Password is not echoed back; only an indicator is shown. Validate Wazuh API URL and serialize env var entries before saving to settings.
- Server: Expose wazuh_api_url to authenticated views so a sidebar link appears when configured.
- Images: Update base image Dockerfile to include and enable wazuh-enroll.service and wazuh-enroll.sh.
- New files: Add systemd unit (wazuh-enroll.service) and enrollment script (wazuh-enroll.sh) which installs the Wazuh agent, uses /etc/machine-id as agent name, optionally writes and then purges the enrollment password, waits for enrollment, and writes a sentinel to avoid rerunning.

These changes allow the cluster manager to automatically provide Wazuh connection details to new containers and offer a management UI for defaults and enrollment credentials.
Switch default_container_env_vars to an array of {key,value,description} while still supporting the old flat-object {KEY: value} format. create-container now parses either format (ignoring descriptions when injecting container envs), updates log messaging, and removes automatic Wazuh API/password injection. The settings router/read/save flow was updated to persist an array, convert legacy objects to the new shape, and build the saved entries from the form. The settings view adds a Description column and UI inputs for descriptions and removes the Wazuh integration form and header link. server.js no longer exposes the Wazuh URL middleware. A new seeder adds WAZUH_MANAGER and WAZUH_REGISTRATION_PASSWORD to default_container_env_vars if missing.
Update the Settings model to use DataTypes.TEXT for the value field and add a Sequelize migration (create-a-container/migrations/20260320000000-change-settings-value-to-text.js) to alter the existing column to TEXT. The migration includes an up (to TEXT) and down (back to STRING) to allow storing longer setting values while preserving rollback capability.
Centralize parsing of default_container_env_vars by adding Setting.getDefaultContainerEnvVars() and updating callers in create-container and the settings router to use it (handles both current array and legacy flat-object formats).

Pre-install the wazuh-agent in the base image so containers don't need network access on first boot, and simplify enrollment: adjust wazuh-enroll.service to trigger only when the agent has never enrolled (checks client.keys), add EnvironmentFile support, and remove build-time service enablement. Rewrite wazuh-enroll.sh to write /var/ossec/etc/ossec.conf at runtime, start the agent, and wait for client.keys (with a warning on timeout); remove runtime package installation and post-enrollment cleanup/ sentinel creation. These changes reduce runtime network dependencies and centralize env-var handling.
Make the first-boot enrollment more reliable and ensure the enrollment helper is executable. Changes:

- Dockerfile: update comment to reflect using ConditionFileNotEmpty and COPY the enroll script with --chmod=0755 so it is executable in the image.
- systemd unit: replace ConditionPathExists with ConditionFileNotEmpty (negated) so the service runs when /var/ossec/etc/client.keys is missing or empty — this avoids skipping re-enrollment after a failed attempt because the agent may create client.keys even on failure.
- enroll script: write an <enrollment><agent_name> entry into ossec.conf and use systemctl restart wazuh-agent (instead of start) so the agent picks up the updated configuration even if a stale instance is running.

These changes ensure re-enrollment can occur after failed attempts and that the enrollment script runs with correct permissions.
Copy link
Collaborator

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

LGTM

@runleveldev
Copy link
Collaborator

image

@runleveldev runleveldev merged commit 8463388 into main Mar 23, 2026
9 checks passed
@runleveldev runleveldev deleted the cmyers_wazuh-int branch March 23, 2026 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants