0

Update named list items

by
Published today

Add items to and/or remove items from a Threat Defense named list without replacing the whole list.

Script infoblox_csp Verified

The script

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

3
/**
4
 * Update named list items
5
 * Add items to and/or remove items from a Threat Defense named list without replacing the whole list. Items are FQDNs or IPv4/IPv6 addresses/CIDRs; additions can carry a description via add_described. Removals run after additions.
6
 */
7
export async function main(
8
  auth: RT.InfobloxCsp,
9
  id: number,
10
  add: string[] | undefined,
11
  add_described: { item: string; description?: string }[] | undefined,
12
  remove: string[] | undefined
13
) {
14
  if (!add?.length && !add_described?.length && !remove?.length) {
15
    throw new Error("Provide at least one item in add, add_described or remove")
16
  }
17

18
  const url = new URL(
19
    `${(auth.base_url || "https://csp.infoblox.com").replace(/\/+$/, "")}/api/atcfw/v1/named_lists/${id}/items`
20
  )
21
  const headers = {
22
    Authorization: `Token ${auth.api_key}`,
23
    "Content-Type": "application/json",
24
    Accept: "application/json",
25
  }
26

27
  let added: unknown = null
28
  if (add?.length || add_described?.length) {
29
    const response = await fetch(url, {
30
      method: "POST",
31
      headers,
32
      body: JSON.stringify({
33
        items: add?.length ? add : undefined,
34
        items_described: add_described?.length ? add_described : undefined,
35
      }),
36
    })
37
    if (!response.ok) {
38
      throw new Error(`${response.status} ${await response.text()}`)
39
    }
40
    const text = await response.text()
41
    added = text ? JSON.parse(text) : { success: true }
42
  }
43

44
  let removed: unknown = null
45
  if (remove?.length) {
46
    const response = await fetch(url, {
47
      method: "DELETE",
48
      headers,
49
      body: JSON.stringify({ items: remove }),
50
    })
51
    if (!response.ok) {
52
      throw new Error(`${response.status} ${await response.text()}`)
53
    }
54
    const text = await response.text()
55
    removed = text ? JSON.parse(text) : { success: true }
56
  }
57

58
  return { added, removed }
59
}
60