σ
ίόΪ\c           @` sϊ   d  d l  m Z m Z m Z y d  d l Z Wn e k
 rE d Z n Xd  d l Z d d d g Z d d g Z	 d d	 g Z
 d
 g Z e e g  e
 D] Z d e ^ q g  e D]# Z e	 D] Z d e e f ^ q΄ qͺ  Z d   Z d e f d     YZ d S(   i    (   t   print_functiont   divisiont   absolute_importNt   clientt	   schedulert   workert   keyt   certt   ca_filet   cipherst   require_encryptions   tls_%ss	   tls_%s_%sc         C` s   |  j  d d  S(   Nt   _t   -(   t   replace(   t   field(    (    s3   lib/python2.7/site-packages/distributed/security.pyt   _field_to_config_key   s    t   Securityc           B` sk   e  Z d  Z e e  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z RS(
   s΄  
    An object to gather and pass around security configuration.
    Default values are gathered from the global ``config`` object and
    can be overriden by constructor args.

    Supported fields:
        - require_encryption
        - tls_ca_file
        - tls_ciphers
        - tls_client_key
        - tls_client_cert
        - tls_scheduler_key
        - tls_scheduler_cert
        - tls_worker_key
        - tls_worker_cert
    c         K` s   |  j  t j j  x9 | j   D]+ \ } } | d  k	 r  t |  | |  q  q  Wx0 t D]( } t |  |  sV t |  | d   qV qV Wd  S(   N(   t   _init_from_dictt   daskt   configt   itemst   Nonet   setattrt   _fieldst   hasattr(   t   selft   kwargst   kt   v(    (    s3   lib/python2.7/site-packages/distributed/security.pyt   __init__2   s    c         C` s0   |  j  | d t i   |  j  | d t t  d S(   s7   
        Initialize Security from nested dict.
        t    t   tlsN(   t   _init_fields_from_dictt   _misc_fieldst   _tls_fieldst   _tls_per_role_fields(   R   t   d(    (    s3   lib/python2.7/site-packages/distributed/security.pyR   ;   s    c   
      C` sι   | r% | j  | i   } | d } n d } xG | D]? } t |  } | | k r2 t |  d | | f | |  q2 q2 Wxm t D]e } | j  | i   }	 xJ | D]B } t |  } | |	 k r t |  d | | | f |	 |  q q Wq| Wd  S(   NR   R   s   %s%ss   %s%s_%s(   t   getR   R   t   _roles(
   R   R$   t   categoryt   fieldst   per_role_fieldst   category_prefixR   R   t   rolet   dd(    (    s3   lib/python2.7/site-packages/distributed/security.pyR    B   s    %c         ` s;   t    f d   t D  } d d j d   | D  d S(   Nc         3` s$   |  ] } | t    |  f Vq d  S(   N(   t   getattr(   t   .0R   (   R   (    s3   lib/python2.7/site-packages/distributed/security.pys	   <genexpr>T   s    s	   Security(s   , c         s` s1   |  ]' \ } } | d k	 r d  | | f Vq d S(   s   %s=%rN(   R   (   R.   R   R   (    (    s3   lib/python2.7/site-packages/distributed/security.pys	   <genexpr>W   s    t   )(   t   sortedR   t   join(   R   R   (    (   R   s3   lib/python2.7/site-packages/distributed/security.pyt   __repr__S   s    c         C` s   |  j  d | t t  S(   sR   
        Return the TLS configuration for the given role, as a flat dict.
        R   (   t   _get_config_for_roleR"   R#   (   R   R+   (    (    s3   lib/python2.7/site-packages/distributed/security.pyt   get_tls_config_for_role[   s    c         C` s   | t  k r" t d | f   n  i  } x1 | D]) } d | | f } t |  |  | | <q/ Wx4 | D], } d | | | f } t |  |  | | <qc W| S(   Ns   unknown role %rs   %s_%ss   %s_%s_%s(   R&   t
   ValueErrorR-   (   R   R'   R+   R(   R)   R$   R   R   (    (    s3   lib/python2.7/site-packages/distributed/security.pyR3   a   s    c         C` sΐ   | j  d  rΌ | j  d  rΌ y  t j d | d | d  } Wn t k
 r] t d   n Xt j | _ t | _ | j	 | d | j  d   | j  d  rΈ | j
 | j  d   n  | Sd  S(   NR   R   t   purposet   cafiles(   TLS functionality requires Python 2.7.9+R   R	   (   R%   t   sslt   create_default_contextt   AttributeErrort   RuntimeErrort   CERT_REQUIREDt   verify_modet   Falset   check_hostnamet   load_cert_chaint   set_ciphers(   R   R   R6   t   ctx(    (    s3   lib/python2.7/site-packages/distributed/security.pyt   _get_tls_contextm   s     	c         C` s]   i  } |  j  |  } t t d  r0 t j j n d } |  j | |  | d <|  j | d <| S(   sh   
        Get the *connection_args* argument for a connect() call with
        the given *role*.
        t   Purposet   ssl_contextR
   N(   R4   R   R8   RD   t   SERVER_AUTHR   RC   R
   (   R   R+   R$   R   R6   (    (    s3   lib/python2.7/site-packages/distributed/security.pyt   get_connection_args|   s    !c         C` s]   i  } |  j  |  } t t d  r0 t j j n d } |  j | |  | d <|  j | d <| S(   sg   
        Get the *connection_args* argument for a listen() call with
        the given *role*.
        RD   RE   R
   N(   R4   R   R8   RD   t   CLIENT_AUTHR   RC   R
   (   R   R+   R$   R   R6   (    (    s3   lib/python2.7/site-packages/distributed/security.pyt   get_listen_args   s    !(   t   __name__t
   __module__t   __doc__t   tupleR   t	   __slots__R   R   R    R2   R4   R3   RC   RG   RI   (    (    (    s3   lib/python2.7/site-packages/distributed/security.pyR      s   									(   t
   __future__R    R   R   R8   t   ImportErrorR   R   R&   R#   R"   R!   t   setR   R+   R   R   t   objectR   (    (    (    s3   lib/python2.7/site-packages/distributed/security.pyt   <module>   s   
	R	