
    M                         d Z ddlmZ ddlmZ ddlmZ ddl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dlmZ ddlmZ ddlmZ  G d de
j2                        Zd Zy)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                   &    e Zd ZdZed        Zd Zy)ActivateServiceAccounta  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                     | j                  ddd       | j                  ddd       | j                         }|j                  d	d
       |j                  ddd       y)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ArgszActivateServiceAccount.ArgsP   s     	E  G
=!%  ' //1E	(7  9 
.|C  E    c                 Z   t        |j                        \  }}|rt        j                  |      }|j                  s|j
                  rt        j                  dd      |j                  }|j                  r|j                  |k7  rt        j                  dd      |j                  }|st        j                  dd      d}|j                  r/	 t        j                  |j                        j                         }n!|j
                  rt!        j"                  d      }t        j$                  |||      }	 t'        j(                  ||       |j4                  }|r8t7        j8                  t6        j:                  j<                  j4                  |       t.        j>                  jA                  d	jC                  |             y# t        j                  $ r}t        j                  d|      d}~ww xY w# t*        j,                  $ r%}t.        j0                  j3                  |        d}~ww xY w)
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   Runz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!!'40
 llG  !2!2!7!7!?!?IJJGfWo'% {{ 	E../@!D
D	E -- 	$$Q's0   -G 6G2 G/G**G/2H* H%%H*N)__name__
__module____qualname____doc__staticmethodr   rG    r    r   r   r   $   s#    )V E E -'r    r   c                 $   t        j                  | d      }	 t        j                  t	        j
                  |            dfS # t        $ rC}| j                  d      r%t        j                  dj                  | |            Y d}~|dfS d}~ww xY w)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&   BadCredentialFileExceptionr@   )filenamecontentrF   s      r   r$   r$      s    **8DA'C::hoog./55	 C! ;;
-
4
4Xq
AC C " 
%	Cs   )A 	B6B

B)rK   
__future__r   r   r   rQ   googlecloudsdk.api_lib.authr   r&   googlecloudsdk.callioper   r   r*   googlecloudsdk.corer	   r
   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr6   r   r4   googlecloudsdk.core.utilr   r   SilentCommandr   r$   rM   r    r   <module>ra      sO     E &  '  O ( 7 # * 2 J < - *j'T// j'Z	r    