Search in sources :

Example 1 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class Samples method getOccurrencesForNote.

// [END discovery_info]
// [START occurrences_for_note]
/**
 * Retrieves all the occurrences associated with a specified note
 *
 * @param noteId the note's unique identifier
 * @param projectId the project's unique identifier
 * @return number of occurrences found
 * @throws Exception on errors while closing the client
 */
public static int getOccurrencesForNote(String noteId, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String parentNoteName = client.formatNoteName(projectId, noteId);
        int i = 0;
        ListNoteOccurrencesPagedResponse response = client.listNoteOccurrences(parentNoteName);
        for (Occurrence o : response.iterateAll()) {
            // print and count each occurrence found
            System.out.println(o.getName());
            i = i + 1;
        }
        return i;
    }
}
Also used : ListNoteOccurrencesPagedResponse(com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListNoteOccurrencesPagedResponse) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence)

Example 2 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class Samples method getOccurrencesForImage.

// [END occurrences_for_note]
// [START occurrences_for_image]
/**
 * Retrieves all the occurrences associated with a specified image
 *
 * @param imageUrl the Container Registry URL associated with the image
 *                 example: "https://gcr.io/project/image@sha256:foo"
 * @param projectId the project's unique identifier
 * @return number of occurrences found
 * @throws Exception on errors while closing the client
 */
public static int getOccurrencesForImage(String imageUrl, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String filterStr = "resourceUrl=\"" + imageUrl + "\"";
        final String projectName = client.formatProjectName(projectId);
        int i = 0;
        // build the request
        ListOccurrencesRequest.Builder b = ListOccurrencesRequest.newBuilder();
        b.setFilter(filterStr);
        b.setParent(projectName);
        ListOccurrencesRequest req = b.build();
        // query for the requested occurrences
        ListOccurrencesPagedResponse response = client.listOccurrences(req);
        for (Occurrence o : response.iterateAll()) {
            // print and count each occurrence found
            System.out.println(o.getName());
            i = i + 1;
        }
        return i;
    }
}
Also used : ListOccurrencesRequest(com.google.containeranalysis.v1alpha1.ListOccurrencesRequest) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) ListOccurrencesPagedResponse(com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)

Example 3 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testOccurrencesForNote.

@Test
public void testOccurrencesForNote() throws Exception {
    int newCount;
    int tries = 0;
    int origCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
    final Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
    do {
        newCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
        sleep(SLEEP_TIME);
    } while (newCount != 1 && tries < TRY_LIMIT);
    assertEquals(0, origCount);
    assertEquals(1, newCount);
    // clean up
    Samples.deleteOccurrence(o.getName());
}
Also used : Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 4 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testPubSub.

@Test
public void testPubSub() throws Exception {
    int newCount;
    int tries;
    String subscriptionId = "drydockOccurrences";
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
    Subscriber subscriber = null;
    Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        // sleep so any messages in the queue can go through and be counted before we start the test
        sleep(SLEEP_TIME);
        // set the initial state of our counter
        int startVal = receiver.messageCount + 1;
        // now, we can test adding 3 more occurrences
        int endVal = startVal + 3;
        for (int i = startVal; i <= endVal; i++) {
            Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
            System.out.println("CREATED: " + o.getName());
            tries = 0;
            do {
                newCount = receiver.messageCount;
                sleep(SLEEP_TIME);
                tries += 1;
            } while (newCount != i && tries < TRY_LIMIT);
            System.out.println(receiver.messageCount + " : " + i);
            assertEquals(i, receiver.messageCount);
            Samples.deleteOccurrence(o.getName());
        }
    } catch (Exception e) {
        fail("exception thrown");
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
        // delete subscription now that we're done with it
        try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
            subscriptionAdminClient.deleteSubscription(subscriptionName);
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 5 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class Samples method createOccurrence.

// [END create_note]
// [START create_occurrence]
/**
 * Creates and returns a new occurrence
 *
 * @param imageUrl the Container Registry URL associated with the image
 *                 example: "https://gcr.io/project/image@sha256:foo"
 * @param parentNoteId the identifier of the note associated with this occurrence
 * @param projectId the GCP project the occurrence will be created under
 * @return an Occurrence object representing the new occurrence
 * @throws Exception on errors while closing the client
 */
public static Occurrence createOccurrence(String imageUrl, String parentNoteId, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String parentNoteName = client.formatNoteName(projectId, parentNoteId);
        final String projectName = client.formatProjectName(projectId);
        Occurrence.Builder occBuilder = Occurrence.newBuilder();
        occBuilder.setNoteName(parentNoteName);
        occBuilder.setResourceUrl(imageUrl);
        VulnerabilityDetails vd = VulnerabilityType.VulnerabilityDetails.newBuilder().build();
        occBuilder.setVulnerabilityDetails(vd);
        Occurrence newOcc = occBuilder.build();
        return client.createOccurrence(projectName, newOcc);
    }
}
Also used : VulnerabilityDetails(com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence)

Aggregations

Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)10 Test (org.junit.Test)6 ContainerAnalysisClient (com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)4 ListOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)2 ListOccurrencesRequest (com.google.containeranalysis.v1alpha1.ListOccurrencesRequest)2 VulnerabilityDetails (com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails)2 ListNoteOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListNoteOccurrencesPagedResponse)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1