
    v(                     n   d 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 ej"                   ej$                  ej&                  j(                  ej&                  j*                  ej&                  j,                         G d dej.                                      Zy)zCreate a key.    )absolute_import)division)unicode_literals)base)
exceptions)flags)maps)resource_args)labels_utilc                   ,    e Zd ZdZed        Zd Zd Zy)Createa  Create a new key.

  Creates a new key within the given keyring.

  The flag `--purpose` is always required when creating a key.
  The flag `--default-algorithm` is required when creating a symmetric signing
  key, an asymmetric key, or an external key. Algorithm and purpose should be
  compatible.

  The optional flags `--rotation-period` and `--next-rotation-time` define a
  rotation schedule for the key. A schedule can also be defined
  by the `--create-rotation-schedule` command.

  The flag `--next-rotation-time` must be in ISO 8601 or RFC3339 format,
  and `rotation-period` must be in the form INTEGER[UNIT], where units
  can be one of seconds (s), minutes (m), hours (h) or days (d).

  The optional flag `--protection-level` specifies the physical environment
  where crypto operations with the key happen. The default is ``software''; use
  ``hsm'' to create a hardware-backed key, ``external'' to create an
  externally backed key, or ``external-vpc'' to create an external key over vpc.

  The optional flag `--labels` defines a user specified key/value pair for the
  given key.

  The flag `--skip-initial-version-creation` creates a CryptoKey with no
  versions. If you import into the CryptoKey, or create a new version in that
  CryptoKey, there will be no primary version until one is set using the
  `--set-primary-version` command. You must include
  `--skip-initial-version-creation` when creating a CryptoKey with protection
  level ``external'' or ``external-vpc''.

  The optional flag `--import-only` restricts the key to imported key versions
  only. To do so, the flag `--skip-initial-version-creation` must also be set.

  The optional flag `--destroy-scheduled-duration` defines the destroy schedule
  for the key, and must be in the form INTEGER[UNIT], where units can be one of
  seconds (s), minutes (m), hours (h) or days (d).

  The flag `--crypto-key-backend` defines the resource name for the
  backend where the key resides. Required for ``external-vpc'' keys.

  The optional flag `--allowed-access-reasons` defines the Key Access
  Justifications Policy for the key, and is specified as a comma separated list
  of zero or more justification codes defined in
  https://cloud.google.com/assured-workloads/key-access-justifications/docs/justification-codes.
  The key must be enrolled in Key Access Justifications to use this flag.

  ## EXAMPLES

  The following command creates a key named ``frodo'' with protection level
  ``software'' within the keyring ``fellowship'' and location ``us-east1'':

    $ {command} frodo \
        --location=us-east1 \
        --keyring=fellowship \
        --purpose=encryption

  The following command creates a key named ``strider'' with protection level
  ``software'' within the keyring ``rangers'' and location ``global'' with a
  specified rotation schedule:

    $ {command} strider \
        --location=global --keyring=rangers \
        --purpose=encryption \
        --rotation-period=30d \
        --next-rotation-time=2017-10-12T12:34:56.1234Z

  The following command creates a raw encryption key named ``foo'' with
  protection level ``software'' within the keyring ``fellowship'' and location
  ``us-east1'' with two specified labels:

    $ {command} foo \
        --location=us-east1 \
        --keyring=fellowship \
        --purpose=raw-encryption \
        --default-algorithm=aes-128-cbc
        --labels=env=prod,team=kms

  The following command creates an asymmetric key named ``samwise'' with
  protection level ``software'' and default algorithm ``ec-sign-p256-sha256''
  within the keyring ``fellowship'' and location ``us-east1'':

    $ {command} samwise \
        --location=us-east1 \
        --keyring=fellowship \
        --purpose=asymmetric-signing \
        --default-algorithm=ec-sign-p256-sha256

  The following command creates a key named ``gimli'' with protection level
  ``hsm'' and default algorithm ``google-symmetric-encryption'' within the
  keyring ``fellowship'' and location ``us-east1'':

    $ {command} gimli \
        --location=us-east1 \
        --keyring=fellowship \
        --purpose=encryption \
        --protection-level=hsm

  The following command creates a key named ``legolas'' with protection level
  ``external'' and default algorithm ``external-symmetric-encryption'' within
  the keyring ``fellowship'' and location ``us-central1'':

    $ {command} legolas \
        --location=us-central1 \
        --keyring=fellowship \
        --purpose=encryption \
        --default-algorithm=external-symmetric-encryption \
        --protection-level=external
        --skip-initial-version-creation

  The following command creates a key named ``bilbo'' with protection level
  ``external-vpc'' and default algorithm ``external-symmetric-encryption'' and
  an EkmConnection of ``eagles'' within the keyring ``fellowship'' and location
  ``us-central1'':

    $ {command} bilbo \
        --location=us-central1 \
        --keyring=fellowship \
        --purpose=encryption \
        --default-algorithm=external-symmetric-encryption \
        --protection-level=external-vpc
        --skip-initial-version-creation
        --crypto-key-backend="projects/$(gcloud config get project)/
        locations/us-central1/ekmConnections/eagles"

  The following command creates a key named ``arwen'' with protection level
  ``software'' within the keyring ``fellowship'' and location ``us-east1'' with
  a Key Access Justifications policy that allows access reasons
  ``customer-initiated-access'' and ``google-initiated-system-operation'':

    $ {command} arwen \
        --location=us-east1 \
        --keyring=fellowship \
        --purpose=encryption \
        --allowed-access-reasons=customer-initiated-access,google-initiated-system-operation
  c                    t        j                  | dd       t        j                  |        t        j                  |        t        j
                  |        t        j                  |        | j                  dt        t        j                  j                               dd       | j                  j                  t        j                         t        j                   |        t        j"                  |        t        j$                  |        t        j&                  |        t        j(                  |        t        j*                  |        y )NTkeyz	--purposezThe "purpose" of the key.)choicesrequiredhelp)r
   AddKmsKeyResourceArgForKMSr   AddRotationPeriodFlagAddNextRotationTimeFlag!AddSkipInitialVersionCreationFlagr   AddCreateLabelsFlagsadd_argumentsortedr	   PURPOSE_MAPkeysdisplay_infoAddCacheUpdaterKeyRingCompleterAddProtectionLevelFlagAddDefaultAlgorithmFlagAddImportOnlyFlagAddDestroyScheduledDurationFlagAddCryptoKeyBackendFlagAddAllowedAccessReasonsFlag)parsers    lib/surface/kms/keys/create.pyArgszCreate.Args   s    ,,VT5A	'	!!&)	++F3$$V,
t'',,./(	  *
 ''(>(>?	  (	!!&)	F#	))&1	!!&)	%%f-    c                 "   t        j                         }t        j                  |j                     }t        j
                  |   }|j                  sT|j                  dk7  r>t        j                  dj                  |j                  dj                  |                  d|_        |j                  |vr>t        j                  dj                  |j                  dj                  |                  |j                  j                  j                         }|j                         }|j                  |j!                         |j#                         |j%                  ||j'                  t        j(                  j+                  |j,                        t        j.                  j+                  |j                              t1        j2                  ||j$                  j4                        |j6                  |j8                        |j:                        }t=        j>                  ||j@                         t=        jB                  ||j@                         t=        jD                  ||j@                         t=        jF                  ||j@                         |S )	N
encryptionzm--default-algorithm needs to be specified when creating a key with --purpose={}. The valid algorithms are: {}z, zgoogle-symmetric-encryptionzbDefault algorithm and purpose are incompatible. Here are the valid algorithms for --purpose={}: {})protectionLevel	algorithm)purposeversionTemplatelabels
importOnlycryptoKeyBackend)parentcryptoKeyId	cryptoKeyskipInitialVersionCreation)$cloudkms_baseGetMessagesModuler	   r   r-   VALID_ALGORITHMS_MAPdefault_algorithmkms_exceptionsArgumentErrorformatjoinCONCEPTSr   ParseParent8CloudkmsProjectsLocationsKeyRingsCryptoKeysCreateRequestRelativeNameName	CryptoKeyCryptoKeyVersionTemplatePROTECTION_LEVEL_MAPPERGetEnumForChoiceprotection_levelALGORITHM_MAPPERr   ParseCreateArgsLabelsValueimport_onlycrypto_key_backendskip_initial_version_creationr   SetNextRotationTimer4   SetRotationPeriodSetDestroyScheduledDuration SetKeyAccessJustificationsPolicy)selfargsmessagesr-   valid_algorithmscrypto_key_ref
parent_refreqs           r&   _CreateRequestzCreate._CreateRequest   s$   ..0Ht||,G009
 !!		%**::@&dii(89;;< 	<  =d %55((,,2F4<<3799=M3N-PQ Q
 ]]&&,,.N&&(J

K
K&&("'')$$$== $ < < M M))!+//@@**, > -
 ..t/7/A/A/M/MO''!44 % 
6 $(#E#E L GC  
dCMM2	D#--0	%%dCMM:	**4?Jr(   c                 ~    t        j                         }|j                  j                  | j	                  |            S )N)r6   GetClientInstance&projects_locations_keyRings_cryptoKeysr   rZ   )rS   rT   clients      r&   Runz
Create.Run   s7    ,,.F88??D!# #r(   N)__name__
__module____qualname____doc__staticmethodr'   rZ   r_    r(   r&   r   r      s(    
HT . .&.`#r(   r   N)rc   
__future__r   r   r   googlecloudsdk.api_lib.cloudkmsr   r6   googlecloudsdk.calliopegooglecloudsdk.command_lib.kmsr   r:   r   r	   r
   $googlecloudsdk.command_lib.util.argsr   UniverseCompatibleReleaseTracksReleaseTrackALPHABETAGACreateCommandr   re   r(   r&   <module>rr      s     &  ' A ( G 0 / 8 < T..33T5F5F5I5IR#T R# R#r(   