
    w+                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl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 G d dej&                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd  Zd! Zd" Z d(d#Z!d$ Z"d% Z# G d& d'e$      Z%y))zCreate ECP configurations.    )absolute_import)division)unicode_literalsN)config)log)files)	platformsz/enterprise-certificate-proxy configuration filec                      t        j                         j                  } | st        d      t        j
                  j                  | dd      S )NzKUnable to find the SDK root path. The gcloud installation may be corrupted.platformenterprise_cert)r   Pathssdk_rootECPConfigErrorospathjoin)r   s    Dlib/googlecloudsdk/command_lib/auth/enterprise_certificate_config.pyget_platform_folderr   !   sB    \\^$$(	
	 
 
h
,=	>>    c                  \    t        j                         j                  } | st        d      | S )NzJUnable to find the SDK bin path. The gcloud installation may be corrupted.)r   r   sdk_bin_pathr   )r   s    r   get_bin_folderr   ,   s0    ,,,	
	 
 
r   c                 2    | r| S t        j                         S N)r   CertConfigDefaultFilePath)output_files    r   get_config_pathr   7   s    		)	)	++r   c                    | st         j                  j                         } | j                  t         j                  j
                  k(  rt        j                  S | j                  t         j                  j                  k(  rt        j                  S | j                  t         j                  j                  k(  rt        j                  S t        dj                  | j                              )NzfUnsupported platform {}. Enterprise Certificate Proxy currently only supports OSX, Windows, and Linux.)r	   PlatformCurrentoperating_systemOperatingSystemMACOSX
ConfigTypeKEYCHAINLINUXPKCS11WINDOWSMYSTOREr   format)r   s    r   platform_to_configr+   =   s    	!!))+H)";";"B"BB  I$=$=$C$CC  I$=$=$E$EE
6
&**
+	 r   c                       e Zd ZdZdZdZdZy)r$               N)__name__
__module____qualname__r'   r%   r)   WORKLOAD r   r   r$   r$   O   s    &('(r   r$   c                       e Zd Zd Zy)WindowsBinaryPathConfigc                 6   |r|n't         j                  j                  t               d      | _        |r|n't         j                  j                  t               d      | _        |r|| _        y t         j                  j                  t               d      | _        y )Nzecp.exez
libecp.dllzlibtls_offload.dllr   r   r   r   ecpr   
ecp_clienttls_offloadselfr:   r;   r<   s       r   __init__z WindowsBinaryPathConfig.__init__X   sy    srww||N,<iHDH  	WW\\-/> 	O  	 	 WW\\-/1EF 	r   Nr1   r2   r3   r?   r5   r   r   r7   r7   V       r   r7   c                       e Zd Zd Zy)LinuxPathConfigc                 6   |r|n't         j                  j                  t               d      | _        |r|n't         j                  j                  t               d      | _        |r|| _        y t         j                  j                  t               d      | _        y )Nr:   z	libecp.sozlibtls_offload.sor9   r=   s       r   r?   zLinuxPathConfig.__init__h   sy    srww||N,<eDDH  	WW\\-/= 	O  	 	 WW\\-/1DE 	r   Nr@   r5   r   r   rC   rC   f   rA   r   rC   c                       e Zd Zd Zy)MacOSBinaryPathConfigc                 6   |r|n't         j                  j                  t               d      | _        |r|n't         j                  j                  t               d      | _        |r|| _        y t         j                  j                  t               d      | _        y )Nr:   zlibecp.dylibzlibtls_offload.dylibr9   r=   s       r   r?   zMacOSBinaryPathConfig.__init__x   sy    srww||N,<eDDH  	WW\\-/@ 	O  	 	 WW\\-/1GH 	r   Nr@   r5   r   r   rF   rF   v   rA   r   rF   c                       e Zd Zd Zy)PKCS11Configc                 B    || _         || _        || _        |r|| _        y y r   )moduleslotlabeluser_pin)r>   rK   rL   rM   rN   s        r   r?   zPKCS11Config.__init__   s&    DKDIDJdm r   Nr@   r5   r   r   rI   rI      s    r   rI   c                       e Zd Zd Zy)KeyChainConfigc                      || _         || _        y r   )issuerkeychain_type)r>   rR   rS   s      r   r?   zKeyChainConfig.__init__   s    DK&Dr   Nr@   r5   r   r   rP   rP      s    'r   rP   c                       e Zd Zd Zy)MyStoreConfigc                 .    || _         || _        || _        y r   )rR   storeprovider)r>   rR   rW   rX   s       r   r?   zMyStoreConfig.__init__   s    DKDJDMr   Nr@   r5   r   r   rU   rU      s    r   rU   c                       e Zd Zd Zy)WorkloadConfigc                      || _         || _        y r   )	cert_pathkey_path)r>   r\   r]   s      r   r?   zWorkloadConfig.__init__   s    DNDMr   Nr@   r5   r   r   rZ   rZ      s    r   rZ   c           	         | r5| j                  di       j                  di       }| j                  di       }ni }i }t        |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd      |j                  d	d      xs |j                  d	d            }t        |j                  d
d      xs |j                  d
d      |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd            }dt        |      idt        |      ifS )a0  Creates a Linux ECP Config.

  Args:
    base_config: Optional parameter to use as a fallback for parameters that are
      not set in kwargs.
    **kwargs: Linux config parameters. See go/enterprise-cert-config for valid
      variables.

  Returns:
    A dictionary object containing the ECP config.
  cert_configspkcs11libsrK   NrL   r   rM   rN   r:   r;   r<   )getrI   rC   vars)base_configkwargsbase_linux_configbase_libs_config
ecp_config
lib_configs         r   create_linux_configrj      sd    #;??"M"vr2jj4 I$5$9$9(D$IjjB"3"7"7"Bjj$G#4#8#8$#GjjT"M&7&;&;J&M	* jjB!1!5!5eT!Bjjt$ 2			lD	1jj% 3			mT	2* D$	%Z0@'A	AAr   c                    | r| d   d   }| d   }ni }i }t        |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd            }t        |j                  dd      xs |j                  dd      |j                  d	d      xs |j                  d	d      |j                  d
d      xs |j                  d
d            }dt        |      idt        |      ifS )a0  Creates a MacOS ECP Config.

  Args:
    base_config: Optional parameter to use as a fallback for parameters that are
      not set in kwargs.
    **kwargs: MacOS config parameters. See go/enterprise-cert-config for valid
      variables.

  Returns:
    A dictionary object containing the ECP config.
  r_   macos_keychainra   rR   NrS   allr:   r;   r<   )rP   rb   rF   rc   )rd   re   base_macos_configrg   rh   ri   s         r   create_macos_configro      s    #N34DE"6*jj4 I$5$9$9(D$Ijj%( 7				6*
 %jjB!1!5!5eT!Bjjt$ 2			lD	1jj% 3			mT	2* D,	-Z8H/I	IIr   c                 B   | r| d   d   }| d   }ni }i }t        |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd            }t        |j                  dd      xs |j                  dd      |j                  d	d      xs |j                  d	d      |j                  d
d      xs |j                  d
d            }dt        |      idt        |      ifS )a4  Creates a Windows ECP Config.

  Args:
    base_config: Optional parameter to use as a fallback for parameters that are
      not set in kwargs.
    **kwargs: Windows config parameters. See go/enterprise-cert-config for valid
      variables.

  Returns:
    A dictionary object containing the ECP config.
  r_   windows_storera   rR   NrW   rX   r:   r;   r<   )rU   rb   r7   rc   )rd   re   base_windows_configrg   rh   ri   s         r   create_windows_configrs      s/    %n5oF"6*jj4 K$7$;$;Hd$Kjj$I#6#:#:7D#IjjT"O&9&=&=j$&O*
 'jjB!1!5!5eT!Bjjt$ 2			lD	1jj% 3			mT	2* 4
+	,vtJ7G.H	HHr   c                     | r	| d   d   }ni }t        |j                  dd      xs |j                  dd      |j                  dd      xs |j                  dd            }dt        |      ii fS )a6  Creates a Workload ECP Config.

  Args:
    base_config: Optional parameter to use as a fallback for parameters that are
      not set in kwargs.
    **kwargs: Workload config parameters. See go/enterprise-cert-config for
      valid variables.

  Returns:
    A dictionary object containing the ECP config.
  r_   workloadr\   Nr]   )rZ   rb   rc   )rd   re   base_workload_configworkload_configs       r   create_workload_configrx     s     &~6zB"jjd# 5		!	!+t	4jjT" 4		!	!*d	3	/ d?+	,b	00r   c                 Z   | t         j                  k(  rt        |fi |\  }}n| t         j                  k(  rt	        |fi |\  }}n`| t         j
                  k(  rt        |fi |\  }}n=| t         j                  k(  rt        |fi |\  }}nt        dj                  |             d|i|S )a  Creates an ECP Config.

  Args:
    config_type: An ConfigType Enum that describes the type of ECP config.
    base_config: Optional parameter to use as a fallback for parameters that are
      not set in kwargs.
    **kwargs: config parameters. See go/enterprise-cert-config for valid
      variables.

  Returns:
    A dictionary object containing the ECP config.
  Raises:
    ECPConfigError: No valid config_type is specified.
  z|Unknown config_type {} passed to create enterprise certificate configuration. Valid options are: [PKCS11, KEYCHAIN, MYSTORE]r_   )r$   r'   rj   r%   ro   r)   rs   r4   rx   r   r*   )config_typerd   re   rh   libs_configs        r   create_ecp_configr|   )  s      J%%%1+HHJj)))1+HHJj(((3KJ6JJj)))4[KFKJ
M
&
	  *	4	44r   c                     t        | dfi |}t        |j                  dd            }t        j                  |t        j                  |d             t        j                  |t               y)z<Creates the ECP config based on the passed in CLI arguments.Nr   r.   indent)
r|   r   rb   r   WriteFileContentsjsondumpsr   CreatedResourceRESOURCE_TYPE)rz   re   outputconfig_paths       r   create_configr   K  sS    [$9&9&

=$ ?@++tzz&'CDk=1r   c                 2   t        |j                  dd            }t        j                  |      }t	        j
                  |      }t        | |fi |}t        j                  |t	        j                  |d             t        j                  |t               y)a7  Updates the ECP config based on the passed in CLI arguments.

  Args:
    config_type: An ConfigType Enum that describes the type of ECP config.
    **kwargs: config parameters that will be updated. See
      go/enterprise-cert-config for valid variables.

  Only explicit args will overwrite existing values
  r   Nr.   r~   )r   rb   r   ReadFileContentsr   loadsr|   r   r   r   r   r   )rz   re   r   dataactive_configr   s         r   update_configr   T  ss      

=$ ?@+				,$**T"-[-B6B&+tzz&'CDk=1r   c                        e Zd Z fdZ xZS )r   c                 8    t         t        |           || _        y r   )superr   r?   message)r>   r   	__class__s     r   r?   zECPConfigError.__init__j  s    	.$(*DLr   )r1   r2   r3   r?   __classcell__)r   s   @r   r   r   h  s     r   r   r   )&__doc__
__future__r   r   r   enumr   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r	   r   r   r   r   r+   Enumr$   objectr7   rC   rF   rI   rP   rU   rZ   rj   ro   rs   rx   r|   r   r   	Exceptionr   r5   r   r   <module>r      s    ! &  '   	 & # * .A?,$ f  f  F  6 'V 'F V  BFJDID165D22(Y r   