diff --git a/salt/elasticfleet/tools/sbin/so-elastic-fleet-common b/salt/elasticfleet/tools/sbin/so-elastic-fleet-common index 2e5ad084c..9f6c25728 100644 --- a/salt/elasticfleet/tools/sbin/so-elastic-fleet-common +++ b/salt/elasticfleet/tools/sbin/so-elastic-fleet-common @@ -43,6 +43,9 @@ elastic_fleet_require_agent_policy() { echo "$POLICY_JSON" } +# Print the single active enrollment token for POLICY_ID. +# Exit 1: retryable (API failure, invalid response, no active token) +# Exit 2: multiple active tokens - Shouldn't get into this state without manual intervention elastic_fleet_active_enrollment_token() { local POLICY_ID=$1 local RESP TOKEN_COUNT API_KEY @@ -66,7 +69,7 @@ elastic_fleet_active_enrollment_token() { if [ "$TOKEN_COUNT" -gt 1 ]; then echo "Error: Found $TOKEN_COUNT active enrollment tokens for agent policy '$POLICY_ID'; expected exactly one." >&2 - return 1 + return 2 fi API_KEY=$(jq -r --arg pid "$POLICY_ID" '.list[] | select(.policy_id == $pid and .active == true) | .api_key' <<<"$RESP") @@ -189,7 +192,16 @@ elastic_fleet_integration_remove() { NAME=$2 - INTEGRATION_ID=$(/usr/sbin/so-elastic-fleet-agent-policy-view "$AGENT_POLICY" | jq -r '.item.package_policies[] | select(.name=="'"$NAME"'") | .id') + local POLICY_JSON + if ! POLICY_JSON=$(elastic_fleet_require_agent_policy "$AGENT_POLICY"); then + return 1 + fi + + INTEGRATION_ID=$(jq -r --arg n "$NAME" '.item.package_policies[]? | select(.name==$n) | .id' <<<"$POLICY_JSON") + if [ -z "$INTEGRATION_ID" ]; then + echo "Error: Integration '$NAME' was not found in agent policy '$AGENT_POLICY'." >&2 + return 1 + fi JSON_STRING=$( jq -n \ --arg INTEGRATIONID "$INTEGRATION_ID" \ diff --git a/salt/elasticfleet/tools/sbin/so-elastic-fleet-integration-policy-elastic-fleet-server b/salt/elasticfleet/tools/sbin/so-elastic-fleet-integration-policy-elastic-fleet-server index caa684829..1a384b2aa 100644 --- a/salt/elasticfleet/tools/sbin/so-elastic-fleet-integration-policy-elastic-fleet-server +++ b/salt/elasticfleet/tools/sbin/so-elastic-fleet-integration-policy-elastic-fleet-server @@ -7,20 +7,35 @@ . /usr/sbin/so-elastic-fleet-common # Get all the fleet policies -json_output=$(curl -s -K /opt/so/conf/elasticsearch/curl.config -L -X GET "localhost:5601/api/fleet/agent_policies" -H 'kbn-xsrf: true') +if ! json_output=$(fleet_api "agent_policies" -H 'kbn-xsrf: true'); then + echo "Error: Failed to retrieve Fleet agent policies." >&2 + exit 1 +fi + +if ! jq -e '.items' <<<"$json_output" >/dev/null 2>&1; then + echo "Error: Invalid Fleet agent policies response." >&2 + exit 1 +fi # Extract the IDs that start with "FleetServer_" -POLICY=$(echo "$json_output" | jq -r '.items[] | select(.id | startswith("FleetServer_")) | .id') +POLICY=$(jq -r '.items[] | select(.id | startswith("FleetServer_")) | .id' <<<"$json_output") # Iterate over each ID in the POLICY variable for POLICYNAME in $POLICY; do printf "\nUpdating Policy: $POLICYNAME\n" - # First get the Integration ID - INTEGRATION_ID=$(/usr/sbin/so-elastic-fleet-agent-policy-view "$POLICYNAME" | jq -r '.item.package_policies[] | select(.package.name == "fleet_server") | .id') + if ! POLICY_JSON=$(elastic_fleet_require_agent_policy "$POLICYNAME"); then + exit 1 + fi + + INTEGRATION_ID=$(jq -r '.item.package_policies[]? | select(.package.name == "fleet_server") | .id' <<<"$POLICY_JSON") + if [ -z "$INTEGRATION_ID" ]; then + echo "Error: fleet_server integration was not found in agent policy '$POLICYNAME'." >&2 + exit 1 + fi # Modify the default integration policy to update the policy_id and an with the correct naming - UPDATED_INTEGRATION_POLICY=$(jq --arg policy_id "$POLICYNAME" --arg name "fleet_server-$POLICYNAME" ' + UPDATED_INTEGRATION_POLICY=$(jq --arg policy_id "$POLICYNAME" --arg name "fleet_server-$POLICYNAME" ' .policy_id = $policy_id | .name = $name' /opt/so/conf/elastic-fleet/integrations/fleet-server/fleet-server.json) diff --git a/salt/elasticfleet/tools/sbin_jinja/so-elastic-agent-gen-installers b/salt/elasticfleet/tools/sbin_jinja/so-elastic-agent-gen-installers index 344f2bdc7..c457c6dc6 100755 --- a/salt/elasticfleet/tools/sbin_jinja/so-elastic-agent-gen-installers +++ b/salt/elasticfleet/tools/sbin_jinja/so-elastic-agent-gen-installers @@ -23,8 +23,15 @@ NUM_RUNNING=$(pgrep -cf "/bin/bash /sbin/so-elastic-agent-gen-installers") for i in {1..30} do ENROLLMENTOKEN=$(elastic_fleet_active_enrollment_token "endpoints-initial") + TOKEN_RC=$? + if [ "$TOKEN_RC" -eq 2 ]; then + exit 1 + fi FLEETHOST=$(curl -K /opt/so/conf/elasticsearch/curl.config 'http://localhost:5601/api/fleet/fleet_server_hosts/grid-default' | jq -r '.item.host_urls[]' | paste -sd ',') -if [[ $FLEETHOST ]] && [[ $ENROLLMENTOKEN ]]; then break; else sleep 10; fi + if [[ -n "$FLEETHOST" ]] && [[ -n "$ENROLLMENTOKEN" ]]; then + break + fi + sleep 10 done if [[ -z "$FLEETHOST" ]] || [[ -z "$ENROLLMENTOKEN" ]]; then