0

List Projects

by
Published 4 days ago

List Wiz projects with their security score, business impact and resource counts.

Script wiz Verified

The script

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

3
/**
4
 * List Projects
5
 * List Wiz projects (logical groupings of cloud resources used to scope issues and access), with their security score and resource counts.
6
 */
7
export async function main(
8
  auth: RT.Wiz,
9
  first: number | undefined,
10
  after: string | undefined
11
) {
12
  const tokenResponse = await fetch(
13
    auth.auth_url || "https://auth.app.wiz.io/oauth/token",
14
    {
15
      method: "POST",
16
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
17
      body: new URLSearchParams({
18
        grant_type: "client_credentials",
19
        audience: auth.audience || "wiz-api",
20
        client_id: auth.client_id,
21
        client_secret: auth.client_secret,
22
      }),
23
    }
24
  )
25
  if (!tokenResponse.ok) {
26
    throw new Error(`${tokenResponse.status} ${await tokenResponse.text()}`)
27
  }
28
  const { access_token } = (await tokenResponse.json()) as {
29
    access_token: string
30
  }
31

32
  const query = `
33
query ListProjects($first: Int, $after: String) {
34
  projects(first: $first, after: $after) {
35
    totalCount
36
    pageInfo { hasNextPage endCursor }
37
    nodes {
38
      id
39
      name
40
      slug
41
      description
42
      businessUnit
43
      archived
44
      securityScore
45
      riskProfile { businessImpact }
46
      cloudAccountCount
47
      repositoryCount
48
      kubernetesClusterCount
49
      workloadCount
50
      entityCount
51
      teamMemberCount
52
      projectOwners { id }
53
    }
54
  }
55
}`
56

57
  const response = await fetch(auth.api_endpoint, {
58
    method: "POST",
59
    headers: {
60
      Authorization: `Bearer ${access_token}`,
61
      "Content-Type": "application/json",
62
      Accept: "application/json",
63
    },
64
    body: JSON.stringify({
65
      query,
66
      variables: { first: first ?? 50, after: after || null },
67
    }),
68
  })
69

70
  if (!response.ok) {
71
    throw new Error(`${response.status} ${await response.text()}`)
72
  }
73

74
  const result = (await response.json()) as { data?: any; errors?: any }
75
  if (result.errors) {
76
    throw new Error(JSON.stringify(result.errors))
77
  }
78
  return result.data.projects
79
}
80