o
    €Ï=3  ã                   @   sp  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d
lmZ dZdd„ Zdd„ Zdd„ Zdd„ ZG dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d d!„ d!eƒZG d"d#„ d#eƒZd$d%„ Zd&d'„ Zd(d)„ Z d*d+„ Z!d4d,d-„Z"d.d/„ Z#d0d1„ Z$G d2d3„ d3e%ƒZ&dS )5zCreate ECP configurations.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úconfig)Úlog)Ú
properties)Úfiles)Ú	platformsz/enterprise-certificate-proxy configuration filec                  C   s&   t  ¡ j} | stdƒ‚t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   © r   úZ/tmp/google-cloud-sdk/lib/googlecloudsdk/command_lib/auth/enterprise_certificate_config.pyÚget_platform_folder"   s   
ÿr   c                  C   s   t  ¡ j} | stdƒ‚| S )NzJUnable to find the SDK bin path. The gcloud installation may be corrupted.)r   r   Úsdk_bin_pathr   )r   r   r   r   Úget_bin_folder-   s   
ÿr   c                 C   s   | r| S t  ¡ S ©N)r   ÚCertConfigDefaultFilePath)Úoutput_filer   r   r   Úget_config_path8   s   r   c                 C   sZ   | st 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¡ƒ‚)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
   r   r   r   Úplatform_to_config>   s   
ür'   c                   @   s   e Zd ZdZdZdZdZdS )r    é   é   é   é   N)Ú__name__Ú
__module__Ú__qualname__r#   r!   r%   ÚWORKLOADr   r   r   r   r    P   s
    r    c                   @   ó   e Zd ZdZdd„ ZdS )ÚWindowsBinaryPathConfiga  Configuration for the paths to the ECP binaries on Windows.

  Attributes:
    ecp: Path to the ECP binary.
    ecp_http_proxy: Path to the ECP HTTP proxy binary.
    ecp_client: Path to the ECP client library.
    tls_offload: Path to the TLS offload library.
  c                 C   ór   |r|nt j tƒ d¡| _|r|nt j tƒ d¡| _|r|nt j tƒ d¡| _|r.|| _d S t j tƒ d¡| _d S )Nzecp.exezecp_http_proxy.exez
libecp.dllzlibtls_offload.dll©	r   r   r   r   ÚecpÚecp_http_proxyr   Ú
ecp_clientÚtls_offload©Úselfr4   r6   r7   r5   r   r   r   Ú__init__a   ó   ÿýÿýÿÿýz WindowsBinaryPathConfig.__init__N©r,   r-   r.   Ú__doc__r:   r   r   r   r   r1   W   ó    	r1   c                   @   r0   )ÚLinuxPathConfiga  Configuration for the paths to the ECP binaries on Linux.

  Attributes:
    ecp: Path to the ECP binary.
    ecp_http_proxy: Path to the ECP HTTP proxy binary.
    ecp_client: Path to the ECP client library.
    tls_offload: Path to the TLS offload library.
  c                 C   r2   )Nr4   r5   z	libecp.sozlibtls_offload.sor3   r8   r   r   r   r:   ~   r;   zLinuxPathConfig.__init__Nr<   r   r   r   r   r?   t   r>   r?   c                   @   r0   )ÚMacOSBinaryPathConfiga  Configuration for the paths to the ECP binaries on MacOS.

  Attributes:
    ecp: Path to the ECP binary.
    ecp_http_proxy: Path to the ECP HTTP proxy binary.
    ecp_client: Path to the ECP client library.
    tls_offload: Path to the TLS offload library.
  c                 C   r2   )Nr4   r5   zlibecp.dylibzlibtls_offload.dylibr3   r8   r   r   r   r:   ›   r;   zMacOSBinaryPathConfig.__init__Nr<   r   r   r   r   r@   ‘   r>   r@   c                   @   ó   e Zd Zdd„ ZdS )ÚPKCS11Configc                 C   s$   || _ || _|| _|r|| _d S d S r   )ÚmoduleÚslotÚlabelÚuser_pin)r9   rC   rD   rE   rF   r   r   r   r:   °   s   
ÿzPKCS11Config.__init__N©r,   r-   r.   r:   r   r   r   r   rB   ®   ó    rB   c                   @   rA   )ÚKeyChainConfigc                 C   ó   || _ || _d S r   )ÚissuerÚkeychain_type)r9   rK   rL   r   r   r   r:   »   ó   
zKeyChainConfig.__init__NrG   r   r   r   r   rI   ¹   rH   rI   c                   @   rA   )ÚMyStoreConfigc                 C   s   || _ || _|| _d S r   )rK   ÚstoreÚprovider)r9   rK   rO   rP   r   r   r   r:   Â   s   
zMyStoreConfig.__init__NrG   r   r   r   r   rN   À   rH   rN   c                   @   rA   )ÚWorkloadConfigc                 C   rJ   r   )Ú	cert_pathÚkey_path)r9   rR   rS   r   r   r   r:   Ê   rM   zWorkloadConfig.__init__NrG   r   r   r   r   rQ   È   rH   rQ   c                 K   s  | r|   di ¡  di ¡}|   di ¡}ni }i }t|  dd¡p#|  dd¡|  dd¡p.|  dd¡|  dd¡p9|  dd¡|  d	d¡pD|  d	d¡ƒ}t|  d
d¡pR|  d
d¡|  dd¡p]|  dd¡|  dd¡ph|  dd¡|  dd¡ps|  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ÚlibsrC   NrD   r   rE   rF   r4   r6   r7   r5   )ÚgetrB   r?   Úvars)Úbase_configÚkwargsÚbase_linux_configÚbase_libs_configÚ
ecp_configÚ
lib_configr   r   r   Úcreate_linux_configÏ   s*   ü


ù	r_   c                 K   sÊ   | r| d d }| d }ni }i }t | dd¡p| dd¡| dd¡p(| dd¡ƒ}t| dd¡p6| dd¡| d	d¡pA| d	d¡| d
d¡pL| d
d¡| dd¡pW| 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.
  rT   Úmacos_keychainrV   rK   NrL   Úallr4   r6   r7   r5   )rI   rW   r@   rX   )rY   rZ   Úbase_macos_configr\   r]   r^   r   r   r   Úcreate_macos_configô   s(   

ý


ù	rc   c                 K   sà   | r| d d }| d }ni }i }t | dd¡p| dd¡| dd¡p(| dd¡| dd¡p3| dd¡ƒ}t| dd¡pA| dd¡| d	d¡pL| d	d¡| d
d¡pW| d
d¡| dd¡pb| 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.
  rT   Úwindows_storerV   rK   NrO   rP   r4   r6   r7   r5   )rN   rW   r1   rX   )rY   rZ   Úbase_windows_configr\   r]   r^   r   r   r   Úcreate_windows_config  s(   
ý


ù	rf   c                 K   sX   | r	| d d }ni }t | dd¡p| dd¡| dd¡p"| 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.
  rT   ÚworkloadrR   NrS   )rQ   rW   rX   )rY   rZ   Úbase_workload_configÚworkload_configr   r   r   Úcreate_workload_config<  s   

ürj   c                 K   sÎ   | t jkrt|fi |¤Ž\}}n7| t jkr t|fi |¤Ž\}}n'| t jkr0t|fi |¤Ž\}}n| t jkr@t|fi |¤Ž\}}nt	d 
| ¡ƒ‚tjjj ¡ rRt ¡ sad|v rad|d v ra|d d= 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]rV   r5   rT   )r    r#   r_   r!   rc   r%   rf   r/   rj   r   r&   r   ÚVALUESÚcontext_awareÚuse_ecp_http_proxyÚGetBoolÚIsInternalUserCheck)Úconfig_typerY   rZ   r]   Úlibs_configr   r   r   Úcreate_ecp_configW  s(   



ü
þý
rr   c                 K   sH   t | dfi |¤Ž}t| dd¡ƒ}t |tj|dd¡ t |t	¡ dS )z<Creates the ECP config based on the passed in CLI arguments.Nr   r)   ©Úindent)
rr   r   rW   r   ÚWriteFileContentsÚjsonÚdumpsr   ÚCreatedResourceÚRESOURCE_TYPE)rp   rZ   ÚoutputÚconfig_pathr   r   r   Úcreate_config…  s   r|   c                 K   s\   t | dd¡ƒ}t |¡}t |¡}t| |fi |¤Ž}t |tj|dd¡ t	 
|t¡ dS )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)   rs   )r   rW   r   ÚReadFileContentsrv   Úloadsrr   ru   rw   r   rx   ry   )rp   rZ   r{   ÚdataÚactive_configrz   r   r   r   Úupdate_configŽ  s   


r   c                       s   e Zd Z‡ fdd„Z‡  ZS )r   c                    s   t t| ƒ ¡  || _d S r   )Úsuperr   r:   Úmessage)r9   rƒ   ©Ú	__class__r   r   r:   ¤  s   
zECPConfigError.__init__)r,   r-   r.   r:   Ú__classcell__r   r   r„   r   r   ¢  s    r   r   )'r=   Ú
__future__r   r   r   Úenumrv   r   Úgooglecloudsdk.corer   r   r   Úgooglecloudsdk.core.utilr   r	   ry   r   r   r   r'   ÚEnumr    Úobjectr1   r?   r@   rB   rI   rN   rQ   r_   rc   rf   rj   rr   r|   r   Ú	Exceptionr   r   r   r   r   Ú<module>   sB   %$$
.	