
    ӂeX              	          U d Z ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddl
mZ  ej                         Zi dddgddd	gd
g dddgddgddgddgddgdddgddgddgddgdd gd!d"gd#d$gd%d&gd'd(gd)gd*gd+gd,gd-gd.Zej                  eej"                  e   f   ed/<   d0efd1Z G d2 d3ej(                  j*                        Zd4ed5ej.                  e   d0dfd6Z	 	 dHd4ed5ej.                  e   d7ej.                  e   d0ej2                  fd8Z ej6                  ej8                  d9d:d;<      Z G d= d>e      ZdId?e d0efd@Z!dAejD                  dBej"                  ej2                     dCej                  eej2                  f   d0ej.                  e   fdDZ#dEed0e fdFZ$d0ej.                  e   fdGZ%y)Jao  
Base class for holding contextual information of a request

This class has several uses:

* Used for storing security information in a web request.
* Used for passing contextual details to oslo.log.

Projects should subclass this class if they wish to enhance the request
context or provide additional information in their specific WSGI pipeline
or logging context.
    N)renames
auth_tokenHTTP_X_AUTH_TOKENHTTP_X_STORAGE_TOKENuser_idHTTP_X_USER_IDHTTP_X_USER
project_id)HTTP_X_PROJECT_IDHTTP_X_TENANT_IDHTTP_X_TENANT	domain_idHTTP_X_DOMAIN_IDsystem_scopeHTTP_OPENSTACK_SYSTEM_SCOPEuser_domain_idHTTP_X_USER_DOMAIN_IDproject_domain_idHTTP_X_PROJECT_DOMAIN_ID	user_nameHTTP_X_USER_NAMEproject_nameHTTP_X_PROJECT_NAMEHTTP_X_TENANT_NAMEuser_domain_nameHTTP_X_USER_DOMAIN_NAMEproject_domain_nameHTTP_X_PROJECT_DOMAIN_NAME
request_idzopenstack.request_idglobal_request_idzopenstack.global_request_idservice_tokenHTTP_X_SERVICE_TOKENservice_user_idHTTP_X_SERVICE_USER_IDservice_user_nameHTTP_X_SERVICE_USER_NAMEservice_user_domain_idHTTP_X_SERVICE_USER_DOMAIN_IDHTTP_X_SERVICE_USER_DOMAIN_NAMEHTTP_X_SERVICE_PROJECT_IDHTTP_X_SERVICE_PROJECT_NAME HTTP_X_SERVICE_PROJECT_DOMAIN_ID"HTTP_X_SERVICE_PROJECT_DOMAIN_NAME)service_user_domain_nameservice_project_idservice_project_nameservice_project_domain_idservice_project_domain_name_ENVIRON_HEADERSreturnc                  0    dt        j                         z  S )zGenerate a unique request id.zreq-%s)uuiduuid4     6/usr/lib/python3/dist-packages/oslo_context/context.pygenerate_request_idr;   H   s    djjl""r9   c                   P   e Zd ZdZdej
                  eej                  f   fdZdedej                  fdZ	dedej                  ddfd	Z
deddfd
Zdej                  ej                     fdZdefdZdefdZdefdZedej
                  eej                  f   fd       Zy)_DeprecatedPolicyValuesa)  A Dictionary that manages current and deprecated policy values.

    Anything added to this dictionary after initial creation is considered a
    deprecated key that we are trying to move services away from. Accessing
    these values as oslo.policy will do will trigger a DeprecationWarning.
    datac                      || _         i | _        y N)_data_deprecated)selfr>   s     r:   __init__z _DeprecatedPolicyValues.__init__U   s    
13r9   kr4   c                     	 | j                   |   S # t        $ r Y nw xY w	 | j                  |   }t        j                  d|z  t
               |S # t        $ r Y t        |      w xY w)NzPolicy enforcement is depending on the value of %s. This key is deprecated. Please update your policy file to use the standard policy values.)rA   KeyErrorrB   warningswarnDeprecationWarning)rC   rE   vals      r:   __getitem__z#_DeprecatedPolicyValues.__getitem__Y   s    	::a=  				""1%C MM KMNO -. J  	 qk	s    	A 	A%$A%vNc                 "    || j                   |<   y r@   rB   )rC   rE   rM   s      r:   __setitem__z#_DeprecatedPolicyValues.__setitem__l   s    r9   c                     | j                   |= y r@   rO   )rC   rE   s     r:   __delitem__z#_DeprecatedPolicyValues.__delitem__o   s    Qr9   c                 ,    t        | j                        S r@   )iter_dictrC   s    r:   __iter__z _DeprecatedPolicyValues.__iter__r   s    DJJr9   c                 ,    t        | j                        S r@   )lenrU   rV   s    r:   __len__z_DeprecatedPolicyValues.__len__u   s    4::r9   c                 6    | j                   j                         S r@   )rU   __str__rV   s    r:   r\   z_DeprecatedPolicyValues.__str__x   s    zz!!##r9   c                 6    | j                   j                         S r@   )rU   __repr__rV   s    r:   r^   z _DeprecatedPolicyValues.__repr__{   s    zz""$$r9   c                 p    | j                   j                         }|j                  | j                         |S r@   )rB   copyupdaterA   )rC   ds     r:   rU   z_DeprecatedPolicyValues._dict~   s+    !!#	r9   )__name__
__module____qualname____doc__tyDictstrAnyrD   rL   rP   rR   IteratorrW   intrZ   r\   r^   propertyrU   r8   r9   r:   r=   r=   M   s    4RWWS"&&[1 4S RVV & S  RVV     S  T   "++bff-   $ $%# % rwwsBFF{+  r9   r=   new_nameold_namec                 P    |r$d}||| dz  }t        j                  |ddd       y y )Nz3Property '%(old_name)s' has moved to '%(new_name)s')ro   rn   z2.63.0   )versionremoval_version
stacklevel)debtcollector	deprecate)rn   ro   deprecated_msgs      r:   
_moved_msgry      s@    N'x7?+A A 	(-05+,	. r9   targetc                      dt         j                  dt         j                  f fd}dt         j                  dt        dd f fd}dt         j                  dd f fd}t        |||      S )NrC   r4   c                 <    t               t        | xs       S r@   )ry   getattrrC   rn   ro   rz   s    r:   getterz_moved_property.<locals>.getter   s    8X&tV/x00r9   valuec                 @    t               t        | xs |       y r@   )ry   setattr)rC   r   rn   ro   rz   s     r:   setterz_moved_property.<locals>.setter   s    8X&f(%0r9   c                 >    t               t        | xs        y r@   )ry   delattrr~   s    r:   deleterz _moved_property.<locals>.deleter   s    8X&f()r9   )rg   rj   ri   rm   )rn   ro   rz   r   r   r   s   ```   r:   _moved_propertyr      sd    1RVV 1 11RVV 1C 1D 1*bff * * FFG,,r9   z2.18rq   T)rs   rt   replacec            D       4   e Zd ZU dZdZg Zej                  e   e	d<    e
dd       e
dd       e
dd	       e
d
d      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;dej                  e   dej                  e   dej                  e   dej                  e   d	ej                  e   dej                  e   dedededej                  e   dej                  e   dedej                  ej                  e      dej                  e   dej                  e   dej                  e   dej                  e   dej                  e   dedej                  e   dej                  e   dej                  e   dej                  e   d ej                  e   d!ej                  e   d"ej                  e   d#ej                  e   d$ej                  e   d%ej                  ej                  e      d&ej                  e   d'ej                  e   f>d(                            Z eddd)*      Z eddd+*      Z ed	dd,*      Z edd
d-*      Z ed)      Z ed.      Z ed+      Z ed,      Z ed-      Zd<d0Zd/efd1Zd/ej6                  eej8                  f   fd2Zd/ej6                  eej8                  f   fd3Zed/efd4       Z d5ej8                  d/d fd6Z!e" e
dd       e
dd       e
dd	       e
d
d      d7ej6                  eej8                  f   d5ej8                  d/d fd8                                   Z#e" e
dd       e
dd       e
dd	       e
d
d      d9ej6                  eej8                  f   d5ej8                  d/d fd:                                   Z$y)=RequestContextzHelper class to represent useful information about a request context.

    Stores information about the security context under which the user
    accesses the system, as well as additional request information.
    z5{user} {project_id} {domain} {user_domain} {p_domain}FROM_DICT_EXTRA_KEYSuserr   domainr   user_domainr   project_domainr   Nr   r
   is_admin	read_onlyshow_deletedr   resource_uuid	overwriterolesr   r   domain_namer   r   is_admin_projectr!   r#   r%   r'   r.   r/   r0   r1   r2   service_rolesr    r   c                  
   || _         || _        || _        || _        || _        || _        || _        || _        || _        || _	        || _
        || _        || _        || _        || _        |	| _        || _        |xs g | _        || _        || _        || _        || _        || _        || _        || _        || _        || _        |xs g | _        |
s
t9               }
|
| _        || _        |s
t?               s| jA                          yy)a  Initialize the RequestContext

        :param overwrite: Set to False to ensure that the greenthread local
                          copy of the index is not overwritten.
        :param is_admin_project: Whether the specified project is specified in
                                 the token as the admin project. Defaults to
                                 True for backwards compatibility.
        :type is_admin_project: bool
        :param system_scope: The system scope of a token. The value ``all``
                             represents the entire deployment system. A service
                             ID represents a specific service within the
                             deployment system.
        :type system_scope: string
        N)!_user_id_project_id
_domain_id_user_domain_id_project_domain_idr   r   r   r   r   r   r   r   r   r   r   r   r   r!   r#   r%   r'   r.   r/   r0   r1   r2   r   r;   r   r    get_currentupdate_store) rC   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r#   r%   r'   r.   r/   r0   r1   r2   r   r    r   s                                    r:   rD   zRequestContext.__init__   s   j  %#-"3$"(&( 0#6   0"(*[b
*.!2&<#(@%"4$8!)B&+F(*0b,.J$!2KM *r9   r   )rz   r   r   r   r   r4   c                     | t         _        y)z(Store the context in the current thread.N)_request_storecontextrV   s    r:   r   zRequestContext.update_store%  s
    !%r9   c                 8   t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  d      S )a  A dictionary of context attributes to enforce policy with.

        oslo.policy enforcement requires a dictionary of attributes
        representing the current logged in user on which it applies policy
        enforcement. This dictionary defines a standard list of attributes that
        should be available for enforcement across services.

        It is expected that services will often have to override this method
        with either deprecated values or additional attributes used by that
        service specific policy.
        )r   r   r   r   r
   r   r   r   r#   r'   r/   r1   r   )r=   r   r   r   r   r
   r   r   r   r#   r'   r/   r1   r   rV   s    r:   to_policy_valueszRequestContext.to_policy_values)  s      '||"11 --//!%!7!7ZZ $ 5 5#33&*&A&A"&"9"9)-)G)G!//(
  	r9   c                 x   | j                   j                  | j                  xs d| j                  xs d| j                  xs d| j
                  xs d| j                  xs d      }i d| j                  d| j                  d| j                  d| j                  d| j                  d| j
                  d	| j                  d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d|d| j                   S )z*Return a dictionary of context attributes.-)r   r
   r   r   p_domainr   r
   r   projectr   r   r   r   r   r   r   r   r    r   r   user_identityr   )user_idt_formatformatr   r
   r   r   r   r   r   r   r   r   r   r    r   r   r   )rC   user_idts     r:   to_dictzRequestContext.to_dictI  sz   ''..$-#>>(S++2s++2s / 
; ;doo; 1 1; 4??; $..	;
 t22; !$"8"8; DMM; T^^;  1 1; doo; doo; $T%;%;;  !3!3; ;  ;  #D$9$9!; 	;r9   c                 
   | j                   | j                  | j                  | j                  | j                  d}|j                  | j                                | j                  rd|d<   nd|d<   |j                  dd       |S )z;Return a dictionary of logging specific context attributes.)r   r   r   r   r   z***r   Nauth_token_info)	r   r   r   r   r   ra   r   r   pop)rC   valuess     r:   get_logging_valuesz!RequestContext.get_logging_valuese  s|    #~~"&"3"3!%!1!1&*&;&;)-)A)A	C
 	dlln%?? $)F< #'F<  	

$d+r9   c                 6    | j                   xs | j                  S )a  Return a sensible value for global_id to pass on.

        When we want to make a call with to another service, it's
        important that we try to use global_request_id if available,
        and fall back to the locally generated request_id if not.
        )r    r   rV   s    r:   	global_idzRequestContext.global_id|  s     %%88r9   kwargsc                     | j                   di d| j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d	| j                  d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                   d| j"                  d| j$                  d| j&                  d| j(                  d| j*                  d| j,                  d| j.                  d| j0                  d| j2                  d| j4                  d| j6                  |S )zReturn a copy of the context with sensitive fields redacted.

        This is useful for creating a context that can be safely logged.

        :returns: A copy of the context with sensitive fields redacted.
        r   r
   r   r   r   r   r   r   r   r   r   r   r#   r'   r.   r/   r0   r1   r2   r   r    r   r   r   r   r   r   r8   )	__class__r   r
   r   r   r   r   r   r   r   r   r   r   r#   r'   r.   r/   r0   r1   r2   r   r    r   r   r   r   r   r   )rC   r   s     r:   redacted_copyzRequestContext.redacted_copy  s    t~~ 
LL

 nn
  ..	

 #44
 
 **
 nn
 **
 ((
 "22
 !% 8 8
 !00
 $(#>#>
 &*%B%B
   $66!
" "&!:!:#
$ '+&D&D%
& )-(H(H'
( ,,)
* #44+
, **-
. /
0 ;;1
2 ((3
4  ..5
6 ]]9
 	
r9   r   c                    |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d	|j                  d
             |j                  d|j                  dd             |j                  d|j                  dd             |j                  d|j                  dd             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  d             |j                  d|j                  dd             |j                  d|j                  d             | j                  D ]#  }|j                  ||j                  |             %  | di |S )z6Construct a context object from a provided dictionary.r   r   r   r
   r   r   r   r   r   r   r   Fr   r   r   r    r   r   r   r   r   r   r   r   Tr   r8   )
setdefaultgetr   )clsr   r   keys       r:   	from_dictzRequestContext.from_dict  sK    	,

<(@A)VZZ%78,

<(@A+vzz(';<*FJJ},EF-vzz:J/KL*fjjU&CD+vzz+u'EF.&**^U*KL,

<(@A-vzz:M/NO/6::o+FG'6::g#67+vzz+'>?.&**^*DE-M)BC,fjj9K.LM/ **%:;	=, **%7>	@.&**^*DE++ 	4Cc6::c?3	4}V}r9   environc                 6   t         j                         D ]  \  }}||v r|D ]  }||v s||   ||<     ! d|vrW|j                  d|j                  d            }|r-|j                  d      D cg c]  }|j	                          c}ng }||d<   d|vrG|j                  d      }|r-|j                  d      D cg c]  }|j	                          c}ng }||d<   d|vr(|j                  dd	      }|j                         d	k(  |d<    | d
i |S c c}w c c}w )a  Load a context object from a request environment.

        If keyword arguments are provided then they override the values in the
        request environment.

        :param environ: The environment dictionary associated with a request.
        :type environ: dict
        r   HTTP_X_ROLESHTTP_X_ROLE,r   HTTP_X_SERVICE_ROLESr   HTTP_X_IS_ADMIN_PROJECTtruer8   )r3   itemsr   splitstriplower)	r   r   r   rE   v_listrM   r   ris_admin_proj_strs	            r:   from_environzRequestContext.from_environ  s.   * *//1 	IAvF{ < '
F1I		 & KKM0JKE=BC(891QWWY9E#F7O&(KK 67E=BC(891QWWY9E&+F?#V+ !(,Ev N):)@)@)Bf)LF%&}V} :
 :s   1D<D)NNNNNNFFFNNTNNNNNNTNNNNNNNNNNNN)r4   N)%rc   rd   re   rf   r   r   rg   Listri   __annotations___renamed_kwargOptionalboolrD   r   r   r   r   r   r   r
   r   r   r   r   r=   r   rh   rj   r   r   rm   r   r   classmethodr   r   r8   r9   r:   r   r      s    NO *,"''#,+FI&Hk*M#34$&9: (,$('+&*+/.2"'+*.+/&*)-(,-104!%*.,0.23759/3156:8<37.2)-AU KK$U  S!U  KK$	U 
 ;;s#U  C(U  ;;s+U  U  U  U  KK$U  {{3'U  U  {{2773<(U  ;;s#U   kk#&!U " [[%#U $ ++c*%U &  [[-'U ( )U * {{3'+U , S)-U . ;;s+/U 0 !#C 01U 2 #%++c"23U 4 KK,5U 6 !kk#.7U 8 $&;;s#39U : &([[%5;U < {{2773<0=U > ;;s+?U @ kk#&AU  ; 5 + 'U t 9fZ@D[(<HF!-0ACK$-6JLN j)G /J-I$%67N'(<=&"9 @;bff- ;8BGGCK$8 . 93 9 9$
bff $
1A $
L FI&Hk*M#34$&9:WWS"&&[)57VV	 ; 5 + ' 
< FI&Hk*M#34$&9:*ggc266k**68ff*	* ; 5 + ' 
*r9   r   r   c                 &    t        ddd| d      }|S )z Create an administrator context.NTF)r
   r   r   r   )r   )r   r   s     r:   get_admin_contextr     s!    T(,&**6',	.G
 Nr9   functionargsr   c                 ~    t        j                  |j                         |      D ]  }t        |t              s|c S  y)zFind an arg of type RequestContext and return it.

    This is useful in a couple of decorators where we don't know much about the
    function we're wrapping.
    N)	itertoolschainr   
isinstancer   )r   r   r   args       r:   "get_context_from_function_and_argsr     s8     v}}5 c>*J r9   r   c                 D    | rt        | t              sy| j                  ryy)z2Indicates if the request context is a normal user.FT)r   r   r   )r   s    r:   is_user_contextr     s     *Wn=r9   c                  $    t        t        dd      S )zQReturn this thread's current context

    If no context is set, returns None
    r   N)r}   r   r8   r9   r:   r   r   %  s    
 >9d33r9   )NN)F)&rf   collections.abccollections	functoolsr   	threadingtypingrg   r6   rH   rv   r   localr   r3   rh   ri   r   r   r;   abcMutableMappingr=   r   ry   rj   r   partialrenamed_kwargr   objectr   r   r   Callabler   r   r   r8   r9   r:   <module>r      s            ! !"0&(>?0 -00 L0 $%	0
 230 ./0 450 $%0 *,@A0 230 890 )*0 780 ,-0  01!0" 45#0$ >?%0& "C C67:;"D!E$H#I/0 "''#rwws|+, 6#S #
5koo<< 5r	. 	.C(8 	.T 	. "&#--kk#- KK- VV	-* #""7#8#8+138+/1QV Qh
D ^ kk
''"&&/ GGCK  [[ 	"^  4R[[0 4r9   