From b8714e05bd6230970454fb7a96857f41fa0f487c Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Thu, 28 Mar 2024 18:50:17 +0000 Subject: [PATCH 1/6] chore: api v2 notifications samples --- .../notifications/CreateNotification.java | 69 +++++++++++ .../notifications/DeleteNotification.java | 62 ++++++++++ .../vtwo/notifications/GetNotification.java | 68 +++++++++++ .../vtwo/notifications/ListNotification.java | 68 +++++++++++ .../notifications/UpdateNotification.java | 86 +++++++++++++ .../src/test/java/vtwo/NotificationIT.java | 113 ++++++++++++++++++ 6 files changed, 466 insertions(+) create mode 100644 security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java create mode 100644 security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java create mode 100644 security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java create mode 100644 security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java create mode 100644 security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java create mode 100644 security-command-center/snippets/src/test/java/vtwo/NotificationIT.java diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java new file mode 100644 index 00000000000..ac4c4d693d6 --- /dev/null +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java @@ -0,0 +1,69 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START securitycenter_create_notification_config] + +package vtwo.notifications; + +import com.google.cloud.securitycenter.v2.LocationName; +import com.google.cloud.securitycenter.v2.NotificationConfig; +import com.google.cloud.securitycenter.v2.SecurityCenterClient; +import java.io.IOException; + +public class CreateNotification { + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String projectId = "{project-id}"; + String topicName = "{your-topic}"; + String notificationConfigId = "{your-notification-id}"; + // Specify the location of the notification config. + String location = "global"; + + createNotificationConfig(projectId, location , topicName, notificationConfigId); + } + + // Crete a notification config. + // Ensure the ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the new topic. + public static NotificationConfig createNotificationConfig( + String projectId, String location, String topicName, String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + String pubsubTopic = String.format("projects/%s/topics/%s", projectId, topicName); + + NotificationConfig notificationConfig = NotificationConfig.newBuilder() + .setDescription("Java notification config") + .setPubsubTopic(pubsubTopic) + .setStreamingConfig( + NotificationConfig.StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"").build()) + .build(); + + // when using the createNotificationConfig method, a notification will be created located at projectId / location + NotificationConfig response = client.createNotificationConfig( + LocationName.of(projectId, location), notificationConfig, notificationConfigId); + + System.out.printf("Notification config was created: %s%n", response); + return response; + } + } +} +// [END securitycenter_create_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java new file mode 100644 index 00000000000..469cd16bcb8 --- /dev/null +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START securitycenter_delete_notification_config] + +package vtwo.notifications; + +import com.google.cloud.securitycenter.v2.DeleteNotificationConfigRequest; +import com.google.cloud.securitycenter.v2.SecurityCenterClient; + +import java.io.IOException; + +public class DeleteNotification { + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String projectId = "{project-id}"; + // Specify the location to list the findings. + String location = "global"; + String notificationConfigId = "{your-notification-id}"; + + deleteNotificationConfig(projectId, location,notificationConfigId); + } + // Delete a notification config. + // Ensure the ServiceAccount has the "securitycenter.notification.delete" permission + public static boolean deleteNotificationConfig(String projectId, String location,String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + DeleteNotificationConfigRequest request = DeleteNotificationConfigRequest.newBuilder() + .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", + projectId, + location, + notificationConfigId)) + .build(); + + client.deleteNotificationConfig(request); + + System.out.printf("Deleted Notification config: %s%n", notificationConfigId); + } + return true; + } +} +// [END securitycenter_delete_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java new file mode 100644 index 00000000000..e1b3987e6db --- /dev/null +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START securitycenter_get_notification_config] + +package vtwo.notifications; + +import com.google.cloud.securitycenter.v2.GetNotificationConfigRequest; +import com.google.cloud.securitycenter.v2.NotificationConfig; +import com.google.cloud.securitycenter.v2.SecurityCenterClient; + +import java.io.IOException; + +public class GetNotification { + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + + String projectId = "{your-project}"; + // Specify the location to list the findings. + String location = "global"; + String notificationConfigId = "{config-id}"; + + getNotificationConfig(projectId, location,notificationConfigId); + } + + // Retrieve an existing notification config. + public static NotificationConfig getNotificationConfig( + String projectId,String location ,String notificationConfigId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + GetNotificationConfigRequest request = GetNotificationConfigRequest.newBuilder() + .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", + projectId, + location, + notificationConfigId)) + .build(); + System.out.printf("getNotificationConfig config::" +String.format("projects/%s/locations/%s/notificationConfigs/%s", + projectId, + location, + notificationConfigId)); + + NotificationConfig response = + client.getNotificationConfig(request); + + System.out.printf("Notification config: %s%n", response); + return response; + } + } +} +// [END securitycenter_get_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java new file mode 100644 index 00000000000..c1720db1d9c --- /dev/null +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START securitycenter_list_notification_configs] + +package vtwo.notifications; + +import com.google.cloud.securitycenter.v2.ListNotificationConfigsRequest; +import com.google.cloud.securitycenter.v2.NotificationConfig; +import com.google.cloud.securitycenter.v2.SecurityCenterClient; +import com.google.common.collect.ImmutableList; + +import java.io.IOException; + +public class ListNotification { + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + + String projectId = "{your-project}"; + + // Specify the location to list the findings. + String location = "global"; + + listNotificationConfigs(projectId,location); + } + + // List notification configs present in the given parent. + public static ImmutableList listNotificationConfigs(String projectId, String location) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + ListNotificationConfigsRequest request = ListNotificationConfigsRequest.newBuilder() + .setParent( String.format("projects/%s/locations/%s", + projectId, + location)) + .build(); + + SecurityCenterClient.ListNotificationConfigsPagedResponse response = client.listNotificationConfigs(request); + + ImmutableList notificationConfigs = + ImmutableList.copyOf(response.iterateAll()); + + System.out.printf("List notifications response: %s%n", response.getPage().getValues()); + return notificationConfigs; + } + } +} +// [END securitycenter_list_notification_configs] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java new file mode 100644 index 00000000000..40aee52fb3d --- /dev/null +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java @@ -0,0 +1,86 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START securitycenter_update_notification_config] +package vtwo.notifications; + +import com.google.cloud.securitycenter.v2.NotificationConfig; +import com.google.cloud.securitycenter.v2.NotificationConfig.StreamingConfig; +import com.google.cloud.securitycenter.v2.SecurityCenterClient; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.UUID; +public class UpdateNotification { + + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String projectId = "{project-id}"; + String topicName = "{your-topic}"; + String notificationConfigId = "{your-notification-id}"; + // Specify the location to list the findings. + String location = "global"; + + updateNotificationConfig(projectId, location, topicName, notificationConfigId); + } + + // Update an existing notification config. + // If updating a Pubsub Topic, ensure the ServiceAccount has the + // "pubsub.topics.setIamPolicy" permission on the new topic. + public static NotificationConfig updateNotificationConfig( + String projectId, String location, String topicName, String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + String notificationConfigName = + String.format("projects/%s/locations/%s/notificationConfigs/%s", + projectId, + location, + notificationConfigId); + + String pubsubTopic = + String.format("projects/%s/topics/%s", + projectId, + topicName); + + NotificationConfig configToUpdate = + NotificationConfig.newBuilder() + .setName(notificationConfigName) + .setDescription("updated description") + .setPubsubTopic(pubsubTopic) + .setStreamingConfig(StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"")) + .build(); + + FieldMask fieldMask = + FieldMask.newBuilder() + .addPaths("description") + .addPaths("pubsub_topic") + .addPaths("streaming_config.filter") + .build(); + + NotificationConfig updatedConfig = client.updateNotificationConfig(configToUpdate, fieldMask); + + System.out.printf("Notification config: %s%n", updatedConfig); + return updatedConfig; + } + } +} +// [END securitycenter_update_notification_config] diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java new file mode 100644 index 00000000000..949531f7fcb --- /dev/null +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -0,0 +1,113 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package vtwo; + +import com.google.cloud.securitycenter.v2.NotificationConfig; +import com.google.cloud.testing.junit4.MultipleAttemptsRule; +import org.junit.*; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import vtwo.notifications.*; + +import java.util.concurrent.TimeUnit; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +@RunWith(JUnit4.class) +public class NotificationIT { + + // TODO(Developer): Replace the below variables. + private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); + private static final String LOCATION = "global"; + private static final String NOTIFICATION_RULE_CREATE = "random-notification-id-" + UUID.randomUUID(); + private static final String NOTIFICATION_TOPIC = "test-topic"; + private static final int MAX_ATTEMPT_COUNT = 3; + private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes + private static ByteArrayOutputStream stdOut; + + @Rule + public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule( + MAX_ATTEMPT_COUNT, + INITIAL_BACKOFF_MILLIS); + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)) + .isNotEmpty(); + } + @BeforeClass + public static void setUp() throws IOException, InterruptedException { + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("SCC_PROJECT_ID"); + + // Create notification rules. + NotificationConfig result = CreateNotification.createNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); + System.out.printf("NotificationConfig: "+ result.getName()+" "+ result.getDescription()); + + stdOut = null; + System.setOut(out); + TimeUnit.MINUTES.sleep(3); + } + @AfterClass + public static void cleanUp() throws IOException { + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + DeleteNotification.deleteNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(stdOut.toString()).contains("Deleted Notification config: " + NOTIFICATION_RULE_CREATE); + + stdOut = null; + System.setOut(out); + } + @Before + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + } + @After + public void afterEach() { + stdOut = null; + System.setOut(null); + } + @Test + public void testGetNotificationRule() throws IOException { + NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(notificationConfig.getName()).contains(NOTIFICATION_RULE_CREATE); + } + @Test + public void testListNotificationRules() throws IOException { + ListNotification.listNotificationConfigs(PROJECT_ID, LOCATION); + assertThat(stdOut.toString()).contains(NOTIFICATION_TOPIC); + } + @Test + public void testUpdateNotificationRule() throws IOException { + UpdateNotification.updateNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); + NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(notificationConfig.getDescription()).contains("updated description"); + } +} \ No newline at end of file From c4a23592414366e41db43cec6cb68c6e348e5e37 Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Mon, 1 Apr 2024 15:09:59 +0000 Subject: [PATCH 2/6] add checkstyle --- .../notifications/CreateNotification.java | 71 ++++--- .../notifications/DeleteNotification.java | 72 ++++--- .../vtwo/notifications/GetNotification.java | 72 ++++--- .../vtwo/notifications/ListNotification.java | 62 +++--- .../notifications/UpdateNotification.java | 100 ++++----- .../src/test/java/vtwo/NotificationIT.java | 200 ++++++++++-------- 6 files changed, 307 insertions(+), 270 deletions(-) diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java index ac4c4d693d6..58d83744b45 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,46 +24,47 @@ import java.io.IOException; public class CreateNotification { - public static void main(String[] args) throws IOException { - // parentId: must be in one of the following formats: - // "organizations/{organization_id}" - // "projects/{project_id}" - // "folders/{folder_id}" - String projectId = "{project-id}"; - String topicName = "{your-topic}"; - String notificationConfigId = "{your-notification-id}"; - // Specify the location of the notification config. - String location = "global"; - createNotificationConfig(projectId, location , topicName, notificationConfigId); - } + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String parentId = "{parent-id}"; + String topicName = "{your-topic}"; + String notificationConfigId = "{your-notification-id}"; + // Specify the location of the notification config. + String location = "global"; + + createNotificationConfig(parentId, location, topicName, notificationConfigId); + } - // Crete a notification config. - // Ensure the ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the new topic. - public static NotificationConfig createNotificationConfig( - String projectId, String location, String topicName, String notificationConfigId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Crete a notification config. + // Ensure the ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the new topic. + public static NotificationConfig createNotificationConfig( + String parentId, String location, String topicName, String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { - String pubsubTopic = String.format("projects/%s/topics/%s", projectId, topicName); + String pubsubTopic = String.format("projects/%s/topics/%s", parentId, topicName); - NotificationConfig notificationConfig = NotificationConfig.newBuilder() - .setDescription("Java notification config") - .setPubsubTopic(pubsubTopic) - .setStreamingConfig( - NotificationConfig.StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"").build()) - .build(); + NotificationConfig notificationConfig = NotificationConfig.newBuilder() + .setDescription("Java notification config") + .setPubsubTopic(pubsubTopic) + .setStreamingConfig( + NotificationConfig.StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"") + .build()) + .build(); - // when using the createNotificationConfig method, a notification will be created located at projectId / location - NotificationConfig response = client.createNotificationConfig( - LocationName.of(projectId, location), notificationConfig, notificationConfigId); + NotificationConfig response = client.createNotificationConfig( + LocationName.of(parentId, location), notificationConfig, notificationConfigId); - System.out.printf("Notification config was created: %s%n", response); - return response; - } + System.out.printf("Notification config was created: %s%n", response); + return response; } + } } // [END securitycenter_create_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java index 469cd16bcb8..057e25271f2 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,43 +20,45 @@ import com.google.cloud.securitycenter.v2.DeleteNotificationConfigRequest; import com.google.cloud.securitycenter.v2.SecurityCenterClient; - import java.io.IOException; public class DeleteNotification { - public static void main(String[] args) throws IOException { - // parentId: must be in one of the following formats: - // "organizations/{organization_id}" - // "projects/{project_id}" - // "folders/{folder_id}" - String projectId = "{project-id}"; - // Specify the location to list the findings. - String location = "global"; - String notificationConfigId = "{your-notification-id}"; - - deleteNotificationConfig(projectId, location,notificationConfigId); - } - // Delete a notification config. - // Ensure the ServiceAccount has the "securitycenter.notification.delete" permission - public static boolean deleteNotificationConfig(String projectId, String location,String notificationConfigId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecurityCenterClient client = SecurityCenterClient.create()) { - - DeleteNotificationConfigRequest request = DeleteNotificationConfigRequest.newBuilder() - .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", - projectId, - location, - notificationConfigId)) - .build(); - - client.deleteNotificationConfig(request); - - System.out.printf("Deleted Notification config: %s%n", notificationConfigId); - } - return true; + + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String parentId = "{parent-id}"; + // Specify the location to list the findings. + String location = "global"; + String notificationConfigId = "{your-notification-id}"; + + deleteNotificationConfig(parentId, location, notificationConfigId); + } + + // Delete a notification config. + // Ensure the ServiceAccount has the "securitycenter.notification.delete" permission + public static boolean deleteNotificationConfig(String parentId, String location, + String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + DeleteNotificationConfigRequest request = DeleteNotificationConfigRequest.newBuilder() + .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", + parentId, + location, + notificationConfigId)) + .build(); + + client.deleteNotificationConfig(request); + + System.out.printf("Deleted Notification config: %s%n", notificationConfigId); } + return true; + } } // [END securitycenter_delete_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java index e1b3987e6db..077ca36ebe0 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,48 +21,50 @@ import com.google.cloud.securitycenter.v2.GetNotificationConfigRequest; import com.google.cloud.securitycenter.v2.NotificationConfig; import com.google.cloud.securitycenter.v2.SecurityCenterClient; - import java.io.IOException; public class GetNotification { - public static void main(String[] args) throws IOException { - // parentId: must be in one of the following formats: - // "organizations/{organization_id}" - // "projects/{project_id}" - // "folders/{folder_id}" - String projectId = "{your-project}"; - // Specify the location to list the findings. - String location = "global"; - String notificationConfigId = "{config-id}"; + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String parentId = "{parent-id}"; + // Specify the location to list the findings. + String location = "global"; + String notificationConfigId = "{config-id}"; - getNotificationConfig(projectId, location,notificationConfigId); - } + getNotificationConfig(parentId, location, notificationConfigId); + } + + // Retrieve an existing notification config. + public static NotificationConfig getNotificationConfig( + String parentId, String location, String notificationConfigId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { + + GetNotificationConfigRequest request = GetNotificationConfigRequest.newBuilder() + .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", + parentId, + location, + notificationConfigId)) + .build(); - // Retrieve an existing notification config. - public static NotificationConfig getNotificationConfig( - String projectId,String location ,String notificationConfigId) throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecurityCenterClient client = SecurityCenterClient.create()) { - GetNotificationConfigRequest request = GetNotificationConfigRequest.newBuilder() - .setName(String.format("projects/%s/locations/%s/notificationConfigs/%s", - projectId, - location, - notificationConfigId)) - .build(); - System.out.printf("getNotificationConfig config::" +String.format("projects/%s/locations/%s/notificationConfigs/%s", - projectId, - location, - notificationConfigId)); + System.out.printf("getNotificationConfig config:" + String.format( + "projects/%s/locations/%s/notificationConfigs/%s", + parentId, + location, + notificationConfigId)); - NotificationConfig response = - client.getNotificationConfig(request); + NotificationConfig response = + client.getNotificationConfig(request); - System.out.printf("Notification config: %s%n", response); - return response; - } + System.out.printf("Notification config: %s%n", response); + return response; } + } } // [END securitycenter_get_notification_config] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java index c1720db1d9c..f3c23057c06 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/ListNotification.java @@ -21,48 +21,48 @@ import com.google.cloud.securitycenter.v2.ListNotificationConfigsRequest; import com.google.cloud.securitycenter.v2.NotificationConfig; import com.google.cloud.securitycenter.v2.SecurityCenterClient; +import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListNotificationConfigsPagedResponse; import com.google.common.collect.ImmutableList; - import java.io.IOException; public class ListNotification { - public static void main(String[] args) throws IOException { - // parentId: must be in one of the following formats: - // "organizations/{organization_id}" - // "projects/{project_id}" - // "folders/{folder_id}" - - String projectId = "{your-project}"; - // Specify the location to list the findings. - String location = "global"; - - listNotificationConfigs(projectId,location); - } + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String parentId = "{parent-id}"; + // Specify the location to list the findings. + String location = "global"; - // List notification configs present in the given parent. - public static ImmutableList listNotificationConfigs(String projectId, String location) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. + listNotificationConfigs(parentId, location); + } - try (SecurityCenterClient client = SecurityCenterClient.create()) { + // List notification configs present in the given parent. + public static ImmutableList listNotificationConfigs(String parentId, + String location) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { - ListNotificationConfigsRequest request = ListNotificationConfigsRequest.newBuilder() - .setParent( String.format("projects/%s/locations/%s", - projectId, - location)) - .build(); + ListNotificationConfigsRequest request = ListNotificationConfigsRequest.newBuilder() + .setParent(String.format("projects/%s/locations/%s", + parentId, + location)) + .build(); - SecurityCenterClient.ListNotificationConfigsPagedResponse response = client.listNotificationConfigs(request); + ListNotificationConfigsPagedResponse response = client.listNotificationConfigs( + request); - ImmutableList notificationConfigs = - ImmutableList.copyOf(response.iterateAll()); + ImmutableList notificationConfigs = + ImmutableList.copyOf(response.iterateAll()); - System.out.printf("List notifications response: %s%n", response.getPage().getValues()); - return notificationConfigs; - } + System.out.printf("List notifications response: %s%n", response.getPage().getValues()); + return notificationConfigs; } + } } // [END securitycenter_list_notification_configs] diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java index 40aee52fb3d..1989ea63f0d 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ // [START securitycenter_update_notification_config] + package vtwo.notifications; import com.google.cloud.securitycenter.v2.NotificationConfig; @@ -23,64 +24,65 @@ import com.google.protobuf.FieldMask; import java.io.IOException; import java.util.UUID; + public class UpdateNotification { - public static void main(String[] args) throws IOException { - // parentId: must be in one of the following formats: - // "organizations/{organization_id}" - // "projects/{project_id}" - // "folders/{folder_id}" - String projectId = "{project-id}"; - String topicName = "{your-topic}"; - String notificationConfigId = "{your-notification-id}"; - // Specify the location to list the findings. - String location = "global"; + public static void main(String[] args) throws IOException { + // parentId: must be in one of the following formats: + // "organizations/{organization_id}" + // "projects/{project_id}" + // "folders/{folder_id}" + String parentId = "{parent-id}"; + String topicName = "{your-topic}"; + String notificationConfigId = "{your-notification-id}"; + // Specify the location to list the findings. + String location = "global"; - updateNotificationConfig(projectId, location, topicName, notificationConfigId); - } + updateNotificationConfig(parentId, location, topicName, notificationConfigId); + } - // Update an existing notification config. - // If updating a Pubsub Topic, ensure the ServiceAccount has the - // "pubsub.topics.setIamPolicy" permission on the new topic. - public static NotificationConfig updateNotificationConfig( - String projectId, String location, String topicName, String notificationConfigId) - throws IOException { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (SecurityCenterClient client = SecurityCenterClient.create()) { + // Update an existing notification config. + // If updating a Pubsub Topic, ensure the ServiceAccount has the + // "pubsub.topics.setIamPolicy" permission on the new topic. + public static NotificationConfig updateNotificationConfig( + String parentId, String location, String topicName, String notificationConfigId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (SecurityCenterClient client = SecurityCenterClient.create()) { - String notificationConfigName = - String.format("projects/%s/locations/%s/notificationConfigs/%s", - projectId, - location, - notificationConfigId); + String notificationConfigName = + String.format("projects/%s/locations/%s/notificationConfigs/%s", + parentId, + location, + notificationConfigId); - String pubsubTopic = - String.format("projects/%s/topics/%s", - projectId, - topicName); + String pubsubTopic = + String.format("projects/%s/topics/%s", + parentId, + topicName); - NotificationConfig configToUpdate = - NotificationConfig.newBuilder() - .setName(notificationConfigName) - .setDescription("updated description") - .setPubsubTopic(pubsubTopic) - .setStreamingConfig(StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"")) - .build(); + NotificationConfig configToUpdate = + NotificationConfig.newBuilder() + .setName(notificationConfigName) + .setDescription("updated description") + .setPubsubTopic(pubsubTopic) + .setStreamingConfig(StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"")) + .build(); - FieldMask fieldMask = - FieldMask.newBuilder() - .addPaths("description") - .addPaths("pubsub_topic") - .addPaths("streaming_config.filter") - .build(); + FieldMask fieldMask = + FieldMask.newBuilder() + .addPaths("description") + .addPaths("pubsub_topic") + .addPaths("streaming_config.filter") + .build(); - NotificationConfig updatedConfig = client.updateNotificationConfig(configToUpdate, fieldMask); + NotificationConfig updatedConfig = client.updateNotificationConfig(configToUpdate, fieldMask); - System.out.printf("Notification config: %s%n", updatedConfig); - return updatedConfig; - } + System.out.printf("Notification config: %s%n", updatedConfig); + return updatedConfig; } + } } // [END securitycenter_update_notification_config] diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java index 949531f7fcb..93959e0b86a 100644 --- a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,98 +16,128 @@ package vtwo; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + import com.google.cloud.securitycenter.v2.NotificationConfig; import com.google.cloud.testing.junit4.MultipleAttemptsRule; -import org.junit.*; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import vtwo.notifications.*; - -import java.util.concurrent.TimeUnit; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.UUID; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import vtwo.notifications.CreateNotification; +import vtwo.notifications.DeleteNotification; +import vtwo.notifications.GetNotification; +import vtwo.notifications.ListNotification; +import vtwo.notifications.UpdateNotification; -import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth.assertWithMessage; - +// Test v2 Notification samples. @RunWith(JUnit4.class) public class NotificationIT { - // TODO(Developer): Replace the below variables. - private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); - private static final String LOCATION = "global"; - private static final String NOTIFICATION_RULE_CREATE = "random-notification-id-" + UUID.randomUUID(); - private static final String NOTIFICATION_TOPIC = "test-topic"; - private static final int MAX_ATTEMPT_COUNT = 3; - private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes - private static ByteArrayOutputStream stdOut; - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule( - MAX_ATTEMPT_COUNT, - INITIAL_BACKOFF_MILLIS); - - // Check if the required environment variables are set. - public static void requireEnvVar(String envVarName) { - assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) - .that(System.getenv(envVarName)) - .isNotEmpty(); - } - @BeforeClass - public static void setUp() throws IOException, InterruptedException { - final PrintStream out = System.out; - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("SCC_PROJECT_ID"); - - // Create notification rules. - NotificationConfig result = CreateNotification.createNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); - System.out.printf("NotificationConfig: "+ result.getName()+" "+ result.getDescription()); - - stdOut = null; - System.setOut(out); - TimeUnit.MINUTES.sleep(3); - } - @AfterClass - public static void cleanUp() throws IOException { - final PrintStream out = System.out; - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - DeleteNotification.deleteNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); - assertThat(stdOut.toString()).contains("Deleted Notification config: " + NOTIFICATION_RULE_CREATE); - - stdOut = null; - System.setOut(out); - } - @Before - public void beforeEach() { - stdOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(stdOut)); - } - @After - public void afterEach() { - stdOut = null; - System.setOut(null); - } - @Test - public void testGetNotificationRule() throws IOException { - NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); - assertThat(notificationConfig.getName()).contains(NOTIFICATION_RULE_CREATE); - } - @Test - public void testListNotificationRules() throws IOException { - ListNotification.listNotificationConfigs(PROJECT_ID, LOCATION); - assertThat(stdOut.toString()).contains(NOTIFICATION_TOPIC); - } - @Test - public void testUpdateNotificationRule() throws IOException { - UpdateNotification.updateNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); - NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); - assertThat(notificationConfig.getDescription()).contains("updated description"); - } + // TODO(Developer): Replace the below variables. + private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); + private static final String LOCATION = "global"; + private static final String NOTIFICATION_RULE_CREATE = + "random-notification-id-" + UUID.randomUUID(); + private static final String NOTIFICATION_TOPIC = "test-topic"; + private static final int MAX_ATTEMPT_COUNT = 3; + private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes + private static ByteArrayOutputStream stdOut; + + @Rule + public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule( + MAX_ATTEMPT_COUNT, + INITIAL_BACKOFF_MILLIS); + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)) + .isNotEmpty(); + + } + + @BeforeClass + public static void setUp() throws IOException { + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("SCC_PROJECT_ID"); + + // Create notification rules. + NotificationConfig result = CreateNotification.createNotificationConfig(PROJECT_ID, LOCATION, + NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); + System.out.printf("NotificationConfig: " + result.getName() + " " + result.getDescription()); + + stdOut = null; + System.setOut(out); + TimeUnit.MINUTES.sleep(3); + + } + + @AfterClass + public static void cleanUp() throws IOException { + final PrintStream out = System.out; + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + DeleteNotification.deleteNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(stdOut.toString()).contains( + "Deleted Notification config: " + NOTIFICATION_RULE_CREATE); + + stdOut = null; + System.setOut(out); + + } + + @Before + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + } + + @After + public void afterEach() { + stdOut = null; + System.setOut(null); + + } + + @Test + public void testGetNotificationRule() throws IOException { + NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, + LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(notificationConfig.getName()).contains(NOTIFICATION_RULE_CREATE); + + } + + @Test + public void testListNotificationRules() throws IOException { + ListNotification.listNotificationConfigs(PROJECT_ID, LOCATION); + assertThat(stdOut.toString()).contains(NOTIFICATION_TOPIC); + + } + + @Test + public void testUpdateNotificationRule() throws IOException { + UpdateNotification.updateNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, + NOTIFICATION_RULE_CREATE); + NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, + LOCATION, NOTIFICATION_RULE_CREATE); + assertThat(notificationConfig.getDescription()).contains("updated description"); + + } + } \ No newline at end of file From 920983e2ae83a942593be4ffffa2ad7e3b947f0b Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Mon, 1 Apr 2024 16:41:25 +0000 Subject: [PATCH 3/6] add InterruptedException in unit-test --- .../snippets/src/test/java/vtwo/NotificationIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java index 93959e0b86a..8412e14f8e5 100644 --- a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -68,7 +68,7 @@ public static void requireEnvVar(String envVarName) { } @BeforeClass - public static void setUp() throws IOException { + public static void setUp() throws IOException, InterruptedException{ final PrintStream out = System.out; stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); From 99b669e1dc3afca4a5887e98b600c4064e3fb363 Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Wed, 3 Apr 2024 19:27:29 +0000 Subject: [PATCH 4/6] update license headers --- .../src/main/java/vtwo/notifications/CreateNotification.java | 2 +- .../src/main/java/vtwo/notifications/DeleteNotification.java | 2 +- .../src/main/java/vtwo/notifications/GetNotification.java | 2 +- .../src/main/java/vtwo/notifications/UpdateNotification.java | 2 +- .../snippets/src/test/java/vtwo/NotificationIT.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java index 58d83744b45..7385a1f6ff9 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/CreateNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java index 057e25271f2..90d39551d73 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/DeleteNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java index 077ca36ebe0..227dec34ba2 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java index 1989ea63f0d..ebcd4cba127 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/UpdateNotification.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java index 8412e14f8e5..3f8aa0a88e1 100644 --- a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 0bbf8002c714f6920663bccb56451bb2fcf5a511 Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Fri, 5 Apr 2024 17:36:27 +0000 Subject: [PATCH 5/6] improve readability --- .../java/vtwo/notifications/GetNotification.java | 7 +------ .../snippets/src/test/java/vtwo/NotificationIT.java | 13 ++++--------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java index 227dec34ba2..407b1cca720 100644 --- a/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java +++ b/security-command-center/snippets/src/main/java/vtwo/notifications/GetNotification.java @@ -53,12 +53,7 @@ public static NotificationConfig getNotificationConfig( notificationConfigId)) .build(); - System.out.printf("getNotificationConfig config:" + String.format( - "projects/%s/locations/%s/notificationConfigs/%s", - parentId, - location, - notificationConfigId)); - + // Call the API. NotificationConfig response = client.getNotificationConfig(request); diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java index 3f8aa0a88e1..c9229938664 100644 --- a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -64,11 +64,10 @@ public static void requireEnvVar(String envVarName) { assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) .that(System.getenv(envVarName)) .isNotEmpty(); - } @BeforeClass - public static void setUp() throws IOException, InterruptedException{ + public static void setUp() throws IOException, InterruptedException { final PrintStream out = System.out; stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); @@ -84,7 +83,6 @@ public static void setUp() throws IOException, InterruptedException{ stdOut = null; System.setOut(out); TimeUnit.MINUTES.sleep(3); - } @AfterClass @@ -98,36 +96,33 @@ public static void cleanUp() throws IOException { stdOut = null; System.setOut(out); - } @Before public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - } @After public void afterEach() { stdOut = null; System.setOut(null); - } @Test public void testGetNotificationRule() throws IOException { NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); - assertThat(notificationConfig.getName()).contains(NOTIFICATION_RULE_CREATE); + assertThat(notificationConfig.getName()).contains(NOTIFICATION_RULE_CREATE); } @Test public void testListNotificationRules() throws IOException { ListNotification.listNotificationConfigs(PROJECT_ID, LOCATION); - assertThat(stdOut.toString()).contains(NOTIFICATION_TOPIC); + assertThat(stdOut.toString()).contains(NOTIFICATION_TOPIC); } @Test @@ -136,8 +131,8 @@ public void testUpdateNotificationRule() throws IOException { NOTIFICATION_RULE_CREATE); NotificationConfig notificationConfig = GetNotification.getNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); - assertThat(notificationConfig.getDescription()).contains("updated description"); + assertThat(notificationConfig.getDescription()).contains("updated description"); } } \ No newline at end of file From a8dfca346714fe6a7e7ca737f1d347827b2bf5ef Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Thu, 18 Apr 2024 12:47:41 +0000 Subject: [PATCH 6/6] fix topic error --- .../src/test/java/vtwo/NotificationIT.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java index c9229938664..280d00028e2 100644 --- a/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/NotificationIT.java @@ -19,13 +19,14 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.cloud.securitycenter.v2.NotificationConfig; import com.google.cloud.testing.junit4.MultipleAttemptsRule; +import com.google.pubsub.v1.ProjectTopicName; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.UUID; -import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -44,12 +45,12 @@ @RunWith(JUnit4.class) public class NotificationIT { - // TODO(Developer): Replace the below variables. + // TODO: Replace the below variables. private static final String PROJECT_ID = System.getenv("SCC_PROJECT_ID"); private static final String LOCATION = "global"; private static final String NOTIFICATION_RULE_CREATE = "random-notification-id-" + UUID.randomUUID(); - private static final String NOTIFICATION_TOPIC = "test-topic"; + private static final String NOTIFICATION_TOPIC = "test-topic-" + UUID.randomUUID(); private static final int MAX_ATTEMPT_COUNT = 3; private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes private static ByteArrayOutputStream stdOut; @@ -75,6 +76,9 @@ public static void setUp() throws IOException, InterruptedException { requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); requireEnvVar("SCC_PROJECT_ID"); + // Create pubsub topic. + createPubSubTopic(PROJECT_ID, NOTIFICATION_TOPIC); + // Create notification rules. NotificationConfig result = CreateNotification.createNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_TOPIC, NOTIFICATION_RULE_CREATE); @@ -82,7 +86,6 @@ public static void setUp() throws IOException, InterruptedException { stdOut = null; System.setOut(out); - TimeUnit.MINUTES.sleep(3); } @AfterClass @@ -90,10 +93,13 @@ public static void cleanUp() throws IOException { final PrintStream out = System.out; stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); + DeleteNotification.deleteNotificationConfig(PROJECT_ID, LOCATION, NOTIFICATION_RULE_CREATE); assertThat(stdOut.toString()).contains( "Deleted Notification config: " + NOTIFICATION_RULE_CREATE); + deletePubSubTopic(PROJECT_ID, NOTIFICATION_TOPIC); + stdOut = null; System.setOut(out); } @@ -135,4 +141,16 @@ public void testUpdateNotificationRule() throws IOException { assertThat(notificationConfig.getDescription()).contains("updated description"); } + public static void createPubSubTopic(String projectId, String topicId) throws IOException { + ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); + TopicAdminClient client = TopicAdminClient.create(); + client.createTopic(topicName); + } + + public static void deletePubSubTopic(String projectId, String topicId) throws IOException { + ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); + TopicAdminClient client = TopicAdminClient.create(); + client.deleteTopic(topicName); + } + } \ No newline at end of file