
    G!                        S 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rSSKJ	r	J
r
  SSKJr  SSKJr  SSKrS	\	\\	\\
\   4   4   S
\
\	\\4      S\S\	\\
\	\\4      4   4S jrS\
\   S\	\\4   4S jrS\	\\4   4S jrS\	\\4   S\\	\\4   44S jrS\S\4S jrS\S\4S jrS\	\\4   S\S\S\S\4
S jrS\S\
\   4S jrS\
\   S\S\
\	\\4      4S jrS\S\
\	\\4      4S jrg) zLibrary for fetching TF Files.    )absolute_import)division)unicode_literalsN)DictList)run_subprocess)filesiam_bindingstfstate_json_listresource_namereturnc           	      `   0 nU R                  5        H  u  pEU H  nSU;   a=  US    H4  n[        XbXt5      nU(       d  M  XC;  a  / X4'   X4   R                  U5        M6     SU;   d  MN  US    H4  n[        XbXt5      nU(       d  M  XC;  a  / X4'   X4   R                  U5        M6     M     Us  $    g)a#  Gets the TFState information for the given IAM bindings.

Args:
  iam_bindings: IAM bindings for the resource.
  tfstate_json_list: List of TFState files.
  resource_name: Resource name for which the finding was generated.

Returns:
  List of TFState information for the given IAM bindings.
ADDREMOVEN)itemsfetch_relevant_modulesappend)	r
   r   r   tfstate_informationmemberbindingtfstate_jsonroleresource_datas	            ;lib/googlecloudsdk/api_lib/scc/iac_remediation/terraform.py"get_tfstate_information_per_memberr      s     :<%++-of)	'	END04- ]0,.!)'..}= # 
W	H%D04- ]0,.!)'..}= & *& ) .    tf_files_pathsc                 |    [         [        [        4   " 5       nU  H  n[        R                  " U5      nX1U'   M     U$ )zReads the original files content.

Args:
  tf_files_paths: List of TF files paths.

Returns:
  Dict of file path and file content.
)dictstrr	   ReadFileContents)r   original_tf_files	file_pathoriginal_file_contents       r   read_original_files_contentr%   F   s@     38n&!i!229=#8i  " 
r   response_dictc                 b    U R                  5        H  u  p[        R                  " X5      nM     g)zuUpdates the TF files with the response dict.

Args:
  response_dict: Response dict containing the updated TF files.

N)r   r	   WriteFileContents)r&   r#   	file_data_s       r   update_tf_filesr+   X   s*     ,113i	5A 4r   c                 |   [         [        [        4   " 5       nU R                  5        Hj  u  p#[        R                  " U5      nXAU'    [        R
                  " X#5      nSSSU/n[        R                  " USSS9  [        R                  " U5      X'   Ml     SS/n [        R                  " USSS9n[        U5        [        R                  " S	US
   [        R                  5      (       a  SU 4$ g! [         a  n[        U5        SU4s SnAs  $ SnAff = f! [         a  n[        U5        SU4s SnA$ SnAff = f)a  Validates the TFState information for the given IAM bindings.

Args:
  response_dict: response dict containing the updated TF files.

Returns:
  True if the response is valid, False otherwise.
  updated_response_dict: Updated response dict containing the original TF
  files.
	terraformfmtz-write=true   F)timeout_secshow_stderrNvalidateSuccessr   T)FN)r   r    r   r	   r!   r(   r   GetOutputLines	Exceptionr+   research
IGNORECASE)	r&   r"   r#   r)   r$   r*   cmdevalidate_outputs	            r   validate_tf_filesr<   e   s0    38n&+113i!229=#8i 

!
!)
7a%	:c##CRUK!&!7!7	!Bm 4 
j!#$33O #$ii	?1-r}}==	  '(AXo 
 %&!8Os=   	A
C0D 0
D:DDD
D;"D60D;6D;dir_pathc                      [         R                  " U 5        SS/n[        R                  " USS9   / SQn[        R                  " USS9nU$ ! [         a
  n SnAgSnAff = f! [         a
  n SnAgSnAff = f)zFetches the TFState json for the given directory.

Args:
  dir_path: The path of the directory to fetch the TFState files from.

Returns:
  The json of the TFState file or None if there is an error.
r-   init
   )r0   N )r-   showz-json)oschdirr   r4   r5   )r=   r9   r*   r   s       r   fetch_tfstate_json_from_dirrE      sz    HHX
C!!#26
(C!00"EL 
 
 
 
 s"   /A A$ 
A!A!$
A83A8r#   c                 ^    [         R                  " U 5      n[        R                  " U5      nU$ )zFetches the TFState json for the given tfstate file path.

Args:
  file_path: The path of the file to fetch the TFState json from.

Returns:
  The json of the TFState files.
)r	   r!   hcl2loads)r#   filer   s      r   fetch_tfstate_json_from_filerJ      s(     
				*$D!,	r   r   	role_namemember_namec                    SnSU ;  d  SU S   ;  d  SU S   S   ;  a  U$ U S   S   S    HY  nSU;   d  M  SUS   ;   d  M  SUS   ;   d  M!  SUS   ;   d  M,  US   S   U:X  d  M:  US   S   U:X  d  MH  US   S   U:X  d  MV  Un  U$    U$ )z:Fetches the relevant modules from the given TFState files.rA   valuesroot_module	resourcesr   r   
project_id )r   r   rK   rL   r   resources         r   r   r      s    
 -l"	l84	4	L2=A	Ax(7DhH**hx((HX..Xx(K7Xv&)3X|,=m	 E 
r   root_dirc                    / n[         R                  " U /5      nU(       Ga"  UR                  5       n[        R                  " U5       H  n[        R
                  R                  X45      n[        R
                  R                  U5      (       a+  UR                  S5      (       d  UR                  U5        Mo  Mq  [        R
                  R                  U5      (       d  M  UR                  S5      (       d0  UR                  S5      (       d  M  UR                  S5      (       a  M  UR                  U5        M     U(       a  GM"  U$ )zFinds all the TF files in the given directory.

Args:
  root_dir: The path of the directory to find the TF files in.

Returns:
  A list of the TF files paths in the given directory.
.z.tfz.tfvars)collectionsdequepopleftrC   listdirpathjoinisdir
startswithr   isfileendswith)rT   tf_filesqueuecurrent_diritem	item_paths         r   find_tf_filesrf      s     (


XJ
'%--/K

;''',,{1i	y	!	!s##
,,y
! $77>>)$$


U
#
#y'9'9)'D'D&&s++	" ( 	 
/r   tfstate_file_pathsc                 t    / nU (       a%  U  H  nUR                  [        U5      5        M     U$ [        U5      $ )zFetches the TFState list for the given TFState file paths.

Args:
  tfstate_file_paths: List of TFState file paths.
  root_dir: The path of the root directory.

Returns:
  List of TFState json.
)r   rJ   find_tfstate_jsons)rg   rT   r   tfstate_file_paths       r   fetch_tfstate_listrk      sD     /
&'8
9 0 
 h''r   c                    / n[         R                  " U /5      nU(       a  UR                  5       nUR                  [	        U5      5        [
        R                  " U5       Hq  nUR                  S5      (       a  M  [
        R                  R                  X45      n[
        R                  R                  U5      (       d  M`  UR                  U5        Ms     U(       a  M  U$ )zFinds the TFState jsons in the given directory.

Args:
  dir_path: The path of the directory to find the TFState jsons in.

Returns:
  List of TFState jsons.
rV   )rW   rX   rY   r   rE   rC   rZ   r^   r[   r\   r]   )r=   tfstate_jsonsrb   rc   rd   re   s         r   ri   ri      s     -


XJ
'%--/K4[AB

;'__S!!GGLL3	77==##
,,y
!	 ( 	 
r   )__doc__
__future__r   r   r   rW   rC   r6   typingr   r   googlecloudsdk.command_lib.coder   googlecloudsdk.core.utilr	   rG   r    r   r%   r+   boolr<   rE   rJ   r   rf   rk   ri   rR   r   r   <module>rt      s   % &  '  	 	  : * $sDd3i001$DcN+$ $ 
#tDcN#
#$	$NIcN$
6S>
6$S>$
DcN$N# # .C C sCx.#&58 	6C DI 4S	 
$sCx.0	$sCx.r   