
    '                         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l	m
Z
 ddlmZ d	Zd
ZdZdZdZdZdZdZdZdZddgZg dZddgZddgZdgZ G d dej8                        Zy)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                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)UserPermissionTroubleshootera\  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                    || _         || _        || _        || _        t	        j
                  t        t              | _        t	        j                  t        t              | _
        t	        j
                  t        t              | _        t	        j                  t        t              | _        t	        j
                  t        t              | _        t	        j                  t        t              | _        t	        j
                  t$        t              | _        t	        j                  t$        t              | _        d| _        i | _        y )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__z%UserPermissionTroubleshooter.__init__U   s    DLDIDM*D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                 .    | j                         | _        y)z)Validate if the user has enabled oslogin.N)_IsOsLoginEnabledr'   r)   s    r*   check_prerequisitez/UserPermissionTroubleshooter.check_prerequisitem   s    002Dr,   c                      y )N r/   s    r*   cleanup_resourcesz.UserPermissionTroubleshooter.cleanup_resourcesq   s    
r,   c                    t         j                  j                  d       | j                  r$| j	                         r>t
        | j                  d<   n*t        j                  d       t        j                  d       t        | j                         j                  | j                                     }|r1t        j                  dj!                  |            | j                  d<   | j"                  j$                  r#| j'                         rt(        | j                  d<   | j*                  r#| j-                         rt.        | j                  d<   t         j                  j                  d	j                  t1        | j                  j3                                            | j                  j5                         D ]!  }t         j                  j                  |       # y )
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*   troubleshootz)UserPermissionTroubleshooter.troubleshoott   s`   JJ:; 
	&	&	(!1I !!"AB  !MN  &d&D&D&F&L&L%%'')  *(@(G(G
((+
,).dkk$% }}$$)M)M)O&=dkk"#  9 9 ;&dkk% JJ>EEDKK! ";;%%'	jjw (r,   c                    | j                   j                  t              }dj                  | j                  j
                  | j                  | j                  j
                        }| j                   j                  ||      }| j                  j                  j                  |      }t        t              t        |j                        z
  S )z^Check if user miss any IAP Permissions.

    Returns:
      set, missing IAM permissions.
    permissionsz,projects/{}/iap_tunnel/zones/{}/instances/{}resourcetestIamPermissionsRequest)r&   TestIamPermissionsRequestiap_permissionsrE   r   namer   r   IapTestIamPermissionsRequestr%   r   TestIamPermissionssetrT   )r)   iam_requestrV   requestresponses        r*   rJ   z1UserPermissionTroubleshooter._CheckIapPermissions   s     ""<<# = %K=DD499dmm&8&8:H;;[ < BG!!44W=H#h&:&:";;;r,   c                    | j                   j                  t              }| j                   j                  dj	                  | j
                  j                  | j                  j                  d   j                        |      }| j                  j                  j                  |      }t        t              t        |j                        z
  S )zoCheck whether user has service account IAM permissions.

    Returns:
       set, missing IAM permissions.
    rS   z3projects/{project}/serviceAccounts/{serviceaccount}r   )r   r8   rU   )r   rX   serviceaccount_permissions3IamProjectsServiceAccountsTestIamPermissionsRequestrE   r   rZ   r   rG   emailr   projects_serviceAccountsr\   r]   rT   )r)   r^   r_   r`   s       r*   rH   z<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    | j                  t              }t        t              t        |j                        z
  S )zfCheck whether user has oslogin IAM permissions.

    Returns:
      set, missing IAM permissions.
    )_ComputeTestIamPermissionsoslogin_permissionsr]   rT   r)   r`   s     r*   r;   z5UserPermissionTroubleshooter._CheckOsLoginPermissions   s1     ../BCH"#c(*>*>&???r,   c                 v    | j                  t              }t        t              t        |j                        z
  S )zrCheck whether user has IAM permission on instance resource.

    Returns:
      set, missing IAM permissions.
    )rg   r=   r]   rT   ri   s     r*   rA   z6UserPermissionTroubleshooter._CheckInstancePermissions   s1     ../CDH#$s8+?+?'@@@r,   c                 &   | j                   j                  |      }| j                   j                  | j                  j                  | j
                  j                  || j                        }| j                  j                  j                  |      S )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.
    rS   )r   rV   testPermissionsRequestr   )
r   TestPermissionsRequest)ComputeInstancesTestIamPermissionsRequestr   rZ   r   r   r   	instancesr\   r)   rT   r^   r_   s       r*   rg   z7UserPermissionTroubleshooter._ComputeTestIamPermissions   s     &&== > !K""LL!!##*YY	 M G
 ((;;GDDr,   c                 v    | j                  t              }t        t              t        |j                        z
  S )zqCheck whether user has IAM permission on project resource.

    Returns:
      set, missing IAM permissions.
    )"_ResourceManagerTestIamPermissionsr?   r]   rT   ri   s     r*   rC   z5UserPermissionTroubleshooter._CheckProjectPermissions   s1     667JKH"#c(*>*>&???r,   c                    | j                   j                  |      }| j                   j                  dj                  | j                  j
                        |      }| j                  j                  j                  |      S )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.
    rS   zprojects/{project})r   rU   )	r#   rX   5CloudresourcemanagerProjectsTestIamPermissionsRequestrE   r   rZ   r"   projectsr\   rp   s       r*   rr   z?UserPermissionTroubleshooter._ResourceManagerTestIamPermissions   s|     11KK L !K--cc%,,T\\5F5F,G"- d /G ))22EEgNNr,   c                     t        j                  | j                  | j                  t         j                        }t        |      S )zuCheck 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*   r.   z.UserPermissionTroubleshooter._IsOsLoginEnabled   s7     22t||S%D%DFO   r,   N)__name__
__module____qualname____doc__r+   r0   r3   rQ   rJ   rH   r;   rA   rg   rC   rr   r.   r2   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!   rD   rI   r<   rK   r=   r?   rb   rh   rY   SshTroubleshooterr   r2   r,   r*   <module>r      s    7 &  ' , A 3 #$  #9     ? ; I : 
 01HI     
 %  66u!#5#G#G u!r,   