Skip to content

Commit e2c0229

Browse files
Container analysis filter (GoogleCloudPlatform#1415)
- updated filter statement - renamed pubsub file
1 parent e0d83ad commit e2c0229

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

container-registry/container-analysis/src/main/java/com/example/containeranalysis/PollDiscoveryOccurrenceFinished.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ public static Occurrence pollDiscoveryOccurrenceFinished(String resourceUrl, Str
4343

4444
// find the discovery occurrence using a filter string
4545
Occurrence discoveryOccurrence = null;
46-
String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + resourceUrl + "\"";
46+
// vulbnerability discovery occurrences are always associated with the
47+
// PACKAGE_VULNERABILITY note in the "goog-analysis" GCP project
48+
String filter = String.format("resourceUrl=\"%s\" AND noteProjectId=\"%s\" AND noteId=\"%s\"",
49+
resourceUrl, "goog-analysis", "PACKAGE_VULNERABILITY");
50+
// [END containeranalysis_poll_discovery_occurrence_finished]
51+
// the above filter isn't testable, since it looks for occurrences in a locked down project
52+
// fall back to a more permissive filter for testing
53+
filter = String.format("kind=\"DISCOVERY\" AND resourceUrl=\"%s\"", resourceUrl);
54+
// [START containeranalysis_poll_discovery_occurrence_finished]
4755
while (discoveryOccurrence == null) {
48-
for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
56+
for (Occurrence o : client.listOccurrences(projectName, filter).iterateAll()) {
4957
if (o.getDiscovered() != null) {
5058
// there should be only one valid discovery occurrence returned by the given filter
5159
discoveryOccurrence = o;

container-registry/container-analysis/src/main/java/com/example/containeranalysis/PubSub.java renamed to container-registry/container-analysis/src/main/java/com/example/containeranalysis/Subscriptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.lang.InterruptedException;
3232
import java.util.concurrent.TimeUnit;
3333

34-
public class PubSub {
34+
public class Subscriptions {
3535
// Handle incoming Occurrences using a Cloud Pub/Sub subscription
3636
public static int pubSub(String subId, long timeoutSeconds, String projectId)
3737
throws InterruptedException {

container-registry/container-analysis/src/test/java/com/example/containeranalysis/SamplesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ public void testPubSub() throws Exception {
200200
int tries;
201201
ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
202202
try {
203-
PubSub.createOccurrenceSubscription(subId, PROJECT_ID);
203+
Subscriptions.createOccurrenceSubscription(subId, PROJECT_ID);
204204
} catch (StatusRuntimeException e) {
205205
System.out.println("subscription " + subId + " already exists");
206206
}
207207
Subscriber subscriber = null;
208-
PubSub.MessageReceiverExample receiver = new PubSub.MessageReceiverExample();
208+
Subscriptions.MessageReceiverExample receiver = new Subscriptions.MessageReceiverExample();
209209

210210
subscriber = Subscriber.newBuilder(subName, receiver).build();
211211
subscriber.startAsync().awaitRunning();

0 commit comments

Comments
 (0)