1 | |
2 |
|
3 | |
4 | * List address objects |
5 | * List the address objects of a firewall vsys, a Panorama device group or the shared location (PAN-OS REST API). PAN-OS returns every object of the location in one response; the API does not paginate. |
6 | */ |
7 | export async function main( |
8 | auth: RT.PaloAltoPanos, |
9 | location: "vsys" | "device-group" | "shared" | undefined, |
10 | vsys: string | undefined, |
11 | device_group: string | undefined |
12 | ) { |
13 | const version = await restApiVersion(auth) |
14 | const url = new URL(`/restapi/${version}/Objects/Addresses`, auth.base_url) |
15 | setLocation(url, location, vsys, device_group) |
16 |
|
17 | const response = await fetch(url, { |
18 | headers: { "X-PAN-KEY": auth.api_key, Accept: "application/json" }, |
19 | }) |
20 | const text = await response.text() |
21 | if (!response.ok) { |
22 | throw new Error(`${response.status} ${text}`) |
23 | } |
24 | const data = JSON.parse(text) |
25 | if (data["@status"] !== "success") { |
26 | throw new Error(`${response.status} ${text}`) |
27 | } |
28 | return data |
29 | } |
30 |
|
31 | function setLocation( |
32 | url: URL, |
33 | location: string | undefined, |
34 | vsys: string | undefined, |
35 | device_group: string | undefined |
36 | ) { |
37 | const loc = location || "vsys" |
38 | url.searchParams.set("location", loc) |
39 | if (loc === "vsys") { |
40 | url.searchParams.set("vsys", vsys || "vsys1") |
41 | } else if (loc === "device-group") { |
42 | if (!device_group) { |
43 | throw new Error("device_group is required when location is device-group") |
44 | } |
45 | url.searchParams.set("device-group", device_group) |
46 | } |
47 | } |
48 |
|
49 | async function restApiVersion(auth: RT.PaloAltoPanos) { |
50 | if (auth.rest_api_version) return auth.rest_api_version |
51 | const url = new URL("/api/", auth.base_url) |
52 | url.searchParams.set("type", "op") |
53 | url.searchParams.set("cmd", "<show><system><info></info></system></show>") |
54 | const response = await fetch(url, { headers: { "X-PAN-KEY": auth.api_key } }) |
55 | const text = await response.text() |
56 | if (!response.ok || text.includes('status="error"')) { |
57 | throw new Error(`${response.status} ${text}`) |
58 | } |
59 | const match = text.match(/<sw-version>(\d+)\.(\d+)/) |
60 | if (!match) { |
61 | throw new Error( |
62 | `Could not read sw-version from show system info; set rest_api_version on the resource. ${text}` |
63 | ) |
64 | } |
65 | return `v${match[1]}.${match[2]}` |
66 | } |
67 |
|