Skip to content

Commit fa78f79

Browse files
author
Patrick Glinsman
committed
Added PHP code samples for SecurityCenter
1 parent 8e3b542 commit fa78f79

File tree

9 files changed

+473
-0
lines changed

9 files changed

+473
-0
lines changed

securitycenter/composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
3+
"require": {
4+
"google/cloud-security-center": "^0.5.0",
5+
"google/cloud-pubsub": "^1.21"
6+
},
7+
"require-dev": {
8+
"phpunit/phpunit": "^8"
9+
}
10+
}

securitycenter/phpunit.xml.dist

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2020 Google LLC.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<phpunit backupGlobals="false"
18+
backupStaticAttributes="false"
19+
bootstrap="../testing/bootstrap.php"
20+
colors="true"
21+
processIsolation="false"
22+
stopOnFailure="false"
23+
timeoutForSmallTests="10"
24+
timeoutForMediumTests="30"
25+
timeoutForLargeTests="120">
26+
<testsuites>
27+
<testsuite name="PHP SecurityCenter tests">
28+
<directory>test</directory>
29+
</testsuite>
30+
</testsuites>
31+
<logging>
32+
<log type="coverage-clover" target="./build/logs/clover.xml"/>
33+
</logging>
34+
<filter>
35+
<whitelist addUncoveredFilesFromWhitelist="true">
36+
<directory>./src</directory>
37+
<exclude>
38+
<directory>./vendor</directory>
39+
</exclude>
40+
</whitelist>
41+
</filter>
42+
</phpunit>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $organizationId, $notificationConfigId, $projectId, $topicName) = $argv;
24+
25+
// [START scc_create_notification_config]
26+
27+
use \Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
28+
use \Google\Cloud\SecurityCenter\V1\NotificationConfig;
29+
30+
31+
/** Uncomment and populate these variables in your code */
32+
// $organizationId = "{your-org-id}";
33+
// $notificationConfigId = {"your-unique-id"};
34+
// $projectId = "{your-project}"";
35+
// $topicName = "{your-topic}";
36+
37+
$securityCenterClient = new SecurityCenterClient();
38+
$organizationName = "organizations/" . $organizationId;
39+
$pubsubTopic = "projects/" . $projectId . "/topics/" . $topicName;
40+
41+
try {
42+
$streamingConfig = new NotificationConfig\StreamingConfig();
43+
$streamingConfig->setFilter("state = \"ACTIVE\"");
44+
$notificationConfig = new NotificationConfig();
45+
$notificationConfig->setDescription("PHP notification config");
46+
$notificationConfig->setPubsubTopic($pubsubTopic);
47+
$notificationConfig->setStreamingConfig($streamingConfig);
48+
49+
$response = $securityCenterClient->createNotificationConfig($organizationName, $notificationConfigId, $notificationConfig);
50+
printf("Notification config was created: %s", $response->getName());
51+
52+
} finally {
53+
$securityCenterClient->close();
54+
}
55+
56+
// [END scc_create_notification_config]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $organizationId, $notificationConfigId) = $argv;
24+
25+
// [START scc_delete_notification_config]
26+
27+
use \Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
28+
29+
/** Uncomment and populate these variables in your code */
30+
// $organizationId = "{your-org-id}";
31+
// $notificationConfigId = {"your-unique-id"};
32+
33+
$securityCenterClient = new SecurityCenterClient();
34+
$organizationName = "organizations/" . $organizationId;
35+
$notificationConfigName = $organizationName . "/notificationConfigs/" . $notificationConfigId;
36+
37+
try {
38+
$response = $securityCenterClient->deleteNotificationConfig($notificationConfigName);
39+
printf("Notification config was deleted");
40+
41+
} finally {
42+
$securityCenterClient->close();
43+
}
44+
45+
// [END scc_delete_notification_config]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $organizationId, $notificationConfigId) = $argv;
24+
25+
// [START scc_get_notification_config]
26+
27+
use \Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
28+
29+
/** Uncomment and populate these variables in your code */
30+
// $organizationId = "{your-org-id}";
31+
// $notificationConfigId = {"your-unique-id"};
32+
33+
$securityCenterClient = new SecurityCenterClient();
34+
$organizationName = "organizations/" . $organizationId;
35+
$notificationConfigName = $organizationName . "/notificationConfigs/" . $notificationConfigId;
36+
37+
try {
38+
$response = $securityCenterClient->getNotificationConfig($notificationConfigName);
39+
printf("Notification config was retrieved: %s", $response->getName());
40+
41+
} finally {
42+
$securityCenterClient->close();
43+
}
44+
// [END scc_get_notification_config]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $organizationId) = $argv;
24+
25+
// [START scc_list_notification_configs]
26+
27+
use \Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
28+
29+
/** Uncomment and populate these variables in your code */
30+
// $organizationId = "{your-org-id}";
31+
32+
$securityCenterClient = new SecurityCenterClient();
33+
$organizationName = "organizations/" . $organizationId;
34+
35+
try {
36+
$pagedResponse = $securityCenterClient->listNotificationConfigs($organizationName);
37+
$count = 0;
38+
foreach ($pagedResponse->iterateAllElements() as $element) {
39+
$count += 1;
40+
}
41+
42+
printf("Notification configs were listed");
43+
return $count;
44+
45+
} finally {
46+
$securityCenterClient->close();
47+
}
48+
49+
// [END scc_list_notification_configs]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $projectId, $subscriptionId) = $argv;
24+
25+
// [START scc_receive_notifications]
26+
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/** Uncomment and populate these variables in your code */
30+
// String projectId = "{your-project}";
31+
// String subscriptionId = "{your-subscription}";
32+
33+
$projectSubscriptionName = "projects/" . $projectId . "/subscriptions/" . $subscriptionId;
34+
$subscription = $pubsub->subscription($projectSubscriptionName);
35+
36+
$pubsub = new PubSubClient([
37+
'projectId' => $projectId,
38+
]);
39+
40+
foreach ($subscription->pull() as $message) {
41+
printf('Message: %s' . PHP_EOL, $message->data());
42+
// Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times.
43+
$subscription->acknowledge($message);
44+
}
45+
// [END scc_receive_notifications]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// Include Google Cloud dependendencies using Composer
19+
require_once __DIR__ . '/../vendor/autoload.php';
20+
if (count($argv) < 1) {
21+
return printf("Usage: php %s PROJECT_ID STRING\n", __FILE__);
22+
}
23+
list($_, $organizationId, $notificationConfigId, $projectId, $topicName) = $argv;
24+
25+
// [START scc_update_notification_config]
26+
27+
use \Google\Cloud\SecurityCenter\V1\SecurityCenterClient;
28+
use \Google\Cloud\SecurityCenter\V1\NotificationConfig;
29+
use \Google\Protobuf\FieldMask;
30+
31+
/** Uncomment and populate these variables in your code */
32+
// $organizationId = "{your-org-id}";
33+
// $notificationConfigId = {"your-unique-id"};
34+
// $projectId = "{your-project}"";
35+
// $topicName = "{your-topic}";
36+
37+
$securityCenterClient = new SecurityCenterClient();
38+
$organizationName = "organizations/" . $organizationId;
39+
40+
// Ensure this ServiceAccount has the "pubsub.topics.setIamPolicy" permission on the topic.
41+
$pubsubTopic = "projects/" . $projectId . "/topics/" . $topicName;
42+
$notificationConfigName = $organizationName . "/notificationConfigs/" . $notificationConfigId;
43+
44+
try {
45+
$streamingConfig = new NotificationConfig\StreamingConfig();
46+
$streamingConfig->setFilter("state = \"ACTIVE\"");
47+
$notificationConfig = new NotificationConfig();
48+
$notificationConfig->setName($notificationConfigName);
49+
$notificationConfig->setDescription("Updated description.");
50+
$notificationConfig->setPubsubTopic($pubsubTopic);
51+
$fieldMask = new FieldMask();
52+
$fieldMask->setPaths(array("description", "pubsub_topic"));
53+
54+
$response = $securityCenterClient->updateNotificationConfig($notificationConfig, array($fieldMask));
55+
printf("Notification config was updated: %s", $response->getName());
56+
57+
} finally {
58+
$securityCenterClient->close();
59+
}
60+
61+
// [END scc_update_notification_config]

0 commit comments

Comments
 (0)