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
+ */
16
+
17
+ const { SecurityCenterClient } = require ( '@google-cloud/security-center' ) . v2 ;
18
+ const { assert } = require ( 'chai' ) ;
19
+ const { execSync } = require ( 'child_process' ) ;
20
+ const exec = cmd => execSync ( cmd , { encoding : 'utf8' } ) ;
21
+ const { describe, it, before } = require ( 'mocha' ) ;
22
+ const uuidv1 = require ( 'uuid' ) . v1 ;
23
+
24
+ const organizationId = process . env [ 'GCLOUD_ORGANIZATION' ] ;
25
+ const location = 'global' ;
26
+
27
+ describe ( 'Client with mute rule V2' , async ( ) => {
28
+ let data ;
29
+ before ( async ( ) => {
30
+ // Creates a new client.
31
+ const client = new SecurityCenterClient ( ) ;
32
+
33
+ // Build the create mute rule request.
34
+ const muteId = 'muteid-' + uuidv1 ( ) . replace ( / - / g, '' ) . substring ( 0 , 20 ) ;
35
+ const createMuteRuleRequest = {
36
+ parent :`organizations/${ organizationId } /locations/${ location } ` ,
37
+ muteConfigId :muteId ,
38
+ muteConfig :{
39
+ name :`organizations/${ organizationId } /locations/${ location } /muteConfigs/${ muteId } ` ,
40
+ description : "Mute low-medium IAM grants excluding 'compute' resources" ,
41
+ filter :
42
+ "severity=\"LOW\" OR severity=\"MEDIUM\" AND " +
43
+ "category=\"Persistence: IAM Anomalous Grant\" AND " +
44
+ "-resource.type:\"compute\"" ,
45
+ type : "STATIC" ,
46
+ } ,
47
+ } ;
48
+
49
+ const [ muteConfigResponse ] = await client
50
+ . createMuteConfig ( createMuteRuleRequest )
51
+ . catch ( error => console . error ( error ) ) ;
52
+
53
+ const muteConfigId = muteConfigResponse . name . split ( '/' ) [ 5 ] ;
54
+
55
+ data = {
56
+ orgId : organizationId ,
57
+ muteConfigId : muteConfigId ,
58
+ muteConfigName : muteConfigResponse . name ,
59
+ untouchedMuteConfigName : "" ,
60
+ } ;
61
+ console . log ( 'my data %j' , data ) ;
62
+ } ) ;
63
+
64
+ after ( async ( ) => {
65
+ const client = new SecurityCenterClient ( ) ;
66
+
67
+ const name = `organizations/${ organizationId } /locations/${ location } /muteConfigs/${ data . muteConfigId } ` ;
68
+ await client . deleteMuteConfig ( { name : name } ) . catch ( error => console . error ( error ) ) ;
69
+ } ) ;
70
+
71
+ it ( 'client can create mute rule V2' , ( ) => {
72
+ const output = exec ( `node v2/createMuteRule.js ${ data . orgId } ` ) ;
73
+ assert . match ( output , new RegExp ( data . orgId ) ) ;
74
+ assert . match ( output , / N e w m u t e r u l e c o n f i g c r e a t e d / ) ;
75
+ assert . notMatch ( output , / u n d e f i n e d / ) ;
76
+ } ) ;
77
+
78
+ it ( 'client can list all mute rules V2' , ( ) => {
79
+ const output = exec ( `node v2/listAllMuteRules.js ${ data . orgId } ` ) ;
80
+ assert . match ( output , new RegExp ( data . orgId ) ) ;
81
+ assert . match ( output , new RegExp ( data . untouchedMuteConfigName ) ) ;
82
+ assert . notMatch ( output , / u n d e f i n e d / ) ;
83
+ } ) ;
84
+
85
+ it ( 'client can get a mute rule V2' , ( ) => {
86
+ const output = exec ( `node v2/getMuteRule.js ${ data . orgId } ${ data . muteConfigId } ` ) ;
87
+ assert . match ( output , new RegExp ( data . muteConfigName ) ) ;
88
+ assert . match ( output , / G e t m u t e r u l e c o n f i g / ) ;
89
+ assert . notMatch ( output , / u n d e f i n e d / ) ;
90
+ } ) ;
91
+
92
+ it ( 'client can update a mute rule V2' , ( ) => {
93
+ const output = exec ( `node v2/updateMuteRule.js ${ data . orgId } ${ data . muteConfigId } ` ) ;
94
+ assert . match ( output , / U p d a t e m u t e r u l e c o n f i g / ) ;
95
+ assert . notMatch ( output , / u n d e f i n e d / ) ;
96
+ } ) ;
97
+
98
+ it ( 'client can delete a mute rule V2' , ( ) => {
99
+ const output = exec ( `node v2/deleteMuteRule.js ${ data . orgId } ${ data . muteConfigId } ` ) ;
100
+ assert . match ( output , / D e l e t e m u t e r u l e c o n f i g / ) ;
101
+ assert . notMatch ( output , / u n d e f i n e d / ) ;
102
+ } ) ;
103
+
104
+ } ) ;
0 commit comments