Improve wazuh agent registration with retry logic to wait for manager to become ready

This commit is contained in:
Jason Ertel
2020-11-24 13:53:16 -05:00
parent 0c4ee94472
commit ea9bbfd1aa
2 changed files with 52 additions and 21 deletions

View File

@@ -57,32 +57,56 @@ register_agent() {
echo "Adding agent:" echo "Adding agent:"
echo "Executing: curl -s -u $USER:**** -k -X POST -d 'name=$AGENT_NAME&ip=$AGENT_IP' $PROTOCOL://$API_IP:$API_PORT/agents" echo "Executing: curl -s -u $USER:**** -k -X POST -d 'name=$AGENT_NAME&ip=$AGENT_IP' $PROTOCOL://$API_IP:$API_PORT/agents"
API_RESULT=$(curl -s -u $USER:"$PASSWORD" -k -X POST -d 'name='$AGENT_NAME'&ip='$AGENT_IP -L $PROTOCOL://$API_IP:$API_PORT/agents) API_RESULT=$(curl -s -u $USER:"$PASSWORD" -k -X POST -d 'name='$AGENT_NAME'&ip='$AGENT_IP -L $PROTOCOL://$API_IP:$API_PORT/agents)
echo "Result: $API_RESULT" # Get agent id and key
echo -e $API_RESULT | grep -q "\"error\":0" 2>&1 AGENT_ID=$(echo "$API_RESULT" | jq -er ".data.id")
GOT_ID=$?
AGENT_KEY=$(echo "$API_RESULT" | jq -er ".data.key")
GOT_KEY=$?
if [ "$?" != "0" ]; then if [[ -z "$AGENT_ID" || -z "$AGENT_KEY" || $GOT_ID -ne 0 || $GOT_KEY -ne 0 ]]; then
echo -e $API_RESULT | sed -rn 's/.*"message":"(.+)".*/\1/p' echo "Failed Result: $API_RESULT"
return 1
else else
# Get agent id and agent key
AGENT_ID=$(echo $API_RESULT | cut -d':' -f 4 | cut -d ',' -f 1)
AGENT_KEY=$(echo $API_RESULT | cut -d':' -f 5 | cut -d '}' -f 1)
echo "Agent '$AGENT_NAME' with ID '$AGENT_ID' added." echo "Agent '$AGENT_NAME' with ID '$AGENT_ID' added."
echo "Key for agent '$AGENT_ID' received." echo "Key for agent '$AGENT_ID' received."
# Importing key # Importing key
echo "" echo ""
echo "Importing authentication key:" echo "Importing authentication key:"
echo "y" | /var/ossec/bin/manage_agents -i $AGENT_KEY echo "y" | /var/ossec/bin/manage_agents -i '$AGENT_KEY'
# Restarting agent # Restarting agent
echo "" echo ""
echo "Restarting:" echo "Restarting:"
echo "" echo ""
/var/ossec/bin/ossec-control restart /var/ossec/bin/ossec-control restart
return 0
fi fi
} }
wait_for_manager() {
echo "Waiting for Wazuh manager to become ready..."
maxAttempts=$1
attempts=0
while [[ $attempts -lt $maxAttempts ]]; do
attempts=$((attempts+1))
AGENTS_OUTPUT=$(curl -s -u $USER:"$PASSWORD" -k -X GET -L $PROTOCOL://$API_IP:$API_PORT/agents)
MANAGER_STATUS=$(echo "$AGENTS_OUTPUT" | jq -r ".data.items[0].status")
if [ "$MANAGER_STATUS" == "Active" ]; then
echo "Wazuh manager is active, ready to proceed."
return 0
else
echo "Received non-Active status response: "
echo "$AGENTS_OUTPUT"
echo
echo "Manager is not ready after attempt $attempts of $maxAttempts, sleeping for 30 seconds."
sleep 30
fi
done
return 1
}
remove_agent() { remove_agent() {
echo "Found: $AGENT_ID" echo "Found: $AGENT_ID"
echo "Removing previous registration for '$AGENT_NAME' using ID: $AGENT_ID ..." echo "Removing previous registration for '$AGENT_NAME' using ID: $AGENT_ID ..."
@@ -141,11 +165,18 @@ if [ -f /opt/so/conf/wazuh/initial_agent_registration.log ]; then
echo "Agent $AGENT_ID already registered!" echo "Agent $AGENT_ID already registered!"
exit 0 exit 0
else else
echo "Waiting before registering agent..." retries=30
sleep 30s if wait_for_manager $retries; then
register_agent if register_agent; then
cleanup_creds cleanup_creds
echo "Initial agent $AGENT_ID with IP $AGENT_IP registered on $DATE." > /opt/so/conf/wazuh/initial_agent_registration.log echo "Initial agent $AGENT_ID with IP $AGENT_IP registered on $DATE." > /opt/so/conf/wazuh/initial_agent_registration.log
exit 0 exit 0
else
echo "ERROR: Failed to register agent"
fi
else
echo "ERROR: Wazuh manager did not become ready after $retries attempts; unable to proceed with registration"
fi
fi fi
#remove_agent
exit 1

View File

@@ -71,7 +71,7 @@ wazuhagentconf:
wazuhdir: wazuhdir:
file.directory: file.directory:
- name: /nsm/wazuh - name: /nsm/wazuh/etc
- user: 945 - user: 945
- group: 945 - group: 945
- makedirs: True - makedirs: True
@@ -115,6 +115,10 @@ append_so-wazuh_so-status.conf:
- name: /opt/so/conf/so-status/so-status.conf - name: /opt/so/conf/so-status/so-status.conf
- text: so-wazuh - text: so-wazuh
/opt/so/conf/wazuh:
file.symlink:
- target: /nsm/wazuh/etc
# Register the agent # Register the agent
registertheagent: registertheagent:
cmd.run: cmd.run:
@@ -128,10 +132,6 @@ whitelistmanager:
- name: /usr/sbin/wazuh-manager-whitelist - name: /usr/sbin/wazuh-manager-whitelist
- cwd: / - cwd: /
/opt/so/conf/wazuh:
file.symlink:
- target: /nsm/wazuh/etc
wazuhagentservice: wazuhagentservice:
service.running: service.running:
- name: wazuh-agent - name: wazuh-agent