סריקה אוטומטית של חבילות מערכת הפעלה

במאמר הזה נסביר איך להפעיל את Container Scanning API, להעביר קובץ אימג' ל-Artifact Registry ולראות את רשימת נקודות החולשה שנמצאו בקובץ האימג'.

בעזרת Artifact Analysis אפשר לקבל מידע על נקודות חולשה בקובצי אימג' של קונטיינרים ב-Artifact Registry. המטא-נתונים נשמרים בתור הערות. מופע נוצר לכל מופע של הערה שמשויכת לתמונה. למידע נוסף, קראו את המאמרים סקירה כללית ותמחור.

לפני שמתחילים

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Artifact Registry and Container Scanning APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  4. Install the Google Cloud CLI.

  5. Configure the gcloud CLI to use your federated identity.

    For more information, see Sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. יוצרים מאגר Docker ב-Artifact Registry ומעבירים אליו קובץ אימג' של קונטיינר. אם אתם לא מכירים את Artifact Registry, כדאי לעיין במדריך למתחילים של Docker.

הפעלת ה-API הזה מפעילה גם סריקה של חבילות שפה ב-Artifact Registry. סוגי החבילות הנתמכים

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות לצפייה בתוצאות הסריקה של החבילה, בקשו מהאדמין להקצות לכם את התפקידים הבאים ב-IAM בפרויקט:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

הצגת נקודות החולשה של קובץ האימג'

‫Artifact Analysis סורק קובצי אימג' חדשים כשהם מועלים ל-Artifact Registry. הסריקה הזו מחלצת מידע על חבילות המערכת במאגר.

אתם יכולים לראות את מופעי נקודות החולשה של קובצי האימג' במאגר באמצעות מסוף Google Cloud , ‏Google Cloud CLI או Container Analysis API. אם יש חולשות באחת מהתמונות, תוכלו לקבל את הפרטים שלהן.

‫Artifact Analysis ממשיך לסרוק קובצי אימג' וחבילות כל עוד הם נשלפו ב-30 הימים האחרונים. אחרי 30 יום, המטא-נתונים של התמונות והחבילות שנסרקו לא יתעדכנו יותר, והתוצאות יהיו מיושנות.

ב-Artifact Analysis נשמרים בארכיון מטא-נתונים שלא השתנו במשך יותר מ-90 יום. אפשר להעריך את המטא-נתונים האלה שהועברו לארכיון רק באמצעות ה-API. כדי לסרוק מחדש תמונה עם מטא-נתונים ישנים או שהועברו לארכיון, צריך למשוך את התמונה. רענון המטא-נתונים יכול להימשך עד 24 שעות. אי אפשר לסרוק מחדש חבילות עם מטא-נתונים ישנים או כאלה שהועברו לארכיון.

הצגת המופעים במסוף Google Cloud

כדי לראות את נקודות החולשה בתמונה:

  1. מציגים את רשימת המאגרים.

    פתיחת הדף Repositories

  2. ברשימת המאגרים, לוחצים על מאגר.

  3. ברשימת התמונות, לוחצים על שם של תמונה.

    המספר הכולל של נקודות החולשה בכל סיכום ביקורת של קובץ אימג' מוצג בעמודה Vulnerabilities.

    תמונה סרוקה עם נקודות חולשה

  4. כדי לראות את רשימת נקודות החולשה של קובץ אימג', לוחצים על הקישור בעמודה Vulnerabilities.

    בקטע Scan results מוצג סיכום של סוגי החבילות שנסרקו, המספר הכולל של נקודות החולשה, נקודות החולשה שיש להן תיקונים זמינים, נקודות החולשה שאין להן תיקונים ורמת החומרה בפועל.

    הדף "תוצאות הסריקה" עם רשימה של נקודות חולשה, תיקונים ורמת החומרה בפועל

    בטבלת נקודות החולשה מפורטים השמות של נקודות החולשה והחשיפה (CVE) שנמצאו, רמת החומרה בפועל, הניקוד לפי Common Vulnerability Scoring System‏ (CVSS), תיקונים (אם יש), שם החבילה שמכילה את נקודת החולשה וסוג החבילה.

    במסוףGoogle Cloud מוצגות עד 1,200 נקודות חולשה בכל דף בטבלה הזו. אתם יכולים לסנן ולמיין את הקבצים האלה כדי לבדוק קובץ, ספרייה, שכבה או סוג קובץ ספציפיים לפי סיומת הקובץ.

  5. כדי לראות פרטים על CVE ספציפי, לוחצים על השם שלו.

  6. כדי לראות את פרטי המופע של נקודת החולשה, כמו מספר הגרסה והמיקום המושפע, בשורה עם שם נקודת החולשה לוחצים על View או על View Fixed. טקסט הקישור הוא View לנקודות חולשה ללא תיקון, ו-View Fixed לנקודות חולשה שבהן הוחל תיקון.

הצגת נקודות חולשה בשכבה ספציפית

ב-Artifact Analysis יש מטא-נתונים ספציפיים לשכבות, שעוזרים לזהות איזו חבילה מכילה נקודות חולשה ואיזו פקודת build של Docker הוסיפה את השכבה הזו.

כדי להציג מטא-נתונים של נקודות חולשה שמבוססות על שכבות במסוף Google Cloud :

  1. ב-Artifact Registry, פותחים את רשימת המאגרים.

    פתיחת הדף Repositories

  2. בדף Repositories, לוחצים על מאגר.

  3. בדף Repository Details, לוחצים על תמונה.

  4. ברשימת גרסאות התמונה, לוחצים על תקציר תמונה.

  5. בדף הפרטים של תקציר התמונה, לוחצים על הכרטיסייה Vulnerabilities.

    אם יש מטא-נתונים שמבוססים על שכבות, ב-Artifact Analysis מוצגת רשימה של שכבות שאפשר לבחור בסרגל Layer:

    תמונה של דף נקודות החולשה של תקציר תמונה, כולל סרגל השכבות. בסרגל השכבות יש תפריט נפתח שבו אפשר לבחור שכבה מתוך רשימה ממוספרת של שכבות.

  6. כדי לראות את רשימת השכבות, לוחצים על התפריט הנפתח של השכבות.

    נפתחת רשימה נפתחת עם רשימה ממוספרת של שכבות. שכבה 0 מייצגת את השכבה התחתונה, וכל שכבה נוספת נבנית על גבי השכבה הקודמת. מוצגות רק שכבות שבהן נמצאו נקודות חולשה.

    תמונה של תפריט נפתח פתוח של שכבות. בתפריט מוצגת פקודת Docker שמשמשת ליצירת השכבה, וגם הסוג ומספר נקודות החולשה בכל שכבה.

    לכל שכבה, ב-Artifact Analysis מוצגת פקודת Docker ששימשה ליצירת השכבה ומספר נקודות החולשה שנמצאו בשכבה הזו.

  7. כדי לקבל מידע נוסף על נקודות החולשה בשכבה ספציפית, בוחרים שכבה מרשימת השכבות.

    טבלת נקודות החולשה מתעדכנת ומוצגות בה רק נקודות החולשה שנמצאו בשכבה שנבחרה. אפשר להפעיל מסננים נוספים או לחקור נקודות חולשה ספציפיות.

  8. לוחצים על VIEW או על VIEW FIX לצד נקודת החולשה כדי לראות מידע מפורט יותר על נקודת החולשה.

    בחלונית הצדדית Vulnerability details מוצגים מיקומי הקבצים שבהם יש נקודות חולשה בחבילות.

  9. בטבלה מיקומי קבצים, מעבירים את העכבר מעל סימן השאלה כדי לפתוח מטא-נתונים נוספים של השכבה.

    בתיבת הדו-שיח מוצגים אינדקס השכבה, פקודת ה-build של השכבה, תקציר השכבה ותמונת הבסיס שבה אפשר לתקן את נקודת החולשה.

הצגת מופעים באמצעות gcloud

כדי לראות את המופעים של קובצי אימג' ב-Artifact Registry, מריצים את הפקודה gcloud artifacts docker images list:

  gcloud artifacts docker images list --show-occurrences \
      LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

כאשר:

  • ‫LOCATION הוא המיקום של המאגר באזור או במספר אזורים.
  • ‫PROJECT_ID הוא מזהה הפרויקט במסוף Google Cloud .
  • ‫REPOSITORY הוא שם המאגר שבו מאוחסנת התמונה.
  • ‫IMAGE_ID הוא שם קובץ האימג' במאגר. אי אפשר לציין תג תמונה בפקודה הזו.

כברירת מחדל, הפקודה מחזירה את 10 התוצאות האחרונות. כדי להציג מספר שונה של תוצאות, משתמשים בדגל --show-occurrences-from. לדוגמה, הפקודה הבאה מחזירה את 25 התוצאות האחרונות:

gcloud artifacts docker images list --show-occurrences-from=25 \
    us-central1-docker.pkg.dev/my-project/my-repo/my-image

הפלט אמור להיראות כך:

תמונה לדוגמה שנוצרה

  {
      "name": "projects/my-project/locations/us-central1/occurrences/22d3edfb-34f2-4d3f-be08-fd90c549f010",
      "resource": {
        "uri": "https://us-central1-docker.pkg.dev/my-project/my-repo/golang-nginx@sha256:5b54ef20748417ccdb0b87afedf98c44e699e2f59bd6cv48cbdc1ca2e72c80e3"
      },
      "noteName": "projects/my-project/locations/us-central1/notes/CVE-2005-2541",
      "kind": "VULNERABILITY",
      "createTime": "2025-01-21T15:57:01.238832Z",
      "updateTime": "2025-01-21T15:57:01.238832Z",
      "vulnerability": {
        "severity": "CRITICAL",
        "cvssScore": 10,
        "packageIssue": [
          {
            "affectedLocation": {
              "cpeUri": "cpe:/o:debian:debian_linux:12",
              "package": "tar",
              "version": {
                "name": "1.34+dfsg",
                "revision": "1.2+deb12u1",
                "kind": "NORMAL"
              }
            },
            "fixedLocation": {
              "cpeUri": "cpe:/o:debian:debian_linux:12",
              "package": "tar",
              "version": {
                "kind": "MAXIMUM"
              }
            },
            "packageType": "OS",
            "effectiveSeverity": "LOW",
            "file_location": [
                {
                "file_path": "/var/lib/dpkg/status",
                "layerDetails": {
                  "index": 0,
                  "diffID": "sha256:123",
                  "buildCommand": "FROM golang:example_sha256",
                  "BaseImages": [
                    {
                      "name": "library/golang",
                    },
                  ],
                },
              },
            ],
          }
        ],
        "shortDescription": "CVE-2005-2541",
        "longDescription": "NIST vectors: AV:N/AC:L/Au:N/C:C/I:C/A:C",
        "relatedUrls": [
          {
            "url": "https://security-tracker.debian.org/tracker/CVE-2005-2541",
            "label": "More Info"
          }
        ],
        "effectiveSeverity": "LOW",
        "cvssVersion": "CVSS_VERSION_2",
        "cvssV2": {
          "baseScore": 10,
          "attackVector": "ATTACK_VECTOR_NETWORK",
          "attackComplexity": "ATTACK_COMPLEXITY_LOW",
          "authentication": "AUTHENTICATION_NONE",
          "confidentialityImpact": "IMPACT_COMPLETE",
          "integrityImpact": "IMPACT_COMPLETE",
          "availabilityImpact": "IMPACT_COMPLETE"
        }
      }
    }
  

סינון מופעים של נקודות חולשה

כדי לסנן מופעים של נקודות חולשה, משתמשים בפרמטר --occurrence-filter:

gcloud artifacts docker images list --show-occurrences \
    LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID \
    --occurrence-filter=FILTER_EXPRESSION

כאשר:

  • ‫LOCATION הוא המיקום של המאגר, באזור אחד או במספר אזורים.
  • ‫PROJECT_ID הוא מזהה הפרויקט במסוף Google Cloud .
  • ‫REPOSITORY הוא שם המאגר שבו מאוחסן קובץ האימג'.
  • ‫IMAGE_ID הוא שם קובץ האימג' במאגר.
  • ‫FILTER_EXPRESSION הוא ביטוי סינון לדוגמה בפורמט שמוסבר בקטע סינון מופעים של נקודות חולשה.

הצגת נקודות חולשה בשכבה ספציפית

אפשר להשתמש בשדה layerDetails כדי לזהות את השכבה הספציפית שבה נמצאת החבילה עם נקודת החולשה, ולראות מידע נוסף על השכבה הזו. לדוגמה, נניח שזה הפלט של layerDetails לתמונה:

  "layerDetails": {
    "index": 0,
    "diffID": "sha256:123",
    "buildCommand": "FROM golang:example_sha256",
    "BaseImages": [
      {
        "name": "library/golang",
      },
    ],
  }

בפלט אפשר לראות שאינדקס השכבה הוא 0 בתמונה, עם גיבוב שכבה של sha256:123. הפקודה של Docker ליצירת השכבה הזו היא FROM golang:example_sha256, והשכבה נמצאת גם בתמונת הבסיס library/golang.

הצגת המופעים באמצעות ה-API או הקוד

כדי להציג מופעים של תמונה, צריך לציין תמונות עם כתובת URL בפורמט הבא:

LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

API

באמצעות cURL

כדי לקבל רשימה של המופעים בפרויקט:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences

כדי לקבל סיכום של נקודות החולשה בפרויקט:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences:vulnerabilitySummary

כדי לקבל פרטים על מופע ספציפי:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences/OCCURRENCE_ID

Java

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Java API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.devtools.containeranalysis.v1.ContainerAnalysisClient;
import io.grafeas.v1.GrafeasClient;
import io.grafeas.v1.Occurrence;
import io.grafeas.v1.ProjectName;
import java.io.IOException;
import java.lang.InterruptedException;

public class OccurrencesForImage {
  // Retrieves all the Occurrences associated with a specified image
  // Here, all Occurrences are simply printed and counted
  public static int getOccurrencesForImage(String resourceUrl, String projectId)
      throws IOException, InterruptedException {
    // String resourceUrl = "https://gcr.io/project/image@sha256:123";
    // String projectId = "my-project-id";
    final String projectName = ProjectName.format(projectId);
    final String filterStr = String.format("resourceUrl=\"%s\"", resourceUrl);

    // Initialize client that will be used to send requests. After completing all of your requests, 
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    int i = 0;
    for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
      // Write custom code to process each Occurrence here
      System.out.println(o.getName());
      i = i + 1;
    }
    return i;
  }
}

Go

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Go API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"
	"io"

	containeranalysis "cloud.google.com/go/containeranalysis/apiv1"
	"google.golang.org/api/iterator"
	grafeaspb "google.golang.org/genproto/googleapis/grafeas/v1"
)

// getOccurrencesForImage retrieves all the Occurrences associated with a specified image.
// Here, all Occurrences are simply printed and counted.
func getOccurrencesForImage(w io.Writer, resourceURL, projectID string) (int, error) {
	// Use this style of URL when you use Google Container Registry.
	// resourceURL := "https://gcr.io/my-project/my-repo/my-image"
	// Use this style of URL when you use Google Artifact Registry.
	// resourceURL := "https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image"
	ctx := context.Background()
	client, err := containeranalysis.NewClient(ctx)
	if err != nil {
		return -1, fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &grafeaspb.ListOccurrencesRequest{
		Parent: fmt.Sprintf("projects/%s", projectID),
		Filter: fmt.Sprintf("resourceUrl=%q", resourceURL),
	}
	it := client.GetGrafeasClient().ListOccurrences(ctx, req)
	count := 0
	for {
		occ, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return -1, fmt.Errorf("occurrence iteration error: %w", err)
		}
		// Write custom code to process each Occurrence here.
		fmt.Fprintln(w, occ)
		count = count + 1
	}
	return count, nil
}

Node.js

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Node.js API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

/**
 * TODO(developer): Uncomment these variables before running the sample
 */
// const projectId = 'your-project-id', // Your GCP Project ID
// If you are using Google Container Registry
// const imageUrl = 'https://gcr.io/my-project/my-repo/my-image@sha256:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// const imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image@sha256:123' // Image to attach metadata to

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Retrieves all the Occurrences associated with a specified image
const [occurrences] = await client.getGrafeasClient().listOccurrences({
  parent: formattedParent,
  filter: `resourceUrl = "${imageUrl}"`,
});

if (occurrences.length) {
  console.log(`Occurrences for ${imageUrl}`);
  occurrences.forEach(occurrence => {
    console.log(`${occurrence.name}:`);
  });
} else {
  console.log('No occurrences found.');
}

Ruby

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Ruby API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

# resource_url = "The URL of the resource associated with the occurrence."
#                # e.g. https://gcr.io/project/image@sha256:123"
# project_id   = "The Google Cloud project ID of the occurrences to retrieve"

require "google/cloud/container_analysis"

# Initialize the client
client = Google::Cloud::ContainerAnalysis.container_analysis.grafeas_client

parent = client.project_path project: project_id
filter = "resourceUrl = \"#{resource_url}\""
count = 0
client.list_occurrences(parent: parent, filter: filter).each do |occurrence|
  # Process occurrence here
  puts occurrence
  count += 1
end
puts "Found #{count} occurrences"

Python

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Python API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud.devtools import containeranalysis_v1


def get_occurrences_for_image(resource_url: str, project_id: str) -> int:
    """Retrieves all the occurrences associated with a specified image.
    Here, all occurrences are simply printed and counted."""
    # resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
    # project_id = 'my-gcp-project'

    filter_str = f'resourceUrl="{resource_url}"'
    client = containeranalysis_v1.ContainerAnalysisClient()
    grafeas_client = client.get_grafeas_client()
    project_name = f"projects/{project_id}"

    response = grafeas_client.list_occurrences(parent=project_name, filter=filter_str)
    count = 0
    for o in response:
        # do something with the retrieved occurrence
        # in this sample, we will simply count each one
        count += 1
    return count

הצגת המופעים ב-Cloud Build

אם אתם משתמשים ב-Cloud Build, אתם יכולים גם לראות את נקודות החולשה של קובץ האימג' בחלונית הצדדית Security insights במסוף Google Cloud .

בחלונית הצדדית Security insights מוצגת סקירה כללית של מידע על אבטחת פריטים שנוצרו ומאוחסנים ב-Artifact Registry. במאמר הצגת תובנות לגבי אבטחת גרסאות build מוסבר בהרחבה על החלונית הצדדית ואיך אפשר להשתמש ב-Cloud Build כדי להגן על שרשרת האספקה של התוכנה.

סינון מופעים

אתם יכולים להשתמש במחרוזות סינון בפקודות gcloud וב-Artifact Analysis API כדי לסנן מופעים לפני הצגתם. בקטעים הבאים מתוארים מסנני החיפוש הנתמכים.

צפייה במופעי הגילוי

כשקובץ אימג' נשלח לראשונה ל-Artifact Registry, נוצר מופע גילוי שמכיל מידע על הסריקה הראשונית של קובץ האימג' של הקונטיינר.

כדי לאחזר את מופע הגילוי של תמונה, משתמשים בביטוי הסינון הבא:

kind="DISCOVERY" AND resourceUrl="RESOURCE_URL"

כאשר RESOURCE_URL הוא בפורמט הבא:

LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

gcloud

כדי לראות את המופעים של תמונה מסוימת:

במקרה הזה, הביטוי לא משמש ישירות בפקודה, אבל אותו מידע מועבר כארגומנטים:

‫Artifact Registry:

gcloud artifacts docker images list --show-occurrences \
--occurrence-filter='kind="DISCOVERY"' --format=json \
LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

API

כדי לאחזר את מופע הגילוי, ביטוי הסינון צריך להיות מקודד בכתובת URL ומוטמע בבקשת GET באופן הבא:

GET https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences?filter=kind%3D%22DISCOVERY%22%20AND%20resourceUrl%3D%22ENCODED_RESOURCE_URL%22

לפרטים נוספים, ראו נקודת הקצה ל-API‏ projects.occurrences.get.

Java

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Java API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.devtools.containeranalysis.v1.ContainerAnalysisClient;
import io.grafeas.v1.GrafeasClient;
import io.grafeas.v1.Occurrence;
import io.grafeas.v1.ProjectName;
import java.io.IOException;
import java.lang.InterruptedException;

public class GetDiscoveryInfo {
  // Retrieves and prints the Discovery Occurrence created for a specified image
  // The Discovery Occurrence contains information about the initial scan on the image
  public static void getDiscoveryInfo(String resourceUrl, String projectId) 
      throws IOException, InterruptedException {
    // String resourceUrl = "https://gcr.io/project/image@sha256:123";
    // String projectId = "my-project-id";
    String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + resourceUrl + "\"";
    final String projectName = ProjectName.format(projectId);

    // Initialize client that will be used to send requests. After completing all of your requests, 
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
      System.out.println(o);
    }
  }
}

Go

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Go API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"
	"io"

	containeranalysis "cloud.google.com/go/containeranalysis/apiv1"
	"google.golang.org/api/iterator"
	grafeaspb "google.golang.org/genproto/googleapis/grafeas/v1"
)

// getDiscoveryInfo retrieves and prints the Discovery Occurrence created for a specified image.
// The Discovery Occurrence contains information about the initial scan on the image.
func getDiscoveryInfo(w io.Writer, resourceURL, projectID string) error {
	// Use this style of URL when you use Google Container Registry.
	// resourceURL := "https://gcr.io/my-project/my-repo/my-image"
	// Use this style of URL when you use Google Artifact Registry.
	// resourceURL := "https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image"
	ctx := context.Background()
	client, err := containeranalysis.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &grafeaspb.ListOccurrencesRequest{
		Parent: fmt.Sprintf("projects/%s", projectID),
		Filter: fmt.Sprintf(`kind="DISCOVERY" AND resourceUrl=%q`, resourceURL),
	}
	it := client.GetGrafeasClient().ListOccurrences(ctx, req)
	for {
		occ, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("occurrence iteration error: %w", err)
		}
		fmt.Fprintln(w, occ)
	}
	return nil
}

Node.js

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Node.js API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

/**
 * TODO(developer): Uncomment these variables before running the sample
 */
// const projectId = 'your-project-id', // Your GCP Project ID
// If you are using Google Container Registry
// const imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// const imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.getGrafeasClient().projectPath(projectId);
// Retrieves and prints the Discovery Occurrence created for a specified image
// The Discovery Occurrence contains information about the initial scan on the image
const [occurrences] = await client.getGrafeasClient().listOccurrences({
  parent: formattedParent,
  filter: `kind = "DISCOVERY" AND resourceUrl = "${imageUrl}"`,
});

if (occurrences.length > 0) {
  console.log(`Discovery Occurrences for ${imageUrl}`);
  occurrences.forEach(occurrence => {
    console.log(`${occurrence.name}:`);
  });
} else {
  console.log('No occurrences found.');
}

Ruby

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Ruby API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

# resource_url = "The URL of the resource associated with the occurrence."
#                # e.g. https://gcr.io/project/image@sha256:123
# project_id   = "The Google Cloud project ID of the occurrences to retrieve"

require "google/cloud/container_analysis"

# Initialize the client
client = Google::Cloud::ContainerAnalysis.container_analysis.grafeas_client

parent = client.project_path project: project_id
filter = "kind = \"DISCOVERY\" AND resourceUrl = \"#{resource_url}\""
client.list_occurrences(parent: parent, filter: filter).each do |occurrence|
  # Process discovery occurrence here
  puts occurrence
end

Python

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Python API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

from google.cloud.devtools import containeranalysis_v1


def get_discovery_info(resource_url: str, project_id: str) -> None:
    """Retrieves and prints the discovery occurrence created for a specified
    image. The discovery occurrence contains information about the initial
    scan on the image."""
    # resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
    # project_id = 'my-gcp-project'

    filter_str = f'kind="DISCOVERY" AND resourceUrl="{resource_url}"'
    client = containeranalysis_v1.ContainerAnalysisClient()
    grafeas_client = client.get_grafeas_client()
    project_name = f"projects/{project_id}"
    response = grafeas_client.list_occurrences(parent=project_name, filter_=filter_str)
    for occ in response:
        print(occ)

הצגת מופעים של נקודות חולשה

כדי להציג מופעים של נקודות חולשה בתמונה ספציפית, יוצרים שאילתה עם ביטוי סינון:

kind="VULNERABILITY" AND resourceUrl="RESOURCE_URL"

כאשר RESOURCE_URL הוא בפורמט הבא:

LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

gcloud

במקרה הזה, הביטוי לא משמש ישירות בפקודה, אבל אותו מידע מועבר כארגומנטים:

Artifact Registry

gcloud artifacts docker images list --show-occurrences \
--occurrence-filter='kind="VULNERABILITY"' --format=json \
LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

API

כתובת ה-URL של המשאב צריכה להיות בקידוד URL, ומוטמעת בבקשת GET באופן הבא:

GET https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences?filter=kind%3D%22VULNERABILITY%22%20AND%20resourceUrl%3D%22ENCODED_RESOURCE_URL%22

לפרטים נוספים, ראו נקודת הקצה ל-API‏ projects.occurrences.get.

Java

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Java API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

import com.google.cloud.devtools.containeranalysis.v1.ContainerAnalysisClient;
import io.grafeas.v1.GrafeasClient;
import io.grafeas.v1.Occurrence;
import io.grafeas.v1.ProjectName;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class VulnerabilityOccurrencesForImage {
  // Retrieve a list of vulnerability occurrences assoviated with a resource
  public static List<Occurrence> findVulnerabilityOccurrencesForImage(String resourceUrl, 
      String projectId) throws IOException {
    // String resourceUrl = "https://gcr.io/project/image@sha256:123";
    // String projectId = "my-project-id";
    final String projectName = ProjectName.format(projectId);
    String filterStr = String.format("kind=\"VULNERABILITY\" AND resourceUrl=\"%s\"", resourceUrl);

    // Initialize client that will be used to send requests. After completing all of your requests, 
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    LinkedList<Occurrence> vulnerabilitylist = new LinkedList<Occurrence>();
    for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
      vulnerabilitylist.add(o);
    }
    return vulnerabilitylist;
  }
}

Go

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Go API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"

	containeranalysis "cloud.google.com/go/containeranalysis/apiv1"
	"google.golang.org/api/iterator"
	grafeaspb "google.golang.org/genproto/googleapis/grafeas/v1"
)

// findVulnerabilityOccurrencesForImage retrieves all vulnerability Occurrences associated with a resource.
func findVulnerabilityOccurrencesForImage(resourceURL, projectID string) ([]*grafeaspb.Occurrence, error) {
	// Use this style of URL when you use Google Container Registry.
	// resourceURL := "https://gcr.io/my-project/my-repo/my-image"
	// Use this style of URL when you use Google Artifact Registry.
	// resourceURL := "https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image"
	ctx := context.Background()
	client, err := containeranalysis.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("NewClient: %w", err)
	}
	defer client.Close()

	req := &grafeaspb.ListOccurrencesRequest{
		Parent: fmt.Sprintf("projects/%s", projectID),
		Filter: fmt.Sprintf("resourceUrl = %q kind = %q", resourceURL, "VULNERABILITY"),
	}

	var occurrenceList []*grafeaspb.Occurrence
	it := client.GetGrafeasClient().ListOccurrences(ctx, req)
	for {
		occ, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("occurrence iteration error: %w", err)
		}
		occurrenceList = append(occurrenceList, occ)
	}

	return occurrenceList, nil
}

Node.js

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Node.js API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

/**
 * TODO(developer): Uncomment these variables before running the sample
 */
// const projectId = 'your-project-id', // Your GCP Project ID
// If you are using Google Container Registry
// const imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// const imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Retrieve a list of vulnerability occurrences assoviated with a resource
const [occurrences] = await client.getGrafeasClient().listOccurrences({
  parent: formattedParent,
  filter: `kind = "VULNERABILITY" AND resourceUrl = "${imageUrl}"`,
});

if (occurrences.length) {
  console.log(`All Vulnerabilities for ${imageUrl}`);
  occurrences.forEach(occurrence => {
    console.log(`${occurrence.name}:`);
  });
} else {
  console.log('No occurrences found.');
}

Ruby

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Ruby API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

# resource_url = "The URL of the resource associated with the occurrence
#                e.g. https://gcr.io/project/image@sha256:123"
# project_id   = "The Google Cloud project ID of the vulnerabilities to find"

require "google/cloud/container_analysis"

# Initialize the client
client = Google::Cloud::ContainerAnalysis.container_analysis.grafeas_client

parent = client.project_path project: project_id
filter = "resourceUrl = \"#{resource_url}\" AND kind = \"VULNERABILITY\""
client.list_occurrences parent: parent, filter: filter

Python

במאמר ספריות הלקוח של Artifact Analysis מוסבר איך להתקין את ספריית הלקוח ולהשתמש בה ב-Artifact Analysis. למידע נוסף, קראו את מאמרי העזרה של Artifact Analysis Python API.

כדי לבצע אימות ב-Artifact Analysis, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

from typing import List

from google.cloud.devtools import containeranalysis_v1
from grafeas.grafeas_v1 import types


def find_vulnerabilities_for_image(
    resource_url: str, project_id: str
) -> List[types.grafeas.Occurrence]:
    """ "Retrieves all vulnerability occurrences associated with a resource."""
    # resource_url = 'https://gcr.io/my-project/my-image@sha256:123'
    # project_id = 'my-gcp-project'

    client = containeranalysis_v1.ContainerAnalysisClient()
    grafeas_client = client.get_grafeas_client()
    project_name = f"projects/{project_id}"

    filter_str = 'kind="VULNERABILITY" AND resourceUrl="{}"'.format(resource_url)
    return list(grafeas_client.list_occurrences(parent=project_name, filter=filter_str))

הצגת מופעים של סוג ספציפי

בשתי הדוגמאות הקודמות, ההבדל היחיד בין ביטויי הסינון הוא הערך של kind, שמזהה את סוג המופע. אפשר להשתמש בשדה הזה כדי להגביל את רשימת המופעים לסוג מסוים, כמו פגיעוּת או פריסה.

כדי לאחזר מופעים של תמונה ספציפית, משתמשים בביטוי הסינון הבא:

kind="NOTE_KIND" AND resourceUrl="RESOURCE_URL"

כאשר:

  • ‫NOTE_KIND הוא סוג ההערה.
    • לדוגמה, אפשר להשתמש בסוג DISCOVERY כדי להציג את מופעי הגילוי. מופעי גילוי נוצרים לתמונות כשהן נשלחות לראשונה ל-Artifact Registry.
    • כדי להציג רשימה של מופעי נקודות חולשה, משתמשים בסוג VULNERABILITY.
  • ‫RESOURCE_URL היא כתובת ה-URL המלאה של התמונה https://HOSTNAME/PROJECT_ID/IMAGE_ID@sha256:HASH

ביטוי המסנן לאחזור מופעים של סוג מסוים בתמונות רבות הוא:

kind="NOTE_KIND" AND has_prefix(resourceUrl, "RESOURCE_URL_PREFIX")

כאשר:

  • ‫RESOURCE_URL_PREFIX היא הקידומת של כתובת ה-URL של חלק מהתמונות
    • כדי להציג את כל הגרסאות של תמונה: https://HOSTNAME/PROJECT_ID/IMAGE_ID@
    • כדי להציג את כל התמונות בפרויקט: https://HOSTNAME/PROJECT_ID/

לראות תמונות שמשויכות לפתק ספציפי

אתם יכולים לאחזר רשימה של משאבים שמשויכים למזהה הערה ספציפי. לדוגמה, אתם יכולים להציג רשימה של קובצי אימג' עם פגיעות CVE ספציפית.

כדי להציג רשימה של כל התמונות בפרויקט שמשויכות להערה מסוימת, משתמשים בביטוי הסינון הבא:

noteProjectId="PROVIDER_PROJECT_ID" AND noteId="NOTE_ID"

כדי לבדוק תמונה ספציפית עם הערה ספציפית, משתמשים בביטוי הסינון הבא:

resourceUrl="RESOURCE_URL" AND noteProjectId="PROVIDER_PROJECT_ID" \
    AND noteId="NOTE_ID"

כאשר:

  • ‫PROVIDER_PROJECT_ID הוא המזהה של פרויקט הספק. לדוגמה, goog-vulnz מספק את ניתוח נקודות החולשה שמוגדר כברירת מחדל.
  • ‫NOTE_ID הוא המזהה של ההערה. הערות שקשורות לאבטחה בדרך כלל מופיעות בפורמט CVE-2019-12345.
  • ‫RESOURCE_URL היא כתובת ה-URL המלאה של התמונה https://HOSTNAME/PROJECT_ID/IMAGE_ID@sha256:HASH

לדוגמה, כדי לבדוק את כל קובצי האימג' שמופיע בהם CVE-2017-16231, כפי שנותח על ידי Google, משתמשים בביטוי הסינון הבא:

noteProjectId="goog-vulnz" AND noteId="CVE-2017-16231"

המאמרים הבאים