index.twig 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. <script>
  96. (function () {
  97. function pad(n) { return String(n).padStart(2, '0'); }
  98. function isoToLocalInput(iso) {
  99. if (!iso) return '';
  100. const d = new Date(iso);
  101. if (isNaN(d.getTime())) return '';
  102. return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate())
  103. + 'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());
  104. }
  105. function localInputToIso(value) {
  106. if (!value) return '';
  107. const d = new Date(value);
  108. if (isNaN(d.getTime())) return '';
  109. return d.toISOString();
  110. }
  111. document.querySelectorAll('#audit-filter-form input[data-irdb-iso-filter]').forEach((el) => {
  112. const iso = el.getAttribute('data-irdb-iso-filter');
  113. if (iso) el.value = isoToLocalInput(iso);
  114. });
  115. const form = document.getElementById('audit-filter-form');
  116. if (form) {
  117. form.addEventListener('submit', () => {
  118. form.querySelectorAll('input[type="datetime-local"]').forEach((el) => {
  119. if (el.value) el.value = localInputToIso(el.value);
  120. });
  121. });
  122. }
  123. })();
  124. </script>
  125. {% if list %}
  126. <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="{ open: null }">
  127. <table class="w-full text-sm" data-sortable-table="audit-index">
  128. <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">
  129. <tr>
  130. {{ sort.th('When', 'when', 'date') }}
  131. {{ sort.th('Actor', 'actor') }}
  132. {{ sort.th('Action', 'action') }}
  133. {{ sort.th('Entity', 'entity') }}
  134. {{ sort.th('Source IP', 'source_ip') }}
  135. <th class="px-4 py-2 font-medium text-right">Payload</th>
  136. </tr>
  137. </thead>
  138. <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
  139. {% for ev in list.items %}
  140. <tr>
  141. <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>
  142. <td class="px-4 py-2 align-top text-xs" data-sort-value="{{ ev.actor_kind }} {{ ev.actor_id|default('') }}">
  143. <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>
  144. {% if ev.actor_id %}<span class="ml-1 font-mono text-slate-500">#{{ ev.actor_id }}</span>{% endif %}
  145. </td>
  146. <td class="px-4 py-2 align-top" data-sort-value="{{ ev.action }}">{{ h.action_pill(ev.action) }}</td>
  147. <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('') }}">
  148. <span class="font-mono text-slate-600 dark:text-slate-300">{{ ev.entity_type|default('—') }}</span>
  149. {% if ev.entity_label %}
  150. <span class="ml-1 font-medium text-slate-800 dark:text-slate-100">{{ ev.entity_label }}</span>
  151. {% if ev.entity_id %}<span class="ml-1 font-mono text-[0.7rem] text-slate-400">#{{ ev.entity_id }}</span>{% endif %}
  152. {% elseif ev.entity_id %}
  153. <span class="ml-1 font-mono text-slate-500">#{{ ev.entity_id }}</span>
  154. {% endif %}
  155. </td>
  156. <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>
  157. <td class="px-4 py-2 align-top text-right">
  158. {% if ev.details %}
  159. <button type="button" x-on:click="open = (open === {{ ev.id }} ? null : {{ 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>
  160. {% else %}
  161. <span class="text-xs text-slate-400">—</span>
  162. {% endif %}
  163. </td>
  164. </tr>
  165. {% if ev.details %}
  166. <tr x-show="open === {{ ev.id }}" x-cloak data-sort-row-detail>
  167. <td colspan="6" class="bg-slate-50 px-4 py-3 dark:bg-slate-950">
  168. {% if ev.details.changes is defined and ev.details.changes is iterable and ev.details.changes|length > 0 %}
  169. <div class="mb-3">
  170. <div class="mb-1 text-[0.7rem] font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Changes</div>
  171. <table class="w-full text-xs">
  172. <thead class="text-left text-[0.7rem] uppercase text-slate-500 dark:text-slate-400">
  173. <tr>
  174. <th class="px-2 py-1 font-medium">Field</th>
  175. <th class="px-2 py-1 font-medium">Before</th>
  176. <th class="px-2 py-1 font-medium">After</th>
  177. </tr>
  178. </thead>
  179. <tbody class="divide-y divide-slate-200 bg-white dark:divide-slate-800 dark:bg-slate-900">
  180. {% for field, change in ev.details.changes %}
  181. <tr>
  182. <td class="px-2 py-1 font-mono text-slate-700 dark:text-slate-200">{{ field }}</td>
  183. <td class="px-2 py-1 align-top">
  184. {% if change.from is null %}
  185. <span class="text-slate-400">∅</span>
  186. {% elseif change.from is iterable %}
  187. <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>
  188. {% else %}
  189. <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>
  190. {% endif %}
  191. </td>
  192. <td class="px-2 py-1 align-top">
  193. {% if change.to is null %}
  194. <span class="text-slate-400">∅</span>
  195. {% elseif change.to is iterable %}
  196. <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>
  197. {% else %}
  198. <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>
  199. {% endif %}
  200. </td>
  201. </tr>
  202. {% endfor %}
  203. </tbody>
  204. </table>
  205. </div>
  206. {% endif %}
  207. <details class="text-xs" {% if ev.details.changes is not defined %}open{% endif %}>
  208. <summary class="cursor-pointer text-slate-500 hover:text-slate-800 dark:text-slate-400 dark:hover:text-slate-200">Raw payload</summary>
  209. <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>
  210. </details>
  211. </td>
  212. </tr>
  213. {% endif %}
  214. {% else %}
  215. <tr><td colspan="6" class="px-4 py-6 text-center text-slate-400">No events match these filters.</td></tr>
  216. {% endfor %}
  217. </tbody>
  218. </table>
  219. </div>
  220. {% if list.total > list.page_size %}
  221. {% set total_pages = (list.total // list.page_size) + (list.total % list.page_size > 0 ? 1 : 0) %}
  222. <nav class="mt-4 flex items-center justify-between text-sm">
  223. <span class="text-slate-500 dark:text-slate-400">Page {{ page }} of {{ total_pages }}</span>
  224. <div class="flex gap-2">
  225. {% set prev_qs = filters|merge({'page': page - 1}) %}
  226. {% set next_qs = filters|merge({'page': page + 1}) %}
  227. {% if page > 1 %}
  228. <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>
  229. {% endif %}
  230. {% if page < total_pages %}
  231. <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>
  232. {% endif %}
  233. </div>
  234. </nav>
  235. {% endif %}
  236. {% endif %}
  237. </div>
  238. {% endblock %}