
    2                    @   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  \	R                  R                  S	\	R                  R                   S
\	R                  R"                  S0rS r " S S\R(                  5      r " S S\5      r " S S\5      rg)zoslogin client functions.    )annotations)Optional)apis)	apis_util)base)
exceptions)
propertiesv1alphav1betav1c                0    [         R                  " SU 5      $ )Noslogin)r   GetClientInstance)versions    ,lib/googlecloudsdk/api_lib/oslogin/client.py
_GetClientr       s    				7	33    c                      \ rS rSrSrSrg)OsloginException$   zDOsloginException is for non-code-bug errors in oslogin client utils. N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   r   r   $   s    Lr   r   c                      \ rS rSrSrSrg)OsloginKeyNotFoundError(   zFOsloginKeyNotFoundError is raised when requested SSH key is not found.r   Nr   r   r   r   r    r    (   s    Nr   r    c                      \ rS rSrSrS rS rS r  SS jrSS jr	   S SS	 jjr
S
 rS r SS jrS r    SS jr S SS jjrSrg)OsloginClient,   z%Class for working with oslogin users.c                J   [         U   U l        [        R                  R                  R
                  R                  5       U l         [        U R                  5      U l        U R                  R                  U l
        g ! [        R                   a    S U l        S U l
         g f = fN)VERSION_MAPr   r	   VALUEScoreprojectGetr   clientMESSAGES_MODULEmessagesr   UnknownVersionError)selfrelease_tracks     r   __init__OsloginClient.__init__/   sy    }-DL$$))11557DLt||,dkkk11dm(( dkdms   5A= ="B"!B"c                "    U R                  5       $ r&   )__bool__r0   s    r   __nonzero__OsloginClient.__nonzero__9   s    ==?r   c                ,    [        U R                  5      $ r&   )boolr,   r6   s    r   r5   OsloginClient.__bool__<   s    r   Nc                <   U R                   R                  nU R                  S:X  a  U" SR                  U5      UUS9nO8U(       a  UR                  R
                  nOSnU" SR                  U5      UUUS9nU R                  R                  R                  U5      nU$ )a+  Return the OS Login profile for a user.

The login profile includes some information about the user, a list of
Posix accounts with things like home directory location, and public SSH
keys for logging into instances.

Args:
  user: str, The email address of the OS Login user.
  project: str, The project ID associated with the desired profile.
  system_id: str, If supplied, only return profiles associated with the
    given system ID.
  include_security_keys: bool, If true, include security key information.

Returns:
  The login profile for the user.
r   	users/{0})name	projectIdsystemIdN)r>   r?   r@   view)	r.   "OsloginUsersGetLoginProfileRequestr   formatViewValueValuesEnumSECURITY_KEYr,   usersGetLoginProfile)	r0   userr*   	system_idinclude_security_keysprofile_requestmessagerA   ress	            r   rG   OsloginClient.GetLoginProfile?   s    & mmFFO||t!!$'g 
22??!!$'	g ++


+
+G
4CJr   c                f   U(       a^  UR                  5       nU R                  R                  R                  U5      nU R                  R                  UR	                  5       US9nO'U R                  R                  UR	                  5       S9nU R
                  R                  R                  U5        g)zDelete the posix accounts for an account in the current project.

Args:
  project_ref: The oslogin.users.projects resource.
  operating_system: str, 'linux' or 'windows' (case insensitive).
Returns:
  None
)r>   operatingSystemTyper>   N)upperr.   !OsloginUsersProjectsDeleteRequest"OperatingSystemTypeValueValuesEnumRelativeNamer,   users_projectsDelete)r0   project_refoperating_systemos_value
os_messagerL   s         r   DeletePosixAccounts!OsloginClient.DeletePosixAccountsh   s     !'')hMMCC77A ??'')( @ *g ??'') @ +g 	KK%%g.r   c                   U R                   R                  nU R                   R                  UUS9nU(       a  U/O/ nU R                  S:X  a#  U" SR	                  U5      U R
                  UUS9n	OCU(       a  UR                  R                  n
OSn
U" SR	                  U5      U R
                  UUU
S9n	U R                  R                  R                  U	5      nU$ )a  Upload an SSH public key to the user's login profile.

Args:
  user: str, The email address of the OS Login user.
  public_key: str, An SSH public key.
  expiration_time: int, microseconds since epoch.
  include_security_keys: bool, If true, return security key information.
  region: str, The region to which to make sure the key is imported.

Returns:
  The login profile for the user.
keyexpirationTimeUsecr   r=   )parentr?   sshPublicKeyregionsN)rb   r?   rc   rd   rA   )r.   %OsloginUsersImportSshPublicKeyRequestSshPublicKeyr   rC   r*   rD   rE   r,   rF   ImportSshPublicKey)r0   rH   
public_keyexpiration_timerJ   regionimport_requestpublic_key_messagerd   rL   rA   rM   s               r   rg    OsloginClient.ImportSshPublicKey~   s    ( ]]HHN33* 4 , !vhbG||t##D)LL)	g 
11>>##D)LL)g ++


.
.w
7CJr   c                    U R                   R                  SR                  X5      S9nU R                  R                  R                  U5        g)zDelete an SSH public key from the user's login profile.

Args:
  user: str, The email address of the OS Login user.
  fingerprint: str, The fingerprint of the SSH key to delete.
Returns:
  None
users/{0}/sshPublicKeys/{1}rQ   N)r.   &OsloginUsersSshPublicKeysDeleteRequestrC   r,   users_sshPublicKeysrW   )r0   rH   fingerprintrL   s       r   DeleteSshPublicKey OsloginClient.DeleteSshPublicKey   sE     mmBB*11$D C FGKK##**73r   c                    U R                   R                  SR                  X5      S9nU R                  R                  R                  U5      nU$ )zGet an SSH public key from the user's login profile.

Args:
  user: str, The email address of the OS Login user.
  fingerprint: str, The fingerprint of the SSH key to delete.
Returns:
  The requested SSH public key.
ro   rQ   )r.   #OsloginUsersSshPublicKeysGetRequestrC   r,   rq   r+   )r0   rH   rr   rL   rM   s        r   GetSshPublicKeyOsloginClient.GetSshPublicKey   sL     mm??*11$D @ FG
++
)
)
-
-g
6CJr   c                    U R                   R                  X5S9nU R                   R                  SR                  X5      UUS9nU R                  R
                  R                  U5      nU$ )a{  Update an existing SSH public key in a user's login profile.

Args:
  user: str, The email address of the OS Login user.
  fingerprint: str, The fingerprint of the SSH key to update.
  public_key: str, An SSH public key.
  update_mask: str, A mask to control which fields get updated.
  expiration_time: int, microseconds since epoch.

Returns:
  The login profile for the user.
r_   ro   )r>   rc   
updateMask)r.   rf   %OsloginUsersSshPublicKeysPatchRequestrC   r,   rq   Patch)	r0   rH   rr   rh   update_maskri   rl   rL   rM   s	            r   UpdateSshPublicKey OsloginClient.UpdateSshPublicKey   sr     33 4  mmAA*11$D' B G
 ++
)
)
/
/
8CJr   c                    U R                   R                  US9nU R                   R                  SR                  XU5      US9nU R                  R
                  R                  U5      $ )a1  Sign an SSH public key for a given user.

Args:
  user: str, The email address of the OS Login user.
  public_key: str, An SSH public key.
  project_id: str, The project ID associated with the VM.
  region: str, The region where the signed SSH public key may be used.

Returns:
  A signed SSH public key.
)rc   z$users/{0}/projects/{1}/locations/{2})rb   signSshPublicKeyRequest)r.   SignSshPublicKeyRequest0OsloginUsersProjectsZonesSignSshPublicKeyRequestrC   r,   users_projects_zonesSignSshPublicKey)r0   rH   rh   
project_idrj   rl   rL   s          r   r   OsloginClient.SignSshPublicKey   sr     >> ?  mmLL5<<f
 !3	 M G ;;++<<WEEr   c                   [        U5      [        U5      -   [        U5      -   nUS:w  a  [        S5      eUUUUS.n	U R                  S;   a  XyS'   SSR                  X#5      0n
U R                  S:X  a=  U R                  R
                  " S0 U	D6nU R                  R                  " S0 U
DS	U0D6nOU R                  S
:X  a=  U R                  R                  " S0 U	D6nU R                  R                  " S0 U
DSU0D6nO<U R                  R                  " S0 U	D6nU R                  R                  " S0 U
DSU0D6nU R                  R                  R                  U5      $ )a  Sign an SSH public key scoped to a given instance.

Args:
  public_key: str, An SSH public key.
  project_id: str, The project ID associated with the VM.
  region: str, The region where the signed SSH public key may be used.
  service_account: str, The service account associated with the VM.
  compute_instance: str, The Compute instance to sign the SSH public key
    for. Only one of compute_instance, app_engine_instance or
    cloud_run_deployment should be specified. The format for this field is
    'projects/{project}/zones/{zone}/instances/{instance_id}'.
  app_engine_instance: str, The App Engine instance to sign the SSH public
    key for. Only one of compute_instance, app_engine_instance or
    cloud_run_deployment should be specified. The format for this field is
    'services/{service}/versions/{version}/instances/{instance}'.
  cloud_run_deployment: str, The Cloud Run deployment to sign the SSH public
    key for. Only one of compute_instance, app_engine_instance, or
    cloud_run_deployment should be specified. The format for this field is
    'projects/{project}/locations/{location}/services/{service}'.

Raises:
  ValueError: If more than one or none of compute_instance,
    app_engine_instance, and cloud_run_service are specified.

Returns:
  A signed SSH public key.
   z]Exactly one of compute_instance, app_engine_instance, or cloud_run_service must be specified.)rc   serviceAccountcomputeInstanceappEngineInstance)r
   r   cloudRunResourcerb   zprojects/{0}/locations/{1}r
   DgoogleCloudOsloginControlplaneRegionalV1alphaSignSshPublicKeyRequestr   CgoogleCloudOsloginControlplaneRegionalV1betaSignSshPublicKeyRequestr   r   )r:   
ValueErrorr   rC   r.   DGoogleCloudOsloginControlplaneRegionalV1alphaSignSshPublicKeyRequest/OsloginProjectsLocationsSignSshPublicKeyRequestCGoogleCloudOsloginControlplaneRegionalV1betaSignSshPublicKeyRequestr   r,   projects_locationsr   )r0   rh   r   rj   service_accountcompute_instanceapp_engine_instancecloud_run_deploymentnumber_of_instancesrequest_kwargsmessage_kwargsrequestrL   s                r   SignSshPublicKeyForInstance)OsloginClient.SignSshPublicKeyForInstance   s   L 	
"
#	$
#
$	% 
 a1 
 #)+0	N ||,,+?'(.55jIN ||y bb g MM OVg 
	!aa g MM NUg
 55 g MM ")g ;;))::7CCr   c                    U(       a  U/O/ nU R                   R                  US9nSR                  X5      nU R                   R                  XeS9nU R                  R
                  R                  U5      nU$ )a  Provision a POSIX account for a given user.

ProvisionPosixAccount is a regional read if the user has the correct
POSIX account. Otherwise, it is a global write that waits on replication
to the provided region before returning success.

Args:
  user: str, The email address of the OS Login user.
  project_id: str, The project ID associated with the VM.
  region: str, The regions to wait to be written to before returning a
    response.

Returns:
  The user's provisioned POSIX account for the given project.
)rd   zusers/{0}/projects/{1})r>   provisionPosixAccountRequest)r.   ProvisionPosixAccountRequestrC   0OsloginUsersProjectsProvisionPosixAccountRequestr,   rV   ProvisionPosixAccount)	r0   rH   r   rj   rd   provision_bodyr>   provision_requestrM   s	            r   r   #OsloginClient.ProvisionPosixAccountQ  sz    $ !vhbG]]???PN#**4<D 	FF 	G 	
  ++
$
$
:
:;L
MCJr   )r,   r.   r*   r   )NNFr&   )NFN)rj   zOptional[str]) NNN)r   r   r   r   r   r2   r7   r5   rG   r\   rg   rs   rw   r~   r   r   r   r   r   r   r   r#   r#   ,   s    - ;?,1'R/4 !"2 2h4 *.2F8 RDj 7;&3 r   r#   N)r   
__future__r   typingr   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.callioper   googlecloudsdk.corer   core_exceptionsr	   ReleaseTrackALPHABETAGAr'   r   Errorr   r    objectr#   r   r   r   <module>r      s      "  , 1 ( = *  &&	  %%x  ##T+
4M,, MO. OCF Cr   