
                          L   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ZdZ G d dej(                        Z G d de      Z G d de      Z G d de
j.                  e
j0                        Z G d dej4                        ZddZy)z,google-auth p12 service account credentials.    )absolute_import)division)unicode_literalsN)_helpers)base)service_account)
exceptions)log)encoding
notasecretz2.5c                       e Zd ZdZy)Errorz!Base Error class for this module.N__name__
__module____qualname____doc__     :lib/googlecloudsdk/core/credentials/p12_service_account.pyr   r   #   s    )r   r   c                       e Zd ZdZy)MissingRequiredFieldsErrorzDError when required fields are missing to construct p12 credentials.Nr   r   r   r   r   r   '   s    Lr   r   c                       e Zd ZdZy)MissingDependencyErrorz7Error when missing a dependency to use p12 credentials.Nr   r   r   r   r   r   +   s    ?r   r   c                   >    e Zd ZdZd Zed        Zd Zedd       Z	y)PKCS12Signerz@Signer for a p12 service account key based on pyca/cryptography.c                     || _         y N)_key)selfkeys     r   __init__zPKCS12Signer.__init__2   s	    DIr   c                      y r   r   r    s    r   key_idzPKCS12Signer.key_id6   s    r   c                     t        j                  |      }ddlm} | j                  j                  ||j                  |j                        S )Nr   )_cryptography_rsa)r   to_bytesgoogle.auth.cryptr'   r   sign_PADDING_SHA256)r    messager'   s      r   r*   zPKCS12Signer.sign:   s?    (G399>>""!!# #r   Nc                     ~d |D        \  }}ddl m} ddlm} |j	                  |||j                               \  }}} | |      S )Nc              3   F   K   | ]  }t        j                  |        y wr   )r   r(   ).0ks     r   	<genexpr>z+PKCS12Signer.from_string.<locals>.<genexpr>E   s     F+QH--a0+s   !r   )pkcs12)backends)backend),cryptography.hazmat.primitives.serializationr3   cryptography.hazmatr4   load_key_and_certificatesdefault_backend)	clskey_stringsr%   
key_stringpasswordr3   r4   r!   _s	            r   from_stringzPKCS12Signer.from_stringB   sO    F+FJC,00Hh&>&>&@ 1 BICAs8Or   r   )
r   r   r   r   r"   propertyr%   r*   classmethodr?   r   r   r   r   r   /   s7    H  #  r   r   c                   H    e Zd ZdZdZed        Zed        Ze	 dd       Z	y)Credentialsa  google-auth service account credentials using p12 keys.

  p12 keys are not supported by the google-auth service account credentials.
  gcloud uses oauth2client to support p12 key users. Since oauth2client was
  deprecated and bundling it is security concern, we decided to support p12
  in gcloud codebase. We prefer not adding it to the google-auth library
  because p12 is not supported from the beginning by google-auth. GCP strongly
  suggests users to use the JSON format. gcloud has to support it to not
  break users.

  oauth2client uses PyOpenSSL to handle p12 keys. PyOpenSSL deprecated
  p12 support from version 20.0.0 and encourages to use pyca/cryptography for
  anything other than TLS connections.
  )service_account_email	token_uriscopesc                     | j                   S r   )_private_key_pkcs12r$   s    r   private_key_pkcs12zCredentials.private_key_pkcs12_   s    ###r   c                     | j                   S r   )_private_key_passwordr$   s    r   private_key_passwordz Credentials.private_key_passwordc   s    %%%r   Nc                    |xs t         }t        j                  ||f      }| j                  D cg c]	  }||vs| }}|r)t	        dj                  dj                  |                   | |fi |}||_        ||_        |S c c}w )NzMissing fields: {}.z, )	_DEFAULT_PASSWORDr   r?   _REQUIRED_FIELDSr   formatjoinrH   rK   )r:   r<   r=   kwargssignerfmissing_fieldscredss           r   %from_service_account_pkcs12_keystringz1Credentials.from_service_account_pkcs12_keystringg   s    
 ,,H%%z8&<=F!$!5!5I!5A&a!5NI&'<'C'C
))N
#(% & &!&!E
 !+E"*EL Js
   	BBr   )
r   r   r   r   rO   r@   rI   rL   rA   rW   r   r   r   rC   rC   M   sO     F$ $ & &  6: r   rC   c                 6   t        j                  d       	 t        j                  | |fi |S # t        $ ra t        j                  t        j                  d      st        dj                  t                    t        dj                  t                    w xY w)zCCreates a service account from a p12 key and handles import errors.z.p12 service account keys are not recommended unless it is necessary for backwards compatibility. Please switch to a newer .json service account key for this account.CLOUDSDK_PYTHON_SITEPACKAGESa  pyca/cryptography is not available. Please install or upgrade it to a version >= {} and set the environment variable CLOUDSDK_PYTHON_SITEPACKAGES to 1. If that does not work, see https://developers.google.com/cloud/sdk/crypto for details or consider using .json private key instead.zpyca/cryptography is not available or the version is < {}. Please install or upgrade it to a newer version. See https://developers.google.com/cloud/sdk/crypto for details or consider using .json private key instead.)r
   warningrC   rW   ImportErrorr   GetEncodedValueosenvironr   rP   _PYCA_CRYPTOGRAPHY_MIN_VERSION)r<   r=   rR   s      r   CreateP12ServiceAccountr`   ~   s    ++ D E4<<H( &( (	 4##BJJ0NO":
 F124 4 #: F124 44s
   . A*Br   )r   
__future__r   r   r   r]   google.authr   r)   r   
crypt_basegoogle.oauth2r   googlecloudsdk.corer	   r
   googlecloudsdk.core.utilr   rN   r_   r   r   r   SignerFromServiceAccountMixinr   rC   r`   r   r   r   <module>ri      s    3 &  ' 	   0 ) * # -  !& *J *M M@U @:$$j&H&H <./-- .b4r   