Skip to content

Commit 00c98db

Browse files
authored
docs: Add v1p1beta1 notifications samples (#214)
1 parent c54115f commit 00c98db

8 files changed

+411
-1
lines changed

security-center/snippets/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
},
1414
"license": "Apache-2.0",
1515
"dependencies": {
16+
"@google-cloud/pubsub": "^1.5.0",
1617
"@google-cloud/security-center": "^3.0.1"
1718
},
1819
"devDependencies": {
1920
"chai": "^4.2.0",
20-
"mocha": "^7.0.0"
21+
"mocha": "^7.0.0",
22+
"uuid": "^3.4.0"
2123
}
2224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {
18+
SecurityCenterClient,
19+
} = require('@google-cloud/security-center').v1p1beta1;
20+
const uuidv1 = require('uuid/v1');
21+
const {assert} = require('chai');
22+
const {describe, it, before, after} = require('mocha');
23+
const {execSync} = require('child_process');
24+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
25+
26+
const organizationId = process.env['GCLOUD_ORGANIZATION'];
27+
const orgName = 'organizations/' + organizationId;
28+
const pubsubTopic = 'projects/project-a-id/topics/notifications-sample-topic';
29+
30+
describe('Client with Notifications', async () => {
31+
const createConfig = 'notif-config-test-node-create' + uuidv1();
32+
const deleteConfig = 'notif-config-test-node-delete' + uuidv1();
33+
const getConfig = 'notif-config-test-node-get' + uuidv1();
34+
const listConfig = 'notif-config-test-node-list' + uuidv1();
35+
const updateConfig = 'notif-config-test-node-update' + uuidv1();
36+
37+
before(async () => {
38+
const client = new SecurityCenterClient();
39+
async function createNotificationConfig(configId) {
40+
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "^_" }]*/
41+
const [_response] = await client.createNotificationConfig({
42+
parent: orgName,
43+
configId: configId,
44+
notificationConfig: {
45+
description: 'Sample config for node.js',
46+
pubsubTopic: pubsubTopic,
47+
eventType: 'FINDING',
48+
streamingConfig: {filter: 'state = "ACTIVE"'},
49+
},
50+
});
51+
}
52+
53+
await createNotificationConfig(deleteConfig);
54+
await createNotificationConfig(getConfig);
55+
await createNotificationConfig(listConfig);
56+
await createNotificationConfig(updateConfig);
57+
});
58+
59+
after(async () => {
60+
const client = new SecurityCenterClient();
61+
async function deleteNotificationConfig(configId) {
62+
const name = client.notificationConfigPath(organizationId, configId);
63+
await client.deleteNotificationConfig({name: name});
64+
}
65+
66+
await deleteNotificationConfig(createConfig);
67+
await deleteNotificationConfig(getConfig);
68+
await deleteNotificationConfig(listConfig);
69+
await deleteNotificationConfig(updateConfig);
70+
});
71+
72+
it('client can create config', () => {
73+
const output = exec(
74+
`node v1p1beta1/createNotificationConfig.js ${organizationId} ${createConfig} ${pubsubTopic}`
75+
);
76+
assert.include(output, createConfig);
77+
assert.match(output, /Notification config creation succeeded/);
78+
assert.notMatch(output, /undefined/);
79+
});
80+
81+
it('client can delete config', () => {
82+
const output = exec(
83+
`node v1p1beta1/deleteNotificationConfig.js ${organizationId} ${deleteConfig}`
84+
);
85+
assert.include(output, 'Notification config deleted');
86+
assert.notMatch(output, /undefined/);
87+
});
88+
89+
it('client can get config', () => {
90+
const output = exec(
91+
`node v1p1beta1/getNotificationConfig.js ${organizationId} ${getConfig}`
92+
);
93+
assert.include(output, getConfig);
94+
assert.match(output, /Notification config/);
95+
assert.notMatch(output, /undefined/);
96+
});
97+
98+
it('client can list configs', () => {
99+
const output = exec(
100+
`node v1p1beta1/listNotificationConfigs.js ${organizationId}`
101+
);
102+
assert.include(output, listConfig);
103+
assert.match(output, /Received Notification configs/);
104+
assert.notMatch(output, /undefined/);
105+
});
106+
107+
it('client can update configs', () => {
108+
const output = exec(
109+
`node v1p1beta1/updateNotificationConfig.js ${organizationId} ${updateConfig} ${pubsubTopic}`
110+
);
111+
assert.include(output, updateConfig);
112+
assert.match(output, /notification config update succeeded/);
113+
assert.notMatch(output, /undefined/);
114+
});
115+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
function main(
17+
organizationId = 'your-org-id',
18+
configId = 'your-config-name',
19+
pubsubTopic = 'projects/{your-project}/topics/{your-topic}'
20+
) {
21+
// [START scc_create_notification_config]
22+
// npm install @google-cloud/security-center/
23+
const {
24+
SecurityCenterClient,
25+
} = require('@google-cloud/security-center').v1p1beta1;
26+
27+
const client = new SecurityCenterClient();
28+
29+
// organizationId = "your-org-id";
30+
// configId = "your-config-name";
31+
// pubsubTopic = "projects/{your-project}/topics/{your-topic}";
32+
// Ensure this Service Account has the "pubsub.topics.setIamPolicy" permission on this topic.
33+
34+
const orgName = client.organizationPath(organizationId);
35+
36+
async function createNotificationConfig() {
37+
const [response] = await client.createNotificationConfig({
38+
parent: orgName,
39+
configId: configId,
40+
notificationConfig: {
41+
description: 'Sample config for node.js',
42+
pubsubTopic: pubsubTopic,
43+
eventType: 'FINDING',
44+
streamingConfig: {filter: 'state = "ACTIVE"'},
45+
},
46+
});
47+
console.log('Notification config creation succeeded: ', response);
48+
}
49+
50+
createNotificationConfig();
51+
// [END scc_create_notification_config]
52+
}
53+
54+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
function main(organizationId = 'your-org-id', configId = 'your-config-id') {
17+
// [START scc_delete_notification_config]
18+
// npm install @google-cloud/security-center/
19+
const {
20+
SecurityCenterClient,
21+
} = require('@google-cloud/security-center').v1p1beta1;
22+
23+
const client = new SecurityCenterClient();
24+
25+
// organizationId = "your-org-id";
26+
// configId = "your-config-id";
27+
const formattedConfigName = client.notificationConfigPath(
28+
organizationId,
29+
configId
30+
);
31+
32+
async function deleteNotificationConfg() {
33+
await client.deleteNotificationConfig({name: formattedConfigName});
34+
console.log('Notification config deleted: ', formattedConfigName);
35+
}
36+
37+
deleteNotificationConfg();
38+
// [END scc_delete_notification_config]
39+
}
40+
41+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
function main(organizationId = 'your-org-id', configId = 'your-config-id') {
17+
// [START scc_get_notification_configs]
18+
// npm install @google-cloud/security-center/
19+
const {
20+
SecurityCenterClient,
21+
} = require('@google-cloud/security-center').v1p1beta1;
22+
23+
const client = new SecurityCenterClient();
24+
25+
// organizationId = "your-org-id";
26+
// configId = "your-config-id";
27+
const formattedConfigName = client.notificationConfigPath(
28+
organizationId,
29+
configId
30+
);
31+
32+
async function getNotificationConfg() {
33+
const [response] = await client.getNotificationConfig({
34+
name: formattedConfigName,
35+
});
36+
console.log('Notification config: ', response);
37+
}
38+
39+
getNotificationConfg();
40+
// [END scc_get_notification_configs]
41+
}
42+
43+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
function main(organizationId = 'your-org-id') {
17+
// [END scc_list_notification_configs]
18+
// npm install @google-cloud/security-center/
19+
const {
20+
SecurityCenterClient,
21+
} = require('@google-cloud/security-center').v1p1beta1;
22+
23+
const client = new SecurityCenterClient();
24+
25+
// organizationId = "your-org-id";
26+
const orgName = client.organizationPath(organizationId);
27+
28+
async function listNotificationConfigs() {
29+
const [resources] = await client.listNotificationConfigs({parent: orgName});
30+
console.log('Received Notification configs: ');
31+
for (const resource of resources) {
32+
console.log(resource);
33+
}
34+
}
35+
36+
listNotificationConfigs();
37+
// [END scc_list_notification_configs]
38+
}
39+
40+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use strict';
15+
16+
function main(
17+
projectId = 'your-project-id',
18+
subscriptionId = 'your-subscription-id'
19+
) {
20+
// [START scc_receive_notifications]
21+
const {PubSub} = require('@google-cloud/pubsub');
22+
const {StringDecoder} = require('string_decoder');
23+
24+
// projectId = 'your-project-id'
25+
// subscriptionId = 'your-subscription-id'
26+
27+
const subscriptionName =
28+
'projects/' + projectId + '/subscriptions/' + subscriptionId;
29+
const pubSubClient = new PubSub();
30+
31+
function listenForMessages() {
32+
const subscription = pubSubClient.subscription(subscriptionName);
33+
34+
// message.data is a buffer array of json
35+
// 1. Convert buffer to normal string
36+
// 2. Convert json to NotificationMessage object
37+
const messageHandler = message => {
38+
const jsonString = new StringDecoder('utf-8').write(message.data);
39+
const parsedNotificationMessage = JSON.parse(jsonString);
40+
41+
console.log(parsedNotificationMessage);
42+
console.log(parsedNotificationMessage.finding);
43+
44+
// ACK when done with message
45+
message.ack();
46+
};
47+
48+
subscription.on('message', messageHandler);
49+
50+
// Set timeout to 10 seconds
51+
setTimeout(() => {
52+
subscription.removeListener('message', messageHandler);
53+
}, 10000);
54+
}
55+
56+
listenForMessages();
57+
// [END scc_receive_notifications]
58+
}
59+
60+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)