
    M                         S r SSKJr  SSKJr  SSKJr  SSK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SKJr  SSKJr  SSKJr   " S S\
R2                  5      rS rg)z>A simple auth command to bootstrap authentication with oauth2.    )absolute_import)division)unicode_literalsN)service_account)base)
exceptions)log)
properties)
console_io)store)encoding)filesc                   .    \ rS rSrSr\S 5       rS rSrg)ActivateServiceAccount$   a  Authorize access to Google Cloud with a service account.

To allow `gcloud` (and other tools in Google Cloud CLI) to use service account
credentials to make requests, use this command to import these credentials
from a file that contains a private authorization key, and activate them for
use in `gcloud`. {command} serves the same function as `gcloud auth login`
but uses a service account rather than Google user credentials.

For more information on authorization and credential types, see:
[](https://cloud.google.com/sdk/docs/authorizing).

_Key File_

To obtain the key file for this command, use either the [Google Cloud
Console](https://console.cloud.google.com) or `gcloud iam
service-accounts keys create`. The key file can be .json (preferred) or
.p12 (legacy) format. In the case of legacy .p12 files, a separate password
might be required and is displayed in the Console when you create the key.

_Credentials_

Credentials will also be activated (similar to running
`gcloud config set account [ACCOUNT_NAME]`).

If a project is specified using the `--project` flag, the project is set in
active configuration, which is the same as running
`gcloud config set project [PROJECT_NAME]`. Any previously active credentials,
will be retained (though no longer default) and can be
displayed by running `gcloud auth list`.

If you want to delete previous credentials, see `gcloud auth revoke`.

_Note:_ Service accounts use client quotas for tracking usage.

## EXAMPLES

To authorize `gcloud` to access Google Cloud using an existing
service account while also specifying a project, run:

          $ {command} SERVICE_ACCOUNT@DOMAIN.COM \
              --key-file=/path/key.json --project=PROJECT_ID
c                     U R                  SSSS9  U R                  SSSS9  U R                  5       nUR                  S	S
S9  UR                  SSSS9  g)zSet args for serviceauth.account?z&E-mail address of the service account.)nargshelpz
--key-filezPath to the private key file.T)r   required--password-filezbPath to a file containing the password for the service account private key (only for a .p12 file).)r   z--prompt-for-password
store_truezSPrompt for the password for the service account private key (only for a .p12 file).)actionr   N)add_argumentadd_mutually_exclusive_group)parsergroups     ,lib/surface/auth/activate_service_account.pyArgsActivateServiceAccount.ArgsP   s     	E  G
=!%  ' //1E	(7  9 
.|C  E    c                    [        UR                  5      u  p#U(       a  [        R                  " U5      nUR                  (       d  UR
                  (       a  [        R                  " SS5      eUR                  nUR                  (       a'  UR                  U:w  a  [        R                  " SS5      eOUR                  nU(       d  [        R                  " SS5      eSnUR                  (       a0   [        R                  " UR                  5      R                  5       nO'UR
                  (       a  [         R"                  " S5      n[        R$                  " X%US9n [&        R(                  " XT5        UR4                  nU(       a9  [6        R8                  " [6        R:                  R<                  R4                  U5        [.        R>                  RA                  S	RC                  U5      5        g! [        R                   a  n[        R                  " SU5      eSnAff = f! [*        R,                   a%  n[.        R0                  R3                  U5        e SnAff = f)
z#Create service account credentials.r   z8A .json service account key does not require a password.ACCOUNTz|The given account name does not match the account name in the key file.  This argument can be omitted when using .json keys.z+An account is required when using .p12 keysNz
Password: )passwordz0Activated service account credentials for: [{0}])"_IsJsonFilekey_fileauth_service_account CredentialsFromAdcDictGoogleAuthpassword_fileprompt_for_passwordc_excInvalidArgumentExceptionservice_account_emailr   RequiredArgumentExceptionr   ReadFileContentsstripErrorUnknownArgumentExceptionr   PromptPasswordCredentialsFromP12Keyc_storeActivateCredentialscreds_exceptionsTokenRefreshErrorr	   file_only_logger	exceptionprojectr
   PersistPropertyVALUEScorestatusPrintformat)	selfargsfile_contentis_jsoncredr   r%   er<   s	            r   RunActivateServiceAccount.Runa   s    (6L!BB
d			t77,,FH 	H **g	$,,'1,,IJ 	J
 g--DF 	Fh				E++D,>,>?EEG( ##,,\:!77
(4d!!'0
 llG  !2!2!7!7!?!?IJJGfWo'% {{ 	E../@!D
D	E -- 	$$Q's0   1.G. H! .HHH!I5 II N)	__name__
__module____qualname____firstlineno____doc__staticmethodr    rI   __static_attributes__rK   r"   r   r   r   $   s#    )V E E -'r"   r   c                 0   [         R                  " U SS9n [        R                  " [        R
                  " U5      5      S4$ ! [         aH  nU R                  S5      (       a%  [        R                  " SR                  X5      5      e SnAUS4$ SnAff = f)z9Check and validate if given filename is proper json file.T)binaryz.jsonz!Could not read json file {0}: {1}NF)r   ReadFromFileOrStdinjsonloadsr   Decode
ValueErrorendswithr(   BadCredentialFileExceptionrB   )filenamecontentrH   s      r   r&   r&      s    **8DA'C::hoog./55	 C!! ;;
-
4
4X
AC C " 
%	Cs   +A 
B;BB)rP   
__future__r   r   r   rV   googlecloudsdk.api_lib.authr   r(   googlecloudsdk.callioper   r   r,   googlecloudsdk.corer	   r
   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr8   r   r6   googlecloudsdk.core.utilr   r   SilentCommandr   r&   rK   r"   r   <module>rf      sO     E &  '  O ( 7 # * 2 J < - *j'T// j'Z	r"   