
    v                        d 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d	d
Z	dddd
Z	d Z
d Z ej                  ej                  j                        ej                   G d dej                                       Z ej                  ej                  j$                        ej                   G d dej                                       Z ej                  ej                  j(                         G d dej                                Zy)zCommand to query activities.    )absolute_import)division)unicode_literals)policy_troubleshooter)basez%Troubleshoot the IAM Policy.
        z      Performs a check on whether a principal is granted a
      permission on a resource and how that access is determined according to
      the resource's effective IAM policy interpretation.
        am      To troubleshoot a permission of a principal on a resource, run:

      $ {command} //cloudresourcemanager.googleapis.com/projects/project-id       --principal-email=my-iam-account@somedomain.com       --permission=resourcemanager.projects.get

    See https://cloud.google.com/iam/help/allow-policies/overview for more
    information about IAM policies.
      )briefDESCRIPTIONEXAMPLESz2Troubleshoot IAM allow and deny policies.
        z      Uses a resource's effective IAM allow policy and IAM deny policy to
      check whether a principal has a specific permission on the resource.
        aI        The following command checks whether the principal ``my-user@example.com''
      has the permission ``resourcemanager.projects.get'' on the project
      ``my-project'':

        $ {command} //cloudresourcemanager.googleapis.com/projects/my-project
        --principal-email=my-user@example.com
        --permission=resourcemanager.projects.get

      The following command checks whether the principal ``my-user@example.com''
      has the ``compute.images.get'' permission on the project
      ``my-project''. The command also provides additional context that lets
      Troubleshooter evaluate conditional role bindings:

        $ {command} //cloudresourcemanager.googleapis.com/projects/my-project         --principal-email=my-user@example.com         --permission=compute.images.get         --resource-name=//compute.googleapis.com/projects/my-project/zones/images/my-image'        --resource-service='compute.googleapis.com'         --resource-type='compute.googleapis.com/Image'         --destination-ip='192.2.2.2'--destination-port=8080 --request-time='2023-01-01T00:00:00Z'
      c                    | j                  ddt        d       | j                  dddt        d	       | j                  d
ddt        d	       | j                  ddt        d       | j                  ddt        d       | j                  ddt        d       | j                  ddt        d       | j                  ddt        d       | j                  ddt        d       y)"Parses arguments for the commands.resourceRESOURCEzFull resource name that access is checked against.
      For a list of full resource name formats, see: https://cloud.google.com/iam/docs/resource-names.
      )metavartypehelpz--principal-emailTEMAILz{Email address that identifies the principal to check. Only Google Accounts and
      service accounts are supported.
      )requiredr   r   r   z--permission
PERMISSIONa[  IAM permission to check. The permssion can be in the `v1` or `v2`
      format. For example, `resourcemanager.projects.get` or
      `cloudresourcemanager.googleapis.com/projects.get`.
      For a list of permissions, see https://cloud.google.com/iam/docs/permissions-reference and https://cloud.google.com/iam/docs/deny-permissions-support
      z--resource-serviceFzThe resource service value to use when checking conditional bindings.
      For accepted values, see: https://cloud.google.com/iam/docs/conditions-resource-attributes#resource-service
      )r   r   r   z--resource-typezThe resource type value to use when checking conditional bindings.
      For accepted values, see: https://cloud.google.com/iam/docs/conditions-resource-attributes#resource-type
      z--resource-namezThe resource name value to use when checking conditional bindings.
      For accepted values, see: https://cloud.google.com/iam/docs/conditions-resource-attributes#resource-name.
      z--request-timezThe request timestamp to use when checking conditional bindings. This string must adhere to UTC format
      (RFC 3339). For example,2021-01-01T00:00:00Z. For more information, see:
      https://tools.ietf.org/html/rfc3339
      z--destination-ipznThe request destination IP address to use when checking conditional bindings. For example, `198.1.1.1`.
      z--destination-portzaThe request destination port to use when checking conditional bindings. For example, 8080.
      N)add_argumentstrintparsers    :lib/surface/policy_intelligence/troubleshoot_policy/iam.py_Argsr   L   sA   
	   	
   	
  
 	
	   	
	   	
	   	
	   	
	   	
	      c                    | j                  |j                  |j                        }| j                  |j                        }| j                  |j                  |j                  |j                        }| j                  |||      }| j                  ||j                  |j                  |j                        }| j                  |      S )Troubleshoot the IAM Policies.)destination_ipdestination_port)request_time)resource_nameresource_serviceresource_type)destinationrequestr   )condition_contextfull_resource_nameprincipal_email
permission)GetPolicyTroubleshooterPeerr   r    GetPolicyTroubleshooterRequestr!   GetPolicyTroubleshooterResourcer"   r#   r$   'GetPolicyTroubleshooterConditionContext"GetPolicyTroubleshooterAccessTupler   r)   r*   TroubleshootIAMPolicies)policy_troubleshooter_apiargsdestination_contextrequest_contextresource_contextr'   access_tuples          r   _Runr7      s    1MM((,, N  .LL$$ M / /NN&&,,&& O   GG)!# H   +MM)**	 N , 
#	:	:<	HHr   c                   *    e Zd ZdZeZed        Zd Zy)TroubleshootAlphar   c                     t        |        yr   Nr   r   s    r   ArgszTroubleshootAlpha.Args        
&Mr   c                 \    t        t        j                  | j                               |      S Nr7   r   PolicyTroubleshooterApiReleaseTrackselfr2   s     r   RunzTroubleshootAlpha.Run   (    55d6G6G6IJD r   N	__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodr=   rF    r   r   r9   r9      s#     ' - r   r9   c                   *    e Zd ZdZeZed        Zd Zy)TroubleshootBeta)Troubleshoot IAM allow and deny policies.c                     t        |        yr;   r<   r   s    r   r=   zTroubleshootBeta.Args   r>   r   c                 \    t        t        j                  | j                               |      S r@   rA   rD   s     r   rF   zTroubleshootBeta.Run   rG   r   NrH   rP   r   r   rR   rR      s#     2 - r   rR   c                   *    e Zd ZdZeZed        Zd Zy)TroubleshootrS   c                     t        |        yr;   r<   r   s    r   r=   zTroubleshoot.Args   r>   r   c                 \    t        t        j                  | j                               |      S r@   rA   rD   s     r   rF   zTroubleshoot.Run   rG   r   NrH   rP   r   r   rW   rW      s!    1 - r   rW   N)rL   
__future__r   r   r   *googlecloudsdk.api_lib.policy_intelligencer   googlecloudsdk.callioper   rM   r   r7   ReleaseTracksrC   ALPHAHiddenCommandr9   BETArR   GArW   rP   r   r   <module>rc      s   # &  ' L (
	
*
@L^I< D%%++,   -  D%%**+t||   ,  D%%(()4<<  *r   