
    $                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr   " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      rS rS rS r S r!S r"S r#S r$S r%S r&S r'S  r(S! r)S" r*S# r+S$ r,S% r-g)&z2Declarative Hooks for Cloud SCC surface arguments.    )absolute_import)division)print_function)unicode_literalsN)encoding)securitycenter_client)InvalidSCCInputError)	yaml_data)resource_args)concept_parsers)
exceptions)
properties)yamlc                       \ rS rSrSrSrg)InvalidCustomConfigFileError$   z6Error if a custom config file is improperly formatted. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       +lib/googlecloudsdk/command_lib/scc/hooks.pyr   r   $   s    >r   r   c                       \ rS rSrSrSrg)InvalidTestDataFileError(   z2Error if a test data file is improperly formatted.r   Nr   r   r   r   r   r   (   s    :r   r   c                       \ rS rSrSrSrg)InvalidResourceFileError,   z1Error if a resource file is improperly formatted.r   Nr   r   r   r   r!   r!   ,   s    9r   r!   c            	          [         R                  R                  S5      n [        R                  " SSSSSU R                  5       S9/n[        R                  " U/ 5      /$ )z*Add Organization as a positional resource.zscc.organizationz8to be used for the SCC (Security Command Center) commandorganizationTF)verbnamerequiredprefixes
positionalresource_datar
   ResourceYAMLDataFromPathr   GetResourcePresentationSpecGetDatar   ConceptParser)org_spec_data	arg_specss     r   AppendOrgArgr3   0   sa    ,,556HI-//I%--/1) 
'
'	2
6	77r   c            
          [         R                  R                  S5      n [        R                  " SSSSSSU R                  5       S9/n[        R                  " U/ 5      /$ )z$Add Parent as a positional resource.z
scc.parentz'to be used for the `gcloud scc` commandparentz{name} organization, folder, or project in the Google Cloud resource hierarchy {verb}. Specify the argument as either [RESOURCE_TYPE/RESOURCE_ID] or [RESOURCE_ID], as shown in the preceding examples.TF)r%   r&   	help_textr'   r(   r)   r*   r+   )parent_spec_datar2   s     r   AppendParentArgr8   ?   sg    //88F//8 ^(0024	) 
'
'	2
6	77r   c                     [         R                  " 5       n[        R                  " U UR                  R
                  5      $ )zAHook to capture "key1=val1,key2=val2" as SourceProperties object.)	sc_clientGetMessagesr   DictToMessageFindingSourcePropertiesValue)source_properties_dictmessagess     r   SourcePropertiesHookrA   O   s7    ""$(			 6 ( 0 0 F F
H Hr   c                     [         R                  " 5       nUR                  5       n[        R                  " XR                  R
                  5      Ul        U$ )z>Hook to capture "key1=val1,key2=val2" as SecurityMarks object.)r:   r;   SecurityMarksr   r<   
MarksValuemarks)parsed_dictr@   security_markss      r   SecurityMarksHookrH   V   sF    ""$())+.!//))446.	r   c                    [         R                  " S5      n[         R                  " S5      nU R                  (       d3  [        R                  R
                  R                  R                  5       nOU R                  nUc  [        S5      eUR                  U5      (       d!  UR                  U5      (       d  [        S5      eUR                  U5      (       a  U$ SU-   $ )+Prepend organizations/ to org if necessary.organizations/[0-9]+[0-9]+zOCould not find Organization argument. Please provide the organization argument.>Organization must match either organizations/[0-9]+ or [0-9]+.organizations/)	recompiler$   r   VALUESsccGetr	   match)argsresource_pattern
id_patternr$   s       r   GetOrganizationrX   _   s    ZZ 67zz(#*			$$((5599;L$$L
  D E E

 
 
.
.


<
(
(
HJ JL))	L	((r   c                  f   [         R                  " S5      n [         R                  " S5      n[        R                  R                  R
                  R                  5       nU R                  U5      (       d!  UR                  U5      (       d  [        S5      eU R                  U5      (       a  U$ SU-   $ )rJ   rK   rL   rM   rN   )	rO   rP   r   rQ   rR   r$   rS   rT   r	   )rV   rW   r$   s      r   GetDefaultOrganizationrZ   s   s    ZZ 67zz(#*""&&33779,

 
 
.
.


<
(
(
HJ JL))	L	((r   c                     [         R                  " S5      n [         R                  " S5      n[        R                  R                  R
                  R                  5       nUR                  U5      (       a  SU-   nU R                  U5      (       d7  UR                  S5      (       d!  UR                  S5      (       d  [        S5      eU$ )z@Converts user input to one of: organization, project, or folder.zorganizations/[0-9]+$rL   rN   z	projects/zfolders/zWParent must match either [0-9]+, organizations/[0-9]+, projects/.*
      or folders/.*.)
rO   rP   r   rQ   rR   r5   rS   rT   
startswithr	   )organization_resource_patternrW   r5   s      r   GetDefaultParentr^      s    "$**-D"Ezz(#*  ''++-&f&F
'
-
-f
5
5


K
(
(F,=,=j,I,I
	  
-r   c                 &    U R                  SS5      $ )z2Removes spaces from a field mask provided by user.  )replace)masks    r   CleanUpUserInputrd      s    	c2	r   c                     [         R                  " S5      nUR                  U 5      (       d  [        S5      eU R	                  S5      nUS   S-   US   -   $ )Nz#(organizations|projects|folders)/.*zrWhen providing a full resource path, it must also include the pattern the organization, project, or folder prefix./r      )rO   rP   rT   r	   split)resource_namerV   list_organization_componentss      r   GetParentFromResourceNamerk      sc    ZZ EF				.	.
	78 8 "/!4!4S!9	%a	(3	.1Ma1P	PPr   c                     [         R                  " S5      nUR                  U 5      (       d  [        S5      eU R	                  S5      n[        U 5      S-   US   -   S-   US   -   $ )Nz2(organizations|projects|folders)/.*/sources/[0-9]+zfWhen providing a full resource path, it must also include the organization, project, or folder prefix.rf         )rO   rP   rT   r	   rh   rk   )ri   rV   list_source_componentss      r   GetSourceParentFromResourceNamerp      s    ZZ:<				.	.
	78 8 )..s3
#M
2S
8
 
#$&)*,B1,EF Gr   c                    [         R                  " 5       n [        R                  " U 5      n[        R
                  " X!R                  R                  5      $ ! [        R                   a  n[        SR                  U5      5      eSnAff = f)z;Processes the configuration file for the ETD custom module.%Error parsing custom config file [{}]N)r:   r;   jsonloadsr   r<    EventThreatDetectionCustomModuleConfigValueJSONDecodeErrorr   format)file_contentsr@   configes       r   ProcessCustomEtdConfigFiler|      sx    ""$(ZZ&F!!99EE  
		 
&/66q9 s   ?A B
+BB
c                    [         R                  " 5       n [        R                  " U 5      n[        R
                  " X!R                  5      $ ! [        R                   a  n[        SR                  U5      5      eSnAff = f)<Process the custom configuration file for the custom module.rr   N)
r:   r;   r   loadr   r<   'GoogleCloudSecuritycenterV1CustomConfigYAMLParseErrorr   rx   )ry   r@   config_dictypes       r   ProcessCustomConfigFiler      sr    ""$(=))M*K!!EE  
		 =
&/66s;= ==   5A B !A;;B c                     [        U [        5      (       a  U $ [        U [        5      (       a  SU ;   a  U S   $ gU (       d  [        S5      eg)zAExtract test data into list structure, accept both list and dict.testDataNz=Error parsing test data file: no data records defined in file)
isinstancelistdictr   )test_data_inputs    r   ExtractTestDatar      sP    &&/4((_$Z(($
I  r   c                 P   [         R                  " 5       n [        [        R                  " U 5      5      n/ nU H2  nUR                  [        R                  " XAR                  5      5        M4     U$ ! [        R                   a  n[        SR                  U5      5      eSnAff = f)zJProcess the test resource data file for the custom module to test against.z!Error parsing test data file [{}]N)r:   r;   r   r   r   appendr   r<   TestDatar   r   rx   )ry   r@   	test_datatest_data_messagesfieldr   s         r   ProcessTestResourceDataFiler      s    ""$(		- 89I

 
 (9(9
: 
 			 
"+2237 s   AA2 2B%B  B%c                    [         R                  " 5       n [        R                  " U 5      n[        R
                  " X!R                  5      $ ! [        R                   a  n[        SR                  U5      5      eSnAff = f)zRProcess the simulate resource data file for the custom module to validate against.z Error parsing resource file [{}]N)
r:   r;   r   r   r   r<   SimulatedResourcer   r!   rx   )ry   r@   resource_dictr   s       r   ProcessSimulatedResourceFiler      sk    ""$(IIm,M!!-1K1KLL			 
"*11#6 r   c                      [         R                  " U 5      n[         R                  " U5      R                  S5      $ ! [         R                   a  n[        SR                  U5      5      eSnAff = f)r~   zutf-8z&Error parsing terraform plan file [{}]N)rs   rt   dumpsencoderw   r   rx   )ry   rz   r{   s      r   ProcessTFPlanFiler      s`    ZZ&F::f$$W--			 
&077: s   := A0A++A0).r   
__future__r   r   r   r   rs   rO   apitools.base.pyr   googlecloudsdk.api_lib.sccr   r:   %googlecloudsdk.command_lib.scc.errorsr	   $googlecloudsdk.command_lib.util.apisr
   $googlecloudsdk.command_lib.util.argsr   (googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   core_exceptionsr   r   Errorr   r   r!   r3   r8   rA   rH   rX   rZ   r^   rd   rk   rp   r|   r   r   r   r   r   r   r   r   <module>r      s    9 &  % '  	 % I F : > D = * $??#8#8 ?;44 ;:44 :88 H)() 
Q	G
= $
r   