mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2026-06-15 22:58:42 +02:00
Merge pull request #15810 from Security-Onion-Solutions/feature/postgres
Split postgres auth pillar from per-minion telegraf creds
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
# 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 as shown at
|
||||||
|
# https://securityonion.net/license; you may not use this file except in compliance with the
|
||||||
|
# Elastic License 2.0.
|
||||||
|
|
||||||
|
# Per-minion Telegraf Postgres credentials. so-telegraf-cred on the manager is
|
||||||
|
# the single writer; it mutates /opt/so/saltstack/local/pillar/telegraf/creds.sls
|
||||||
|
# under flock. Pillar_roots order (local before default) means the populated
|
||||||
|
# copy shadows this default on any real grid; this file exists so the pillar
|
||||||
|
# key is always defined on fresh installs and when no minions have creds yet.
|
||||||
|
telegraf:
|
||||||
|
postgres_creds: {}
|
||||||
@@ -17,6 +17,7 @@ base:
|
|||||||
- sensoroni.adv_sensoroni
|
- sensoroni.adv_sensoroni
|
||||||
- telegraf.soc_telegraf
|
- telegraf.soc_telegraf
|
||||||
- telegraf.adv_telegraf
|
- telegraf.adv_telegraf
|
||||||
|
- telegraf.creds
|
||||||
- versionlock.soc_versionlock
|
- versionlock.soc_versionlock
|
||||||
- versionlock.adv_versionlock
|
- versionlock.adv_versionlock
|
||||||
- soc.license
|
- soc.license
|
||||||
|
|||||||
@@ -281,22 +281,18 @@ function deleteMinionFiles () {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove this minion's postgres Telegraf credential from both the aggregate
|
# Remove this minion's postgres Telegraf credential from the shared creds
|
||||||
# pillar and the postgres database. Paired with add_telegraf_to_minion:
|
# pillar and drop the matching role in Postgres. Always returns 0 so a dead
|
||||||
# add/delete cycle both here and in the DB. Always returns 0 so a dead or
|
# or unreachable so-postgres doesn't block minion deletion — in that case we
|
||||||
# unreachable so-postgres doesn't block minion deletion — in that case we
|
|
||||||
# log a warning and leave the role behind for manual cleanup.
|
# log a warning and leave the role behind for manual cleanup.
|
||||||
function remove_postgres_telegraf_from_minion() {
|
function remove_postgres_telegraf_from_minion() {
|
||||||
local MINION_SAFE
|
local MINION_SAFE
|
||||||
MINION_SAFE=$(echo "$MINION_ID" | tr '.-' '__' | tr '[:upper:]' '[:lower:]')
|
MINION_SAFE=$(echo "$MINION_ID" | tr '.-' '__' | tr '[:upper:]' '[:lower:]')
|
||||||
local PG_USER="so_telegraf_${MINION_SAFE}"
|
local PG_USER="so_telegraf_${MINION_SAFE}"
|
||||||
local AGGREGATE=/opt/so/saltstack/local/pillar/postgres/auth.sls
|
|
||||||
|
|
||||||
log "INFO" "Removing postgres telegraf cred for $MINION_ID"
|
log "INFO" "Removing postgres telegraf cred for $MINION_ID"
|
||||||
|
|
||||||
if [[ -f "$AGGREGATE" ]]; then
|
so-telegraf-cred remove "$MINION_ID" >/dev/null 2>&1 || true
|
||||||
so-yaml.py remove "$AGGREGATE" "postgres.auth.users.telegraf_${MINION_SAFE}" >/dev/null 2>&1 || true
|
|
||||||
fi
|
|
||||||
|
|
||||||
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^so-postgres$'; then
|
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^so-postgres$'; then
|
||||||
if ! docker exec -i so-postgres psql -v ON_ERROR_STOP=1 -U postgres -d so_telegraf >/dev/null 2>&1 <<EOSQL
|
if ! docker exec -i so-postgres psql -v ON_ERROR_STOP=1 -U postgres -d so_telegraf >/dev/null 2>&1 <<EOSQL
|
||||||
@@ -311,7 +307,7 @@ END
|
|||||||
\$\$;
|
\$\$;
|
||||||
EOSQL
|
EOSQL
|
||||||
then
|
then
|
||||||
log "WARN" "Failed to drop postgres role $PG_USER; aggregate pillar entry was removed — drop manually if the role persists"
|
log "WARN" "Failed to drop postgres role $PG_USER; pillar entry was removed — drop manually if the role persists"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
log "WARN" "so-postgres container is not running; skipping DB role cleanup for $PG_USER"
|
log "WARN" "so-postgres container is not running; skipping DB role cleanup for $PG_USER"
|
||||||
@@ -580,35 +576,15 @@ function add_telegraf_to_minion() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Provision the per-minion postgres Telegraf credential so telegraf.conf
|
# Provision the per-minion postgres Telegraf credential in the shared
|
||||||
# renders correctly on the minion's first highstate and postgres.telegraf_users
|
# telegraf/creds.sls pillar. so-telegraf-cred is the only writer; it
|
||||||
# picks up the matching aggregate entry on the next manager apply.
|
# generates a password on first add and is a no-op on re-add so the cred
|
||||||
#
|
# is stable across repeated so-minion runs. postgres.telegraf_users on the
|
||||||
# Writes:
|
# manager creates/updates the DB role from the same pillar.
|
||||||
# - postgres.telegraf.{user,pass} into the minion's own pillar file
|
so-telegraf-cred add "$MINION_ID"
|
||||||
# (distributed to only this minion via pillar/top.sls).
|
if [ $? -ne 0 ]; then
|
||||||
# - postgres.auth.users.telegraf_<safe>.{user,pass} into the aggregate
|
log "ERROR" "Failed to provision postgres telegraf cred for $MINION_ID"
|
||||||
# pillar so postgres.telegraf_users CREATE ROLE finds it.
|
return 1
|
||||||
#
|
|
||||||
# An existing password is reused if the aggregate already has one (re-add),
|
|
||||||
# so rerunning so-minion for the same minion keeps the cred stable.
|
|
||||||
local MINION_SAFE
|
|
||||||
MINION_SAFE=$(echo "$MINION_ID" | tr '.-' '__' | tr '[:upper:]' '[:lower:]')
|
|
||||||
local PG_USER="so_telegraf_${MINION_SAFE}"
|
|
||||||
local AGGREGATE=/opt/so/saltstack/local/pillar/postgres/auth.sls
|
|
||||||
local PG_PASS=""
|
|
||||||
if [[ -f "$AGGREGATE" ]]; then
|
|
||||||
PG_PASS=$(so-yaml.py get -r "$AGGREGATE" "postgres.auth.users.telegraf_${MINION_SAFE}.pass" 2>/dev/null || true)
|
|
||||||
fi
|
|
||||||
if [[ -z "$PG_PASS" ]]; then
|
|
||||||
PG_PASS=$(tr -dc 'A-Za-z0-9~!@#^&*()_=+[]|;:,.<>?-' < /dev/urandom | head -c 72)
|
|
||||||
fi
|
|
||||||
|
|
||||||
so-yaml.py replace "$PILLARFILE" postgres.telegraf.user "$PG_USER" >/dev/null
|
|
||||||
so-yaml.py replace "$PILLARFILE" postgres.telegraf.pass "$PG_PASS" >/dev/null
|
|
||||||
if [[ -f "$AGGREGATE" ]]; then
|
|
||||||
so-yaml.py replace "$AGGREGATE" "postgres.auth.users.telegraf_${MINION_SAFE}.user" "$PG_USER" >/dev/null
|
|
||||||
so-yaml.py replace "$AGGREGATE" "postgres.auth.users.telegraf_${MINION_SAFE}.pass" "$PG_PASS" >/dev/null
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 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 as shown at
|
||||||
|
# https://securityonion.net/license; you may not use this file except in compliance with the
|
||||||
|
# Elastic License 2.0.
|
||||||
|
|
||||||
|
# Single writer for the Telegraf Postgres credentials pillar. Thin wrapper
|
||||||
|
# around so-yaml.py that generates a password on first add and no-ops on
|
||||||
|
# re-add so the cred is stable across repeated so-minion runs.
|
||||||
|
#
|
||||||
|
# Note: so-yaml.py splits keys on '.' with no escape. SO minion ids are
|
||||||
|
# dot-free by construction (setup/so-functions:1884 takes the short_name
|
||||||
|
# before the first '.'), so using the raw minion id as the key is safe.
|
||||||
|
|
||||||
|
CREDS=/opt/so/saltstack/local/pillar/telegraf/creds.sls
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
echo "Usage: $0 <add|remove> <minion_id>" >&2
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
seed_creds_file() {
|
||||||
|
mkdir -p "$(dirname "$CREDS")"
|
||||||
|
if [[ ! -f "$CREDS" ]]; then
|
||||||
|
(umask 027 && printf 'telegraf:\n postgres_creds: {}\n' > "$CREDS")
|
||||||
|
chown socore:socore "$CREDS" 2>/dev/null || true
|
||||||
|
chmod 640 "$CREDS"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
OP=$1
|
||||||
|
MID=$2
|
||||||
|
[[ -z "$OP" || -z "$MID" ]] && usage
|
||||||
|
|
||||||
|
case "$OP" in
|
||||||
|
add)
|
||||||
|
SAFE=$(echo "$MID" | tr '.-' '__' | tr '[:upper:]' '[:lower:]')
|
||||||
|
seed_creds_file
|
||||||
|
if so-yaml.py get -r "$CREDS" "telegraf.postgres_creds.${MID}.user" >/dev/null 2>&1; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
PASS=$(tr -dc 'A-Za-z0-9~!@#^&*()_=+[]|;:,.<>?-' < /dev/urandom | head -c 72)
|
||||||
|
so-yaml.py replace "$CREDS" "telegraf.postgres_creds.${MID}.user" "so_telegraf_${SAFE}" >/dev/null
|
||||||
|
so-yaml.py replace "$CREDS" "telegraf.postgres_creds.${MID}.pass" "$PASS" >/dev/null
|
||||||
|
;;
|
||||||
|
remove)
|
||||||
|
[[ -f "$CREDS" ]] || exit 0
|
||||||
|
so-yaml.py remove "$CREDS" "telegraf.postgres_creds.${MID}" >/dev/null 2>&1 || true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
@@ -490,32 +490,16 @@ up_to_3.1.0() {
|
|||||||
post_to_3.1.0() {
|
post_to_3.1.0() {
|
||||||
/usr/sbin/so-kibana-space-defaults
|
/usr/sbin/so-kibana-space-defaults
|
||||||
|
|
||||||
# One-time backfill for minions that existed before the postgres Telegraf
|
# Backfill the Telegraf creds pillar for every accepted minion. so-telegraf-cred
|
||||||
# feature shipped. postgres.auth's up_minions fallback loop generates any
|
# add is idempotent — it no-ops when an entry already exists — so this is safe
|
||||||
# missing aggregate pillar entries; postgres.telegraf_users CREATEs the
|
# to run on every soup. The subsequent state.apply creates/updates the matching
|
||||||
# matching DB roles; then the bash loop below copies each minion's cred
|
# Postgres roles from the reconciled pillar.
|
||||||
# into its own pillar file. Going forward, so-minion owns add/delete for
|
echo "Reconciling Telegraf Postgres creds for accepted minions."
|
||||||
# every new minion, so this backfill is only needed on the upgrade boundary.
|
for mid in $(salt-key --out=json --list=accepted 2>/dev/null | jq -r '.minions[]?' 2>/dev/null); do
|
||||||
echo "Provisioning Telegraf Postgres users for existing minions."
|
[[ -n "$mid" ]] || continue
|
||||||
salt-call --local state.apply postgres.auth,postgres.telegraf_users queue=True || true
|
/usr/sbin/so-telegraf-cred add "$mid" || echo " warning: so-telegraf-cred add $mid failed" >&2
|
||||||
|
done
|
||||||
AGGREGATE_PILLAR=/opt/so/saltstack/local/pillar/postgres/auth.sls
|
salt-call --local state.apply postgres.telegraf_users queue=True || true
|
||||||
MINIONS_DIR=/opt/so/saltstack/local/pillar/minions
|
|
||||||
if [[ -f "$AGGREGATE_PILLAR" && -d "$MINIONS_DIR" ]]; then
|
|
||||||
for pillar_file in "$MINIONS_DIR"/*.sls; do
|
|
||||||
[[ -f "$pillar_file" ]] || continue
|
|
||||||
mid=$(basename "$pillar_file" .sls)
|
|
||||||
[[ "$mid" == adv_* ]] && continue
|
|
||||||
safe=$(echo "$mid" | tr '.-' '__' | tr '[:upper:]' '[:lower:]')
|
|
||||||
existing_user=$(so-yaml.py get -r "$pillar_file" postgres.telegraf.user 2>/dev/null || true)
|
|
||||||
[[ "$existing_user" == "so_telegraf_${safe}" ]] && continue
|
|
||||||
user=$(so-yaml.py get -r "$AGGREGATE_PILLAR" "postgres.auth.users.telegraf_${safe}.user" 2>/dev/null || true)
|
|
||||||
pass=$(so-yaml.py get -r "$AGGREGATE_PILLAR" "postgres.auth.users.telegraf_${safe}.pass" 2>/dev/null || true)
|
|
||||||
[[ -z "$user" || -z "$pass" ]] && continue
|
|
||||||
so-yaml.py replace "$pillar_file" postgres.telegraf.user "$user" >/dev/null
|
|
||||||
so-yaml.py replace "$pillar_file" postgres.telegraf.pass "$pass" >/dev/null
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
POSTVERSION=3.1.0
|
POSTVERSION=3.1.0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,8 +25,33 @@ manager_run_es_soc:
|
|||||||
- salt: {{NEWNODE}}_update_mine
|
- salt: {{NEWNODE}}_update_mine
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
# so-minion has already added the new minion's entry to telegraf/creds.sls
|
||||||
|
# via so-telegraf-cred before this orch fires. Reconcile the Postgres role
|
||||||
|
# on the manager so the new minion can authenticate on its first highstate,
|
||||||
|
# then refresh the minion's pillar so its telegraf.conf renders with the
|
||||||
|
# freshly-written cred.
|
||||||
|
manager_create_postgres_telegraf_role:
|
||||||
|
salt.state:
|
||||||
|
- tgt: {{ MANAGER }}
|
||||||
|
- sls:
|
||||||
|
- postgres.telegraf_users
|
||||||
|
- queue: True
|
||||||
|
- require:
|
||||||
|
- salt: {{NEWNODE}}_update_mine
|
||||||
|
|
||||||
|
{{NEWNODE}}_refresh_pillar:
|
||||||
|
salt.function:
|
||||||
|
- name: saltutil.refresh_pillar
|
||||||
|
- tgt: {{ NEWNODE }}
|
||||||
|
- kwarg:
|
||||||
|
wait: True
|
||||||
|
- require:
|
||||||
|
- salt: manager_create_postgres_telegraf_role
|
||||||
|
|
||||||
{{NEWNODE}}_run_highstate:
|
{{NEWNODE}}_run_highstate:
|
||||||
salt.state:
|
salt.state:
|
||||||
- tgt: {{ NEWNODE }}
|
- tgt: {{ NEWNODE }}
|
||||||
- highstate: True
|
- highstate: True
|
||||||
- queue: True
|
- queue: True
|
||||||
|
- require:
|
||||||
|
- salt: {{NEWNODE}}_refresh_pillar
|
||||||
|
|||||||
+2
-23
@@ -13,24 +13,8 @@
|
|||||||
{% set CHARS = DIGITS~LOWERCASE~UPPERCASE~SYMBOLS %}
|
{% set CHARS = DIGITS~LOWERCASE~UPPERCASE~SYMBOLS %}
|
||||||
{% set so_postgres_user_pass = salt['pillar.get']('postgres:auth:users:so_postgres_user:pass', salt['random.get_str'](72, chars=CHARS)) %}
|
{% set so_postgres_user_pass = salt['pillar.get']('postgres:auth:users:so_postgres_user:pass', salt['random.get_str'](72, chars=CHARS)) %}
|
||||||
|
|
||||||
{# Per-minion Telegraf Postgres credentials. Merge currently-up minions with any #}
|
# Admin cred only. Per-minion Telegraf creds live in telegraf/creds.sls,
|
||||||
{# previously-known entries in pillar so existing passwords persist across runs. #}
|
# managed by /usr/sbin/so-telegraf-cred (called from so-minion).
|
||||||
{% set existing = salt['pillar.get']('postgres:auth:users', {}) %}
|
|
||||||
{% set up_minions = salt['saltutil.runner']('manage.up') or [] %}
|
|
||||||
{% set telegraf_users = {} %}
|
|
||||||
{% for key, entry in existing.items() %}
|
|
||||||
{%- if key.startswith('telegraf_') and entry.get('user') and entry.get('pass') %}
|
|
||||||
{%- do telegraf_users.update({key: entry}) %}
|
|
||||||
{%- endif %}
|
|
||||||
{% endfor %}
|
|
||||||
{% for mid in up_minions %}
|
|
||||||
{%- set safe = mid | replace('.','_') | replace('-','_') | lower %}
|
|
||||||
{%- set key = 'telegraf_' ~ safe %}
|
|
||||||
{%- if key not in telegraf_users %}
|
|
||||||
{%- do telegraf_users.update({key: {'user': 'so_telegraf_' ~ safe, 'pass': salt['random.get_str'](72, chars=CHARS)}}) %}
|
|
||||||
{%- endif %}
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
postgres_auth_pillar:
|
postgres_auth_pillar:
|
||||||
file.managed:
|
file.managed:
|
||||||
- name: /opt/so/saltstack/local/pillar/postgres/auth.sls
|
- name: /opt/so/saltstack/local/pillar/postgres/auth.sls
|
||||||
@@ -43,11 +27,6 @@ postgres_auth_pillar:
|
|||||||
so_postgres_user:
|
so_postgres_user:
|
||||||
user: so_postgres
|
user: so_postgres
|
||||||
pass: "{{ so_postgres_user_pass }}"
|
pass: "{{ so_postgres_user_pass }}"
|
||||||
{% for key, entry in telegraf_users.items() %}
|
|
||||||
{{ key }}:
|
|
||||||
user: {{ entry.user }}
|
|
||||||
pass: "{{ entry.pass }}"
|
|
||||||
{% endfor %}
|
|
||||||
- show_changes: False
|
- show_changes: False
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
{# postgres_wait_ready below requires `docker_container: so-postgres`, which is
|
{# postgres_wait_ready below requires `docker_container: so-postgres`, which is
|
||||||
declared in postgres.enabled. Include it here so state.apply postgres.telegraf_users
|
declared in postgres.enabled. Include it here so state.apply postgres.telegraf_users
|
||||||
on its own (from the reactor orch or from soup) still has that ID in scope. Salt
|
on its own (e.g. from orch.deploy_newnode) still has that ID in scope. Salt
|
||||||
de-duplicates the circular include. #}
|
de-duplicates the circular include. #}
|
||||||
include:
|
include:
|
||||||
- postgres.enabled
|
- postgres.enabled
|
||||||
@@ -96,9 +96,9 @@ postgres_telegraf_group_role:
|
|||||||
- require:
|
- require:
|
||||||
- cmd: postgres_create_telegraf_db
|
- cmd: postgres_create_telegraf_db
|
||||||
|
|
||||||
{% set users = salt['pillar.get']('postgres:auth:users', {}) %}
|
{% set creds = salt['pillar.get']('telegraf:postgres_creds', {}) %}
|
||||||
{% for key, entry in users.items() %}
|
{% for mid, entry in creds.items() %}
|
||||||
{% if key.startswith('telegraf_') and entry.get('user') and entry.get('pass') %}
|
{% if entry.get('user') and entry.get('pass') %}
|
||||||
{% set u = entry.user %}
|
{% set u = entry.user %}
|
||||||
{% set p = entry.pass | replace("'", "''") %}
|
{% set p = entry.pass | replace("'", "''") %}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,12 @@
|
|||||||
{%- set LOGSTASH_ENABLED = LOGSTASH_MERGED.enabled %}
|
{%- set LOGSTASH_ENABLED = LOGSTASH_MERGED.enabled %}
|
||||||
{%- set TG_OUT = TELEGRAFMERGED.output | upper %}
|
{%- set TG_OUT = TELEGRAFMERGED.output | upper %}
|
||||||
{%- set PG_HOST = GLOBALS.manager_ip %}
|
{%- set PG_HOST = GLOBALS.manager_ip %}
|
||||||
{#- Per-minion telegraf creds are written into the minion's own pillar file
|
{#- Per-minion telegraf creds live in the grid-wide telegraf/creds.sls pillar,
|
||||||
(/opt/so/saltstack/local/pillar/minions/<id>.sls) by postgres.auth on the
|
written by /usr/sbin/so-telegraf-cred on the manager. Each minion looks up
|
||||||
manager. Each minion only sees its own password — the aggregate map in
|
its own entry by grains.id. #}
|
||||||
postgres:auth:users is manager-scoped. #}
|
{%- set PG_ENTRY = salt['pillar.get']('telegraf:postgres_creds:' ~ grains.id, {}) %}
|
||||||
{%- set PG_USER = salt['pillar.get']('postgres:telegraf:user', '') %}
|
{%- set PG_USER = PG_ENTRY.get('user', '') %}
|
||||||
{%- set PG_PASS = salt['pillar.get']('postgres:telegraf:pass', '') %}
|
{%- set PG_PASS = PG_ENTRY.get('pass', '') %}
|
||||||
# Global tags can be specified here in key="value" format.
|
# Global tags can be specified here in key="value" format.
|
||||||
[global_tags]
|
[global_tags]
|
||||||
role = "{{ GLOBALS.role.split('-') | last }}"
|
role = "{{ GLOBALS.role.split('-') | last }}"
|
||||||
|
|||||||
Reference in New Issue
Block a user