
    '                         S 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K	J
r
  SSKJr  S	rS
rSrSrSrSrSrSrSrSrSS/r/ SQrSS/rSS/rS/r " S S\R8                  5      rg)z0Troubleshoot user permission for ssh connection.    )absolute_import)division)unicode_literals)apis)ssh_troubleshooter)ssh)logcomputeiamcloudresourcemanageriapv1v3z!You need the IAM permissions {0}
a  The VM has an attached service account. You need the permission iam.serviceAccounts.actAs on the project or service account. Alternatively, this permission is included in the roles/iam.serviceAccountUser role.
Help for service account permission: https://cloud.google.com/iam/docs/service-accounts-actas
Help for service account role: https://cloud.google.com/iam/docs/service-accounts
zYou need the Compute OS Admin Login role (roles/compute.osAdminLogin) or the Compute OS Login role (roles/compute.osLogin).
Help for roles: https://cloud.google.com/compute/docs/access/iam#predefinedroles
zYou need permission to SSH to a private IP address: iap.tunnelInstances.accessViaIAP.
Help for IAP permissions: https://cloud.google.com/iap/docs/managing-access
zcompute.instances.getzcompute.instances.use)zresourcemanager.projects.getzcompute.projects.getzcompute.zoneOperations.getzcompute.globalOperations.getziam.serviceAccounts.actAsziam.serviceAccounts.getzcompute.instances.osAdminLoginzcompute.instances.osLoginz iap.tunnelInstances.accessViaIAPc                   `    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rSrg)UserPermissionTroubleshooterH   aL  Check user permission.

Perform IAM authorization checks for the following IAM resources: instance,
project, service account, IAP, and OS Login if applicable.

Attributes:
  project: The project object.
  instance: The instance object.
  zone: str, the zone name.
  iap_tunnel_args: SshTunnelArgs or None if IAP Tunnel is disabled.
c                    Xl         X l        X0l        X@l        [        R
                  " [        [        5      U l        [        R                  " [        [        5      U l
        [        R
                  " [        [        5      U l        [        R                  " [        [        5      U l        [        R
                  " [        [        5      U l        [        R                  " [        [        5      U l        [        R
                  " [$        [        5      U l        [        R                  " [$        [        5      U l        SU l        0 U l        g )NF)projectzoneinstanceiap_tunnel_argsr   GetClientInstance_API_COMPUTE_CLIENT_NAME_API_CLIENT_VERSION_V1compute_clientGetMessagesModulecompute_message_API_IAM_CLIENT_NAME
iam_clientiam_message _API_RESOURCEMANAGER_CLIENT_NAME_API_CLIENT_VERSION_V3resourcemanager_client_v3resourcemanager_message_v3_API_IAP_CLIENT_NAME
iap_clientiap_messageenable_osloginissues)selfr   r   r   r   s        Hlib/googlecloudsdk/command_lib/compute/user_permission_troubleshooter.py__init__%UserPermissionTroubleshooter.__init__U   s    LIM*001I1GID112J2HJD,,-A-CEDO--.B.DFD%)%;%;(*@&BD"&*&<&<(*@'BD#,,-A-CEDO--.B.DFDDDK    c                 .    U R                  5       U l        g)z)Validate if the user has enabled oslogin.N)_IsOsLoginEnabledr(   r*   s    r+   check_prerequisite/UserPermissionTroubleshooter.check_prerequisitem   s    002Dr.   c                     g )N r1   s    r+   cleanup_resources.UserPermissionTroubleshooter.cleanup_resourcesq   s    
r.   c                    [         R                  R                  S5        U R                  (       a)  U R	                  5       (       a  [
        U R                  S'   O*[        R                  S5        [        R                  S5        [        U R                  5       R                  U R                  5       5      5      nU(       a1  [        R                  SR!                  U5      5      U R                  S'   U R"                  R$                  (       a(  U R'                  5       (       a  [(        U R                  S'   U R*                  (       a(  U R-                  5       (       a  [.        U R                  S'   [         R                  R                  S	R                  [1        U R                  R3                  5       5      5      5        U R                  R5                  5        H"  n[         R                  R                  U5        M$     g )
Nz#---- Checking user permissions ----osloginzcompute.instances.setMetadataz*compute.projects.setCommonInstanceMetadata instance_projectserviceaccountr   z&User permissions: {0} issue(s) found.
)r	   statusPrintr(   _CheckOsLoginPermissionsOS_LOGIN_MESSAGEr)   instance_permissionsappendproject_permissionssorted_CheckInstancePermissionsunion_CheckProjectPermissionsINSTANCE_PROJECT_MESSAGEformatjoinr   serviceAccounts_CheckServiceAccountPermissionsSERVICE_ACCOUNT_MESSAGEr   _CheckIapPermissionsIAP_MESSAGElenkeysvalues)r*   missing_instance_projectmessages      r+   troubleshoot)UserPermissionTroubleshooter.troubleshoott   sj   JJ:; 
	&	&	(	(!1I !!"AB  !MN  &d&D&D&F&L&L%%'')  *(@(G(G
((+
,).dkk$% }}$$)M)M)O)O&=dkk"#  9 9 ; ;&dkk% JJ>EEDKK! ";;%%'	jjw (r.   c                    U R                   R                  [        S9nSR                  U R                  R
                  U R                  U R                  R
                  5      nU R                   R                  X!S9nU R                  R                  R                  U5      n[        [        5      [        UR                  5      -
  $ )zRCheck if user miss any IAP Permissions.

Returns:
  set, missing IAM permissions.
permissionsz,projects/{}/iap_tunnel/zones/{}/instances/{}resourcetestIamPermissionsRequest)r'   TestIamPermissionsRequestiap_permissionsrI   r   namer   r   IapTestIamPermissionsRequestr&   r   TestIamPermissionssetrY   )r*   iam_requestr[   requestresponses        r+   rN   1UserPermissionTroubleshooter._CheckIapPermissions   s     ""<<# = %K=DD499dmm&8&8:H;; < BG!!44W=H#h&:&:";;;r.   c                    U R                   R                  [        S9nU R                   R                  SR	                  U R
                  R                  U R                  R                  S   R                  S9US9nU R                  R                  R                  U5      n[        [        5      [        UR                  5      -
  $ )zcCheck whether user has service account IAM permissions.

Returns:
   set, missing IAM permissions.
rX   z3projects/{project}/serviceAccounts/{serviceaccount}r   )r   r<   rZ   )r    r]   serviceaccount_permissions3IamProjectsServiceAccountsTestIamPermissionsRequestrI   r   r_   r   rK   emailr   projects_serviceAccountsra   rb   rY   )r*   rc   rd   re   s       r+   rL   <UserPermissionTroubleshooter._CheckServiceAccountPermissions   s     ""<<. = 0KRRFMMLL%%==88;AA N C #.	 S /G
 77JJH )*S1E1E-FFFr.   c                 v    U R                  [        5      n[        [        5      [        UR                  5      -
  $ )zZCheck whether user has oslogin IAM permissions.

Returns:
  set, missing IAM permissions.
)_ComputeTestIamPermissionsoslogin_permissionsrb   rY   r*   re   s     r+   r?   5UserPermissionTroubleshooter._CheckOsLoginPermissions   s1     ../BCH"#c(*>*>&???r.   c                 v    U R                  [        5      n[        [        5      [        UR                  5      -
  $ )zfCheck whether user has IAM permission on instance resource.

Returns:
  set, missing IAM permissions.
)rn   rA   rb   rY   rp   s     r+   rE   6UserPermissionTroubleshooter._CheckInstancePermissions   s1     ../CDH#$s8+?+?'@@@r.   c                    U R                   R                  US9nU R                   R                  U R                  R                  U R
                  R                  UU R                  S9nU R                  R                  R                  U5      $ )zCall TestIamPermissions to check whether user has certain IAM permissions.

Args:
  permissions: list, the permissions to check for the instance resource.

Returns:
  TestPermissionsResponse, the API response from TestIamPermissions.
rX   )r   r[   testPermissionsRequestr   )
r   TestPermissionsRequest)ComputeInstancesTestIamPermissionsRequestr   r_   r   r   r   	instancesra   r*   rY   rc   rd   s       r+   rn   7UserPermissionTroubleshooter._ComputeTestIamPermissions   s     &&== > !K""LL!!##*YY	 M G
 ((;;GDDr.   c                 v    U R                  [        5      n[        [        5      [        UR                  5      -
  $ )zeCheck whether user has IAM permission on project resource.

Returns:
  set, missing IAM permissions.
)"_ResourceManagerTestIamPermissionsrC   rb   rY   rp   s     r+   rG   5UserPermissionTroubleshooter._CheckProjectPermissions   s1     667JKH"#c(*>*>&???r.   c                     U R                   R                  US9nU R                   R                  SR                  U R                  R
                  S9US9nU R                  R                  R                  U5      $ )zCheck whether user has IAM permission on resource manager.

Args:
  permissions: list, the permissions to check for the project resource.

Returns:
  set, missing IAM permissions.
rX   zprojects/{project})r   rZ   )	r$   r]   5CloudresourcemanagerProjectsTestIamPermissionsRequestrI   r   r_   r#   projectsra   ry   s       r+   r|   ?UserPermissionTroubleshooter._ResourceManagerTestIamPermissions   s|     11KK L !K--cc%,,T\\5F5F,G"- d /G ))22EEgNNr.   c                     [         R                  " U R                  U R                  [         R                  5      n[        U5      $ )ziCheck whether OS Login is enabled on the VM.

Returns:
  boolean, indicates whether OS Login is enabled.
)r   FeatureEnabledInMetadatar   r   OSLOGIN_ENABLE_METADATA_KEYbool)r*   oslogin_enableds     r+   r0   .UserPermissionTroubleshooter._IsOsLoginEnabled   s7     22t||S%D%DFO   r.   )r   r   r(   r   r    r&   r'   r   r   r)   r   r#   r$   r   N)__name__
__module____qualname____firstlineno____doc__r,   r2   r6   rU   rN   rL   r?   rE   rn   rG   r|   r0   __static_attributes__r5   r.   r+   r   r   H   sJ    
03  D<G$@AE$@O 	!r.   r   N)r   
__future__r   r   r   googlecloudsdk.api_lib.utilr   "googlecloudsdk.command_lib.computer   #googlecloudsdk.command_lib.util.sshr   googlecloudsdk.corer	   r   r   r!   r%   r   r"   rH   rM   r@   rO   rA   rC   rh   ro   r^   SshTroubleshooterr   r5   r.   r+   <module>r      s    7 &  ' , A 3 #$  #9     ? ; I : 
 01HI     
 %  66u!#5#G#G u!r.   