Skip to content

Commit 665fbc6

Browse files
authored
Merge branch 'main' into batch-create-custom-network
2 parents f39b003 + f76c2fb commit 665fbc6

24 files changed

+1907
-29
lines changed

.github/workflows/security-center-snippets.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
- run: npm test
8383
env:
8484
GCLOUD_ORGANIZATION: 1081635000895
85+
GOOGLE_SAMPLES_PROJECT: "long-door-651"
8586
- name: upload test results for FlakyBot workflow
8687
if: github.event.action == 'schedule' && always()
8788
uses: actions/upload-artifact@v3

.github/workflows/utils/workflows.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,5 @@
9494
"video-intelligence",
9595
"vision/productSearch",
9696
"workflows",
97-
"workflows/invoke-private-endpoint",
98-
"security-center/snippets/v2"
97+
"workflows/invoke-private-endpoint"
9998
]

security-center/snippets/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
"node": ">=16.0.0"
1010
},
1111
"scripts": {
12-
"test": "c8 mocha -p -j 2 system-test/ --recursive --timeout 6000000"
12+
"test": "c8 mocha -p -j 2 --recursive --timeout 6000000 system-test/v2/findings.test.js"
1313
},
1414
"license": "Apache-2.0",
1515
"dependencies": {
1616
"@google-cloud/pubsub": "^4.0.0",
17-
"@google-cloud/security-center": "^8.0.0"
17+
"@google-cloud/security-center": "^8.7.0"
1818
},
1919
"devDependencies": {
2020
"c8": "^10.0.0",
21-
"chai": "^4.5.0",
22-
"mocha": "^10.0.0",
21+
"chai": "^4.2.0",
22+
"mocha": "^10.4.0",
2323
"uuid": "^10.0.0"
2424
}
2525
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2024 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+
const spawnSync = require('node:child_process').spawnSync;
16+
17+
// TODO: replace chai with standard assert/strict
18+
const {assert} = require('chai');
19+
const {describe, it} = require('mocha');
20+
21+
// Can't use arrow function to access this context in describe hook, needed
22+
// to pass the configured timeout to the hook's process runner (assertExec).
23+
// eslint-disable-next-line prefer-arrow-callback
24+
describe('Client v2 with sources and findings', async function () {
25+
// Spawn child process and assert that it exits normally.
26+
const assertChild = (...args) => {
27+
const result = spawnSync('node', args, {timeout: this.timeout()});
28+
const output = result.stdout.toString();
29+
console.log(output);
30+
console.error(result.stderr.toString());
31+
assert(
32+
result.status === 0,
33+
`process exited with non-zero status: ${result.status}`
34+
);
35+
return output;
36+
};
37+
38+
// Register test for the child process.
39+
const testChild = (msg, ...args) => {
40+
it(msg, () => {
41+
assertChild(...args);
42+
});
43+
};
44+
45+
testChild('can create source', 'v2/createSource.js');
46+
testChild('can create a finding', 'v2/createFinding.js');
47+
testChild('can list all findings', 'v2/listAllFindings.js');
48+
testChild('can list only some findings', 'v2/listFilteredFindings.js');
49+
testChild('can mute a finding', 'v2/setMuteFinding.js');
50+
testChild('can unmute a finding', 'v2/setUnmuteFinding.js');
51+
testChild('can group all findings', 'v2/groupFindings.js');
52+
testChild('can group filtered findings', 'v2/groupFindingsWithFilter.js');
53+
testChild('can bulk mute findings', 'v2/bulkMuteFindings.js');
54+
testChild('can get IAM policy', 'v2/getIamPolicy.js');
55+
testChild('can IAM policies', 'v2/testIam.js');
56+
testChild('can set access control policy on a source', 'v2/setIamPolicy.js');
57+
testChild('client can update a finding state', 'v2/setFindingState.js');
58+
testChild(
59+
'can create or update a finding source',
60+
'v2/updateFindingSource.js'
61+
);
62+
testChild(
63+
'can list all security sources in an organization',
64+
'v2/listAllSources.js'
65+
);
66+
testChild('client can create or update a source', 'v2/updateSource.js');
67+
testChild('client can get a specific source', 'v2/getSource.js');
68+
});

security-center/snippets/system-test/v2/notifications.test.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/*
2-
* Copyright 2024 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-
*/
1+
// Copyright 2024 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+
1615
'use strict';
1716

1817
const {execSync} = require('node:child_process');

security-center/snippets/v1/createFinding.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright 2019, Google, LLC.
2+
* Copyright 2019 Google LLC
3+
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
56
* You may obtain a copy of the License at
67
*
7-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
89
*
910
* Unless required by applicable law or agreed to in writing, software
1011
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,7 +18,7 @@
1718
/**
1819
* Demonstrates how to create a new security finding in CSCC.
1920
*/
20-
function main(sourceName = 'FULL_PATH_TO_SOURCE_FOR_FINDING') {
21+
async function main(sourceName = 'FULL_PATH_TO_SOURCE_FOR_FINDING') {
2122
// [START securitycenter_create_finding]
2223
// Imports the Google Cloud client library.
2324
const {SecurityCenterClient} = require('@google-cloud/security-center');
@@ -54,8 +55,11 @@ function main(sourceName = 'FULL_PATH_TO_SOURCE_FOR_FINDING') {
5455
});
5556
console.log('New finding created: %j', newFinding);
5657
}
57-
createFinding();
58+
await createFinding();
5859
// [END securitycenter_create_finding]
5960
}
6061

61-
main(...process.argv.slice(2));
62+
main(process.argv.slice(2)[0]).catch(err => {
63+
console.log(err);
64+
process.exitCode = 1;
65+
});

security-center/snippets/v1/listAllFindings.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'use strict';
1515

1616
/** Prints all findings across all sources. */
17-
function main(organizationId = 'YOUR_NUMERIC_ORG_ID') {
17+
async function main(organizationId = 'YOUR_NUMERIC_ORG_ID') {
1818
// [START securitycenter_list_all_findings]
1919
// Imports the Google Cloud client library.
2020
const {SecurityCenterClient} = require('@google-cloud/security-center');
@@ -43,8 +43,11 @@ function main(organizationId = 'YOUR_NUMERIC_ORG_ID') {
4343
)
4444
);
4545
}
46-
listAllFindings();
46+
await listAllFindings();
4747
// [END securitycenter_list_all_findings]
4848
}
4949

50-
main(...process.argv.slice(2));
50+
main(process.argv.slice(2)[0]).catch(err => {
51+
console.error(err);
52+
process.exitCode = 1;
53+
});
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2024 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+
// Kicks off a long-running operation (LRO) to bulk mute findings for a parent based on a filter.
18+
// The parent can be either an organization, folder, or project. The findings
19+
// matched by the filter will be muted after the LRO is done.
20+
async function main() {
21+
// [START securitycenter_bulk_mute_v2]
22+
// Imports the Google Cloud client library.
23+
const {SecurityCenterClient} = require('@google-cloud/security-center').v2;
24+
25+
// Create a Security Center client
26+
const client = new SecurityCenterClient();
27+
28+
// TODO(developer): Update the following for your own environment.
29+
const organizationId = '1081635000895';
30+
const projectId = 'long-door-651';
31+
const location = 'global';
32+
33+
async function createSampleFinding() {
34+
const uuid = require('uuid');
35+
36+
const [source] = await client.createSource({
37+
source: {
38+
displayName: 'Customized Display Name V2',
39+
description: 'A new custom source that does X',
40+
},
41+
parent: client.organizationPath(organizationId),
42+
});
43+
44+
const sourceId = source.name.split('/')[3];
45+
46+
// Resource name of the new finding's parent. Examples:
47+
// - `organizations/[organization_id]/sources/[source_id]`
48+
// - `organizations/[organization_id]/sources/[source_id]/locations/[location_id]`
49+
const parent = `organizations/${organizationId}/sources/${sourceId}/locations/${location}`;
50+
51+
// The resource this finding applied to. The Cloud Security Command Center UI can link the
52+
// findings for a resource to the corresponding asset of a resource if there are matches.
53+
const resourceName = `//cloudresourcemanager.googleapis.com/organizations/${organizationId}`;
54+
55+
// Unique identifier provided by the client within the parent scope.
56+
// It must be alphanumeric and less than or equal to 32 characters and
57+
// greater than 0 characters in length.
58+
const findingId = uuid.v4().replace(/-/g, '');
59+
60+
// Get the current timestamp.
61+
const eventDate = new Date();
62+
63+
// Finding category.
64+
const category = 'MEDIUM_RISK_ONE';
65+
66+
// Build the finding request object.
67+
const createFindingRequest = {
68+
parent: parent,
69+
findingId: findingId,
70+
finding: {
71+
resourceName,
72+
category,
73+
state: 'ACTIVE',
74+
// The time associated with discovering the issue.
75+
eventTime: {
76+
seconds: Math.floor(eventDate.getTime() / 1000),
77+
nanos: (eventDate.getTime() % 1000) * 1e6,
78+
},
79+
},
80+
};
81+
82+
await client.createFinding(createFindingRequest);
83+
}
84+
85+
await createSampleFinding();
86+
87+
// Required. The parent, at which bulk action needs to be applied. If no
88+
// location is specified, findings are updated in global. The following list
89+
// shows some examples:
90+
// - `organizations/[organization_id]`
91+
// - `organizations/[organization_id]/locations/[location_id]`
92+
// - `folders/[folder_id]`
93+
// - `folders/[folder_id]/locations/[location_id]`
94+
// - `projects/[project_id]`
95+
// - `projects/[project_id]/locations/[location_id]`
96+
const parent = `organizations/${organizationId}/locations/${location}`;
97+
98+
// muteRule: Expression that identifies findings that should be muted.
99+
// Can also refer to an organization/ folder.
100+
// eg: "resource.project_display_name=\"PROJECT_ID\""
101+
const filter = `resource.project_display_name="${projectId}"`;
102+
103+
// Build the request.
104+
const bulkMuteFindingRequest = {
105+
parent,
106+
filter,
107+
};
108+
109+
async function callBulkMuteFindings() {
110+
// Call the API.
111+
const [operation] = await client.bulkMuteFindings(bulkMuteFindingRequest);
112+
const [response] = await operation.promise();
113+
console.log('Bulk mute findings completed successfully: %j', response);
114+
}
115+
116+
await callBulkMuteFindings();
117+
// [END securitycenter_bulk_mute_v2]
118+
}
119+
120+
main().catch(err => {
121+
console.error(err);
122+
process.exitCode = 1;
123+
});

0 commit comments

Comments
 (0)