-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rafaelMurata/finding-source-samples
feat: Adding API v2 Source Finding sample
- Loading branch information
Showing
6 changed files
with
386 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
security-center/snippets/system-test/v2/findings.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
const { SecurityCenterClient } = require('@google-cloud/security-center').v2; | ||
const { assert } = require('chai'); | ||
const { execSync } = require('child_process'); | ||
const exec = cmd => execSync(cmd, { encoding: 'utf8' }); | ||
const { describe, it, before } = require('mocha'); | ||
const uuid = require('uuid'); | ||
|
||
const organizationId = process.env['GCLOUD_ORGANIZATION']; | ||
const location = 'global'; | ||
|
||
describe('Client with SourcesAndFindings V2', async () => { | ||
let data; | ||
before(async () => { | ||
// Creates a new client. | ||
const client = new SecurityCenterClient(); | ||
const [source] = await client | ||
.createSource({ | ||
source: { | ||
displayName: 'Customized Display Name V2', | ||
description: 'A new custom source that does X', | ||
}, | ||
parent: client.organizationPath(organizationId), | ||
}) | ||
.catch(error => console.error(error)); | ||
|
||
const sourceId = source.name.split('/')[3]; | ||
const parent = `organizations/${organizationId}/sources/${sourceId}/locations/${location}`; | ||
const resourceName = `//cloudresourcemanager.googleapis.com/organizations/${organizationId}`; | ||
const findingId = uuid.v4().replace(/-/g, ''); | ||
|
||
const eventTime = new Date(); | ||
const createFindingTemplate = { | ||
parent: parent, | ||
findingId: findingId, | ||
finding: { | ||
state: 'ACTIVE', | ||
// Resource the finding is associated with. This is an | ||
// example any resource identifier can be used. | ||
resourceName: resourceName, | ||
// A free-form category. | ||
category: 'MEDIUM_RISK_ONE', | ||
// The time associated with discovering the issue. | ||
eventTime: { | ||
seconds: Math.floor(eventTime.getTime() / 1000), | ||
nanos: (eventTime.getTime() % 1000) * 1e6, | ||
}, | ||
}, | ||
}; | ||
const [finding] = await client.createFinding(createFindingTemplate); | ||
createFindingTemplate.findingId = 'untouchedFindingId'; | ||
createFindingTemplate.finding.category = 'XSS'; | ||
const [untouchedFinding] = await client | ||
.createFinding(createFindingTemplate) | ||
.catch(error => console.error(error)); | ||
data = { | ||
orgId: organizationId, | ||
sourceId: sourceId, | ||
sourceName: source.name, | ||
findingName: finding.name, | ||
untouchedFindingName: untouchedFinding.name, | ||
}; | ||
console.log('my data %j', data); | ||
}); | ||
|
||
it('client can create source V2', () => { | ||
const output = exec(`node v2/createSource.js ${data.orgId}`); | ||
assert.match(output, new RegExp(data.orgId)); | ||
assert.match(output, /New Source created/); | ||
assert.notMatch(output, /undefined/); | ||
}); | ||
|
||
it('client can create a finding V2', () => { | ||
const output = exec(`node v2/createFinding.js ${data.orgId} ${data.sourceId}`); | ||
assert.match(output, new RegExp(data.sourceName)); | ||
assert.match(output, /New finding created/); | ||
assert.notMatch(output, /undefined/); | ||
}); | ||
|
||
it('client can list all findings V2', () => { | ||
const output = exec(`node v2/listAllFindings.js ${data.orgId}`); | ||
assert.match(output, new RegExp(data.findingName)); | ||
assert.match(output, new RegExp(data.untouchedFindingName)); | ||
assert.notMatch(output, /undefined/); | ||
}); | ||
|
||
it('client can list only some findings V2', () => { | ||
const output = exec(`node v2/listFilteredFindings.js ${data.orgId}`); | ||
assert.match(output, new RegExp(data.findingName)); | ||
assert.notMatch(output, new RegExp(data.untouchedFindingName)); | ||
assert.notMatch(output, /undefined/); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Demonstrates how to create a new security finding in CSCC. | ||
*/ | ||
function main(organizationId, sourceId, location = 'global', category = 'LOW_RISK_ONE') { | ||
// [START securitycenter_create_finding_v2] | ||
// Imports the Google Cloud client library. | ||
const { SecurityCenterClient } = require('@google-cloud/security-center').v2; | ||
const uuid = require('uuid'); | ||
|
||
// Create a Security Center client | ||
const client = new SecurityCenterClient(); | ||
|
||
/** | ||
* Required. Resource name of the new finding's parent. The following list | ||
* shows some examples of the format: | ||
* `organizations/[organization_id]/sources/[source_id]` | ||
* `organizations/[organization_id]/sources/[source_id]/locations/[location_id]` | ||
*/ | ||
const parent = `organizations/${organizationId}/sources/${sourceId}/locations/${location}`; | ||
|
||
// The resource this finding applies to. The Cloud Security Command Center UI can link the | ||
// findings for a resource to the corresponding asset of a resource if there are matches. | ||
const resourceName = `//cloudresourcemanager.googleapis.com/organizations/${organizationId}`; | ||
|
||
/** | ||
* Required. Unique identifier provided by the client within the parent scope. | ||
* It must be alphanumeric and less than or equal to 32 characters and | ||
* greater than 0 characters in length. | ||
*/ | ||
const findingId = uuid.v4().replace(/-/g, ''); | ||
|
||
// Get the current timestamp. | ||
const eventTime = new Date(); | ||
|
||
// Build the finding object. | ||
const finding = { | ||
parent: parent, | ||
state: 'ACTIVE', | ||
severity: 'LOW', | ||
mute: 'UNMUTED', | ||
findingClass: 'OBSERVATION', | ||
resourceName: resourceName, | ||
eventTime: { | ||
seconds: Math.floor(eventTime.getTime() / 1000), | ||
nanos: (eventTime.getTime() % 1000) * 1e6, | ||
}, | ||
category, | ||
}; | ||
|
||
// Build the create finding request. | ||
const createFindingRequest = { | ||
parent, | ||
findingId, | ||
finding, | ||
}; | ||
|
||
async function createFinding() { | ||
|
||
// Call the API. | ||
const [finding] = await client.createFinding(createFindingRequest); | ||
console.log('New finding created: %j', finding); | ||
} | ||
|
||
createFinding(); | ||
// [END securitycenter_create_finding_v2] | ||
} | ||
|
||
main(...process.argv.slice(2)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Demonstrates how to create a security source. | ||
*/ | ||
function main(organizationId) { | ||
// [START securitycenter_create_source_v2] | ||
// Imports the Google Cloud client library. | ||
const { SecurityCenterClient } = require('@google-cloud/security-center').v2; | ||
|
||
// Create a new Security Center client | ||
const client = new SecurityCenterClient(); | ||
|
||
/** | ||
* Required. Resource name of the new source's parent. Its format should be | ||
* "organizations/[organization_id]". | ||
*/ | ||
const parent = client.organizationPath(organizationId); | ||
|
||
// Build the source object. | ||
const source = { | ||
displayName: 'Customized Display Name V2', | ||
description: 'A new custom source that does X', | ||
}; | ||
|
||
// Build the create source request. | ||
const createSourceRequest = { | ||
parent, | ||
source, | ||
}; | ||
|
||
// The source is not visible in the Security Command Center dashboard | ||
// until it generates findings. | ||
async function createSource() { | ||
|
||
// Call the API | ||
const [source] = await client.createSource(createSourceRequest); | ||
console.log('New Source created: %j', source); | ||
} | ||
|
||
createSource(); | ||
// [END securitycenter_create_source] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Demonstrates how to create a security source. | ||
*/ | ||
function main(organizationId, location = 'global') { | ||
// [START securitycenter_list_all_findings_v2] | ||
// Imports the Google Cloud client library. | ||
const { SecurityCenterClient } = require('@google-cloud/security-center').v2; | ||
|
||
// Creates a new client. | ||
const client = new SecurityCenterClient(); | ||
/** | ||
* Required. Name of the source the findings belong to. If no location is | ||
* specified, the default is global. The following list shows some examples: | ||
* `organizations/[organization_id]/sources/[source_id]/locations/[location_id]` | ||
* `folders/[folder_id]/sources/[source_id]` | ||
* `folders/[folder_id]/sources/[source_id]/locations/[location_id]` | ||
* `projects/[project_id]/sources/[source_id]` | ||
* `projects/[project_id]/sources/[source_id]/locations/[location_id]` | ||
*/ | ||
const parent = `organizations/${organizationId}/sources/-/locations/${location}`; | ||
|
||
// Build the list findings request. | ||
const listFindingsRequest = { | ||
parent, | ||
}; | ||
|
||
async function listAllFindings() { | ||
|
||
// Call the API. | ||
const iterable = client.listFindingsAsync(listFindingsRequest); | ||
let count = 0; | ||
|
||
for await (const response of iterable) { | ||
console.log( | ||
`${++count} ${response.finding.name} ${response.finding.resourceName}` | ||
); | ||
} | ||
} | ||
|
||
listAllFindings(); | ||
// [END securitycenter_list_all_findings_v2] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
Oops, something went wrong.