
                             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  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\	R,                  5      rg)z/Sign a user input file using a MAC signing key.    )absolute_import)division)unicode_literals)
exceptions)base)crc32c)e2e_integrity)flags)log)
console_io)filesc                   F    \ rS rSrSr\S 5       rS rS rS r	S r
S rS	rg
)MacSign!   a2  Sign a user input file using a MAC key version.

Creates a digital signature of the input file using the provided
MAC signing key version and saves the base64 encoded signature.

The required flag `signature-file` indicates the path to store signature.

By default, the command performs integrity verification on data sent to and
received from Cloud KMS. Use --skip-integrity-verification to disable
integrity verification.

## EXAMPLES
The following command will read the file '/tmp/my/file.to.sign', and sign it
using the symmetric MAC CryptoKey `dont-panic` Version 3, and save the
signature in base64 format to '/tmp/my/signature'.

  $ {command} \
  --location=us-central1 \
  --keyring=hitchhiker \
  --key=dont-panic \
  --version=3 \
  --input-file=/tmp/my/file.to.sign \
  --signature-file=/tmp/my/signature

c                     [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        [         R
                  " U 5        g )Nzto use for signing.zto use for signingzto signz	to output)r
   AddKeyResourceFlagsAddCryptoKeyVersionFlagAddInputFileFlagAddSignatureFileFlagAddSkipIntegrityVerification)parsers    lib/surface/kms/mac_sign.pyArgsMacSign.Args<   sR    	f&;<	!!&*>?	69-	v{3	&&v.    c                     [         R                  " USS9n[        U5      U:  a%  [        R                  " SR                  X5      5      eU$ )NT)binaryz<The file [{0}] is larger than the maximum size of {1} bytes.)r   ReadFromFileOrStdinlenr   BadFileExceptionformat)selfpath	max_bytesdatas       r   _ReadFileOrStdinMacSign._ReadFileOrStdinD   sK    ))$t<D
4y9''
H
O
O    Kr   c                 $    UR                   (       + $ )N)skip_integrity_verification)r"   argss     r   _PerformIntegrityVerification%MacSign._PerformIntegrityVerificationL   s    ////r   c                     U R                  UR                  SS9n[        R                  " 5       nUR                  [        R                  " U5      R                  5       S9nU R                  U5      (       a,  [        R                  " U5      nUR                  X&S9Ul        U$ UR                  US9Ul        U$ ! [         a5  n[        R                  " SR                  UR                  U5      5      eS nAff = f)Ni   )r$   z$Failed to read input file [{0}]: {1})name)r%   
dataCrc32c)r%   )r&   
input_fileEnvironmentErrorr   r    r!   cloudkms_baseGetMessagesModuleJCloudkmsProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersionsMacSignRequestr
   ParseCryptoKeyVersionNameRelativeNamer+   r   Crc32cMacSignRequestmacSignRequest)r"   r*   r%   emessagesreqdata_crc32cs          r   _CreateMacSignRequestMacSign._CreateMacSignRequestO   s    M""4??e"Dd
 ..0H

]
],,T2??A ^ CC ))$//MM$'k#22 3 -c
 J $222=cJ  M''
0
7
7
KM MMs   B: :
C90C44C9c                    UR                   UR                   :w  a?  [        R                  " [        R                  " UR                   UR                   5      5      eUR                  (       d)  [        R
                  " [        R                  " 5       5      e[        R                  " UR                  UR                  5      (       d)  [        R
                  " [        R                  " 5       5      eg)z-Verifies integrity fields in MacSignResponse.N)r.   r	   ResourceNameVerificationError#GetResourceNameMismatchErrorMessageverifiedDataCrc32c$ClientSideIntegrityVerificationError'GetRequestToServerCorruptedErrorMessager   Crc32cMatchesmac	macCrc32c*GetResponseFromServerCorruptedErrorMessage)r"   r<   resps      r   _VerifyResponseIntegrityFields&MacSign._VerifyResponseIntegrityFieldsd   s     xx49977

;
;hh		#$ $
 "">>

?
?
AC C $..99>>

B
B
DF F :r   c                    [         R                  " 5       nU R                  U5      n UR                  R	                  U5      nU R                  U5      (       a  U R                  UW5         [        R                  " UR                  WR                  SSSS9  g ! [
        R                   a   n[        R                  " U5         S nANS nAff = f! [        R                    a  n["        R$                  " U5      eS nAff = f)NT)	overwriter   private)r2   GetClientInstancer>   8projects_locations_keyRings_cryptoKeys_cryptoKeyVersionsr   apitools_exceptionsHttpBadRequestErrorr	   ProcessHttpBadRequestErrorr+   rK   r   WriteToFileOrStdoutsignature_filerG   r   Errorr   r    )r"   r*   clientr<   rJ   errorr:   s          r   RunMacSign.Runw   s    ,,.F

$
$T
*C6

I
I73<  ))$//
))#t4+	



(( 22 6..u556 ;; +''**+s/   B ,,C C-CCC?$C::C? N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r&   r+   r>   rK   rZ   __static_attributes__r\   r   r   r   r   !   s5    4 / /0*F&+r   r   N)ra   
__future__r   r   r   apitools.base.pyr   rR   googlecloudsdk.api_lib.cloudkmsr   r2   googlecloudsdk.calliopegooglecloudsdk.command_lib.kmsr   r	   r
   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   Commandr   r\   r   r   <module>rm      sC    6 &  ' > A ( . 1 8 0 # 2 *m+dll m+r   