
                  	           d 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m	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ d Zd ZdefdZdefdZdedeeeee	e   f   f   fdZdedefdZd Zy)zFLibrary for interacting with the Security Command Center Findings API.    )absolute_import)division)unicode_literalsN)DictList)encoding)const)apis)errorsc                  f    t        j                  t        j                  t        j                        S )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                  f    t        j                  t        j                  t        j                        S )z:Returns the Security Command Center Findings API messages.)r
   GetMessagesModuler	   r   r   r   r   r   GetMessagesr   %   r   r   returnc                     d}t        j                  ||       }|r|j                  d      S t        j                  |       )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                    t               }t               }t        |      }|j                         }d| d|_        d|  d|_        |j                  j                  |      }t        j                  |      }t        |       |S )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                    i }| d   d   d   d   D ]  }|d   |vrt               ||d   <   |d   dk(  rDd||d      vrg ||d      d<   |d   ||d      d   vsH||d      d   j                  |d          f|d   d	k(  sod	||d      vrg ||d      d	<   |d   ||d      d	   vs||d      d	   j                  |d           |S )
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   s1    ,34Q7	Bg x,(,l78$%xE!	l78#45	513WX&'.	WX-> ? F	FWX&'.55gfoF		h	&	gh&78	846WX&'1	WX-> ? I	IWX&'188I 
r   c                     | d   d   d   d   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                 v   	 t        j                  |       } | d   d   d   }|d   }|t        j
                  vrt        j                  |      t        |       }t        |      t        j                  kD  st        |      dk  rt        j                  t        |            y#  t        j                         xY w)ziChecks 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" "B8)__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   