Search in sources :

Example 1 with Occurrence

use of io.grafeas.v1beta1.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 io.grafeas.v1beta1.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 io.grafeas.v1beta1.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 io.grafeas.v1beta1.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 io.grafeas.v1beta1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTest method testPubSub.

@Test
public void testPubSub() throws Exception {
    // create new topic and subscription if needed
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        String topicId = "container-analysis-occurrences-v1";
        ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
        topicAdminClient.createTopic(topicName);
    } catch (AlreadyExistsException e) {
        System.out.println("Topic already exists");
    }
    ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
    try {
        Subscriptions.createOccurrenceSubscription(subId, PROJECT_ID);
    } catch (AlreadyExistsException e) {
        System.out.println("subscription " + subId + " already exists");
    }
    Subscriber subscriber = null;
    Subscriptions.MessageReceiverExample receiver = new Subscriptions.MessageReceiverExample();
    subscriber = Subscriber.newBuilder(subName, 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 * 3);
    // set the initial state of our counter
    int startVal = receiver.messageCount + 1;
    // add 3 new occurrences
    for (int i = 0; i < 3; i++) {
        Occurrence o = CreateOccurrence.createOccurrence(imageUrl, noteId, PROJECT_ID, PROJECT_ID);
        System.out.println("CREATED: " + o.getName());
        String[] nameArr = o.getName().split("/");
        String occId = nameArr[nameArr.length - 1];
        DeleteOccurrence.deleteOccurrence(occId, PROJECT_ID);
    }
    // verify the pubsub channel has new entries
    int newCount;
    int tries = 0;
    do {
        newCount = receiver.messageCount;
        sleep(SLEEP_TIME);
        tries += 1;
    } while (newCount <= startVal && tries < TRY_LIMIT);
    assertTrue(receiver.messageCount > startVal);
    if (subscriber != null) {
        subscriber.stopAsync();
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Subscriber(com.google.cloud.pubsub.v1.Subscriber) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Occurrence(io.grafeas.v1.Occurrence) VulnerabilityOccurrence(io.grafeas.v1.VulnerabilityOccurrence) DiscoveryOccurrence(io.grafeas.v1.DiscoveryOccurrence) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)25 Occurrence (io.grafeas.v1.Occurrence)17 Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)10 VulnerabilityOccurrence (io.grafeas.v1.VulnerabilityOccurrence)10 DiscoveryOccurrence (io.grafeas.v1.DiscoveryOccurrence)9 GrafeasClient (io.grafeas.v1.GrafeasClient)9 Occurrence (io.grafeas.v1beta1.Occurrence)9 AbstractMessage (com.google.protobuf.AbstractMessage)8 OccurrenceName (com.google.containeranalysis.v1beta1.OccurrenceName)5 ProjectName (com.google.containeranalysis.v1beta1.ProjectName)5 ContainerAnalysisClient (com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)4 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)3 NoteName (io.grafeas.v1.NoteName)3 DeleteOccurrenceRequest (io.grafeas.v1beta1.DeleteOccurrenceRequest)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 ListOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)2 ListOccurrencesRequest (com.google.containeranalysis.v1alpha1.ListOccurrencesRequest)2 VulnerabilityDetails (com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails)2 FieldMask (com.google.protobuf.FieldMask)2