1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616
17- from typing import Dict
18- import time
1917import random
18+ import time
19+ from typing import Dict
20+
2021from google .cloud import securitycentermanagement_v1
22+ from google .api_core .exceptions import NotFound
23+
2124
2225# [START securitycenter_create_security_health_analytics_custom_module]
2326def create_security_health_analytics_custom_module (parent : str ) -> Dict :
2427 """
2528 Creates a Security Health Analytics custom module.
29+
30+ This custom module evaluates Cloud KMS CryptoKeys to ensure their rotation period exceeds 30 days (2592000 seconds),
31+ as per security best practices. A shorter rotation period helps reduce the risk of exposure in the event of a compromise.
32+
2633 Args:
2734 parent: Use any one of the following options:
2835 - organizations/{organization_id}/locations/{location_id}
@@ -42,24 +49,33 @@ def create_security_health_analytics_custom_module(parent: str) -> Dict:
4249 "display_name" : display_name ,
4350 "enablement_state" : "ENABLED" ,
4451 "custom_config" : {
45- "description" : "Sample custom module for testing purpose. Please do not delete." ,
52+ "description" : (
53+ "Sample custom module for testing purposes. This custom module evaluates "
54+ "Cloud KMS CryptoKeys to ensure their rotation period exceeds 30 days (2592000 seconds)."
55+ ),
4656 "predicate" : {
4757 "expression" : "has(resource.rotationPeriod) && (resource.rotationPeriod > duration('2592000s'))" ,
48- "title" : "GCE Instance High Severity" ,
49- "description" : "Custom module to detect high severity issues on GCE instances." ,
58+ "title" : "Cloud KMS CryptoKey Rotation Period" ,
59+ "description" : (
60+ "Evaluates whether the rotation period of a Cloud KMS CryptoKey exceeds 30 days. "
61+ "A longer rotation period might increase the risk of exposure."
62+ ),
5063 },
51- "recommendation" : "Ensure proper security configurations on GCE instances." ,
64+ "recommendation" : (
65+ "Review and adjust the rotation period for Cloud KMS CryptoKeys to align with your security policies. "
66+ "Consider setting a shorter rotation period if possible."
67+ ),
5268 "resource_selector" : {"resource_types" : ["cloudkms.googleapis.com/CryptoKey" ]},
5369 "severity" : "CRITICAL" ,
5470 "custom_output" : {
5571 "properties" : [
5672 {
5773 "name" : "example_property" ,
5874 "value_expression" : {
59- "description" : "The name of the instance " ,
75+ "description" : "The resource name of the CryptoKey being evaluated. " ,
6076 "expression" : "resource.name" ,
6177 "location" : "global" ,
62- "title" : "Instance Name" ,
78+ "title" : "CryptoKey Resource Name" ,
6379 },
6480 }
6581 ]
@@ -68,15 +84,16 @@ def create_security_health_analytics_custom_module(parent: str) -> Dict:
6884 }
6985
7086 request = securitycentermanagement_v1 .CreateSecurityHealthAnalyticsCustomModuleRequest (
71- parent = parent ,
72- security_health_analytics_custom_module = custom_module ,
87+ parent = parent ,
88+ security_health_analytics_custom_module = custom_module ,
7389 )
7490
7591 response = client .create_security_health_analytics_custom_module (request = request )
7692 print (f"Created Security Health Analytics Custom Module: { response .name } " )
7793 return response
7894# [END securitycenter_create_security_health_analytics_custom_module]
7995
96+
8097# [START securitycenter_get_security_health_analytics_custom_module]
8198def get_security_health_analytics_custom_module (parent : str , module_id : str ):
8299 """
@@ -95,7 +112,7 @@ def get_security_health_analytics_custom_module(parent: str, module_id: str):
95112
96113 try :
97114 request = securitycentermanagement_v1 .GetSecurityHealthAnalyticsCustomModuleRequest (
98- name = f"{ parent } /securityHealthAnalyticsCustomModules/{ module_id } " ,
115+ name = f"{ parent } /securityHealthAnalyticsCustomModules/{ module_id } " ,
99116 )
100117
101118 response = client .get_security_health_analytics_custom_module (request = request )
@@ -107,7 +124,6 @@ def get_security_health_analytics_custom_module(parent: str, module_id: str):
107124# [END securitycenter_get_security_health_analytics_custom_module]
108125
109126
110-
111127# [START securitycenter_list_security_health_analytics_custom_module]
112128def list_security_health_analytics_custom_module (parent : str ):
113129 """
@@ -122,22 +138,20 @@ def list_security_health_analytics_custom_module(parent: str):
122138 Raises:
123139 NotFound: If the specified custom module does not exist.
124140 """
125- from google .api_core .exceptions import NotFound
126141
127142 client = securitycentermanagement_v1 .SecurityCenterManagementClient ()
128143
129144 try :
130145 request = securitycentermanagement_v1 .ListSecurityHealthAnalyticsCustomModulesRequest (
131- parent = parent ,
146+ parent = parent ,
132147 )
133148
134149 response = client .list_security_health_analytics_custom_modules (request = request )
135150
136151 custom_modules = []
137152 for custom_module in response :
138153 print (f"Custom Module: { custom_module .name } " )
139- custom_modules .append (custom_module )
140-
154+ custom_modules .append (custom_module )
141155 return custom_modules
142156 except NotFound as e :
143157 print (f"Parent resource not found: { parent } " )
@@ -147,6 +161,7 @@ def list_security_health_analytics_custom_module(parent: str):
147161 raise e
148162# [END securitycenter_list_security_health_analytics_custom_module]
149163
164+
150165# [START securitycenter_delete_security_health_analytics_custom_module]
151166def delete_security_health_analytics_custom_module (parent : str , module_id : str ):
152167 """
@@ -165,7 +180,7 @@ def delete_security_health_analytics_custom_module(parent: str, module_id: str):
165180
166181 try :
167182 request = securitycentermanagement_v1 .DeleteSecurityHealthAnalyticsCustomModuleRequest (
168- name = f"{ parent } /securityHealthAnalyticsCustomModules/{ module_id } " ,
183+ name = f"{ parent } /securityHealthAnalyticsCustomModules/{ module_id } " ,
169184 )
170185
171186 client .delete_security_health_analytics_custom_module (request = request )
@@ -175,6 +190,7 @@ def delete_security_health_analytics_custom_module(parent: str, module_id: str):
175190 raise e
176191# [END securitycenter_delete_security_health_analytics_custom_module]
177192
193+
178194# [START securitycenter_update_security_health_analytics_custom_module]
179195def update_security_health_analytics_custom_module (parent : str , module_id : str ):
180196 """
@@ -190,7 +206,6 @@ def update_security_health_analytics_custom_module(parent: str, module_id: str):
190206 NotFound: If the specified custom module does not exist.
191207 """
192208 from google .protobuf .field_mask_pb2 import FieldMask
193- from google .api_core .exceptions import NotFound , InternalServerError , ServiceUnavailable
194209
195210 client = securitycentermanagement_v1 .SecurityCenterManagementClient ()
196211 try :
0 commit comments