diff --git a/.kokoro/secrets-example.sh b/.kokoro/secrets-example.sh index 88710d0c69..287eb010cc 100644 --- a/.kokoro/secrets-example.sh +++ b/.kokoro/secrets-example.sh @@ -100,6 +100,10 @@ export REDIS_PORT= export GOOGLE_PUBSUB_SUBSCRIPTION=php-example-subscription export GOOGLE_PUBSUB_TOPIC=php-example-topic +# Security Center +export GOOGLE_ORGANIZATION_ID= +export GOOGLE_SECURITYCENTER_PUBSUB_TOPIC= + # Spanner export GOOGLE_SPANNER_INSTANCE_ID= export GOOGLE_SPANNER_DATABASE_ID=test-database diff --git a/.kokoro/secrets.sh.enc b/.kokoro/secrets.sh.enc index 0bb610715f..e966790143 100644 Binary files a/.kokoro/secrets.sh.enc and b/.kokoro/secrets.sh.enc differ diff --git a/securitycenter/composer.json b/securitycenter/composer.json new file mode 100644 index 0000000000..32fff1ac26 --- /dev/null +++ b/securitycenter/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "google/cloud-security-center": "^0.5.0", + "google/cloud-pubsub": "^1.21" + } +} diff --git a/securitycenter/phpunit.xml.dist b/securitycenter/phpunit.xml.dist new file mode 100644 index 0000000000..d59be6c31b --- /dev/null +++ b/securitycenter/phpunit.xml.dist @@ -0,0 +1,42 @@ + + + + + + test + + + + + + + + ./src + + ./vendor + + + + diff --git a/securitycenter/src/create_notification.php b/securitycenter/src/create_notification.php new file mode 100644 index 0000000000..26934a6989 --- /dev/null +++ b/securitycenter/src/create_notification.php @@ -0,0 +1,53 @@ +setFilter("state = \"ACTIVE\""); +$notificationConfig = (new NotificationConfig()) + ->setDescription('A sample notification config') + ->setPubsubTopic($pubsubTopic) + ->setStreamingConfig($streamingConfig); + +$response = $securityCenterClient->createNotificationConfig( + $organizationName, + $notificationConfigId, + $notificationConfig +); +printf('Notification config was created: %s' . PHP_EOL, $response->getName()); + +// [END scc_create_notification_config] diff --git a/securitycenter/src/delete_notification.php b/securitycenter/src/delete_notification.php new file mode 100644 index 0000000000..e318c2079e --- /dev/null +++ b/securitycenter/src/delete_notification.php @@ -0,0 +1,41 @@ +deleteNotificationConfig($notificationConfigName); +print('Notification config was deleted' . PHP_EOL); + +// [END scc_delete_notification_config] diff --git a/securitycenter/src/get_notification.php b/securitycenter/src/get_notification.php new file mode 100644 index 0000000000..4000761e77 --- /dev/null +++ b/securitycenter/src/get_notification.php @@ -0,0 +1,41 @@ +getNotificationConfig($notificationConfigName); +printf('Notification config was retrieved: %s' . PHP_EOL, $response->getName()); + +// [END scc_get_notification_config] diff --git a/securitycenter/src/list_notification.php b/securitycenter/src/list_notification.php new file mode 100644 index 0000000000..b67e23ab5c --- /dev/null +++ b/securitycenter/src/list_notification.php @@ -0,0 +1,40 @@ +listNotificationConfigs($organizationName) as $element) { + printf('Found notification config %s' . PHP_EOL, $element->getName()); +} + +print('Notification configs were listed' . PHP_EOL); + +// [END scc_list_notification_configs] diff --git a/securitycenter/src/receive_notification.php b/securitycenter/src/receive_notification.php new file mode 100644 index 0000000000..ddb39bf785 --- /dev/null +++ b/securitycenter/src/receive_notification.php @@ -0,0 +1,43 @@ + $projectId, +]); +$subscription = $pubsub->subscription($subscriptionId); + +foreach ($subscription->pull() as $message) { + printf('Message: %s' . PHP_EOL, $message->data()); + // Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times. + $subscription->acknowledge($message); +} + +// [END scc_receive_notifications] diff --git a/securitycenter/src/update_notification.php b/securitycenter/src/update_notification.php new file mode 100644 index 0000000000..0711b0e4c5 --- /dev/null +++ b/securitycenter/src/update_notification.php @@ -0,0 +1,54 @@ +setFilter("state = \"ACTIVE\""); +$fieldMask = (new FieldMask())->setPaths(['description', 'pubsub_topic']); +$notificationConfig = (new NotificationConfig()) + ->setName($notificationConfigName) + ->setDescription('Updated description.') + ->setPubsubTopic($pubsubTopic); + +$response = $securityCenterClient->updateNotificationConfig($notificationConfig, [$fieldMask]); +printf('Notification config was updated: %s' . PHP_EOL, $response->getName()); + +// [END scc_update_notification_config] diff --git a/securitycenter/test/SecurityCenterTest.php b/securitycenter/test/SecurityCenterTest.php new file mode 100644 index 0000000000..d3828a394e --- /dev/null +++ b/securitycenter/test/SecurityCenterTest.php @@ -0,0 +1,127 @@ +runSnippet('delete_notification', [ + self::getOrganizationId(), + $configId, + ]); + + $this->assertContains('Notification config was deleted', $deleteOutput); + } + + public function testCreateNotification() + { + $createOutput = $this->runSnippet('create_notification', [ + self::getOrganizationId(), + self::$testNotificationCreate, + self::$projectId, + self::getTopicName() + ]); + + $this->assertContains('Notification config was created', $createOutput); + + self::deleteConfig(self::$testNotificationCreate); + } + + public function testGetNotificationConfig() + { + $createOutput = $this->runSnippet('create_notification', [ + self::getOrganizationId(), + self::$testNotificationGet, + self::$projectId, + self::getTopicName() + ]); + + $this->assertContains('Notification config was created', $createOutput); + + $getOutput = $this->runSnippet('get_notification', [ + self::getOrganizationId(), + self::$testNotificationGet + ]); + + $this->assertContains('Notification config was retrieved', $getOutput); + + self::deleteConfig(self::$testNotificationGet); + } + + public function testUpdateNotificationConfig() + { + $createOutput = $this->runSnippet('create_notification', [ + self::getOrganizationId(), + self::$testNotificationUpdate, + self::$projectId, + self::getTopicName() + ]); + + $this->assertContains('Notification config was created', $createOutput); + + $getOutput = $this->runSnippet('update_notification', [ + self::getOrganizationId(), + self::$testNotificationUpdate, + self::$projectId, + self::getTopicName() + ]); + + $this->assertContains('Notification config was updated', $getOutput); + + self::deleteConfig(self::$testNotificationUpdate); + } + + public function testListNotificationConfig() + { + $listOutput = $this->runSnippet('list_notification', [ + self::getOrganizationId(), + ]); + + $this->assertContains('Notification configs were listed', $listOutput); + } + + private static function getOrganizationId() + { + return self::requireEnv('GOOGLE_ORGANIZATION_ID'); + } + + private static function getTopicName() + { + return self::requireEnv('GOOGLE_SECURITYCENTER_PUBSUB_TOPIC'); + } + + private static function randomNotificationId() + { + return uniqid('php-notification-config-'); + } +} diff --git a/spanner/test/spannerTest.php b/spanner/test/spannerTest.php index d36f8f695a..fd166a9ecd 100644 --- a/spanner/test/spannerTest.php +++ b/spanner/test/spannerTest.php @@ -791,7 +791,6 @@ public static function tearDownAfterClass() if (strstr($db->name(), self::$databaseId) !== false) { try { $db->drop(); - } catch (\Exception $e) { $exceptions[] = $e; }