0

New System Log Event

by
Published 4 days ago

Emits Okta System Log events published since the last poll, with an optional filter expression.

Scriptยท trigger okta Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
import * as wmill from "windmill-client"
4

5
/**
6
 * New System Log Event
7
 * Emits Okta System Log events published since the last poll, tracked via Windmill state. Pass an optional `filter` expression (e.g. eventType eq "user.session.start") to scope the events watched. The first run sets the watermark to now and emits nothing.
8
 */
9
export async function main(auth: RT.Okta, filter: string | undefined) {
10
  const lastChecked: string | undefined = await wmill.getState()
11

12
  // First run: set the watermark to now and don't emit a backlog.
13
  if (!lastChecked) {
14
    await wmill.setState(new Date().toISOString())
15
    return []
16
  }
17

18
  const url = new URL(`${auth.org_url}/api/v1/logs`)
19
  url.searchParams.append("since", lastChecked)
20
  url.searchParams.append("sortOrder", "ASCENDING")
21
  url.searchParams.append("limit", "1000")
22
  if (filter !== undefined && filter !== "")
23
    url.searchParams.append("filter", filter)
24

25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      Authorization: `SSWS ${auth.token}`,
29
      Accept: "application/json",
30
    },
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  const events: Array<{ uuid: string; published: string }> =
38
    await response.json()
39

40
  // `since` is inclusive, so drop events at or before the watermark already emitted.
41
  const fresh = events.filter((e) => e.published > lastChecked)
42

43
  if (fresh.length > 0) {
44
    await wmill.setState(fresh[fresh.length - 1].published)
45
  }
46

47
  return fresh
48
}
49