How can I list all image URLs inside a GCP project with an API?

Viewed 122

I'm trying to write an application in GO that will get all the image vulnerabilities inside a GCP project for me using the Container Analysis API.

The GO Client library for this API has the function findVulnerabilityOccurrencesForImage() to do this, however it requires you to pass the URL of the image you want to get the vulnerability report from in the form resourceURL := "https://gcr.io/my-project/my-repo/my-image" and the projectID. This means that if there are multiple images in your project, you have to list and store them first and only after that you can recursively call the findVulnerabilityOccurrencesForImage() function to get ALL of the vulnerabilities.

So I need a way to get and store all of the images' URLs inside all of the repos inside a given GCP project, but so far I couldn't find a solution. I can easily do that in the CLI by running gcloud container images list command but I don't see a way how that can be done with an API.

Thank you in advance for your help!

1 Answers

You can use the Cloud Storage package and the Objects method to do so. For example:

func GetURLs() ([]string, error) {
    bucket := "bucket-name"
    urls := []string{}
    results := client.Bucket(bucket).Objects(context.Background(), nil)

    for {
        attrs, err := results.Next()
        if err != nil {
            if err == iterator.Done {
                break
            }

            return nil, fmt.Errorf("iterating results: %w", err)
        }

        urls = append(urls, fmt.Sprint("https://storage.googleapis.com", "/", bucket, "/", attrs.Name))
    }

    return urls, nil
}
Related