index.twig 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. <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">
  31. <div>
  32. <label for="f-actor-kind" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Actor kind</label>
  33. <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">
  34. <option value="">— any —</option>
  35. {% for k in allowed_kinds %}
  36. <option value="{{ k }}" {% if filters.actor_kind == k %}selected{% endif %}>{{ k }}</option>
  37. {% endfor %}
  38. </select>
  39. </div>
  40. <div>
  41. <label for="f-actor-id" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Actor id</label>
  42. <input type="number" id="f-actor-id" name="actor_id" min="1" value="{{ filters.actor_id|default('') }}"
  43. 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">
  44. </div>
  45. <div class="col-span-2">
  46. <label for="f-action" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Action</label>
  47. <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">
  48. <option value="">— any —</option>
  49. {% for a in allowed_actions %}
  50. <option value="{{ a }}" {% if filters.action == a %}selected{% endif %}>{{ a }}</option>
  51. {% endfor %}
  52. </select>
  53. </div>
  54. <div>
  55. <label for="f-entity-type" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Entity type</label>
  56. <input type="text" id="f-entity-type" name="entity_type" value="{{ filters.entity_type|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>
  60. <label for="f-entity-id" class="block text-xs font-medium text-slate-600 dark:text-slate-400">Entity id</label>
  61. <input type="text" id="f-entity-id" name="entity_id" value="{{ filters.entity_id|default('') }}"
  62. 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">
  63. </div>
  64. <div class="col-span-2 md:col-span-7 grid grid-cols-1 gap-3 md:grid-cols-3 md:items-end">
  65. <div>
  66. <label for="f-from" class="block text-xs font-medium text-slate-600 dark:text-slate-400">From</label>
  67. <input type="datetime-local" id="f-from" name="from" step="1" data-irdb-iso-filter="{{ filters.from|default('') }}"
  68. 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">
  69. </div>
  70. <div>
  71. <label for="f-to" class="block text-xs font-medium text-slate-600 dark:text-slate-400">To</label>
  72. <input type="datetime-local" id="f-to" name="to" step="1" data-irdb-iso-filter="{{ filters.to|default('') }}"
  73. 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">
  74. </div>
  75. <div class="flex justify-end gap-2">
  76. <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>
  77. <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>
  78. </div>
  79. </div>
  80. </form>
  81. <script>
  82. (function () {
  83. function pad(n) { return String(n).padStart(2, '0'); }
  84. function isoToLocalInput(iso) {
  85. if (!iso) return '';
  86. const d = new Date(iso);
  87. if (isNaN(d.getTime())) return '';
  88. return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate())
  89. + 'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds());
  90. }
  91. function localInputToIso(value) {
  92. if (!value) return '';
  93. const d = new Date(value);
  94. if (isNaN(d.getTime())) return '';
  95. return d.toISOString();
  96. }
  97. document.querySelectorAll('#audit-filter-form input[data-irdb-iso-filter]').forEach((el) => {
  98. const iso = el.getAttribute('data-irdb-iso-filter');
  99. if (iso) el.value = isoToLocalInput(iso);
  100. });
  101. const form = document.getElementById('audit-filter-form');
  102. if (form) {
  103. form.addEventListener('submit', () => {
  104. form.querySelectorAll('input[type="datetime-local"]').forEach((el) => {
  105. if (el.value) el.value = localInputToIso(el.value);
  106. });
  107. });
  108. }
  109. })();
  110. </script>
  111. {% if list %}
  112. <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 }">
  113. <table class="w-full text-sm" data-sortable-table="audit-index">
  114. <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">
  115. <tr>
  116. {{ sort.th('When', 'when', 'date') }}
  117. {{ sort.th('Actor', 'actor') }}
  118. {{ sort.th('Action', 'action') }}
  119. {{ sort.th('Entity', 'entity') }}
  120. {{ sort.th('Source IP', 'source_ip') }}
  121. <th class="px-4 py-2 font-medium text-right">Payload</th>
  122. </tr>
  123. </thead>
  124. <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
  125. {% for ev in list.items %}
  126. <tr>
  127. <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>
  128. <td class="px-4 py-2 align-top text-xs" data-sort-value="{{ ev.actor_kind }} {{ ev.actor_id|default('') }}">
  129. <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>
  130. {% if ev.actor_id %}<span class="ml-1 font-mono text-slate-500">#{{ ev.actor_id }}</span>{% endif %}
  131. </td>
  132. <td class="px-4 py-2 align-top" data-sort-value="{{ ev.action }}">{{ h.action_pill(ev.action) }}</td>
  133. <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('') }}">
  134. <span class="font-mono text-slate-600 dark:text-slate-300">{{ ev.entity_type|default('—') }}</span>
  135. {% if ev.entity_label %}
  136. <span class="ml-1 font-medium text-slate-800 dark:text-slate-100">{{ ev.entity_label }}</span>
  137. {% if ev.entity_id %}<span class="ml-1 font-mono text-[0.7rem] text-slate-400">#{{ ev.entity_id }}</span>{% endif %}
  138. {% elseif ev.entity_id %}
  139. <span class="ml-1 font-mono text-slate-500">#{{ ev.entity_id }}</span>
  140. {% endif %}
  141. </td>
  142. <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>
  143. <td class="px-4 py-2 align-top text-right">
  144. {% if ev.details %}
  145. <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>
  146. {% else %}
  147. <span class="text-xs text-slate-400">—</span>
  148. {% endif %}
  149. </td>
  150. </tr>
  151. {% if ev.details %}
  152. <tr x-show="open === {{ ev.id }}" x-cloak data-sort-row-detail>
  153. <td colspan="6" class="bg-slate-50 px-4 py-3 dark:bg-slate-950">
  154. {% if ev.details.changes is defined and ev.details.changes is iterable and ev.details.changes|length > 0 %}
  155. <div class="mb-3">
  156. <div class="mb-1 text-[0.7rem] font-semibold uppercase tracking-wider text-slate-500 dark:text-slate-400">Changes</div>
  157. <table class="w-full text-xs">
  158. <thead class="text-left text-[0.7rem] uppercase text-slate-500 dark:text-slate-400">
  159. <tr>
  160. <th class="px-2 py-1 font-medium">Field</th>
  161. <th class="px-2 py-1 font-medium">Before</th>
  162. <th class="px-2 py-1 font-medium">After</th>
  163. </tr>
  164. </thead>
  165. <tbody class="divide-y divide-slate-200 bg-white dark:divide-slate-800 dark:bg-slate-900">
  166. {% for field, change in ev.details.changes %}
  167. <tr>
  168. <td class="px-2 py-1 font-mono text-slate-700 dark:text-slate-200">{{ field }}</td>
  169. <td class="px-2 py-1 align-top">
  170. {% if change.from is null %}
  171. <span class="text-slate-400">∅</span>
  172. {% elseif change.from is iterable %}
  173. <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>
  174. {% else %}
  175. <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>
  176. {% endif %}
  177. </td>
  178. <td class="px-2 py-1 align-top">
  179. {% if change.to is null %}
  180. <span class="text-slate-400">∅</span>
  181. {% elseif change.to is iterable %}
  182. <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>
  183. {% else %}
  184. <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>
  185. {% endif %}
  186. </td>
  187. </tr>
  188. {% endfor %}
  189. </tbody>
  190. </table>
  191. </div>
  192. {% endif %}
  193. <details class="text-xs" {% if ev.details.changes is not defined %}open{% endif %}>
  194. <summary class="cursor-pointer text-slate-500 hover:text-slate-800 dark:text-slate-400 dark:hover:text-slate-200">Raw payload</summary>
  195. <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>
  196. </details>
  197. </td>
  198. </tr>
  199. {% endif %}
  200. {% else %}
  201. <tr><td colspan="6" class="px-4 py-6 text-center text-slate-400">No events match these filters.</td></tr>
  202. {% endfor %}
  203. </tbody>
  204. </table>
  205. </div>
  206. {% if list.total > list.page_size %}
  207. {% set total_pages = (list.total // list.page_size) + (list.total % list.page_size > 0 ? 1 : 0) %}
  208. <nav class="mt-4 flex items-center justify-between text-sm">
  209. <span class="text-slate-500 dark:text-slate-400">Page {{ page }} of {{ total_pages }}</span>
  210. <div class="flex gap-2">
  211. {% set prev_qs = filters|merge({'page': page - 1}) %}
  212. {% set next_qs = filters|merge({'page': page + 1}) %}
  213. {% if page > 1 %}
  214. <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>
  215. {% endif %}
  216. {% if page < total_pages %}
  217. <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>
  218. {% endif %}
  219. </div>
  220. </nav>
  221. {% endif %}
  222. {% endif %}
  223. </div>
  224. {% endblock %}