# Copyright Security Onion Solutions LLC and/or licensed to Security Onion Solutions LLC under one # or more contributor license agreements. Licensed under the Elastic License 2.0; you may not use # this file except in compliance with the Elastic License 2.0. {% from 'vars/globals.map.jinja' import GLOBALS %} . /usr/sbin/so-common # Only run on Managers if ! is_manager_node; then printf "Not a Manager Node... Exiting" exit 0 fi # Function to check if an array contains a value array_contains () { local array="$1[@]" local seeking=$2 local in=1 for element in "${!array}"; do if [[ $element == "$seeking" ]]; then in=0 break fi done return $in } # Query for the current Grid Nodes that are running Logstash (which includes Fleet Nodes) LOGSTASHNODES=$(salt-call --out=json pillar.get logstash:nodes | jq '.local') # Initialize an array for new hosts from Fleet Nodes declare -a NEW_LIST=() # Query for Fleet Nodes & add them to the list (Hostname) if grep -q "fleet" <<< "$LOGSTASHNODES"; then readarray -t FLEETNODES < <(jq -r '.fleet | keys_unsorted[]' <<< "$LOGSTASHNODES") for NODE in "${FLEETNODES[@]}"; do NEW_LIST+=("http://$NODE:8443/artifacts/") done fi # Create an array for expected hosts and their names declare -A expected_hosts=( ["http://{{ GLOBALS.url_base }}:8443/artifacts/"]="FleetServer_{{ GLOBALS.hostname }}" ["https://artifacts.elastic.co/downloads/"]="Elastic Artifacts" ) # Merge NEW_LIST into expected_hosts for host in "${NEW_LIST[@]}"; do expected_hosts[$host]="FleetServer" done # Fetch the current hosts from the API current_hosts=$(curl -K /opt/so/conf/elasticsearch/curl.config 'http://localhost:5601/api/fleet/agent_download_sources' | jq -r .items[].host) # Convert current hosts to an array IFS=$'\n' read -rd '' -a current_hosts_array <<<"$current_hosts" # Check each expected host for host in "${!expected_hosts[@]}"; do array_contains current_hosts_array "$host" || { echo "$host (${expected_hosts[$host]}) is missing. Adding it..." # Prepare the JSON payload JSON_STRING=$( jq -n \ --arg NAME "${expected_hosts[$host]}" \ --arg URL "$host" \ '{"name":$NAME,"host":$URL}' ) # Create the missing host curl -K /opt/so/conf/elasticsearch/curl.config -L -X POST "localhost:5601/api/fleet/agent_download_sources" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d "$JSON_STRING" } done