|
| 1 | +// Copyright 2019 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 {SecurityCenterClient} = require('@google-cloud/security-center'); |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {describe, it, before} = require('mocha'); |
| 20 | +const {execSync} = require('child_process'); |
| 21 | +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); |
| 22 | + |
| 23 | +const organizationId = process.env['GCLOUD_ORGANIZATION']; |
| 24 | + |
| 25 | +describe('Client with SourcesAndFindings', async () => { |
| 26 | + let data; |
| 27 | + before(async () => { |
| 28 | + // Creates a new client. |
| 29 | + const client = new SecurityCenterClient(); |
| 30 | + const [source] = await client |
| 31 | + .createSource({ |
| 32 | + source: { |
| 33 | + displayName: 'Customized Display Name', |
| 34 | + description: 'A new custom source that does X', |
| 35 | + }, |
| 36 | + parent: client.organizationPath(organizationId), |
| 37 | + }) |
| 38 | + .catch(error => console.error(error)); |
| 39 | + const eventTime = new Date(); |
| 40 | + const createFindingTemplate = { |
| 41 | + parent: source.name, |
| 42 | + findingId: 'somefinding', |
| 43 | + finding: { |
| 44 | + state: 'ACTIVE', |
| 45 | + // Resource the finding is associated with. This is an |
| 46 | + // example any resource identifier can be used. |
| 47 | + resourceName: |
| 48 | + '//cloudresourcemanager.googleapis.com/organizations/11232', |
| 49 | + // A free-form category. |
| 50 | + category: 'MEDIUM_RISK_ONE', |
| 51 | + // The time associated with discovering the issue. |
| 52 | + eventTime: { |
| 53 | + seconds: Math.floor(eventTime.getTime() / 1000), |
| 54 | + nanos: (eventTime.getTime() % 1000) * 1e6, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + const [finding] = await client.createFinding(createFindingTemplate); |
| 59 | + createFindingTemplate.findingId = 'untouchedFindingId'; |
| 60 | + createFindingTemplate.finding.category = 'XSS'; |
| 61 | + const [untouchedFinding] = await client |
| 62 | + .createFinding(createFindingTemplate) |
| 63 | + .catch(error => console.error(error)); |
| 64 | + data = { |
| 65 | + orgId: organizationId, |
| 66 | + sourceName: source.name, |
| 67 | + findingName: finding.name, |
| 68 | + untouchedFindingName: untouchedFinding.name, |
| 69 | + }; |
| 70 | + console.log('my data %j', data); |
| 71 | + }); |
| 72 | + |
| 73 | + it('client can create source', () => { |
| 74 | + const output = exec(`node v1/createSource.js ${data.orgId}`); |
| 75 | + assert.match(output, new RegExp(data.orgId)); |
| 76 | + assert.match(output, /New Source/); |
| 77 | + assert.notMatch(output, /undefined/); |
| 78 | + }); |
| 79 | + |
| 80 | + it('client can get source', () => { |
| 81 | + const output = exec(`node v1/getSource.js ${data.sourceName}`); |
| 82 | + assert.match(output, new RegExp(data.sourceName)); |
| 83 | + assert.match(output, /Source/); |
| 84 | + assert.match(output, /"description":"A new custom source that does X"/); |
| 85 | + assert.notMatch(output, /undefined/); |
| 86 | + }); |
| 87 | + |
| 88 | + it('client can list all sources', () => { |
| 89 | + const output = exec(`node v1/listAllSources.js ${data.orgId}`); |
| 90 | + assert.match(output, new RegExp(data.sourceName)); |
| 91 | + assert.match(output, /Sources/); |
| 92 | + assert.notMatch(output, /undefined/); |
| 93 | + }); |
| 94 | + |
| 95 | + it('client can update a source', () => { |
| 96 | + const output = exec(`node v1/updateSource.js ${data.sourceName}`); |
| 97 | + assert.match(output, new RegExp(data.sourceName)); |
| 98 | + assert.match(output, /New Display Name/); |
| 99 | + assert.match(output, /source that does X/); |
| 100 | + assert.notMatch(output, /undefined/); |
| 101 | + }); |
| 102 | + |
| 103 | + it('client can create a finding', () => { |
| 104 | + const output = exec(`node v1/createFinding.js ${data.sourceName}`); |
| 105 | + assert.match(output, new RegExp(data.sourceName)); |
| 106 | + assert.match(output, /New finding created/); |
| 107 | + assert.notMatch(output, /undefined/); |
| 108 | + }); |
| 109 | + |
| 110 | + it('client can create a finding with source properties', () => { |
| 111 | + const output = exec( |
| 112 | + `node v1/createFindingSourceProperties.js ${data.sourceName}` |
| 113 | + ); |
| 114 | + assert.match(output, new RegExp(data.sourceName)); |
| 115 | + assert.match(output, /New finding created/); |
| 116 | + assert.match(output, /n_value/); |
| 117 | + assert.notMatch(output, /undefined/); |
| 118 | + }); |
| 119 | + |
| 120 | + it('client can update a findings source properties', () => { |
| 121 | + const output = exec( |
| 122 | + `node v1/updateFindingSourceProperties.js ${data.findingName}` |
| 123 | + ); |
| 124 | + assert.match(output, new RegExp(data.findingName)); |
| 125 | + assert.match(output, /Updated Finding/); |
| 126 | + assert.match(output, /new_string_example/); |
| 127 | + assert.notMatch(output, /undefined/); |
| 128 | + }); |
| 129 | + |
| 130 | + it('client can set finding state', () => { |
| 131 | + const output = exec(`node v1/setFindingState.js ${data.findingName}`); |
| 132 | + assert.match(output, new RegExp(data.findingName)); |
| 133 | + assert.match(output, /INACTIVE/); |
| 134 | + assert.notMatch(output, /undefined/); |
| 135 | + }); |
| 136 | + |
| 137 | + it('client can test IAM privileges', () => { |
| 138 | + const output = exec(`node v1/testIam.js ${data.sourceName}`); |
| 139 | + assert.equal( |
| 140 | + (output.match(/true/g) || []).length, |
| 141 | + 2, |
| 142 | + `${output} contains true twice` |
| 143 | + ); |
| 144 | + assert.notMatch(output, /undefined/); |
| 145 | + }); |
| 146 | + |
| 147 | + it('client can list all findings', () => { |
| 148 | + const output = exec(`node v1/listAllFindings.js ${data.orgId}`); |
| 149 | + assert.match(output, new RegExp(data.findingName)); |
| 150 | + assert.match(output, new RegExp(data.untouchedFindingName)); |
| 151 | + assert.notMatch(output, /undefined/); |
| 152 | + }); |
| 153 | + |
| 154 | + it('client can list only some findings', () => { |
| 155 | + const output = exec(`node v1/listFilteredFindings.js ${data.sourceName}`); |
| 156 | + assert.match(output, new RegExp(data.findingName)); |
| 157 | + assert.notMatch(output, new RegExp(data.untouchedFindingName)); |
| 158 | + assert.notMatch(output, /undefined/); |
| 159 | + }); |
| 160 | + |
| 161 | + it('client can list findings at a time.', () => { |
| 162 | + const output = exec(`node v1/listFindingsAtTime.js ${data.sourceName}`); |
| 163 | + // Nothing was created for the source more then a few minutes ago, so |
| 164 | + // days ago should return nothing. |
| 165 | + assert.equal(output, ''); |
| 166 | + }); |
| 167 | + |
| 168 | + it('client can add security marks to finding', () => { |
| 169 | + const output = exec( |
| 170 | + `node v1/addFindingSecurityMarks.js ${data.findingName}` |
| 171 | + ); |
| 172 | + assert.match(output, new RegExp(data.findingName)); |
| 173 | + assert.match(output, /key_a/); |
| 174 | + assert.match(output, /value_a/); |
| 175 | + assert.match(output, /key_b/); |
| 176 | + assert.match(output, /value_b/); |
| 177 | + assert.notMatch(output, /undefined/); |
| 178 | + }); |
| 179 | + |
| 180 | + it('client can list findings withe security marks', () => { |
| 181 | + // Ensure marks are set. |
| 182 | + exec(`node v1/addFindingSecurityMarks.js ${data.findingName}`); |
| 183 | + const output = exec( |
| 184 | + `node v1/listFindingsWithSecurityMarks.js ${data.sourceName}` |
| 185 | + ); |
| 186 | + assert.notMatch(output, new RegExp(data.findingName)); |
| 187 | + assert.match(output, new RegExp(data.untouchedFindingName)); |
| 188 | + assert.notMatch(output, /undefined/); |
| 189 | + }); |
| 190 | + |
| 191 | + it('client can get a sources policy', () => { |
| 192 | + const output = exec(`node v1/getSourceIam.js ${data.sourceName}`); |
| 193 | + assert.match(output, /Current policy/); |
| 194 | + assert.notMatch(output, /undefined/); |
| 195 | + }); |
| 196 | + |
| 197 | + it('client set a sources policy', () => { |
| 198 | + |
| 199 | + const output = exec(`node v1/setSourceIam.js ${data.sourceName} ${user}`); |
| 200 | + assert.match(output, /Updated policy/); |
| 201 | + assert.include(output, user); |
| 202 | + assert.notMatch(output, /undefined/); |
| 203 | + }); |
| 204 | +}); |
0 commit comments