Search in sources :

Example 31 with Occurrence

use of io.grafeas.v1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTest method testOccurrencesForNote.

@Test
public void testOccurrencesForNote() throws Exception {
    int newCount;
    int tries = 0;
    int origCount = OccurrencesForNote.getOccurrencesForNote(noteId, PROJECT_ID);
    final Occurrence o = CreateOccurrence.createOccurrence(imageUrl, noteId, PROJECT_ID, PROJECT_ID);
    do {
        newCount = OccurrencesForNote.getOccurrencesForNote(noteId, PROJECT_ID);
        sleep(SLEEP_TIME);
        tries += 1;
    } while (newCount != 1 && tries < TRY_LIMIT);
    assertEquals(0, origCount);
    assertEquals(1, newCount);
    // clean up
    String[] nameArr = o.getName().split("/");
    String occId = nameArr[nameArr.length - 1];
    DeleteOccurrence.deleteOccurrence(occId, PROJECT_ID);
}
Also used : Occurrence(io.grafeas.v1.Occurrence) VulnerabilityOccurrence(io.grafeas.v1.VulnerabilityOccurrence) DiscoveryOccurrence(io.grafeas.v1.DiscoveryOccurrence) Test(org.junit.Test)

Example 32 with Occurrence

use of io.grafeas.v1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTest method testPollDiscoveryOccurrenceFinished.

@Test
public void testPollDiscoveryOccurrenceFinished() throws Exception {
    try {
        // expect fail on first try
        PollDiscoveryOccurrenceFinished.pollDiscoveryOccurrenceFinished(imageUrl, PROJECT_ID, 5);
        Assert.fail("found unexpected discovery occurrence");
    } catch (TimeoutException e) {
    // test passes
    }
    // create discovery note
    Note newNote = Note.newBuilder().setDiscovery(DiscoveryNote.newBuilder().setAnalysisKind(NoteKind.DISCOVERY)).build();
    String discNoteId = "discovery-note-" + (new Date()).getTime();
    NoteName noteName = NoteName.of(PROJECT_ID, discNoteId);
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    client.createNote(ProjectName.format(PROJECT_ID), discNoteId, newNote);
    // create discovery occurrence
    Occurrence newOcc = Occurrence.newBuilder().setNoteName(noteName.toString()).setResourceUri(imageUrl).setDiscovery(DiscoveryOccurrence.newBuilder().setAnalysisStatus(AnalysisStatus.FINISHED_SUCCESS)).build();
    Occurrence result = client.createOccurrence(ProjectName.format(PROJECT_ID), newOcc);
    // poll again
    Occurrence found = PollDiscoveryOccurrenceFinished.pollDiscoveryOccurrenceFinished(imageUrl, PROJECT_ID, 5);
    AnalysisStatus foundStatus = found.getDiscovery().getAnalysisStatus();
    assertEquals(foundStatus, AnalysisStatus.FINISHED_SUCCESS);
    // clean up
    String[] nameArr = found.getName().split("/");
    String occId = nameArr[nameArr.length - 1];
    DeleteOccurrence.deleteOccurrence(occId, PROJECT_ID);
    DeleteNote.deleteNote(discNoteId, PROJECT_ID);
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) VulnerabilityNote(io.grafeas.v1.VulnerabilityNote) DiscoveryNote(io.grafeas.v1.DiscoveryNote) Note(io.grafeas.v1.Note) NoteName(io.grafeas.v1.NoteName) AnalysisStatus(io.grafeas.v1.DiscoveryOccurrence.AnalysisStatus) Occurrence(io.grafeas.v1.Occurrence) VulnerabilityOccurrence(io.grafeas.v1.VulnerabilityOccurrence) DiscoveryOccurrence(io.grafeas.v1.DiscoveryOccurrence) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 33 with Occurrence

use of io.grafeas.v1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class GetDiscoveryInfo method 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);
    }
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) Occurrence(io.grafeas.v1.Occurrence)

Example 34 with Occurrence

use of io.grafeas.v1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class OccurrencesForImage method getOccurrencesForImage.

// 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;
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) Occurrence(io.grafeas.v1.Occurrence)

Example 35 with Occurrence

use of io.grafeas.v1.Occurrence in project java-containeranalysis by googleapis.

the class GrafeasV1Beta1ClientTest method updateOccurrenceExceptionTest.

@Test
@SuppressWarnings("all")
public void updateOccurrenceExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockGrafeasV1Beta1.addException(exception);
    try {
        OccurrenceName name = OccurrenceName.of("[PROJECT]", "[OCCURRENCE]");
        Occurrence occurrence = Occurrence.newBuilder().build();
        FieldMask updateMask = FieldMask.newBuilder().build();
        client.updateOccurrence(name, occurrence, updateMask);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) OccurrenceName(com.google.containeranalysis.v1beta1.OccurrenceName) StatusRuntimeException(io.grpc.StatusRuntimeException) Occurrence(io.grafeas.v1beta1.Occurrence) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)24 Occurrence (io.grafeas.v1.Occurrence)17 Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)10 GrafeasClient (io.grafeas.v1.GrafeasClient)10 VulnerabilityOccurrence (io.grafeas.v1.VulnerabilityOccurrence)10 DiscoveryOccurrence (io.grafeas.v1.DiscoveryOccurrence)9 Occurrence (io.grafeas.v1beta1.Occurrence)9 AbstractMessage (com.google.protobuf.AbstractMessage)6 ProjectName (com.google.containeranalysis.v1beta1.ProjectName)5 ContainerAnalysisClient (com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)4 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)3 OccurrenceName (com.google.containeranalysis.v1beta1.OccurrenceName)3 NoteName (io.grafeas.v1.NoteName)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 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)2