feat(agent_vault,opnsense): support basic auth and register OPNsense API
ansible-deploy / plan (push) Successful in 1s
ansible-lint / lint (push) Successful in 3s
ansible-deploy / deploy (opnsense, ) (push) Successful in 10s

This commit is contained in:
karpenok committed 2026-11-17 15:27:02 +07:00
1 parent 15beca7bf0
commit 8061dd7604
3 files changed
+63 -23

No files matched your search

+18
View File
@@ -7,3 +7,21 @@
tags: [netbird]
- role: opnsense/xray
tags: [xray]
post_tasks:
- name: Зарегистрировать OPNsense в Agent Vault
ansible.builtin.include_role:
name: agent_vault_service
vars:
agent_vault_service_name: opnsense
agent_vault_service_description: >-
Маршрутизатор OPNsense (192.168.1.1:4433): статус системы, интерфейсы,
правила фаервола, маршруты, NetBird и Xray. Read-only доступ (GET) через
Basic Auth (OPNSENSE_API_KEY / OPNSENSE_API_SECRET).
agent_vault_service_host: "{{ opnsense_host }}"
agent_vault_service_paths: ["/api"]
agent_vault_service_methods: [GET]
agent_vault_service_credential_type: basic
agent_vault_secret_path: /services/opnsense
agent_vault_secret_username_key: OPNSENSE_API_KEY
agent_vault_secret_password_key: OPNSENSE_API_SECRET
tags: [agent_vault, netbird, xray]
@@ -17,13 +17,17 @@ agent_vault_service_host: ""
agent_vault_service_methods: [GET]
agent_vault_service_paths: []
# Как прокси подставляет креденшл. По умолчанию — «Authorization: Bearer <токен>».
# Для сервисов вроде NetBird нужно «Authorization: Token <токен>»: тогда header_prefix: Token.
# Как прокси подставляет креденшл: bearer (по умолчанию) или basic.
# Для bearer — «Authorization: Bearer <токен>» (или header_name / header_prefix).
# Для basic — «Authorization: Basic <base64(username:password)>» по паре ключей.
agent_vault_service_credential_type: bearer
agent_vault_service_header_name: ""
agent_vault_service_header_prefix: ""
# Откуда брать креденшл: путь и ключ секрета в проекте infra.
# Откуда брать креденшл: путь и ключ(и) секрета в проекте infra.
agent_vault_secret_project_id: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}"
agent_vault_secret_env: prod
agent_vault_secret_path: ""
agent_vault_secret_key: ""
agent_vault_secret_username_key: ""
agent_vault_secret_password_key: ""
@@ -21,17 +21,32 @@
- agent_vault_service_name | default('') | length > 0
- agent_vault_service_host | default('') | length > 0
- agent_vault_secret_path | default('') | length > 0
- agent_vault_secret_key | default('') | length > 0
- agent_vault_service_credential_type in ['bearer', 'basic']
- >-
(agent_vault_service_credential_type == 'bearer' and agent_vault_secret_key | default('') | length > 0)
or
(agent_vault_service_credential_type == 'basic'
and agent_vault_secret_username_key | default('') | length > 0
and agent_vault_secret_password_key | default('') | length > 0)
- agent_vault_domain | default('') | length > 0
- agent_vault_access_token | default('') | length > 0
fail_msg: >-
agent_vault_service: нужны agent_vault_service_name, agent_vault_service_host,
agent_vault_secret_path, agent_vault_secret_key, а также INFISICAL_DOMAIN
и INFISICAL_TOKEN в окружении.
agent_vault_secret_path, ключ(и) секрета (agent_vault_secret_key для bearer или
agent_vault_secret_username_key/agent_vault_secret_password_key для basic),
а также INFISICAL_DOMAIN и INFISICAL_TOKEN в окружении.
quiet: true
no_log: true
- name: Прочитать токен сервиса из Infisical
- name: Определить список ключей секретов для чтения
ansible.builtin.set_fact:
agent_vault_secret_keys_to_read: >-
{{ [agent_vault_secret_key]
if agent_vault_service_credential_type == 'bearer'
else [agent_vault_secret_username_key, agent_vault_secret_password_key] }}
no_log: true
- name: Прочитать секреты сервиса из Infisical
ansible.builtin.shell: |
set -euo pipefail
infisical secrets get "${SECRET_KEY}" \
@@ -47,27 +62,26 @@
INFISICAL_DOMAIN: "{{ agent_vault_domain }}"
INFISICAL_TOKEN: "{{ agent_vault_access_token }}"
INFISICAL_PROJECT_ID: "{{ agent_vault_secret_project_id }}"
SECRET_KEY: "{{ agent_vault_secret_key }}"
SECRET_KEY: "{{ item }}"
SECRET_PATH: "{{ agent_vault_secret_path }}"
SECRET_ENV: "{{ agent_vault_secret_env }}"
register: agent_vault_secret_raw
loop: "{{ agent_vault_secret_keys_to_read }}"
register: agent_vault_secrets_raw
changed_when: false
no_log: true
delegate_to: localhost
become: false
- name: Подставить прочитанный токен
ansible.builtin.set_fact:
agent_vault_service_token: "{{ agent_vault_secret_raw.stdout | trim }}"
no_log: true
- name: Проверить, что секрет не пустой
- name: Проверить, что прочитанные секреты не пустые
ansible.builtin.assert:
that:
- agent_vault_service_token | length > 0
- >-
agent_vault_secrets_raw.results | map(attribute='stdout') | map('trim')
| select('equalto', '') | list | length == 0
fail_msg: >-
В Infisical нет {{ agent_vault_secret_key }} по пути {{ agent_vault_secret_path }}
(env {{ agent_vault_secret_env }}), либо у идентичности нет прав на чтение.
В Infisical отсутствует один из ключей ({{ agent_vault_secret_keys_to_read | join(', ') }})
по пути {{ agent_vault_secret_path }} (env {{ agent_vault_secret_env }}),
либо у идентичности нет прав на чтение.
quiet: true
no_log: true
@@ -149,11 +163,15 @@
- name: Собрать креденшл сервиса
ansible.builtin.set_fact:
agent_vault_service_credential: >-
{{ {'type': 'bearer', 'value': agent_vault_service_token}
| combine({'headerName': agent_vault_service_header_name}
if agent_vault_service_header_name | length > 0 else {})
| combine({'headerPrefix': agent_vault_service_header_prefix}
if agent_vault_service_header_prefix | length > 0 else {}) }}
{{ ({'type': 'bearer', 'value': (agent_vault_secrets_raw.results[0].stdout | trim)}
| combine({'headerName': agent_vault_service_header_name}
if agent_vault_service_header_name | length > 0 else {})
| combine({'headerPrefix': agent_vault_service_header_prefix}
if agent_vault_service_header_prefix | length > 0 else {}))
if agent_vault_service_credential_type == 'bearer'
else {'type': 'basic',
'username': (agent_vault_secrets_raw.results[0].stdout | trim),
'password': (agent_vault_secrets_raw.results[1].stdout | trim)} }}
no_log: true
- name: Собрать тело сервиса