
    $                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d dej4                        Z G d dej4                        Z G d dej4                        Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*d# Z+d$ Z,d% Z-y)&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                       e Zd ZdZy)InvalidCustomConfigFileErrorz6Error if a custom config file is improperly formatted.N__name__
__module____qualname____doc__     +lib/googlecloudsdk/command_lib/scc/hooks.pyr   r   $   s    >r   r   c                       e Zd ZdZy)InvalidTestDataFileErrorz2Error if a test data file is improperly formatted.Nr   r   r   r   r   r   (   s    :r   r   c                       e Zd ZdZy)InvalidResourceFileErrorz1Error if a resource file is improperly formatted.Nr   r   r   r   r   r   ,   s    9r   r   c            	          t         j                  j                  d      } t        j                  ddddd| j                               g}t        j                  |g       gS )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   AppendOrgArgr.   0   sa    ,,556HI-//I%--/1) 
'
'	2
6	77r   c            
          t         j                  j                  d      } t        j                  dddddd| j                               g}t        j                  |g       gS )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_datar-   s     r   AppendParentArgr3   ?   sg    //88F//8 ^(0024	) 
'
'	2
6	77r   c                 ~    t        j                         }t        j                  | |j                  j
                        S )zAHook to capture "key1=val1,key2=val2" as SourceProperties object.)	sc_clientGetMessagesr   DictToMessageFindingSourcePropertiesValue)source_properties_dictmessagess     r   SourcePropertiesHookr<   O   s7    ""$(			 6 ( 0 0 F F
H Hr   c                     t        j                         }|j                         }t        j                  | |j                  j
                        |_        |S )z>Hook to capture "key1=val1,key2=val2" as SecurityMarks object.)r5   r6   SecurityMarksr   r7   
MarksValuemarks)parsed_dictr;   security_markss      r   SecurityMarksHookrC   V   sH    ""$())+.!//8))446.	r   c                    t        j                  d      }t        j                  d      }| j                  s3t        j                  j
                  j                  j                         }n| j                  }|t        d      |j                  |      s|j                  |      st        d      |j                  |      r|S d|z   S )+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   GetOrganizationrS   _   s    ZZ 67zz(#*			$$((5599;L$$L
  D E E

 
 
.


<
(
HJ JL)	L	((r   c                  D   t        j                  d      } t        j                  d      }t        j                  j                  j
                  j                         }| j                  |      s|j                  |      st        d      | j                  |      r|S d|z   S )rE   rF   rG   rH   rI   )	rJ   rK   r   rL   rM   r   rN   rO   r	   )rQ   rR   r   s      r   GetDefaultOrganizationrU   s   s    ZZ 67zz(#*""&&33779,

 
 
.


<
(
HJ JL)	L	((r   c                  f   t        j                  d      } t        j                  d      }t        j                  j                  j
                  j                         }|j                  |      rd|z   }| j                  |      s-|j                  d      s|j                  d      st        d      |S )z@Converts user input to one of: organization, project, or folder.zorganizations/[0-9]+$rG   rI   z	projects/zfolders/zWParent must match either [0-9]+, organizations/[0-9]+, projects/.*
      or folders/.*.)
rJ   rK   r   rL   rM   r0   rN   rO   
startswithr	   )organization_resource_patternrR   r0   s      r   GetDefaultParentrY      s    "$**-D"Ezz(#*  ''++-&f&F
'
-
-f
5


K
(F,=,=j,I
	  
-r   c                 &    | j                  dd      S )z2Removes spaces from a field mask provided by user.  )replace)masks    r   CleanUpUserInputr_      s    	c2	r   c                     t        j                  d      }|j                  |       st        d      | j	                  d      }|d   dz   |d   z   S )Nz#(organizations|projects|folders)/.*zrWhen providing a full resource path, it must also include the pattern the organization, project, or folder prefix./r      )rJ   rK   rO   r	   split)resource_namerQ   list_organization_componentss      r   GetParentFromResourceNamerf      s`    ZZ EF				.
	78 8 "/!4!4S!9	%a	(3	.1Ma1P	PPr   c                     t        j                  d      }|j                  |       st        d      | j	                  d      }t        |       dz   |d   z   dz   |d   z   S )Nz2(organizations|projects|folders)/.*/sources/[0-9]+zfWhen providing a full resource path, it must also include the organization, project, or folder prefix.ra         )rJ   rK   rO   r	   rc   rf   )rd   rQ   list_source_componentss      r   GetSourceParentFromResourceNamerk      s}    ZZ:<				.
	78 8 )..s3
#M
2S
8
 
#$&)*,B1,EF Gr   c                    t        j                         }	 t        j                  |       }t	        j
                  ||j                  j                        S # t        j                  $ r}t        dj                  |            d}~ww xY w)z;Processes the configuration file for the ETD custom module.%Error parsing custom config file [{}]N)r5   r6   jsonloadsr   r7    EventThreatDetectionCustomModuleConfigValueJSONDecodeErrorr   format)file_contentsr;   configes       r   ProcessCustomEtdConfigFilerw      sz    ""$(ZZ&F!!99EE  
		 
&/66q9 s   >A B(BBc                     t        j                         }	 t        j                  |       }t	        j
                  ||j                        S # t        j                  $ r}t        dj                  |            d}~ww xY w)<Process the custom configuration file for the custom module.rm   N)
r5   r6   r   loadr   r7   'GoogleCloudSecuritycenterV1CustomConfigYAMLParseErrorr   rs   )rt   r;   config_dictypes       r   ProcessCustomConfigFiler      st    ""$(=))M*K!!XEE  
		 =
&/66s;= ==   4A A=A88A=c                 v    t        | t              r| S t        | t              r
d| v r| d   S y| st        d      y)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      sJ    &/4(_$Z(($
I  r   c                 F   t        j                         }	 t        t        j                  |             }g }|D ]1  }|j                  t        j                  ||j                               3 |S # t        j                  $ r}t        dj                  |            d}~ww xY w)zJProcess the test resource data file for the custom module to test against.z!Error parsing test data file [{}]N)r5   r6   r   r   rz   appendr   r7   TestDatar|   r   rs   )rt   r;   	test_datatest_data_messagesfieldr~   s         r   ProcessTestResourceDataFiler      s    ""$(		- 89I

 
 (9(9
: 
 			 
"+2237 s   AA. .B BB c                     t        j                         }	 t        j                  |       }t	        j
                  ||j                        S # t        j                  $ r}t        dj                  |            d}~ww xY w)zRProcess the simulate resource data file for the custom module to validate against.z Error parsing resource file [{}]N)
r5   r6   r   rz   r   r7   SimulatedResourcer|   r   rs   )rt   r;   resource_dictr~   s       r   ProcessSimulatedResourceFiler      sm    ""$(IIm,M!!-1K1KLL			 
"*11#6 r   c                     	 t        j                  |       }t        j                  |      j                  d      S # t         j                  $ r}t        dj                  |            d}~ww xY w)ry   zutf-8z&Error parsing terraform plan file [{}]N)rn   ro   dumpsencoderr   r   rs   )rt   ru   rv   s      r   ProcessTFPlanFiler      s`    ZZ&F::f$$W--			 
&077: s   8; A-A((A-).r   
__future__r   r   r   r   rn   rJ   apitools.base.pyr   googlecloudsdk.api_lib.sccr   r5   %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   r.   r3   r<   rC   rS   rU   rY   r_   rf   rk   rw   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   