0

Search rules

by
Published today

Search a device's security rules with SecureTrack rule search text, one page at a time.

Script tufin_securetrack Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * Search rules
5
 * Search the security rules of one device with SecureTrack's rule search text (e.g. an IP, an object name, or "action:accept"). Returns one page; pass next_start as start to get the next one.
6
 */
7
export async function main(
8
  auth: RT.TufinSecuretrack,
9
  device_id: number,
10
  search_text: string | undefined,
11
  start: number | undefined,
12
  count: number | undefined
13
) {
14
  const pageStart = start ?? 0
15
  const pageCount = count || 100
16

17
  const base = auth.base_url.replace(/\/+$/, "")
18
  const url = new URL(`${base}/securetrack/api/rule_search/${device_id}`)
19
  url.searchParams.append("search_text", search_text || "")
20
  url.searchParams.append("start", String(pageStart))
21
  url.searchParams.append("count", String(pageCount))
22

23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
27
      Accept: "application/json",
28
    },
29
  })
30

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

35
  const data = await response.json()
36
  // rules.count is the number of rules in this page, rules.total the number of matches.
37
  const returned = Number(data?.rules?.count ?? 0)
38
  const total = Number(data?.rules?.total ?? 0)
39
  const nextStart = pageStart + returned
40
  return {
41
    ...data,
42
    next_start: returned > 0 && nextStart < total ? nextStart : null,
43
  }
44
}
45