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

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

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

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

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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

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

  4. Enable the Artifact Registry and Container Scanning APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  8. 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

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

  10. Enable the Artifact Registry and Container Scanning APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

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

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

צפייה בנקודות החולשה בתמונה

הכלי Artifact Analysis סורק תמונות חדשות כשהן מועלות אל Artifact Registry. בסריקה הזו נחלץ מידע על חבילות המערכת במאגר.

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

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

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

הצגת מקרים במסוף Google Cloud

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

  1. מקבלים את רשימת המאגרים.

    פתיחת הדף Repositories

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

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

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

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

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

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

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

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

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

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

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

צפייה בפגיעויות בשכבה ספציפית

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

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

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

    פתיחת הדף Repositories

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

  3. בדף Repository Details (פרטי המאגר), לוחצים על תמונה.

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

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

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

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

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

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

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

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

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

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

  8. כדי לראות מידע מפורט יותר על פגיעות, לוחצים על הצגה או על הצגת התיקון לצד הפגיעות.

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

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

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

צפייה במקרים באמצעות 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. פקודת ה-build של 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, אתם יכולים לראות את נקודות החולשה של התמונות גם בחלונית הצדדית תובנות אבטחה במסוף Google Cloud .

בחלונית הצדדית Security insights מוצגת סקירה כללית של מידע על אבטחת בנייה של ארטיפקטים שמאוחסנים ב-Artifact Registry. מידע נוסף על חלונית הצד ועל האופן שבו אפשר להשתמש ב-Cloud Build כדי להגן על שרשרת האספקה של התוכנה זמין במאמר הצגת תובנות לגבי אבטחת ה-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 כדי להציג רשימה של מקרים של גילוי. מופעים של 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"

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