0

List Cloud Resources

by
Published 4 days ago

Search the cloud inventory by free-text, entity type, subscription or project. Each node wraps the security-graph entity.

Script wiz Verified

The script

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

3
/**
4
 * List Cloud Resources
5
 * Search the cloud inventory. Filter by free-text search, entity type, cloud subscription, native type or project. Each node wraps the security-graph entity.
6
 */
7
export async function main(
8
  auth: RT.Wiz,
9
  search: string | undefined,
10
  entity_type: string | undefined,
11
  subscription_external_id: string | undefined,
12
  project_id: string | undefined,
13
  first: number | undefined,
14
  after: string | undefined
15
) {
16
  const tokenResponse = await fetch(
17
    auth.auth_url || "https://auth.app.wiz.io/oauth/token",
18
    {
19
      method: "POST",
20
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
21
      body: new URLSearchParams({
22
        grant_type: "client_credentials",
23
        audience: auth.audience || "wiz-api",
24
        client_id: auth.client_id,
25
        client_secret: auth.client_secret,
26
      }),
27
    }
28
  )
29
  if (!tokenResponse.ok) {
30
    throw new Error(`${tokenResponse.status} ${await tokenResponse.text()}`)
31
  }
32
  const { access_token } = (await tokenResponse.json()) as {
33
    access_token: string
34
  }
35

36
  const filterBy: { [key: string]: any } = {}
37
  if (search !== undefined && search !== "") filterBy.search = search
38
  if (entity_type !== undefined && entity_type !== "")
39
    filterBy.type = [entity_type]
40
  if (subscription_external_id !== undefined && subscription_external_id !== "")
41
    filterBy.subscriptionExternalId = [subscription_external_id]
42
  if (project_id !== undefined && project_id !== "")
43
    filterBy.projectId = [project_id]
44

45
  const query = `
46
query ListCloudResources($filterBy: CloudResourceFilters, $first: Int, $after: String) {
47
  cloudResources(filterBy: $filterBy, first: $first, after: $after) {
48
    totalCount
49
    pageInfo { hasNextPage endCursor }
50
    nodes {
51
      id
52
      name
53
      type
54
      subscriptionId
55
      subscriptionExternalId
56
      graphEntity {
57
        id
58
        providerUniqueId
59
        name
60
        type
61
        firstSeen
62
        lastSeen
63
        projects { id }
64
        properties
65
      }
66
    }
67
  }
68
}`
69

70
  const response = await fetch(auth.api_endpoint, {
71
    method: "POST",
72
    headers: {
73
      Authorization: `Bearer ${access_token}`,
74
      "Content-Type": "application/json",
75
      Accept: "application/json",
76
    },
77
    body: JSON.stringify({
78
      query,
79
      variables: { filterBy, first: first ?? 50, after: after || null },
80
    }),
81
  })
82

83
  if (!response.ok) {
84
    throw new Error(`${response.status} ${await response.text()}`)
85
  }
86

87
  const result = (await response.json()) as { data?: any; errors?: any }
88
  if (result.errors) {
89
    throw new Error(JSON.stringify(result.errors))
90
  }
91
  return result.data.cloudResources
92
}
93