Configura las notificaciones de Pub/Sub

En este documento, se describe cómo configurar las notificaciones sobre actualizaciones de notas y casos.

Artifact Analysis proporciona notificaciones a través de Pub/Sub sobre las vulnerabilidades detectadas en el análisis automatizado y sobre otros metadatos. Cuando se crea o actualiza una nota o un caso, se publica un mensaje en el tema correspondiente para cada versión de la API. Usa el tema para la versión de la API que estás usando.

Antes de comenzar

  1. Accede a tu cuenta de Google Cloud . Si eres nuevo en Google Cloud, crea una cuenta para evaluar el rendimiento de nuestros productos en situaciones reales. Los clientes nuevos también obtienen $300 en créditos gratuitos para ejecutar, probar y, además, implementar cargas de trabajo.
  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. Enable the Container Analysis API.

    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 API

  4. Instala Google Cloud CLI.

  5. Si usas un proveedor de identidad externo (IdP), primero debes acceder a la gcloud CLI con tu identidad federada.

  6. Para inicializar gcloud CLI, ejecuta el siguiente comando:

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

  8. Enable the Container Analysis API.

    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 API

  9. Instala Google Cloud CLI.

  10. Si usas un proveedor de identidad externo (IdP), primero debes acceder a la gcloud CLI con tu identidad federada.

  11. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  12. Obtén información para configurar el control de acceso a los metadatos de tu proyecto. Omite este paso si solo consumes metadatos de las ocurrencias creadas por el análisis de contenedores de Artifact Analysis.

Crea temas de Pub/Sub

Después de activar la API de Artifact Analysis, esta crea automáticamente temas de Pub/Sub con los siguientes IDs de tema:

  • container-analysis-notes-v1
  • container-analysis-occurrences-v1

Si los temas se borraron por accidente o no se encuentran disponibles, puedes agregarlos tú mismo. Por ejemplo, es posible que falten los temas si tu organización de Google Cloudtiene una restricción de política de la organización que requiere encriptación con claves de encriptación administradas por el cliente (CMEK). Cuando la API de Pub/Sub se encuentra en la lista de bloqueo de esta restricción, los servicios no pueden crear automáticamente temas conGoogle-owned and Google-managed encryption keys.

Para crear los temas con Google-owned and Google-managed encryption keys, haz lo siguiente:

Console

  1. Ve a la página de temas de Pub/Sub en la Google Cloud consola.

    Abrir la página de temas de Pub/Sub

  2. Haz clic en Crear tema.

  3. Ingresa un ID de tema:

    container-analysis-notes-v1
    

    para que el nombre coincida con el URI:

    projects/PROJECT_ID/topics/container-analysis-notes-v1
    

    En el ejemplo anterior, PROJECT_ID es el Google Cloud ID del proyecto.

  4. Haz clic en Crear.

  5. Ingresa un ID de tema:

    container-analysis-occurrences-v1
    

    para que el nombre coincida con el URI:

    projects/PROJECT_ID/topics/container-analysis-occurrences-v1
    

gcloud

Ejecuta el siguiente comando en el shell o la ventana de la terminal:

gcloud pubsub topics create projects/PROJECT_ID/topics/container-analysis-notes-v1
gcloud pubsub topics create projects/PROJECT_ID/topics/container-analysis-occurrences-v1

Para obtener más información sobre el comando gcloud pubsub topics, consulta la documentación de topics.

Para crear los temas con encriptación de CMEK, consulta las instrucciones para encriptar temas de Pub/Sub.

Cada vez que se crea o actualiza una nota o un caso, se publica un mensaje en el tema correspondiente, aunque también debes crear una suscripción a Pub/Sub para escuchar eventos y recibir mensajes del servicio de Pub/Sub.

Crea suscripciones a Pub/Sub

Para escuchar eventos, crea una suscripción a Pub/Sub asociada con el tema:

Console

  1. Ve a la página de suscripciones a Pub/Sub en laGoogle Cloud consola.

    Abre la página de suscripciones a Pub/Sub.

  2. Haz clic en Crear suscripción.

  3. Escribe un nombre para la suscripción. Por ejemplo, notas.

  4. Ingresa el URI del tema para las notas:

    projects/PROJECT_ID/topics/container-analysis-notes-v1
    

    En el ejemplo anterior, PROJECT_ID es el Google Cloud ID del proyecto.

  5. Haz clic en Crear.

  6. Crea otra suscripción para los casos con el URI:

    projects/PROJECT_ID/topics/container-analysis-occurrences-v1
    

gcloud

Para recibir eventos de Pub/Sub, primero debes crear una suscripción asociada con el tema container-analysis-occurrences-v1:

gcloud pubsub subscriptions create \
    --topic container-analysis-occurrences-v1 occurrences

A partir de ahora, puedes extraer mensajes relacionados con tus casos mediante tu nueva suscripción:

gcloud pubsub subscriptions pull \
    --auto-ack occurrences

Java

Para obtener información sobre cómo instalar y usar la biblioteca cliente de Artifact Analysis, consulta las bibliotecas cliente de Artifact Analysis. Para obtener más información, consulta la documentación de referencia de la API de Artifact Analysis Java.

Para autenticarte en Artifact Analysis, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import com.google.cloud.pubsub.v1.AckReplyConsumer;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.PushConfig;
import com.google.pubsub.v1.Subscription;
import com.google.pubsub.v1.SubscriptionName;
import com.google.pubsub.v1.TopicName;
import io.grpc.StatusRuntimeException;
import java.io.IOException;
import java.lang.InterruptedException;
import java.util.concurrent.TimeUnit;

public class Subscriptions {
  // Handle incoming Occurrences using a Cloud Pub/Sub subscription
  public static int pubSub(String subId, long timeoutSeconds, String projectId)
      throws InterruptedException {
    // String subId = "my-occurrence-subscription";
    // long timeoutSeconds = 20;
    // String projectId = "my-project-id";
    Subscriber subscriber = null;
    MessageReceiverExample receiver = new MessageReceiverExample();

    try {
      // Subscribe to the requested Pub/Sub channel
      ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);
      subscriber = Subscriber.newBuilder(subName, receiver).build();
      subscriber.startAsync().awaitRunning();
      // Sleep to listen for messages
      TimeUnit.SECONDS.sleep(timeoutSeconds);
    } finally {
      // Stop listening to the channel
      if (subscriber != null) {
        subscriber.stopAsync();
      }
    }
    // Print and return the number of Pub/Sub messages received
    System.out.println(receiver.messageCount);
    return receiver.messageCount;
  }

  // Custom class to handle incoming Pub/Sub messages
  // In this case, the class will simply log and count each message as it comes in
  static class MessageReceiverExample implements MessageReceiver {
    public int messageCount = 0;

    @Override
    public synchronized void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
      // Every time a Pub/Sub message comes in, print it and count it
      System.out.println("Message " + messageCount + ": " + message.getData().toStringUtf8());
      messageCount += 1;
      // Acknowledge the message
      consumer.ack();
    }
  }

  // Creates and returns a Pub/Sub subscription object listening to the Occurrence topic
  public static Subscription createOccurrenceSubscription(String subId, String projectId) 
      throws IOException, StatusRuntimeException, InterruptedException {
    // This topic id will automatically receive messages when Occurrences are added or modified
    String topicId = "container-analysis-occurrences-v1";
    TopicName topicName = TopicName.of(projectId, topicId);
    SubscriptionName subName = SubscriptionName.of(projectId, subId);

    SubscriptionAdminClient client = SubscriptionAdminClient.create();
    PushConfig config = PushConfig.getDefaultInstance();
    Subscription sub = client.createSubscription(subName, topicName, config, 0);
    return sub;
  }
}

Go

Para obtener información sobre cómo instalar y usar la biblioteca cliente de Artifact Analysis, consulta las bibliotecas cliente de Artifact Analysis. Para obtener más información, consulta la documentación de referencia de la API de Artifact Analysis Go.

Para autenticarte en Artifact Analysis, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.


import (
	"context"
	"fmt"
	"io"
	"sync"
	"time"

	pubsub "cloud.google.com/go/pubsub"
)

// occurrencePubsub handles incoming Occurrences using a Cloud Pub/Sub subscription.
func occurrencePubsub(w io.Writer, subscriptionID string, timeout time.Duration, projectID string) (int, error) {
	// subscriptionID := fmt.Sprintf("my-occurrences-subscription")
	// timeout := time.Duration(20) * time.Second
	ctx := context.Background()

	var mu sync.Mutex
	client, err := pubsub.NewClient(ctx, projectID)
	if err != nil {
		return -1, fmt.Errorf("pubsub.NewClient: %w", err)
	}
	// Subscribe to the requested Pub/Sub channel.
	sub := client.Subscription(subscriptionID)
	count := 0

	// Listen to messages for 'timeout' seconds.
	ctx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()
	err = sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
		mu.Lock()
		count = count + 1
		fmt.Fprintf(w, "Message %d: %q\n", count, string(msg.Data))
		msg.Ack()
		mu.Unlock()
	})
	if err != nil {
		return -1, fmt.Errorf("sub.Receive: %w", err)
	}
	// Print and return the number of Pub/Sub messages received.
	fmt.Fprintln(w, count)
	return count, nil
}

// createOccurrenceSubscription creates a new Pub/Sub subscription object listening to the Occurrence topic.
func createOccurrenceSubscription(subscriptionID, projectID string) error {
	// subscriptionID := fmt.Sprintf("my-occurrences-subscription")
	ctx := context.Background()
	client, err := pubsub.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("pubsub.NewClient: %w", err)
	}
	defer client.Close()

	// This topic id will automatically receive messages when Occurrences are added or modified
	topicID := "container-analysis-occurrences-v1"
	topic := client.Topic(topicID)
	config := pubsub.SubscriptionConfig{Topic: topic}
	_, err = client.CreateSubscription(ctx, subscriptionID, config)
	return fmt.Errorf("client.CreateSubscription: %w", err)
}

Node.js

Para obtener información sobre cómo instalar y usar la biblioteca cliente de Artifact Analysis, consulta las bibliotecas cliente de Artifact Analysis. Para obtener más información, consulta la documentación de referencia de la API de Artifact Analysis Node.js.

Para autenticarte en Artifact Analysis, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

/**
 * TODO(developer): Uncomment these variables before running the sample
 */
// const projectId = 'your-project-id', // Your GCP Project ID
// const subscriptionId = 'my-sub-id', // A user-specified subscription to the 'container-analysis-occurrences-v1' topic
// const timeoutSeconds = 30 // The number of seconds to listen for the new Pub/Sub Messages

// Import the pubsub library and create a client, topic and subscription
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub({projectId});
const subscription = pubsub.subscription(subscriptionId);

// Handle incoming Occurrences using a Cloud Pub/Sub subscription
let count = 0;
const messageHandler = message => {
  count++;
  message.ack();
};

// Listen for new messages until timeout is hit
subscription.on('message', messageHandler);

setTimeout(() => {
  subscription.removeListener('message', messageHandler);
  console.log(`Polled ${count} occurrences`);
}, timeoutSeconds * 1000);

Ruby

Para obtener información sobre cómo instalar y usar la biblioteca cliente de Artifact Analysis, consulta las bibliotecas cliente de Artifact Analysis. Para obtener más información, consulta la documentación de referencia de la API de Artifact Analysis Ruby.

Para autenticarte en Artifact Analysis, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

# subscription_id = "A user-specified identifier for the new subscription"
# timeout_seconds = "The number of seconds to listen for new Pub/Sub messages"
# project_id      = "Your Google Cloud project ID"

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new project_id: project_id
subscription_admin = pubsub.subscription_admin
subscription = subscription_admin.create_subscription \
  name: pubsub.subscription_path(subscription_id),
  topic: pubsub.topic_path("container-analysis-occurrences-v1")

subscriber = pubsub.subscriber subscription.name
count = 0
listener = subscriber.listen do |received_message|
  count += 1
  # Process incoming occurrence here
  puts "Message #{count}: #{received_message.data}"
  received_message.acknowledge!
end

listener.start
# Wait for incoming occurrences
sleep timeout_seconds
listener.stop.wait!

subscription_admin.delete_subscription subscription: subscription.name

# Print and return the total number of Pub/Sub messages received
puts "Total Messages Received: #{count}"
count

Python

Para obtener información sobre cómo instalar y usar la biblioteca cliente de Artifact Analysis, consulta las bibliotecas cliente de Artifact Analysis. Para obtener más información, consulta la documentación de referencia de la API de Artifact Analysis Python.

Para autenticarte en Artifact Analysis, configura las credenciales predeterminadas de la aplicación. Para obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import time

from google.api_core.exceptions import AlreadyExists
from google.cloud.pubsub import SubscriberClient
from google.cloud.pubsub_v1.subscriber.message import Message


def pubsub(subscription_id: str, timeout_seconds: int, project_id: str) -> int:
    """Respond to incoming occurrences using a Cloud Pub/Sub subscription."""
    # subscription_id := 'my-occurrences-subscription'
    # timeout_seconds = 20
    # project_id = 'my-gcp-project'

    client = SubscriberClient()
    subscription_name = client.subscription_path(project_id, subscription_id)
    receiver = MessageReceiver()
    client.subscribe(subscription_name, receiver.pubsub_callback)

    # listen for 'timeout' seconds
    for _ in range(timeout_seconds):
        time.sleep(1)
    # print and return the number of pubsub messages received
    print(receiver.msg_count)
    return receiver.msg_count


class MessageReceiver:
    """Custom class to handle incoming Pub/Sub messages."""

    def __init__(self) -> None:
        # initialize counter to 0 on initialization
        self.msg_count = 0

    def pubsub_callback(self, message: Message) -> None:
        # every time a pubsub message comes in, print it and count it
        self.msg_count += 1
        print(f"Message {self.msg_count}: {message.data}")
        message.ack()


def create_occurrence_subscription(subscription_id: str, project_id: str) -> bool:
    """Creates a new Pub/Sub subscription object listening to the
    Container Analysis Occurrences topic."""
    # subscription_id := 'my-occurrences-subscription'
    # project_id = 'my-gcp-project'

    topic_id = "container-analysis-occurrences-v1"
    client = SubscriberClient()
    topic_name = f"projects/{project_id}/topics/{topic_id}"
    subscription_name = client.subscription_path(project_id, subscription_id)
    success = True
    try:
        client.create_subscription({"name": subscription_name, "topic": topic_name})
    except AlreadyExists:
        # if subscription already exists, do nothing
        pass
    else:
        success = False
    return success

Las aplicaciones de suscriptor solo reciben los mensajes que se publican en el tema después de que se crea la suscripción.

Las cargas útiles de Pub/Sub están en formato JSON y su esquema es el siguiente:

Notas:

{
    "name": "projects/PROJECT_ID/notes/NOTE_ID",
    "kind": "NOTE_KIND",
    "notificationTime": "NOTIFICATION_TIME",
}

Casos:

{
    "name": "projects/PROJECT_ID/occurrences/OCCURRENCE_ID",
    "kind": "NOTE_KIND",
    "notificationTime": "NOTIFICATION_TIME",
}

Donde:

  • NOTE_KIND es uno de los valores en NoteKind
  • NOTIFICATION_TIME es una marca de tiempo en formato RFC 3339 UTC “Zulu”, con precisión de nanosegundos.

Ver detalles

Para obtener más información sobre una nota o un evento, puedes acceder a los metadatos almacenados en Artifact Analysis. Por ejemplo, puedes solicitar todos los detalles de un evento específico. Consulta las instrucciones en Investigating Vulnerabilities.

¿Qué sigue?