
    ,                         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 ddlmZ d Zd Zd Zd Zd Zd Zd Zy)zHelpers for create commands.    )absolute_import)division)unicode_literals)
exceptions)base)certificate_utils)request_utils)flags)resource_args)labels_utilc                 :   t        j                  | d       t        j                  | d       t        j                  | d       | j                  j                  j	                         }t        j
                  |dd       | j                  j                  j	                         }|rH|j                  |j                  k7  r/t        j                  ddj                  |j                              t        | d      r$| j                  j                  j	                         nd	}| j                  j                  j	                         }|rS|j                         j                         |j                         j                         k7  rt        j                  d
d      |||fS )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'      sd    55d<MN55dMJ55dIF==..446&((%t 55;;=


 3 ? ?
?

-
-	v))*  
}	% mm%%' 
 --''--/- 



 
-
-
/		%	%	'( 
-
-	'  -	,,    c           	      *   t        j                  d      }t        j                  d      }t        |       \  }}}|j	                         }d}|rQ|j
                  j                  |j                  |j                                     }|st        j                  dd      |j                  j                  |j                  |j                                     }	t        j                  |       }
|	j                  |j                   j"                  j$                  k(  r"|
j&                  rt        j                  dd      |j)                  |j+                         |j-                         	      }| j/                  d
      rt        j0                  |       |_        nS| j5                  d      rt        j6                  |       |_        n'|r%|j8                  j:                  j2                  |_        t        j<                  |       rt        j>                  |       |_         n'|r%|j8                  j:                  j@                  |_         t        jB                  |       t        jD                  | d      }|r+t        jF                  |       s|j8                  jH                  }t        jJ                  |       }|r| j/                  d      s|jL                  }tO        jP                  | |jR                  jT                        }t        jV                  | |      }t        jX                  | |      }|jS                  |r |jR                  jZ                  j\                  n|jR                  jZ                  j^                  ||ja                  |||      |
d||      }|||fS )a  Creates 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   s/    ++=&--$?(&: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!!H&&
H))55& 	h/# #==dHM((	 ((<<HH((<<HH''&$ ( 
 4 ) &  &*	%%r(   c                 p    | D ]1  }|j                   |j                  j                  j                  k(  s1 y y)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                    t        j                  d      }|j                  j                  j                  |j                  j                  j
                  g}d}|D ]"  }dj                  |      |j                  v s!|}$ |s&t        j                  ddj                  ||             |j                  |vr&t        j                  ddj                  ||             y)	aB  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

-
-	  &}l C	  3r(   c           	      F   	 t        j                  d      }t        j                  d      }|j                  j                  j
                  }|j                  j                  |j                  |             }|j                  }|rt        | ||       y|D cg c]  }|j                   }}||vr/t        j                  ddj                  | t        |                  yc c}w # t         j"                  $ r& t        j                  ddj                  |             w xY w)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,I%//
K6,G-	  & - 
	.	. 

-
-	%%+VL%9 s$   BC' C' C",5C' "C' '9D c                    t        j                  d      }dj                  t        j                               }| j                         }dj                  ||      }|j                  }|j                  ||t        j                         ||j                  |||            }	|j                  j                  j                  j                  r/|j                  j                  j                   |	j"                  _        |	S )aH  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                     t        j                  d      }t        | |||      }|j                  j	                  |      S )aO  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sL&, 
	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(   