Skip to content

Commit 4e7c5e5

Browse files
committed
feat(securitycenter): Add Resource SCC Management API Org security center service custom module samples
1 parent 37eec28 commit 4e7c5e5

File tree

5 files changed

+265
-1
lines changed

5 files changed

+265
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// Retrieve a specific security center service by its name.
19+
function main(organizationId, service, location = 'global') {
20+
// [START securitycenter_get_security_center_service]
21+
// Imports the Google Cloud client library.
22+
const {SecurityCenterManagementClient} =
23+
require('@google-cloud/securitycentermanagement').v1;
24+
25+
// Create a Security Center Management client
26+
const client = new SecurityCenterManagementClient();
27+
28+
/*
29+
* Required. Resource name of security center service
30+
* Its format is
31+
* `organizations/[organizationId]/locations/[location]/securityCenterServices/[service]`
32+
* `folders/[folderId]/locations/[location]/securityCenterServices/[service]`
33+
* `projects/[projectId]/locations/[location]/securityCenterServices/[service]`
34+
*/
35+
// TODO(developer): Update the following references for your own environment before running the sample.
36+
// const organizationId = 'YOUR_ORGANIZATION_ID';
37+
// const location = 'LOCATION_ID';
38+
// const service = 'SERVICE';
39+
// Replace SERVICE with one of the valid values:
40+
// container-threat-detection, event-threat-detection, security-health-analytics,
41+
// vm-threat-detection, web-security-scanner
42+
const name = `organizations/${organizationId}/locations/${location}/securityCenterServices/${service}`;
43+
44+
// Build the request.
45+
const getSecurityCenterServiceRequest = {
46+
name: name,
47+
};
48+
49+
async function getSecurityCenterService() {
50+
// Call the API.
51+
const [response] = await client.getSecurityCenterService(
52+
getSecurityCenterServiceRequest
53+
);
54+
console.log('Retrieved SecurityCenterService:', response.name);
55+
}
56+
57+
getSecurityCenterService();
58+
// [END securitycenter_get_security_center_service]
59+
}
60+
61+
main(...process.argv.slice(2));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// List all security center services for the given parent.
19+
function main(organizationId, location = 'global') {
20+
// [START securitycenter_list_security_center_service]
21+
// Imports the Google Cloud client library.
22+
const {SecurityCenterManagementClient} =
23+
require('@google-cloud/securitycentermanagement').v1;
24+
25+
// Create a Security Center Management client
26+
const client = new SecurityCenterManagementClient();
27+
28+
/**
29+
* Required. The name of the parent resource. Its
30+
* format is "organizations/[organizationId]/locations/[location]",
31+
* "folders/[folderId]/locations/[location]", or
32+
* "projects/[projectId]/locations/[location]".
33+
*/
34+
//TODO(developer): Update the following references for your own environment before running the sample.
35+
// const organizationId = 'YOUR_ORGANIZATION_ID';
36+
// const location = 'LOCATION_ID';
37+
const parent = `organizations/${organizationId}/locations/${location}`;
38+
39+
// Build the request.
40+
const listSecurityCenterServicesRequest = {
41+
parent: parent,
42+
};
43+
44+
async function listSecurityCenterServices() {
45+
// Call the API.
46+
const [services] = await client.listSecurityCenterServices(
47+
listSecurityCenterServicesRequest
48+
);
49+
for (const service of services) {
50+
console.log('Security Center Service Name:', service.name);
51+
}
52+
}
53+
54+
listSecurityCenterServices();
55+
// [END securitycenter_list_security_center_service]
56+
}
57+
58+
main(...process.argv.slice(2));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// Updates a security center service configuration.
19+
function main(organizationId, service, location = 'global') {
20+
// [START securitycenter_update_security_center_service]
21+
// Imports the Google Cloud client library.
22+
const {SecurityCenterManagementClient} =
23+
require('@google-cloud/securitycentermanagement').v1;
24+
25+
// Create a Security Center Management client
26+
const client = new SecurityCenterManagementClient();
27+
28+
/*
29+
* Required. Resource name of security center service
30+
* Its format is
31+
* `organizations/[organizationId]/locations/[location]/securityCenterServices/[service]`
32+
* `folders/[folderId]/locations/[location]/securityCenterServices/[service]`
33+
* `projects/[projectId]/locations/[location]/securityCenterServices/[service]`
34+
*/
35+
// TODO(developer): Update the following references for your own environment before running the sample.
36+
// const organizationId = 'YOUR_ORGANIZATION_ID';
37+
// const location = 'LOCATION_ID';
38+
// const service = 'SERVICE';
39+
// Replace SERVICE with one of the valid values:
40+
// container-threat-detection, event-threat-detection, security-health-analytics,
41+
// vm-threat-detection, web-security-scanner
42+
const name = `organizations/${organizationId}/locations/${location}/securityCenterServices/${service}`;
43+
44+
// Define the security center service configuration, update the
45+
// IntendedEnablementState accordingly.
46+
const securityCenterService = {
47+
name: name,
48+
intendedEnablementState: 'ENABLED',
49+
};
50+
51+
// Set the field mask to specify which properties should be updated.
52+
const fieldMask = {
53+
paths: ['intended_enablement_state'],
54+
};
55+
56+
// Build the request.
57+
const updateSecurityCenterServiceRequest = {
58+
securityCenterService: securityCenterService,
59+
updateMask: fieldMask,
60+
};
61+
62+
async function updateSecurityCenterService() {
63+
// Call the API.
64+
const [response] = await client.updateSecurityCenterService(
65+
updateSecurityCenterServiceRequest
66+
);
67+
console.log(
68+
`Updated SecurityCenterService: ${response.name} with new enablement state: ${response.intendedEnablementState}`
69+
);
70+
}
71+
72+
updateSecurityCenterService();
73+
// [END securitycenter_update_security_center_service]
74+
}
75+
76+
main(...process.argv.slice(2));

security-center/snippets/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"license": "Apache-2.0",
1515
"dependencies": {
1616
"@google-cloud/pubsub": "^4.0.0",
17-
"@google-cloud/security-center": "^8.7.0"
17+
"@google-cloud/security-center": "^8.7.0",
18+
"@google-cloud/securitycentermanagement": "^0.5.0"
1819
},
1920
"devDependencies": {
2021
"c8": "^10.0.0",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const {assert} = require('chai');
18+
const {execSync} = require('child_process');
19+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
20+
const {describe, it} = require('mocha');
21+
22+
// TODO(developer): update for your own environment
23+
const organizationId = '1081635000895';
24+
const location = 'global';
25+
// Replace service with one of the valid values:
26+
// container-threat-detection, event-threat-detection, security-health-analytics,
27+
// vm-threat-detection, web-security-scanner
28+
const service = 'event_threat_detection';
29+
30+
describe('Security Center Service', async () => {
31+
let data = {
32+
orgId: organizationId,
33+
service: service,
34+
};
35+
36+
it('should get the security center service', done => {
37+
const output = exec(
38+
`node management_api/getSecurityCenterService.js ${data.orgId} ${data.service}`
39+
);
40+
assert(output.includes(data.orgId));
41+
assert(output.includes(data.service));
42+
assert.match(output, /Retrieved SecurityCenterService/);
43+
assert.notMatch(output, /undefined/);
44+
done();
45+
});
46+
47+
it('should list the security center services', done => {
48+
const output = exec(
49+
`node management_api/listSecurityCenterServices.js ${data.orgId}`
50+
);
51+
assert(output.includes(data.orgId));
52+
assert(output.includes(data.service.toUpperCase()));
53+
assert.match(output, /Security Center Service Name/);
54+
assert.notMatch(output, /undefined/);
55+
done();
56+
});
57+
58+
it('should update the security center service', done => {
59+
const output = exec(
60+
`node management_api/updateSecurityCenterService.js ${data.orgId} ${data.service}`
61+
);
62+
assert(output.includes(data.orgId));
63+
assert(output.includes(data.service));
64+
assert.match(output, /Updated SecurityCenterService/);
65+
assert.notMatch(output, /undefined/);
66+
done();
67+
});
68+
});

0 commit comments

Comments
 (0)