/** * Retrieves and prints the Discovery Occurrence created for a specified image * The Discovery Occurrence contains information about the initial scan on the image * @param client The Grafeas client used to perform the API requests. * @param imageUrl the Container Registry URL associated with the image * example: "https://gcr.io/project/image@sha256:foo" * @param projectId the GCP project the image belongs to */ public static void getDiscoveryInfo(GrafeasV1Beta1Client client, String imageUrl, String projectId) { String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + imageUrl + "\""; final String projectName = ProjectName.format(projectId); for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) { System.out.println(o); } } // [END discovery_info]
/** * Retrieves all the Occurrences associated with a specified image * Here, all Occurrences are simply printed and counted * @param client The Grafeas client used to perform the API requests. * @param imageUrl the Container Registry URL associated with the image * example: "https://gcr.io/project/image@sha256:foo" * @param projectId the GCP project to search for Occurrences in * @return number of Occurrences found */ public static int getOccurrencesForImage(GrafeasV1Beta1Client client, String imageUrl, String projectId) { final String filterStr = "resourceUrl=\"" + imageUrl + "\""; final String projectName = ProjectName.format(projectId); 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; } // [END occurrences_for_image]
/** * Creates and returns a new vulnerability Note * @param client The Grafeas client used to perform the API requests. * @param noteId A user-specified identifier for the Note. * @param projectId the GCP project the Note will be created under * @return the newly created Note object */ public static Note createNote(GrafeasV1Beta1Client client, String noteId, String projectId) { Note.Builder noteBuilder = Note.newBuilder(); Vulnerability.Builder vulBuilder = Vulnerability.newBuilder(); // Details about the your vulnerability can be added here // Example: vulBuilder.setSeverity(Severity.CRITICAL); noteBuilder.setVulnerability(vulBuilder); Note newNote = noteBuilder.build(); final String projectName = ProjectName.format(projectId); return client.createNote(projectName, noteId, newNote); } // [END create_note]
/** * Creates and returns a new Occurrence of a previously created vulnerability Note * @param client The Grafeas client used to perform the API requests. * @param imageUrl the Container Registry URL associated with the image * example: "https://gcr.io/project/image@sha256:foo" * @param noteId the identifier of the Note associated with this Occurrence * @param occProjectId the GCP project the Occurrence will be created under * @param noteProjectId the GCP project the associated Note belongs to * @return the newly created Occurrence object */ public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String imageUrl, String noteId, String occProjectId, String noteProjectId) { final NoteName noteName = NoteName.of(noteProjectId, noteId); final String occProjectName = ProjectName.format(occProjectId); Occurrence.Builder occBuilder = Occurrence.newBuilder(); occBuilder.setNoteName(noteName.toString()); Details.Builder detailsBuilder = Details.newBuilder(); // Details about the vulnerability instance can be added here occBuilder.setVulnerability(detailsBuilder); // Attach the occurrence to the associated image uri Resource.Builder resourceBuilder = Resource.newBuilder(); resourceBuilder.setUri(imageUrl); occBuilder.setResource(resourceBuilder); Occurrence newOcc = occBuilder.build(); return client.createOccurrence(occProjectName, newOcc); } // [END create_occurrence]