
    ,                         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 rS rS rS rS rS rS rg)zHelpers for create commands.    )absolute_import)division)unicode_literals)
exceptions)base)certificate_utils)request_utils)flags)resource_args)labels_utilc                 `   [         R                  " U S5        [         R                  " U S5        [         R                  " U S5        U R                  R                  R	                  5       n[         R
                  " USSS9  U R                  R                  R	                  5       nU(       aJ  UR                  UR                  :w  a0  [        R                  " SSR                  UR                  5      5      e[        U S5      (       a$  U R                  R                  R	                  5       OS	nU R                  R                  R	                  5       nU(       aU  UR                  5       R                  5       UR                  5       R                  5       :w  a  [        R                  " S
S5      eXU4$ )zParses, validates and returns the resource args from the CLI.

Args:
  args: The parsed arguments from the command-line.

Returns:
  Tuple containing the Resource objects for (CA, source CA, issuer).
kms_key_versionissuer_poolfrom_caCERTIFICATE_AUTHORITYv1)version--kms-key-versionzGKMS key must be in the same location as the Certificate Authority ({}).N	--from-cazYThe provided source CA must be a part of the same pool as the specified CA to be created.)r   %ValidateResourceIsCompleteIfSpecifiedCONCEPTScertificate_authorityParseValidateResourceLocationr   locationsIdr   InvalidArgumentExceptionformathasattrr   r   ParentRelativeName)argsca_refkms_key_version_ref
issuer_refsource_ca_refs        8lib/googlecloudsdk/command_lib/privateca/create_utils.py_ParseCAResourceArgsr'      se    55d<MN55dMJ55dIF==..446&((%t 55;;=


 3 ? ?
?

-
-	v))*  
}	%	% mm%%' 
 --''--/- 



 
-
-
/		%	%	'( 
-
-	'  	,,    c           
         [         R                  " SS9n[         R                  " SS9n[        U 5      u  pEnUR	                  5       nSnU(       aT  UR
                  R                  UR                  UR                  5       S95      nU(       d  [        R                  " SS5      eUR                  R                  UR                  UR                  5       S95      n	[        R                  " U 5      n
U	R                  UR                   R"                  R$                  :X  a(  U
R&                  (       a  [        R                  " SS5      eUR)                  UR+                  5       UR-                  5       S	9nU R/                  S
5      (       a  [        R0                  " U 5      Ul        O^U R5                  S5      (       a  [        R6                  " U 5      Ul        O,U(       a%  UR8                  R:                  R2                  Ul        [        R<                  " U 5      (       a  [        R>                  " U 5      Ul         O,U(       a%  UR8                  R:                  R@                  Ul         [        RB                  " U5        [        RD                  " U SS9nU(       a1  [        RF                  " U 5      (       d  UR8                  RH                  n[        RJ                  " U 5      nU(       a"  U R/                  S5      (       d  URL                  n[N        RP                  " XRR                  RT                  5      n[        RV                  " X5      n[        RX                  " X5      nURS                  U(       a   URR                  RZ                  R\                  OURR                  RZ                  R^                  UURa                  UUUS9U
SUUS9nUXF4$ )zCreates a GA CA object from CA create flags.

Args:
  args: The parser that contains the flag values.
  is_subordinate: If True, a subordinate CA is returned, otherwise a root CA.

Returns:
  A tuple for the CA to create with (CA object, CA ref, issuer).
r   api_versionN)namer   z.The provided source CA could not be retrieved.r   z9The DevOps tier does not support user-specified KMS keys.)subjectsubjectAltNamer-   subject_fileT)is_ca_commandvalidity)subjectConfig
x509ConfigsubjectKeyId)typelifetimeconfigkeySpec	gcsBucketuserDefinedAccessUrlslabels)1privateca_baseGetClientInstanceGetMessagesModuler'   r   1projects_locations_caPools_certificateAuthoritiesGetAPrivatecaProjectsLocationsCaPoolsCertificateAuthoritiesGetRequestr    r   r   projects_locations_caPools+PrivatecaProjectsLocationsCaPoolsGetRequestr
   ParseKeySpectierCaPoolTierValueValuesEnumDEVOPScloudKmsKeyVersionSubjectConfigSubjectSubjectAltNamesIsSpecifiedParseSubjectr-   IsKnownAndSpecifiedParseSubjectFiler7   r2   SanFlagsAreSpecifiedParseSanFlagsr.   ValidateSubjectConfigParseX509ParametersX509ConfigFlagsAreSpecifiedr3   ParseValidityFlagr6   r   ParseCreateArgsCertificateAuthorityLabelsValueParseSubjectKeyIdParseUserDefinedAccessUrlsTypeValueValuesEnumSUBORDINATESELF_SIGNEDCertificateConfig)r!   is_subordinateclientmessagesr"   r%   r$   pool_ref	source_caca_poolkeyspecsubject_configx509_parametersr6   r;   skiuser_defined_access_urlsnew_cas                     r&   CreateCAFromArgsrl   R   s4    ++=&--$?(&:4&@#&]]_()HHLLRR++- 	S 	
I
 //
G  --11::$$& ; ' t$'llhoo99@@@

$
$

-
-C 
 )) 1I1I1K * . 
i  "//5N//"33D9N&--;;CCN
%%$)$7$7$=N!&&55 ! n- --d$G/u88>>&&11O $$T*(t''
33!!H&&
))55& 	/# #==dM((	 ((<<HH((<<HH''&$ ( 
 4 ) &  &	%%r(   c                 v    U  H3  nUR                   UR                  R                  R                  :X  d  M3    g   g)z3Checks if there are any enabled CAs in the CA list.TF)staterX   StateValueValuesEnumENABLED)ca_listrb   cas      r&   HasEnabledCars      s2    b	xx800EEMMM  
r(   c                    [         R                  " SS9nUR                  R                  R                  UR                  R                  R
                  /nSnU H&  nSR                  U5      UR                  ;   d  M$  UnM(     U(       d&  [        R                  " SSR                  X5      5      eUR                  U;  a&  [        R                  " SSR                  X5      5      eg)	a4  Checks that an issuing CA is in the CA Pool and has a valid state.

Args:
  ca_pool_name: The resource name of the containing CA Pool.
  issuing_ca_id: The CA ID of the CA to verify.
  ca_list: The list of JSON CA objects in the CA pool to check from

Raises:
  InvalidArgumentException on validation errors
r   r*   NzcertificateAuthorities/{}z--issuer-caz;The specified CA with ID [{}] was not found in CA Pool [{}]--issuer-poolzThe specified CA with ID [{}] in CA Pool [{}] is not ENABLED or STAGED. Please choose a CA that has one of these states to issue the CA certificate from.)r<   r>   rX   ro   rp   STAGEDr   r,   r   r   rn   )ca_pool_nameissuing_ca_idrq   rb   allowd_issuing_states
issuing_carr   s          r&   _ValidateIssuingCar{      s     --$?(##88@@##88?? *b"))-8BGGCj  


-
-ELL	
  22

-
-	  &} C	  3r(   c           	      L    [         R                  " SS9n[         R                  " SS9nUR                  R                  R
                  nUR                  R                  UR                  U S95      nUR                  nU(       a  [        XU5        gU Vs/ s H  owR                  PM     nnXH;  a0  [        R                  " SSR                  U [        U5      5      5      egs  snf ! [         R"                   a'    [        R                  " SSR                  U 5      5      ef = f)a  Checks that a CA Pool is valid to be issuing Pool for a subordinate.

Args:
  ca_pool_name: The resource name of the issuing CA Pool.
  issuing_ca_id: The optional CA ID in the CA Pool to validate.

Raises:
  InvalidArgumentException if the CA Pool does not exist or is not enabled.
r   r*   )parentNru   zThe issuing CA Pool [{}] did not have any CAs in ENABLED state of the {} CAs found. Please create or enable a CA and try again.z`The issuing CA Pool [{}] was not found. Please verify this information is correct and try again.)r<   r=   r>   rX   ro   rp   r?   ListBPrivatecaProjectsLocationsCaPoolsCertificateAuthoritiesListRequestcertificateAuthoritiesr{   rn   r   r   r   lenapitools_exceptionsHttpNotFoundError)	rw   rx   ra   rb   enabled_stateca_list_responserq   rr   	ca_statess	            r&   ValidateIssuingPoolr      s    --$?F//DAH11FFNNMOOTTSS 	T 	
 55G g> %,,GbGI,%//
K6,G-	  & - 
	.	. 

-
-	%%+VL%9 s$   BC( C( C#+7C( #C( (;D#c                    [         R                  " SS9nSR                  [        R                  " 5       5      nU R                  5       nSR                  Xe5      nUR                  nUR                  UU[        R                  " 5       UUR                  UUUS9S9n	UR                  R                  R                  R                  (       a/  UR                  R                  R                   U	R"                  l        U	$ )a8  Returns the certificate create request with the given settings.

Args:
  issuer_pool_ref: The resource reference for the issuing CA pool.
  csr: The Certificate Signing Request.
  issuer_ca_id: The CA ID of the CA to sign the CSR, if specified.
  new_ca: The CA object.

Returns:
  A certificate create request.
r   r*   zsubordinate-{}z{}/certificates/{})r,   r6   pemCsr)certificateIdr}   	requestIdissuingCertificateAuthorityIdcertificate)r<   r>   r   r   GenerateCertIdr    r6   :PrivatecaProjectsLocationsCaPoolsCertificatesCreateRequestr	   GenerateRequestIdCertificater7   r2   r-   rdnSequenceSubjectModeValueValuesEnumRDN_SEQUENCEr   subjectMode)
issuer_pool_refcsrissuer_ca_idrk   rb   certificate_idissuer_pool_namecertificate_namer6   cert_requests
             r&   _CreateCertificateCreateRequestr     s     --$?(#**+<+K+K+MN.$113)00 __(II&!!335(4**# +  J 
  ]]  ((4477DD ( 
r(   c                 x    [         R                  " SS9n[        XX#5      nUR                  R	                  U5      $ )a?  Issues a certificate under the given issuer with the given settings.

Args:
  issuer_pool_ref: The resource reference for the issuing CA pool.
  csr: The Certificate Signing Request.
  issuer_ca_id: The CA ID of the CA to sign the CSR, if specified.
  new_ca: The CA object.

Returns:
  The certificate for the new CA.
r   r*   )r<   r=   r   'projects_locations_caPools_certificatesCreate)r   r   r   rk   ra   r   s         r&   SignCsrr   <  s<     ++=&0L, 
	7	7	>	>|	LLr(   N)__doc__
__future__r   r   r   apitools.base.pyr   r    googlecloudsdk.api_lib.privatecar   r<   r   r	   googlecloudsdk.calliope$googlecloudsdk.command_lib.privatecar
   r   $googlecloudsdk.command_lib.util.argsr   r'   rl   rs   r{   r   r   r    r(   r&   <module>r      sU    # &  ' > C > : . 6 > <0-fb&J#L*Z'TMr(   