index.twig 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. {% extends 'layout.twig' %}
  2. {% block title %}Audit — IRDB{% endblock %}
  3. {% macro action_pill(action) %}
  4. {%- set bucket = action|split('.')[0]|default('') -%}
  5. {%- set classes = {
  6. 'reporter': 'bg-blue-100 text-blue-900 dark:bg-blue-900 dark:text-blue-100',
  7. 'consumer': 'bg-cyan-100 text-cyan-900 dark:bg-cyan-900 dark:text-cyan-100',
  8. 'token': 'bg-purple-100 text-purple-900 dark:bg-purple-900 dark:text-purple-100',
  9. 'policy': 'bg-indigo-100 text-indigo-900 dark:bg-indigo-900 dark:text-indigo-100',
  10. 'category': 'bg-violet-100 text-violet-900 dark:bg-violet-900 dark:text-violet-100',
  11. 'manual_block': 'bg-amber-100 text-amber-900 dark:bg-amber-900 dark:text-amber-100',
  12. 'allowlist': 'bg-emerald-100 text-emerald-900 dark:bg-emerald-900 dark:text-emerald-100',
  13. 'job': 'bg-slate-200 text-slate-800 dark:bg-slate-700 dark:text-slate-100',
  14. } -%}
  15. <span class="inline-block rounded px-2 py-0.5 font-mono text-[0.7rem] uppercase tracking-tight {{ classes[bucket]|default('bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300') }}">{{ action }}</span>
  16. {% endmacro %}
  17. {% block content %}
  18. {% import _self as h %}
  19. {% import 'partials/sort.twig' as sort %}
  20. <div class="mx-auto max-w-6xl">
  21. <div class="flex items-center justify-between">
  22. <h1 class="text-2xl font-semibold tracking-tight">Audit log</h1>
  23. {% if list %}
  24. <span class="text-sm text-slate-500 dark:text-slate-400">{{ list.total }} total</span>
  25. {% endif %}
  26. </div>
  27. {% if error %}
  28. <div class="mt-4 rounded-md border border-red-300 bg-red-50 px-4 py-2 text-sm text-red-800 dark:border-red-800 dark:bg-red-950 dark:text-red-300">{{ error }}</div>
  29. {% endif %}
  30. {% if filters.subject_kind and filters.subject_id %}
  31. <div class="mt-4 flex items-center justify-between rounded-md border border-indigo-200 bg-indigo-50 px-4 py-2 text-sm text-indigo-900 dark:border-indigo-800 dark:bg-indigo-950 dark:text-indigo-200">
  32. <span>
  33. Filtering events for {{ filters.subject_kind }} <span class="font-mono">#{{ filters.subject_id }}</span>
  34. — both events <em>about</em> it and events emitted <em>by</em> it.
  35. </span>
  36. <a href="/app/audit" class="text-xs font-medium underline">clear</a>
  37. </div>
  38. {% endif %}
  39. <form id="audit-filter-form" method="get" action="/app/audit" class="mt-4 grid grid-cols-2 gap-3 rounded-2xl border border-slate-200 bg-white p-4 text-sm shadow-sm dark:border-slate-800 dark:bg-slate-900 md:grid-cols-7">
  40. {# Subject filter is set by per-entity links (reporter/consumer detail) and
  41. preserved across re-submits of this form so a manual filter change
  42. doesn't silently widen the result. Form has no UI control for it. #}
  43. {% if filters.subject_kind %}<input type="hidden" name="subject_kind" value="{{ filters.subject_kind }}">{% endif %}
  44. {% if filters.subject_id %}<input type="hidden" name="subject_id" value="{{ filters.subject_id }}">{% endif %}
  45. <div>
  46. <label for="f-actor-kind" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Actor kind</label>
  47. <select id="f-actor-kind" name="actor_kind" class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  48. <option value="">— any —</option>
  49. {% for k in allowed_kinds %}
  50. <option value="{{ k }}" {% if filters.actor_kind == k %}selected{% endif %}>{{ k }}</option>
  51. {% endfor %}
  52. </select>
  53. </div>
  54. <div>
  55. <label for="f-actor-id" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Actor id</label>
  56. <input type="number" id="f-actor-id" name="actor_id" min="1" value="{{ filters.actor_id|default('') }}"
  57. class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  58. </div>
  59. <div class="col-span-2">
  60. <label for="f-action" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Action</label>
  61. <select id="f-action" name="action" class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  62. <option value="">— any —</option>
  63. {% for a in allowed_actions %}
  64. <option value="{{ a }}" {% if filters.action == a %}selected{% endif %}>{{ a }}</option>
  65. {% endfor %}
  66. </select>
  67. </div>
  68. <div>
  69. <label for="f-entity-type" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Entity type</label>
  70. <input type="text" id="f-entity-type" name="entity_type" value="{{ filters.entity_type|default('') }}"
  71. class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  72. </div>
  73. <div>
  74. <label for="f-entity-id" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Entity id</label>
  75. <input type="text" id="f-entity-id" name="entity_id" value="{{ filters.entity_id|default('') }}"
  76. class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  77. </div>
  78. <div class="col-span-2 md:col-span-7 grid grid-cols-1 gap-3 md:grid-cols-3 md:items-end">
  79. <div>
  80. <label for="f-from" class="block text-xs font-medium text-slate-600 dark:text-slate-400">From</label>
  81. <input type="datetime-local" id="f-from" name="from" step="1" data-irdb-iso-filter="{{ filters.from|default('') }}"
  82. class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  83. </div>
  84. <div>
  85. <label for="f-to" class="block text-xs font-medium text-slate-600 dark:text-slate-400">To</label>
  86. <input type="datetime-local" id="f-to" name="to" step="1" data-irdb-iso-filter="{{ filters.to|default('') }}"
  87. class="mt-1 w-full rounded-md border border-slate-300 bg-white px-2 py-1.5 text-sm dark:border-slate-700 dark:bg-slate-950">
  88. </div>
  89. <div class="flex justify-end gap-2">
  90. <a href="/app/audit" class="rounded-md border border-slate-300 px-3 py-1.5 text-sm hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">Reset</a>
  91. <button type="submit" class="rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-indigo-500">Filter</button>
  92. </div>
  93. </div>
  94. </form>
  95. {# Datetime-local ⇄ ISO round-tripping for the filter form lives in
  96. app.js (initAuditFilterForm) so this page has no inline JS. #}
  97. {% if list %}
  98. <div class="mt-6 overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900" x-data="rowExpander">
  99. <table class="w-full text-sm" data-sortable-table="audit-index">
  100. <thead class="border-b border-slate-200 bg-slate-50 text-left text-xs uppercase tracking-wider text-slate-500 dark:border-slate-800 dark:bg-slate-950 dark:text-slate-400">
  101. <tr>
  102. {{ sort.th('When', 'when', 'date') }}
  103. {{ sort.th('Actor', 'actor') }}
  104. {{ sort.th('Action', 'action') }}
  105. {{ sort.th('Entity', 'entity') }}
  106. {{ sort.th('Source IP', 'source_ip') }}
  107. <th class="px-4 py-2 font-medium text-right">Payload</th>
  108. </tr>
  109. </thead>
  110. <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
  111. {% for ev in list.items %}
  112. <tr>
  113. <td class="px-4 py-2 align-top" data-sort-value="{{ ev.occurred_at }}"><time class="irdb-dt font-mono text-xs text-slate-600 dark:text-slate-300" datetime="{{ ev.occurred_at }}" title="{{ ev.occurred_at }}">{{ ev.occurred_at }}</time></td>
  114. <td class="px-4 py-2 align-top text-xs" data-sort-value="{{ ev.actor_kind }} {{ ev.actor_id|default('') }}">
  115. <span class="rounded bg-slate-100 px-1.5 py-0.5 font-mono uppercase tracking-tight text-slate-700 dark:bg-slate-800 dark:text-slate-300">{{ ev.actor_kind }}</span>
  116. {% if ev.actor_id %}<span class="ml-1 font-mono text-slate-500">#{{ ev.actor_id }}</span>{% endif %}
  117. </td>
  118. <td class="px-4 py-2 align-top" data-sort-value="{{ ev.action }}">{{ h.action_pill(ev.action) }}</td>
  119. <td class="px-4 py-2 align-top text-xs" data-sort-value="{{ ev.entity_type|default('') }} {{ ev.entity_label|default('') }} {{ ev.entity_id|default('') }}">
  120. <span class="font-mono text-slate-600 dark:text-slate-300">{{ ev.entity_type|default('—') }}</span>
  121. {% if ev.entity_label %}
  122. <span class="ml-1 font-medium text-slate-800 dark:text-slate-100">{{ ev.entity_label }}</span>
  123. {% if ev.entity_id %}<span class="ml-1 font-mono text-[0.7rem] text-slate-400">#{{ ev.entity_id }}</span>{% endif %}
  124. {% elseif ev.entity_id %}
  125. <span class="ml-1 font-mono text-slate-500">#{{ ev.entity_id }}</span>
  126. {% endif %}
  127. </td>
  128. <td class="px-4 py-2 align-top font-mono text-xs text-slate-500" data-sort-value="{{ ev.source_ip|default('') }}">{{ ev.source_ip|default('—') }}</td>
  129. <td class="px-4 py-2 align-top text-right">
  130. {% if ev.details %}
  131. <button type="button" x-on:click="toggle({{ ev.id }})" class="rounded border border-slate-300 px-2 py-0.5 text-xs hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">View</button>
  132. {% else %}
  133. <span class="text-xs text-slate-400">—</span>
  134. {% endif %}
  135. </td>
  136. </tr>
  137. {% if ev.details %}
  138. <tr x-show="isOpen({{ ev.id }})" x-cloak data-sort-row-detail>
  139. <td colspan="6" class="bg-slate-50 px-4 py-3 dark:bg-slate-950">
  140. {% if ev.details.changes is defined and ev.details.changes is iterable and ev.details.changes|length > 0 %}
  141. <div class="mb-3">
  142. <div class="mb-1 text-[0.7rem] font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Changes</div>
  143. <table class="w-full text-xs">
  144. <thead class="text-left text-[0.7rem] uppercase text-slate-500 dark:text-slate-400">
  145. <tr>
  146. <th class="px-2 py-1 font-medium">Field</th>
  147. <th class="px-2 py-1 font-medium">Before</th>
  148. <th class="px-2 py-1 font-medium">After</th>
  149. </tr>
  150. </thead>
  151. <tbody class="divide-y divide-slate-200 bg-white dark:divide-slate-800 dark:bg-slate-900">
  152. {% for field, change in ev.details.changes %}
  153. <tr>
  154. <td class="px-2 py-1 font-mono text-slate-700 dark:text-slate-200">{{ field }}</td>
  155. <td class="px-2 py-1 align-top">
  156. {% if change.from is null %}
  157. <span class="text-slate-400">∅</span>
  158. {% elseif change.from is iterable %}
  159. <pre class="overflow-x-auto rounded bg-rose-50 px-2 py-1 font-mono text-[0.7rem] text-rose-900 dark:bg-rose-950 dark:text-rose-200">{{ change.from|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
  160. {% else %}
  161. <span class="rounded bg-rose-50 px-1.5 py-0.5 font-mono text-rose-900 dark:bg-rose-950 dark:text-rose-200">{{ change.from }}</span>
  162. {% endif %}
  163. </td>
  164. <td class="px-2 py-1 align-top">
  165. {% if change.to is null %}
  166. <span class="text-slate-400">∅</span>
  167. {% elseif change.to is iterable %}
  168. <pre class="overflow-x-auto rounded bg-emerald-50 px-2 py-1 font-mono text-[0.7rem] text-emerald-900 dark:bg-emerald-950 dark:text-emerald-200">{{ change.to|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
  169. {% else %}
  170. <span class="rounded bg-emerald-50 px-1.5 py-0.5 font-mono text-emerald-900 dark:bg-emerald-950 dark:text-emerald-200">{{ change.to }}</span>
  171. {% endif %}
  172. </td>
  173. </tr>
  174. {% endfor %}
  175. </tbody>
  176. </table>
  177. </div>
  178. {% endif %}
  179. <details class="text-xs" {% if ev.details.changes is not defined %}open{% endif %}>
  180. <summary class="cursor-pointer text-slate-500 hover:text-slate-800 dark:text-slate-400 dark:hover:text-slate-200">Raw payload</summary>
  181. <pre class="mt-2 overflow-x-auto rounded bg-white p-3 dark:bg-slate-900">{{ ev.details|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
  182. </details>
  183. </td>
  184. </tr>
  185. {% endif %}
  186. {% else %}
  187. <tr><td colspan="6" class="px-4 py-6 text-center text-slate-400">No events match these filters.</td></tr>
  188. {% endfor %}
  189. </tbody>
  190. </table>
  191. </div>
  192. {% if list.total > list.page_size %}
  193. {% set total_pages = (list.total // list.page_size) + (list.total % list.page_size > 0 ? 1 : 0) %}
  194. <nav class="mt-4 flex items-center justify-between text-sm">
  195. <span class="text-slate-500 dark:text-slate-400">Page {{ page }} of {{ total_pages }}</span>
  196. <div class="flex gap-2">
  197. {% set prev_qs = filters|merge({'page': page - 1}) %}
  198. {% set next_qs = filters|merge({'page': page + 1}) %}
  199. {% if page > 1 %}
  200. <a href="/app/audit?{{ prev_qs|url_encode }}" class="rounded-md border border-slate-300 px-3 py-1.5 hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">‹ Prev</a>
  201. {% endif %}
  202. {% if page < total_pages %}
  203. <a href="/app/audit?{{ next_qs|url_encode }}" class="rounded-md border border-slate-300 px-3 py-1.5 hover:bg-slate-50 dark:border-slate-700 dark:hover:bg-slate-800">Next ›</a>
  204. {% endif %}
  205. </div>
  206. </nav>
  207. {% endif %}
  208. {% endif %}
  209. </div>
  210. {% endblock %}