
                          N   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rSr " S S\R(                  5      r " S S\5      r " S S\5      r " S S\
R.                  \
R0                  5      r " S S\R4                  5      rSS jrg)z,google-auth p12 service account credentials.    )absolute_import)division)unicode_literalsN)_helpers)base)service_account)
exceptions)log)encoding
notasecretz2.5c                       \ rS rSrSrSrg)Error#   z!Base Error class for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       :lib/googlecloudsdk/core/credentials/p12_service_account.pyr   r   #   s    )r   r   c                       \ rS rSrSrSrg)MissingRequiredFieldsError'   zDError when required fields are missing to construct p12 credentials.r   Nr   r   r   r   r   r   '   s    Lr   r   c                       \ rS rSrSrSrg)MissingDependencyError+   z7Error when missing a dependency to use p12 credentials.r   Nr   r   r   r   r   r   +   s    ?r   r   c                   H    \ rS rSrSrS r\S 5       rS r\	S	S j5       r
Srg)
PKCS12Signer/   z@Signer for a p12 service account key based on pyca/cryptography.c                     Xl         g N_key)selfkeys     r   __init__PKCS12Signer.__init__2   s    Ir   c                     g r$   r   r'   s    r   key_idPKCS12Signer.key_id6   s    r   c                     [         R                  " U5      nSSKJn  U R                  R                  UUR                  UR                  5      $ )Nr   )_cryptography_rsa)r   to_bytesgoogle.auth.cryptr0   r&   sign_PADDING_SHA256)r'   messager0   s      r   r3   PKCS12Signer.sign:   s?    (G399>>""!!# #r   Nc                     AS U 5       u  p4SSK Jn  SSKJn  UR	                  X4UR                  5       S9u  n  nU " U5      $ )Nc              3   N   #    U  H  n[         R                  " U5      v   M     g 7fr$   )r   r1   ).0ks     r   	<genexpr>+PKCS12Signer.from_string.<locals>.<genexpr>E   s     F+QH--a00+s   #%r   )pkcs12)backends)backend),cryptography.hazmat.primitives.serializationr>   cryptography.hazmatr?   load_key_and_certificatesdefault_backend)	clskey_stringsr-   
key_stringpasswordr>   r?   r(   _s	            r   from_stringPKCS12Signer.from_stringB   sK    F+FJC,00h&>&>&@ 1 BICAs8Or   r%   r$   )r   r   r   r   r   r)   propertyr-   r3   classmethodrJ   r   r   r   r   r!   r!   /   s7    H  #  r   r!   c                   R    \ rS rSrSrSr\S 5       r\S 5       r\	 S	S j5       r
Srg)
CredentialsM   a  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                     U R                   $ r$   )_private_key_pkcs12r,   s    r   private_key_pkcs12Credentials.private_key_pkcs12_   s    ###r   c                     U R                   $ r$   )_private_key_passwordr,   s    r   private_key_password Credentials.private_key_passwordc   s    %%%r   Nc                 *   U=(       d    [         n[        R                  X45      nU R                   Vs/ s H  oUU;  d  M
  UPM     nnU(       a)  [	        SR                  SR                  U5      5      5      eU " U40 UD6nXl        X'l        U$ s  snf )NzMissing fields: {}.z, )	_DEFAULT_PASSWORDr!   rJ   _REQUIRED_FIELDSr   formatjoinrU   rY   )rE   rG   rH   kwargssignerfmissing_fieldscredss           r   %from_service_account_pkcs12_keystring1Credentials.from_service_account_pkcs12_keystringg   s    
 ,,H%%z&<=F!$!5!5I!5A&a!5NI&'<'C'C
))N
#(% & &!&!E
 !+"*L Js   	BBr   r$   )r   r   r   r   r   r^   rL   rV   rZ   rM   rf   r   r   r   r   rO   rO   M   sO     F$ $ & &  6: r   rO   c                 F   [         R                  " S5         [        R                  " X40 UD6$ ! [         ag    [
        R                  " [        R                  S5      (       d  [        SR                  [        5      5      e[        SR                  [        5      5      ef = f)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
   warningrO   rf   ImportErrorr   GetEncodedValueosenvironr   r_   _PYCA_CRYPTOGRAPHY_MIN_VERSION)rG   rH   ra   s      r   CreateP12ServiceAccountrp   ~   s    ++ D E4<<( &( (	 4##BJJ0NOO":
 F124 4 #: F124 44s
   / A1B r$   )r   
__future__r   r   r   rm   google.authr   r2   r   
crypt_basegoogle.oauth2r   googlecloudsdk.corer	   r
   googlecloudsdk.core.utilr   r]   ro   r   r   r   SignerFromServiceAccountMixinr!   rO   rp   r   r   r   <module>ry      s    3 &  ' 	   0 ) * # -  !& *J *M M@U @:$$j&H&H <./-- .b4r   