Edits history of script submission #22715 for ' List Cloud Resources (wiz)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Cloud Resources
     * Search the cloud inventory. Filter by free-text search, entity type, cloud subscription, native type or project. Each node wraps the security-graph entity.
     */
    export async function main(
      auth: RT.Wiz,
      search: string | undefined,
      entity_type: string | undefined,
      subscription_external_id: string | undefined,
      project_id: string | undefined,
      first: number | undefined,
      after: string | undefined
    ) {
      const tokenResponse = await fetch(
        auth.auth_url || "https://auth.app.wiz.io/oauth/token",
        {
          method: "POST",
          headers: { "Content-Type": "application/x-www-form-urlencoded" },
          body: new URLSearchParams({
            grant_type: "client_credentials",
            audience: auth.audience || "wiz-api",
            client_id: auth.client_id,
            client_secret: auth.client_secret,
          }),
        }
      )
      if (!tokenResponse.ok) {
        throw new Error(`${tokenResponse.status} ${await tokenResponse.text()}`)
      }
      const { access_token } = (await tokenResponse.json()) as {
        access_token: string
      }
    
      const filterBy: { [key: string]: any } = {}
      if (search !== undefined && search !== "") filterBy.search = search
      if (entity_type !== undefined && entity_type !== "")
        filterBy.type = [entity_type]
      if (subscription_external_id !== undefined && subscription_external_id !== "")
        filterBy.subscriptionExternalId = [subscription_external_id]
      if (project_id !== undefined && project_id !== "")
        filterBy.projectId = [project_id]
    
      const query = `
    query ListCloudResources($filterBy: CloudResourceFilters, $first: Int, $after: String) {
      cloudResources(filterBy: $filterBy, first: $first, after: $after) {
        totalCount
        pageInfo { hasNextPage endCursor }
        nodes {
          id
          name
          type
          subscriptionId
          subscriptionExternalId
          graphEntity {
            id
            providerUniqueId
            name
            type
            firstSeen
            lastSeen
            projects { id }
            properties
          }
        }
      }
    }`
    
      const response = await fetch(auth.api_endpoint, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${access_token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          query,
          variables: { filterBy, first: first ?? 50, after: after || null },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const result = (await response.json()) as { data?: any; errors?: any }
      if (result.errors) {
        throw new Error(JSON.stringify(result.errors))
      }
      return result.data.cloudResources
    }
    

    Submitted by hugo989 5 days ago