
                  	           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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 rS rS\4S jrS\4S jrS\S\\\\\	\   4   4   4S jrS\S\4S jrS rg)zFLibrary for interacting with the Security Command Center Findings API.    )absolute_import)division)unicode_literalsN)DictList)encoding)const)apis)errorsc                  h    [         R                  " [        R                  [        R                  5      $ )z8Returns the Security Command Center Findings API client.)r
   GetClientInstancer	   FINDINGS_API_NAMEFINDINGS_API_VERSION     :lib/googlecloudsdk/api_lib/scc/iac_remediation/findings.py	GetClientr      &    			 7 7 % : :
< <r   c                  h    [         R                  " [        R                  [        R                  5      $ )z:Returns the Security Command Center Findings API messages.)r
   GetMessagesModuler	   r   r   r   r   r   GetMessagesr   %   r   r   returnc                     Sn[         R                  " X5      nU(       a  UR                  S5      $ [        R                  " U 5      e)zParses the finding name to get the finding id.

Args:
  finding_name: Canonical name of the finding.

Returns:
  Finding id, if found else throws an error
z;projects/(\d+)/sources/(\d+)/locations/(\w+)/findings/(\w+)   )researchgroupr   InvalidFindingNameError)finding_namepatternmatchs      r   	ParseNamer"   +   s:     K'
))G
*%
;;q>

(
(
66r   c                    [        5       n[        5       n[        U5      nUR                  5       nSU S3Ul        SU  S3Ul        UR                  R                  U5      n[        R                  " U5      n[        U5        U$ )zMakes an API call to the Security Command Center Findings API.

Args:
  finding_org_id: Organization ID of the finding
  finding_name: Canonical name of the finding.

Returns:
  JSON response from the API call.
zname : "z" zorganizations/z
/sources/-)r   r   r"   5SecuritycenterOrganizationsSourcesFindingsListRequestfilterparentorganizations_sources_findingsr   r   MessageToJsonValidateFinding)finding_org_idr   clientmessages
finding_idrequestresp	json_resps           r   MakeApiCallr1   =   s     ;&](&*JJL'zl#.'.#N#3:>'.		.	.	3	3G	<$$$T*))	r   finding_jsonc                    0 nU S   S   S   S    H  nUS   U;  a  [        5       XS   '   US   S:X  aF  SXS      ;  a
  / XS      S'   US   XS      S   ;  a  XS      S   R                  US   5        Mh  Mj  US   S	:X  d  Mu  S	XS      ;  a
  / XS      S	'   US   XS      S	   ;  d  M  XS      S	   R                  US   5        M     U$ )
zFetches the IAMBindings from the finding data.

Args:
  finding_json: JSON response from the API call to fetch the finding.

Returns:
  IAM binding details per member.
listFindingsResultsr   findingiamBindingsmemberactionADDroleREMOVE)dictappend)r2   iam_bindingsbindings      r   FetchIAMBindingr@   S   s%    ,34Q7	Bg x,(,l8$%xE!	l8#45	513X&'.	X-> ? F	FX&'.55gfoF 
G		h	&	h&78	846X&'1	X-> ? I	IX&'188I 
r   c                     U S   S   S   S   $ )zFetches the resource name from the finding data.

Args:
  finding_json: JSON response from the API call to fetch the finding.

Returns:
  Resource name for which the finding was generated.
r4   r   resourcedisplayNamer   )r2   s    r   FetchResourceNamerD   q   s     
+	,Q	/
	;M	JJr   c                     [         R                  " U 5      n U S   S   S   nUS   nU[        R
                  ;  a  [        R                  " U5      e[        U 5      n[        U5      [        R                  :  d  [        U5      S:  a  [        R                  " [        U5      5      eg!   [        R                  " 5       e= f)zcChecks if the finding is supported or not.

Args:
  finding_data: JSON response from the API call.
r4   r   r5   category   N)jsonloadsr   FindingNotFoundErrorr	   SUPPORTED_FINDING_CATEGORIESUnsupportedFindingCategoryErrorr@   len SUPPORTED_IAM_MEMBER_COUNT_LIMITExcessiveMembersError)finding_datar5   finding_categoryr>   s       r   r)   r)   }   s    (::l+L01!4Y?G Z(U???

0
01A
BB .,	,%@@@	\	Q	

&
&s<'8
99 
(

%
%
''s   !B) )C )__doc__
__future__r   r   r   rH   r   typingr   r   apitools.base.pyr   *googlecloudsdk.api_lib.scc.iac_remediationr	   googlecloudsdk.api_lib.utilr
   .googlecloudsdk.command_lib.scc.iac_remediationr   r   r   strr"   r1   r@   rD   r)   r   r   r   <module>rZ      s    M &  '  	  % < , A<<7s 7$ ,S$sDI~&&'<	KD 	KS 	K:r   