mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2026-12-02 08:36:08 +01:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f9981ebf9 | ||
|
|
399219b735 | ||
|
|
180a4814f9 |
No files matched your search
@@ -11,7 +11,8 @@
|
||||
{% set DATA_RETENTION_METHOD = ELASTICSEARCHMERGED.data_retention_method %}
|
||||
|
||||
{% set PILLAR_GLOBAL_OVERRIDES = {} %}
|
||||
{% set ES_INDEX_PILLAR = salt['pillar.get']('elasticsearch:index_settings', {}) %}
|
||||
{# merge into a copy: pillar.get returns a live reference and this map pops/updates ES_INDEX_PILLAR #}
|
||||
{% set ES_INDEX_PILLAR = salt['defaults.merge'](salt['pillar.get']('elasticsearch:index_settings', {}), {}, in_place=False) %}
|
||||
{% if ES_INDEX_PILLAR.global_overrides is defined %}
|
||||
{% set PILLAR_GLOBAL_OVERRIDES = ES_INDEX_PILLAR.pop('global_overrides') %}
|
||||
{% endif %}
|
||||
|
||||
@@ -17,7 +17,6 @@ is older than debounce_seconds, this script:
|
||||
* concatenates the actions lists from every ready intent
|
||||
* dedupes by (state or __highstate__, tgt, tgt_type)
|
||||
* drops actions whose target matches no accepted minion (e.g. a role with no nodes yet)
|
||||
* holds everything for a later pass while salt-master has only just started
|
||||
* dispatches a single `salt-run state.orchestrate orch.push_batch --async`
|
||||
with the deduped actions list passed as pillar kwargs
|
||||
* deletes the contributed intent files on successful dispatch
|
||||
@@ -47,12 +46,6 @@ PENDING_DIR = '/opt/so/state/push_pending'
|
||||
LOCK_FILE = os.path.join(PENDING_DIR, '.lock')
|
||||
LOG_FILE = '/opt/so/log/salt/so-push-drainer.log'
|
||||
MASTER_CONFIG = '/etc/salt/master'
|
||||
MASTER_PIDFILE = '/var/run/salt-master.pid'
|
||||
|
||||
# Minions rejoin a restarted master's publisher up to ~22s after it returns (zmq reconnect interval
|
||||
# plus jitter, each up to recon_default + recon_max) and a job published before then reaches nobody.
|
||||
# Doubled to allow for a slow master start.
|
||||
MASTER_SETTLE_SECONDS = 45
|
||||
|
||||
DISPATCHED_DIR = '/opt/so/state/push_dispatched'
|
||||
|
||||
@@ -147,14 +140,6 @@ def _targeted(actions, log):
|
||||
return targeted
|
||||
|
||||
|
||||
def _master_settling(now):
|
||||
try:
|
||||
age = now - os.path.getmtime(MASTER_PIDFILE)
|
||||
except OSError:
|
||||
return False
|
||||
return 0 <= age < MASTER_SETTLE_SECONDS
|
||||
|
||||
|
||||
def _dispatch(actions, log):
|
||||
pillar_arg = json.dumps({'actions': actions})
|
||||
cmd = [
|
||||
@@ -425,10 +410,6 @@ def main():
|
||||
log.debug('no ready intents (%d still in debounce window)', skipped)
|
||||
return 0
|
||||
|
||||
if _master_settling(now):
|
||||
log.info('salt-master just started; holding %d intent(s) until minions reconnect', len(ready))
|
||||
return 0
|
||||
|
||||
combined_actions = []
|
||||
oldest_first_touch = now
|
||||
all_paths = []
|
||||
|
||||
@@ -199,17 +199,6 @@ class TestHelpers(DrainerTestCase):
|
||||
drainer._unlink(self.tmpdir, self.log)
|
||||
self.log.exception.assert_called_once()
|
||||
|
||||
def test_master_settling(self):
|
||||
pidfile = os.path.join(self.tmpdir, 'salt-master.pid')
|
||||
now = time.time()
|
||||
with patch.object(drainer, 'MASTER_PIDFILE', pidfile):
|
||||
self.assertFalse(drainer._master_settling(now))
|
||||
self.write_json(self.tmpdir, 'salt-master.pid', '1100')
|
||||
for age, settling in ((0, True), (drainer.MASTER_SETTLE_SECONDS - 1, True),
|
||||
(drainer.MASTER_SETTLE_SECONDS, False), (-60, False)):
|
||||
os.utime(pidfile, (now - age, now - age))
|
||||
self.assertIs(drainer._master_settling(now), settling, age)
|
||||
|
||||
|
||||
class TestDispatch(DrainerTestCase):
|
||||
|
||||
@@ -486,7 +475,6 @@ class TestMain(DrainerTestCase):
|
||||
('_load_push_cfg', {'side_effect': lambda: self.cfg}),
|
||||
('_check_dispatched', {}),
|
||||
('_targeted', {'side_effect': lambda actions, log: actions}),
|
||||
('_master_settling', {'return_value': False}),
|
||||
):
|
||||
patcher = patch.object(drainer, name, **kwargs)
|
||||
setattr(self, name, patcher.start())
|
||||
@@ -540,16 +528,6 @@ class TestMain(DrainerTestCase):
|
||||
with patch.object(drainer.os, 'unlink', side_effect=OSError('busy')):
|
||||
self.assertEqual(drainer.main(), 0)
|
||||
|
||||
def test_holds_while_master_settling(self):
|
||||
path = self.intent('pillar_soc.json')
|
||||
self._master_settling.return_value = True
|
||||
with patch.object(drainer, '_dispatch') as dispatch:
|
||||
self.assertEqual(drainer.main(), 0)
|
||||
dispatch.assert_not_called()
|
||||
self._check_dispatched.assert_called_once()
|
||||
self.assertTrue(os.path.exists(path))
|
||||
self.assertIn('holding 1 intent(s)', self.logged('info'))
|
||||
|
||||
def test_dispatch_failure_keeps_intents(self):
|
||||
path = self.intent('pillar_soc.json')
|
||||
with patch.object(drainer, '_dispatch', return_value=None):
|
||||
|
||||
@@ -20,6 +20,8 @@ CRITICAL_AGENTS_FILE="/opt/so/conf/agents/critical-agents.txt"
|
||||
OFFLINE_THRESHOLD_HOURS={{ OFFLINE_THRESHOLD_HOURS }}
|
||||
REALERT_THRESHOLD={{ REALERT_THRESHOLD }}
|
||||
PAGE_SIZE="{{ PAGE_SIZE }}"
|
||||
# Safety limit on pagination loops. Very large fleets should raise page_size instead.
|
||||
MAX_PAGES=100
|
||||
|
||||
log_message() {
|
||||
local level="$1"
|
||||
@@ -135,14 +137,16 @@ main() {
|
||||
local page=1
|
||||
local total_agents=0
|
||||
local processed_agents=0
|
||||
local agents_in_page=0
|
||||
local agents=()
|
||||
local current_timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
{%- if CUSTOM_KQUERY != None and CUSTOM_KQUERY | length > 0 %}
|
||||
log_message "INFO" "Using custom kquery: {{ CUSTOM_KQUERY }}"
|
||||
FLEET_QUERY="${FLEET_API}?kuery={{ CUSTOM_KQUERY | urlencode }}&perPage=${PAGE_SIZE}&page=${page}"
|
||||
FLEET_QUERY="${FLEET_API}?kuery={{ CUSTOM_KQUERY | urlencode }}&perPage=${PAGE_SIZE}"
|
||||
{%- else %}
|
||||
log_message "INFO" "Using default query (all offline or degraded agents)"
|
||||
FLEET_QUERY="${FLEET_API}?kuery=status%3Aoffline%20OR%20status%3Adegraded&perPage=${PAGE_SIZE}&page=${page}"
|
||||
FLEET_QUERY="${FLEET_API}?kuery=status%3Aoffline%20OR%20status%3Adegraded&perPage=${PAGE_SIZE}"
|
||||
{%- endif %}
|
||||
|
||||
while true; do
|
||||
@@ -150,27 +154,48 @@ main() {
|
||||
|
||||
if ! response_body=$(curl -K "$CURL_CONFIG" \
|
||||
-s --fail \
|
||||
"$FLEET_QUERY" \
|
||||
"${FLEET_QUERY}&page=${page}" \
|
||||
-H 'kbn-xsrf: true' 2>/dev/null); then
|
||||
log_message "ERROR" "Failed to query Fleet API (page $page)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# pagination info
|
||||
current_total=$(echo "$response_body" | jq -r '.total // 0')
|
||||
current_page=$(echo "$response_body" | jq -r '.page // 1')
|
||||
agents_in_page=$(echo "$response_body" | jq -r '.list | length')
|
||||
# Validate the response before parsing it so a bad response can never be
|
||||
# mistaken for an empty page.
|
||||
if ! echo "$response_body" | jq -e 'type == "object"' >/dev/null 2>&1; then
|
||||
log_message "ERROR" "Malformed Fleet API response on page $page: not a valid JSON object"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update total
|
||||
if ! current_total=$(echo "$response_body" | jq -e '.total | select(type == "number" and . >= 0 and . == floor)' 2>/dev/null); then
|
||||
log_message "ERROR" "Malformed Fleet API response on page $page: missing or invalid total"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Fleet 9.x returns agents under "items"; older responses used "list".
|
||||
if ! agents_json=$(echo "$response_body" | jq -c '(.items // .list) | if type == "array" then .[] else error("no agent array") end' 2>/dev/null); then
|
||||
log_message "ERROR" "Malformed Fleet API response on page $page: missing or invalid agent collection (items/list)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Use the latest page's total: agents changing status between pages
|
||||
# (e.g. coming back online) shrink the offline/degraded result set.
|
||||
total_agents="$current_total"
|
||||
if [ "$page" -eq 1 ]; then
|
||||
total_agents="$current_total"
|
||||
log_message "INFO" "Found $total_agents total agents across all pages"
|
||||
fi
|
||||
|
||||
log_message "INFO" "Processing page $current_page with $agents_in_page agents"
|
||||
|
||||
# Process agents from current page
|
||||
mapfile -t agents < <(echo "$response_body" | jq -c '.list[]')
|
||||
agents=()
|
||||
agents_in_page=0
|
||||
if [ -n "$agents_json" ]; then
|
||||
while IFS= read -r agent; do
|
||||
agents+=("$agent")
|
||||
agents_in_page=$((agents_in_page + 1))
|
||||
done <<< "$agents_json"
|
||||
fi
|
||||
|
||||
log_message "INFO" "Processing page $page with $agents_in_page agents"
|
||||
|
||||
for agent in "${agents[@]}"; do
|
||||
# Grab agent details
|
||||
@@ -234,20 +259,28 @@ main() {
|
||||
|
||||
processed_agents=$((processed_agents + agents_in_page))
|
||||
|
||||
if [ "$agents_in_page" -eq 0 ] || [ "$processed_agents" -ge "$total_agents" ]; then
|
||||
log_message "INFO" "Completed processing all pages. Total processed: $processed_agents agents"
|
||||
if [ "$processed_agents" -ge "$total_agents" ]; then
|
||||
break
|
||||
fi
|
||||
|
||||
if [ "$agents_in_page" -eq 0 ]; then
|
||||
log_message "ERROR" "Incomplete agent scan: page $page was empty after $processed_agents of $total_agents agents"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
page=$((page + 1))
|
||||
|
||||
# Limit pagination loops incase of any issues. If agent count is high enough increase page_size in SOC manager.agent_monitoring.config.page_size
|
||||
if [ "$page" -gt 100 ]; then
|
||||
log_message "ERROR" "Reached maximum page limit (100). Issue with script or extremely large fleet deployment. Consider increasing page_size in SOC -> manager.agent_monitoring.config.page_size"
|
||||
break
|
||||
if [ "$page" -gt "$MAX_PAGES" ]; then
|
||||
log_message "ERROR" "Reached maximum page limit ($MAX_PAGES) after $processed_agents of $total_agents agents. Issue with script or extremely large fleet deployment. Consider increasing page_size under Administration -> Configuration -> manager -> agent_monitoring -> config -> page_size (enable 'Show advanced settings')."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$total_agents" -gt 0 ] && [ "$processed_agents" -eq 0 ]; then
|
||||
log_message "ERROR" "Incomplete agent scan: Fleet reported $total_agents agents but none were examined"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_message "INFO" "Fleet agent status check completed. Processed $processed_agents out of $total_agents agents"
|
||||
}
|
||||
|
||||
|
||||
@@ -4,19 +4,8 @@
|
||||
{% set BATCH_WAIT = AUTOAPPLY.batch_wait %}
|
||||
|
||||
{# queue must be a top-level salt.state arg (kwarg is ignored); an int is max_queue and still fails on conflict #}
|
||||
{# refresh_pillar doubles as the reachability check: minions take up to ~22s to rejoin a restarted master's
|
||||
publisher, and a job published before then reaches nobody. Each failed attempt takes ~15s to time out. #}
|
||||
|
||||
{% for action in actions %}
|
||||
refresh_pillar_{{ loop.index }}:
|
||||
salt.function:
|
||||
- name: saltutil.refresh_pillar
|
||||
- tgt: '{{ action.tgt }}'
|
||||
- tgt_type: {{ action.get('tgt_type', 'compound') }}
|
||||
- retry:
|
||||
attempts: 3
|
||||
interval: 10
|
||||
|
||||
{% if action.get('highstate') %}
|
||||
apply_highstate_{{ loop.index }}:
|
||||
salt.state:
|
||||
@@ -26,9 +15,13 @@ apply_highstate_{{ loop.index }}:
|
||||
- batch: {{ action.get('batch', BATCH) }}
|
||||
- batch_wait: {{ action.get('batch_wait', BATCH_WAIT) }}
|
||||
- queue: True
|
||||
- require:
|
||||
- salt: refresh_pillar_{{ loop.index }}
|
||||
{% else %}
|
||||
refresh_pillar_{{ loop.index }}:
|
||||
salt.function:
|
||||
- name: saltutil.refresh_pillar
|
||||
- tgt: '{{ action.tgt }}'
|
||||
- tgt_type: {{ action.get('tgt_type', 'compound') }}
|
||||
|
||||
apply_{{ action.state | replace('.', '_') }}_{{ loop.index }}:
|
||||
salt.state:
|
||||
- tgt: '{{ action.tgt }}'
|
||||
|
||||
Reference in new issue
Block a user