- .github
- ansible
- anthias_app
- anthias_django
- api
- bin
- docker
-
docs
- d2
-
images
- balena-deployment-01-dashboard.png
- balena-deployment-02-create-fleet.png
- balena-deployment-03-fleet-summary-page.png
- balena-deployment-04-fleet-config-page.png
- balena-deployment-05-term-deployment-successful.png
- balena-deployment-06-fleet-releases-page.png
- balena-deployment-07-add-device.png
- balena-deployment-08-etcher.png
- balena-deployment-09-device-list.png
- balena-deployment-10-downloading-images.png
- imager-01.png
- imager-02.png
- imager-03.png
- install-anthias.gif
- nmtui-01.png
- nmtui-02.png
- nmtui-03.png
- nmtui-04.png
- README.md
- balena-fleet-deployment.md
- developer-documentation.md
- installation-options.md
- migrating-assets-to-screenly.md
- qa-checklist.md
- wifi-setup.md
- x86-installation.md
- lib
- requirements
-
static
- coffee
- css
-
favicons
- apple-touch-icon-114x114.png
- apple-touch-icon-120x120.png
- apple-touch-icon-144x144.png
- apple-touch-icon-152x152.png
- apple-touch-icon-57x57.png
- apple-touch-icon-60x60.png
- apple-touch-icon-72x72.png
- apple-touch-icon-76x76.png
- favicon-128.png
- favicon-16x16.png
- favicon-196x196.png
- favicon-32x32.png
- favicon-96x96.png
- favicon.ico
- mstile-144x144.png
- mstile-150x150.png
- mstile-310x150.png
- mstile-310x310.png
- mstile-70x70.png
- fontawesome
- img
- js
- sass
- spec
- templates
- tests
- tools
- website
- webview
- .dockerignore
- .flake8
- .gitignore
- LICENSE
- README.md
- balena.yml
- celery_tasks.py
- docker-compose.balena.dev.yml.tmpl
- docker-compose.balena.yml.tmpl
- docker-compose.dev.yml
- docker-compose.test.yml
- docker-compose.yml.tmpl
- host_agent.py
- manage.py
- package-lock.json
- package.json
- poetry.lock
- pyproject.toml
- run_gunicorn.py
- send_zmq_message.py
- settings.py
- start_wifi_connect_service.sh
- viewer.py
- webpack.common.js
- webpack.dev.js
- webpack.prod.js
- websocket_server_layer.py
Explanation
The host_agent.py
script is designed to handle remote commands sent to a host machine via Redis.
Core Functionality
- IP Address Management:
get_ip_addresses()
: Retrieves all IP addresses associated with network interfaces on the host. It filters out link-local addresses and those not starting with supported interface prefixes (wlan
,eth
,wlp
,enp
).set_ip_addresses()
: Stores the fetched IP addresses as a JSON string in a Redis key namedip_addresses
.
- Command Handling:
CMD_TO_ARGV
: A dictionary mapping command names (as byte strings) to corresponding command arguments. The supported commands are:reboot
: Executes thesystemctl reboot
command withsudo
.shutdown
: Executes thesystemctl poweroff
command withsudo
.set_ip_addresses
: Calls theset_ip_addresses
function to update the Redis key.execute_host_command(cmd_name)
: Handles execution of commands based on their names. It uses theCMD_TO_ARGV
mapping to retrieve arguments, logs command execution details, and takes precautions against potential risks (e.g., checking for testing environment).
- Redis Subscription:
subscriber_loop()
: Connects to a Redis instance (defaults to127.0.0.1:6379, db=0
), subscribes to thehostcmd
channel, and listens for incoming messages.process_message(message)
: Parses incoming messages. If the message type is ‘message’ and its channel ishostcmd
, it extracts the command data and executes the corresponding host command.
Security Considerations
- The script implements a command whitelist (
CMD_TO_ARGV
) to prevent unauthorized actions. - There’s an
os.getenv('TESTING')
check to prevent execution of sensitive commands in a testing environment.
Overall Structure
The script follows a straightforward structure:
- Defines helper functions for retrieving IP addresses and executing commands.
- Establishes a Redis connection and subscribes to the
hostcmd
channel. - Enters a loop to continuously listen for messages and process them accordingly.
Potential Improvements
- Error Handling: Could be enhanced by adding more robust error handling for both network and command execution failures.
- Command Validation: Adding validation checks for command arguments (e.g., data types, ranges) could increase security and robustness.
- Redis Key Management: Consider using a more specific key prefix for the
ip_addresses
key to avoid potential conflicts. - Security Auditing: Logging attempted commands and execution outcomes could provide valuable security auditing capabilities.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph