
    (                     @   d 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 ddlmZ  ej"                  ej$                  j&                        ej(                   G d dej*                  ej,                                      Zy)7Command for semi-automatic remediation of SCC findings.    N)const)
converters)extended_service)git)sps_api)	terraform)
validators)base)flags)logc                   2    e Zd ZdZdddZed        ZddZy)	AutoRemediater   z
        Orchestrates the semi-automatic remediation process for SCC findings
        by calling the Remediation Intent APIs.
        a`  
          Sample usage:
          Remediate a SCC finding for the organization 1234567890, in the
          terraform repository located at ./terraform-repo.
          $ {{command}} scc remediation-intents auto-remediate \
            --org-id=1234567890 \
            --root-dir-path=./terraform-repo \
            --git-config-path=./git-config.yaml)DESCRIPTIONEXAMPLESc                     t         j                  j                  |        t         j                  j                  | d       t         j                  j                  |        t         j
                  j                  |        y )N.)r   ROOT_DIR_PATH_FLAGAddToParser
SetDefaultORG_ID_FLAGGIT_CONFIG_FILE_PATH_FLAG)parsers    5lib/surface/scc/remediation_intents/auto_remediate.pyArgszAutoRemediate.Args6   sT    	((0	''4	!!&)	##//7    Nc                    |j                   }|j                  }|j                  }t        j                  |t
        j                  j                        }t        j                  t
        j                  j                        }t        j                  t
        j                  j                        }t        j                  |       t        j                  |       |j                         }|8|j!                          |j                         }|t#        j$                  d       y|j&                  }	t)        j*                  |      }
|
st#        j$                  d       yt)        j,                  ||j.                        }t#        j$                  d       t1        j2                  |      }|j4                  j6                  j8                  |_        |j=                  |j?                  |jA                  |
      |            |_!        d}|jE                  |	||      }|j:                  |j4                  j6                  jF                  k(  rt#        j$                  d       yd	}d
}|s*|tH        jJ                  k  rt#        j$                  d|       |jM                  |jN                  jP                  d
   jR                  jT                        }t)        jV                  |      }|d}n|jY                  |      |jB                  _-        d}|jE                  |	||      }|j:                  |j4                  j6                  jF                  k(  rt#        j$                  d       y|dz  }t#        j$                  d       |s|tH        jJ                  k  r|sPt#        j$                  d       |j4                  j6                  jF                  |_        d}|jE                  |	||      }yt#        j$                  d       |j4                  j6                  j\                  |_        d|jB                  _-        d}|jE                  |	||      }t#        j$                  d       |jM                  |jN                  jP                  d
   jR                  jT                        }|dxx   t_        ta        jb                               z  cc<   te        jf                  |tH        jh                  jk                  |j.                  jl                  jo                  d      d   |j.                  jl                  jo                  d      d   |j.                  jp                        |d   |d          t#        j$                  d       te        jr                  tH        jt                  jk                  |j.                  jl                  jo                  d      d   |j.                  jl                  jo                  d      d   |j.                  jp                        tH        jv                  jk                  |jN                  jx                  j{                  dd      dj}                  d te        j~                  |      j                         D              dj}                  d  te        j~                  |d!      j                         D              "      |d   |d   |d#   |d$         \  }}|sgt#        j$                  d%       |j4                  j6                  j                  |_        |jY                  |      |_-        d&}|jE                  |	||      }yt#        j$                  d'       |j4                  j6                  j                  |_        |j                  |j                  |t        te        j~                  |d!      j                               t        te        j~                  |d!      j                               (      )      |_H        d*}|jE                  |	||      }y)+zThe main function which is called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.
    Nz9No remediation intent found to be remediated, exitting...zNo TF files found, exitting...zRemediation started....)fileDatatfStateInfo)tfDatazstate,remediation_inputzRemediation failed, exitting...Fr   zRemediation retry count: T)reasonzremediation_input.error_details   zRemediation failed, retrying...z,Remediation failed: Max retry limit reached.statez#Remediation completed successfully.z%state,remediation_input.error_detailsz!Starting PR generation process...zbranch-prefix/)
project_id
finding_idcategoryremotezCommit pushed successfully.`z\`
c              3   0   K   | ]  \  }}| d |   ywz: N .0fpeas      r   	<genexpr>z$AutoRemediate.Run.<locals>.<genexpr>   s&      %PFB $bP   c              3   0   K   | ]  \  }}| d |   ywr-   r.   r/   s      r   r3   z$AutoRemediate.Run.<locals>.<genexpr>   s*      "FB $br4   )first)remediation_explanationfile_modifiersfile_ownerszmain-branch-name	reviewerszPR creation failed, exitting...zstate,error_detailszPR created successfully.)urlmodifiedFileOwnersmodifiedFilePaths)prDatazstate,remediation_artifacts)Iorg_idgit_config_pathroot_dir_pathr   ExtendedSPSClientr   ReleaseTrackALPHAr   RemediationIntentConverterr   GetMessagesModuler
   validate_git_configvalidate_relative_dir_path!fetch_enqueued_remediation_intent)create_semi_autonomous_remediation_intentr   Printnamer	   fetch_tf_filesparse_tf_filefindingDatacopydeepcopyRemediationIntentStateValueValuesEnumREMEDIATION_IN_PROGRESSr#   RemediationInputTfDataDictFilesToMessageremediationInputupdate_remediation_intentREMEDIATION_FAILEDr   REMEDIATION_RETRY_COUNTMessageFilesToDictremediatedOutput
outputDatar    r   validate_tf_filesErrorDetailserrorDetailsREMEDIATION_SUCCESSstruuiduuid4r   push_commit
COMMIT_MSGformatfindingNamesplitr(   	create_prPR_TITLEPR_DESCremediationExplanationreplacejoinget_file_modifiersitemsPR_GENERATION_FAILEDPR_GENERATION_SUCCESSRemediationArtifactsPullRequestlistvalueskeysremediationArtifacts)selfargsr?   git_config_datarA   client	convertermessagesintent_dataintent_nametf_filestfstate_dataintent_updatedupdate_maskis_remediatedretry_countupdated_tf_files	error_msg_	pr_statuspr_msgs                        r   RunzAutoRemediate.Run=   s    [[F**O&&M//8I8I8O8OPF55d6G6G6M6MNI(():):)@)@AH ""?3))-8 ::<K668<<>k				MN""K''6H	ii01**{..L II'(]];/N""77OO  '/&?&?11(;$  
 '@ 'N# ,K55[.N 	%%::MM	N 
ii12 MKe.K.K K	ii+[9"55

)
)
4
4Q
7
>
>
G
G --.>?i		5=5J5J 6K 6n%%2 6k77
{Nn 

''<<OOP 			34Qk	ii121 e.K.K K4 	ii>?

$
$
9
9
L
L  k

*
*
{Na II34""77KK  48N##09K55[.N
 II12 33''2215<<EE O$DJJL(99$OO%11==CCCHK%11==CCCHL#//88 	  	

 	!(	 II+,%11==CCCHK%11==CCCHL#//88 	 	

 	$2$C$C$Z$Z$b$bU%  99 %"556FGNNP%  		 " **+;4H%'"  	 	
 	!(*+$1Iv6 	ii12

$
$
9
9
N
N  %-$9$9$9$Hn!)k

*
*
{Na  II()""77MM  +3*G*G###''(8EMMO  #''(8EKKM $ 
 +H 
+N' 0K(([.	Ar   )returnN)__name__
__module____qualname____doc__detailed_helpstaticmethodr   r   r.   r   r   r   r       s3    
 @
3-  8 8Br   r   )r   rP   rd   .googlecloudsdk.api_lib.scc.remediation_intentsr   r   r   r   r   r	   r
   googlecloudsdk.callioper   2googlecloudsdk.command_lib.scc.remediation_intentsr   googlecloudsdk.corer   ReleaseTracksrC   rD   UniverseCompatibleSilentCommandCacheCommandr   r.   r   r   <module>r      s    >   @ E K > B D E ( D # D%%++,[D&&(9(9 [  -[r   