
                         $   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rSSK	J
r
Jr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\
\\4   4S jrS\S\4S jrS\
\\4   S\4S jrS\
\\4   S\\
\\4      4S jrS\S\4S jrg)z,Module for interacting with Terraform files.    )absolute_import)division)unicode_literalsN)DictListAny)const)parsers)run_subprocess)errors)filesroot_dirreturnc                    0 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  [        R                  " U5      X'   M     U(       a  GM)  U$ )a  Fetches all the relevant TF files in the given directory recusively and returns a dictionary of the file paths and contents.

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

Returns:
  A dictionary of the TF files in the given directory {path: contents}.
.z.tfz.tfvars)collectionsdequepopleftoslistdirpathjoinisdir
startswithappendisfileendswithr   ReadFileContents)r   tf_files	dir_queuecurrent_diritem	item_paths         ?lib/googlecloudsdk/api_lib/scc/remediation_intents/terraform.pyfetch_tf_filesr%   "   s      (
+)
 	##%K

;''',,{1i	y	!	!s##


9
% $77>>)$$


U
#
#y'9'9)'D'D&&s++#44Y? ( 		 
/    dir_pathc                     [         R                  " 5       n[         R                  " U 5        SS/n[        R                  " U[
        R                  S9   / SQn[        R                  " U[
        R                  SS9n[         R                  " U5        [        R                  " US   5      $ ! [         a$  n[        R                  " [        U5      5      eSnAff = f! [         a$  n[        R                  " [        U5      5      eSnAff = f)	zFetches the TfState json for the given directory and returns in json format.

Args:
  dir_path: The path of the directory to fetch the TfState data from.

Returns:
  The json of the TfState data or throws an exception if there is an error.
	terraforminit)timeout_secN)r)   showz-jsonT)r+   strip_outputr   )r   getcwdchdirr   GetOutputLinesr	   TF_CMD_TIMEOUT	Exceptionr   TfStateFetchingErrorstrjsonloads)r'   org_dircmdetfstate_datas        r$   fetch_tfstater;   @   s    .iikGHHX
C!!#53G3GH.
(C!00--DL HHW::l1o&& 
 .

%
%c!f
--. 
 .

%
%c!f
--.s1   AB, AC ,
C6CC
D'DDmodified_tf_filesc           
      x   0 nU R                  5        H  u  p#[        R                  " U5      X'   M     U R                  5        HV  u  p$[        R                  " X$5         SSSU/n[        R
                  " USS[        R                  [        R                  S9nMX     SS/n [        R
                  " USS[        R                  [        R                  S9nUR                  5        VVs/ s H  u  px[        R                  " Xx5      PM     nnng! [        R                   a}  nUR                  5        VVs/ s H  u  px[        R                  " Xx5      PM     Os  snnf nnn[        R                  R                  X&R                  UR                  S9s SnAs  $ SnAff = f! [        R                   a{  nUR                  5        VVs/ s H  u  px[        R                  " Xx5      PM     Os  snnf nnn[        R                  R                  UR                  UR                  S	9s SnA$ SnAff = fs  snnf )
zValidates the given TF files and returns the appropriate error message if any.

Args:
  modified_tf_files: The dictionary of the modified TF files {path: contents}.

Returns:
  The error message if any in string format, otherwise None.
r)   fmtz-write=trueT)textcheckstdoutstderr)	file_pathrA   rB   Nvalidate)rA   rB   )itemsr   r   WriteFileContents
subprocessrunPIPECalledProcessErrorr	   TF_FMT_ERROR_MSGformatrA   rB   TF_VALIDATE_ERROR_MSG)	r<   original_tf_filesrC   _file_contentr8   r9   fpfcs	            r$   validate_tf_filesrS   ^   s    ')'--/li#(#9#9)#D  0 "3!8!8!:i	I4%	:c
..
a	 ";. 
j!#	A& 6G5L5L5NO5N62ur&5N!O	E (( 	 *//11fb 
!
!"
)1a 
 ##**hhqxx +  	* 
	&	& 	 (--/	/FB 	'/	A 	
 &&--xx .  	 PsZ    :D#4F$ *"H6F!$F7"E
;FF!F!$H38H."G.-;H.(H3.H3tfstate_jsonc                    ^^ / mS[         [        [        4   4UU4S jjmU R                  S0 5      R                  S0 5      nT" U5        T$ )aS  Traverses the TfState json and returns a list of resources in json format.

Args:
  tfstate_json: The json of the TfState data. Structure:
                {
                    "values": {
                        "root_module": {
                            "resources": [ ... ],  # List of resources
                            "child_modules": [     # List of nested modules
                                {
                                    "resources": [ ... ],
                                    "child_modules": [ ... ]
                                }
                            ]
                        }
                    }
                }
Returns:
  A list of json objects, each representing a resource in the TfState.
  or an empty list if there are no resources in the TfState or if the TfState
  is not in the expected format.
modulec                 p   > SU ;   a  TR                  U S   5        SU ;   a  U S    H  nT" U5        M     g g )N	resourceschild_modules)extend)rV   childall_resourcestraverses     r$   r]   ,get_resources_from_tfstate.<locals>.traverse   sA    f6+./& /*% + !r&   valuesroot_module)r   r4   r   get)rT   r`   r\   r]   s     @@r$   get_resources_from_tfstaterb      sR    4 -tCH~     2.22="E+
;	r&   c                 P   [        U 5      n[        U5      n0 [        R                   Vs0 s H  nU[        R
                  _M     snE[        R                   Vs0 s H  nU[        R                  _M     snEnUR                  U;   a  XQR                     " X15      $ gs  snf s  snf )ap  Parses the tfstate file for the given finding.

Args:
  dir_path: The path of the directory to parse the tfstate file from.
  finding_data: SCC Finding data in form of class
    (securityposture.messages.Finding).

Returns:
  The structured data depending on the finding category, in string format. If
  the finding category is not supported, returns an empty string.
 )	r;   rb   r	   IAM_RECOMMENDER_FINDINGSr
   iam_recommender_parserFIREWALL_FINDINGSfirewall_parsercategory)r'   finding_datatftstate_jsonrX   ri   
parser_maps         r$   parse_tf_filerm      s      )-(7)	  88	8h G22
28		  11	1h G++
+1		* j(++,YEE			s   BB#)__doc__
__future__r   r   r   r   r5   r   rG   typingr   r   r   .googlecloudsdk.api_lib.scc.remediation_intentsr	   r
   googlecloudsdk.command_lib.coder   2googlecloudsdk.command_lib.scc.remediation_intentsr   googlecloudsdk.core.utilr   r4   r%   r;   rS   rb   rm    r&   r$   <module>rv      s    3 &  '   	  " " @ B : E *S T#s(^ <.C .4 .<<c3h <C <~%sCx.%	$sCx.%PC # r&   