
    x[h V                     (   S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJr  SSKJrJr  SSKJr  SSKJrJr  \R2                  " \5      r " S S	5      r " S
 S5      r " S S5      r " S S5      rS\	\\ \4      S\\    4S jr!g)z.Module for ephemeral network context managers
    N)partial)AnyCallableDictListLiteralOptionalTuple)NoDHCPLeaseErrormaybe_perform_dhcp_discovery)ProcessExecutionError)UrlErrorwait_for_urlc                   L    \ rS rSrSr  SS\4S jjrS rS rS r	S	 r
S
 rSrg)EphemeralIPv4Network   a  Context manager which sets up temporary static network configuration.

No operations are performed if the provided interface already has the
specified configuration.
This can be verified with the connectivity_urls_data.
If unconnected, bring up the interface with valid ip, prefix and broadcast.
If router is provided setup a default route for that interface. Upon
context exit, clean up the interface leaving no configuration behind.
Ninterface_addrs_before_dhcpc	                    [        X#XE/5      (       d  [        SR                  X#XE5      5      e [        R                  " U5      U l        X l        X0l        XPl        Xpl	        Xl
        / U l        Xl        U R                   SU R
                   3U l        UR                  U R                  0 5      U l        g! [         a   n	[        SR                  U	5      5      U	eSn	A	ff = f)a  Setup context manager and validate call signature.

@param interface: Name of the network interface to bring up.
@param ip: IP address to assign to the interface.
@param prefix_or_mask: Either netmask of the format X.X.X.X or an int
    prefix.
@param broadcast: Broadcast address for the IPv4 network.
@param router: Optionally the default gateway IP.
@param static_routes: Optionally a list of static routes from DHCP
z5Cannot init network on {0} with {1}/{2} and bcast {3}z4Cannot setup network, invalid prefix or netmask: {0}N/)all
ValueErrorformatnetipv4_mask_to_net_prefixprefix	interfaceip	broadcastrouterstatic_routescleanup_cmdsdistrocidrgetr   )
selfr"   r   r   prefix_or_maskr   r   r   r    es
             9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py__init__EphemeralIPv4Network.__init__   s    * I>=>>GNN> 
	55nEDK #"*,.wwiq.	+F+J+JNNB,
(  	%vay 	s   B7 7
C!CC!c                      U R                  5         U R                  (       a  U R                  5         gU R                  (       a  U R                  5         gg! [         a=  nS[        UR                  5      ;  a  S[        UR                  5      ;  a  e  SnANSnAff = f! [         a    U R                  SSS5        e f = f)zSet up ephemeral network if interface is not connected.

This context manager handles the lifecycle of the network interface,
addresses, routes, etc
zFile existszAddress already assignedN)	_bringup_devicer   strstderrr    _bringup_static_routesr   _bringup_router__exit__)r%   r'   s     r(   	__enter__EphemeralIPv4Network.__enter__N   s    	$$&& !!++-$$& ) )  HH) 0AHHE	, % 	MM$d+	s3   A !B" !B" 
B"3BB" BB" "Cc                 8    U R                    H
  nU" 5         M     g)zTeardown anything we set up.N)r!   )r%   	excp_type
excp_valueexcp_tracebackcmds        r(   r1   EphemeralIPv4Network.__exit__r   s    $$CE %    c                    [         R                  SU R                  U R                  U R                  5        [
        R                  " 5       R                  U R                  0 5      nUR                  S5      nU R                  R                  S5      nU R                  UR                  S0 5       Vs/ s H  oDR                  S5      PM     sn;   nU R                  U R                  R                  S0 5       Vs/ s H  nUR                  S5      PM     sn;   nU(       a,  [         R                  SU R                  U R                  5        OEU R                  R                  R                  U R                  U R                  U R                  5        U(       a!  [         R                  SU R                  5        O.U R                  R                  R                  U R                  SS9  U(       a!  [         R                  S	U R                  5        OLU R                  R                  [!        U R                  R                  R"                  U R                  SS95        U(       a!  [         R                  S
U R                  5        gU R                  R                  [!        U R                  R                  R$                  U R                  U R                  5      5        gs  snf s  snf )a6  Perform the ip commands to fully set up the device.

Dhcp clients behave differently in how they leave link state and ip
address assignment.

Attempt assigning address and setting up link if needed to be done.
Set cleanup_cmds to return the interface state to how it was prior
to execution of the dhcp client.
z:Attempting setup of ephemeral network on %s with %s brd %supipv4r   z1Skip adding ip address: %s already has address %sz9Skip bringing up network link: interface %s is already upinet)familyzLNot queueing link down: link [%s] was up prior before receiving a dhcp leasezSNot queueing address removal: address %s was assigned before receiving a dhcp leaseN)LOGdebugr   r#   r   netinfonetdev_infor$   r   r   r"   net_opsadd_addrlink_upr!   appendr   	link_downdel_addr)r%   interface_addrs_after_dhcphas_linkhad_linkr   has_iphad_ips          r(   r,   $EphemeralIPv4Network._bringup_devicew   s<    			HNNIINN		
 &-%8%8%:%>%>NNB&
" .11$73377=#=#A#A&"#M
#MRFF4L#M
 
 66::62F
F FF4LF
 

 IIC KK((		4>> IIK
 KK''v'FII) $$KK''11NN! II) $$KK''00$..$))]

s   1K9K!c                 *   U R                    H  u  pU R                  R                  R                  U R                  X5        U R
                  R                  S[        U R                  R                  R                  U R                  UUS95        M     g )Nr   gateway)	r    r"   rD   append_router   r!   insertr   	del_route)r%   net_addressrR   s      r(   r/   +EphemeralIPv4Network._bringup_static_routes   s{     %)$6$6 K KK,, $$KK''11NN#	 %7r:   c           
         U R                   R                  R                  5       nSU;   a0  [        R	                  SU R
                  UR                  5       5        gU R                   R                  R                  U R
                  U R                  U R                  S9  U R                  R                  S[        U R                   R                  R                  U R
                  U R                  U R                  S95        U R                   R                  R                  U R
                  SU R                  S9  U R                  R                  S[        U R                   R                  R                  U R
                  S5      5        g)z<Perform the ip commands to fully setup the router if needed.defaultz<Skip ephemeral route setup. %s already has default route: %sN)source_addressr   rQ   )r"   rD   get_default_router@   rA   r   strip	add_router   r   r!   rT   r   rU   )r%   outs     r(   r0   $EphemeralIPv4Network._bringup_router   s(    kk!!335IIN		
 %%NNDKK 	& 	
 	  ##--#ww		
 	%%NNIt{{ 	& 	
 	  DKK''114>>9M	
r:   )
r   r#   r!   r"   r   r   r   r   r   r    )NN)__name__
__module____qualname____firstlineno____doc__dictr)   r2   r1   r,   r/   r0   __static_attributes__ r:   r(   r   r      s;    $ .
 &*.
`"H
GR0
r:   r   c                   *    \ rS rSrSrS rS rS rSrg)EphemeralIPv6Network   zContext manager which sets up a ipv6 link local address

The linux kernel assigns link local addresses on link-up, which is
sufficient for link-local communication.
c                 ^    U(       d  [        SR                  U5      5      eX l        Xl        g)zSetup context manager and validate call signature.

@param interface: Name of the network interface to bring up.
@param ip: IP address to assign to the interface.
@param prefix: IPv6 uses prefixes, not netmasks
zCannot init network on {0}N)r   r   r   r"   )r%   r"   r   s      r(   r)   EphemeralIPv6Network.__init__   s(     9@@KLL"r:   c                     [         R                  " U R                  S5      S:w  a0  U R                  R                  R                  U R                  5        gg)zvlinux kernel does autoconfiguration even when autoconf=0

https://www.kernel.org/doc/html/latest/networking/ipv6.html
	operstater<   N)r   read_sys_netr   r"   rD   rF   r%   s    r(   r2   EphemeralIPv6Network.__enter__  s?    
 DNNK8D@KK''7 Ar:   c                     g)z%No need to set the link to down stateNrg   r%   _argss     r(   r1   EphemeralIPv6Network.__exit__  s    r:   )r"   r   N)	r`   ra   rb   rc   rd   r)   r2   r1   rf   rg   r:   r(   ri   ri      s    84r:   ri   c                   f    \ rS rSr   SS\\\\\4         4S jjr	S r
S rS rS rS	 rS
 rSrg)EphemeralDHCPv4i  Nconnectivity_urls_datac                     X l         S U l        S U l        X@l        U=(       d    / U l        Xl        [        R                  " 5       U l        g N)	iface_ephipv4leasedhcp_log_funcrx   r"   rB   rC   r   )r%   r"   r{   rx   r~   s        r(   r)   EphemeralDHCPv4.__init__  s@     
8</3
*&<&B#+2+>+>+@(r:   c                     [        U R                  5      =n(       a  [        R                  SU5        gU R	                  5       $ )zMSetup sandboxed dhcp context, unless connectivity_url can already be
reached.z:Skip ephemeral DHCP setup, instance has connectivity to %sN)_check_connectivity_to_imdsrx   r@   rA   obtain_lease)r%   imds_reached_at_urls     r(   r2   EphemeralDHCPv4.__enter__(  sL     #>''#
 
 
 II#
   ""r:   c                 $    U R                  5         g)z Teardown sandboxed dhcp context.N)clean_network)r%   r5   r6   r7   s       r(   r1   EphemeralDHCPv4.__exit__7  s    r:   c                 p    SU l         U R                  (       a  U R                  R                  SSS5        gg)z@Exit _ephipv4 context to teardown of ip configuration performed.N)r}   r|   r1   rp   s    r(   r   EphemeralDHCPv4.clean_network;  s+    
==MM""4t4 r:   c                    U R                   (       a  U R                   $ [        U R                  U R                  U R                  5      U l         U R                   (       d
  [        5       e[        R                  SU R                   S   U R                   S   U R                   S   5        SSSS/ SQSS.nU R                  U5      nUS	   (       d   [        R                  " US
   US   5      US	'   US   (       a+  U R                  R                  R                  US   5      US'   [        U R                  4SU R                  0UD6nUR                  5         X0l        U R                   $ )a  Perform dhcp discovery in a sandboxed environment if possible.

@return: A dict representing dhcp options on the most recent lease
    obtained from the dhclient discovery if run, otherwise an error
    is raised.

@raises: NoDHCPLeaseError if no leases could be obtained.
z#Received dhcp lease on %s for %s/%sr   zfixed-addresszsubnet-maskzbroadcast-address)zrfc3442-classless-static-routeszclassless-static-routesr    zunknown-121routers)r   r   r&   r   r    r   r   r&   r   r    r   )r}   r   r"   r{   r~   r   r@   rA   extract_dhcp_options_mappingr   mask_and_ipv4_to_bcast_addrdhcp_clientparse_static_routesr   r   r2   r|   )r%   nmapkwargsephipv4s       r(   r   EphemeralDHCPv4.obtain_leaseA  sT    ::::1KKT%7%7

 zz"$$		1JJ{#JJ'JJ}%		
 %!+,  
 2248k""%"A"A'(&,#F; /"'';;?+ ?#
 'KK
(,(H(H
 

 	zzr:   c                     0 nUR                  5        HK  u  p4[        U[        5      (       a  U R                  X4U5        M.  U R                  R                  U5      X#'   MM     U$ rz   )items
isinstancelistget_first_option_valuer}   r$   )r%   r   resultinternal_referencelease_option_namess        r(   r   ,EphemeralDHCPv4.extract_dhcp_options_mappingx  s\    6:jjl2,d33++&F .2ZZ^^<N-O* 7C r:   c                     U H8  nUR                  U5      (       a  M  U R                  R                  U5      X1'   M:     g rz   )r$   r}   )r%   internal_mappingr   r   different_namess        r(   r   &EphemeralDHCPv4.get_first_option_value  s3      2O::.//+/::>>/+J(  2r:   )r|   rx   r~   r"   r{   r   r}   )NNN)r`   ra   rb   rc   r	   r   r   r-   r   r)   r2   r1   r   r   r   r   rf   rg   r:   r(   rw   rw     sP     AEA !)d38n)= >	A#55n	Kr:   rw   c                       \ rS rSrSr   SS\S\S\\\\	\
4         4S jjrS rS	\S
   S\\\\   4   4S jrS rSrg)EphemeralIPNetworki  aT  Combined ephemeral context manager for IPv4 and IPv6

Either ipv4 or ipv6 ephemeral network may fail to initialize, but if either
succeeds, then this context manager will not raise exception. This allows
either ipv4 or ipv6 ephemeral network to succeed, but requires that error
handling for networks unavailable be done within the context.
Nipv6r=   rx   c                     X l         X@l        X0l        [        R                  " 5       U l        SU l        Xl        XPl        g)a  
Args:
    distro: The distro object
    interface: The interface to bring up
    ipv6: Whether to bring up an ipv6 network
    ipv4: Whether to bring up an ipv4 network
    connectivity_urls_data: List of url data to use for connectivity
        check before attempting to bring up ephemeral networks. If
        connectivity can be established to any of the urls, then the
        ephemeral network setup is skipped.
 N)	r   r=   r   
contextlib	ExitStackstack	state_msgr"   rx   )r%   r"   r   r   r=   rx   s         r(   r)   EphemeralIPNetwork.__init__  s5    & #		))+
 &<#r:   c                    U R                   (       d  U R                  (       d  U $ / nSn[        U R                  5      =n(       a  [        R                  SU5        U $ [        R                  S5        U R                   (       a-  U R                  SS9u  pEX$-  nU(       a  UR                  U5        U R                  (       a-  U R                  SS9u  pgX&-  nU(       a  UR                  U5        U R                  (       a  W(       d  U R                   (       d  SU l        U(       d  [        R                  S5        US	   eU $ )
NFz:We already have connectivity to IMDS at %s, skipping DHCP.z/No connectivity to IMDS, attempting DHCP setup.r=   )
ip_versionr   zusing link-local ipv6zGFailed to bring up EphemeralIPNetwork. Datasource setup cannot continuer   )
r=   r   r   rx   r@   rA    _perform_ephemeral_network_setuprG   r   error)r%   
exceptionsephemeral_obtainedimds_urlipv4_ephemeral_obtainedipv4_exceptionipv6_ephemeral_obtainedipv6_exceptions           r(   r2   EphemeralIPNetwork.__enter__  s$   yy K
" 3''
 
8 
 IIL K 			CD 99555H 4# 9!!.199555H 4# 9!!.1 II1$))4DN! II3 Q-r:   r   )r=   r   returnc                     US:X  a8  U R                   R                  [        U R                  U R                  S95        ONUS:X  a:  U R                   R                  [        U R                  U R                  5      5        O[        SU 35      e[        R                  SU R                  U5        g! [        [        4 a/  n[        R                  SU R                  U5        SU4s S	nA$ S	nAff = f)
a  
Attempt to bring up an ephemeral network for the specified IP version.

Args:
    ip_version (str): The IP version to bring up ("ipv4" or "ipv6").

Returns:
    Tuple: A tuple containing:
        - a boolean indicating whether an ephemeral network was
            successfully obtained
        - an optional exception if ephemeral network setup failed
            or None if successful
r=   )r"   r{   r   zUnsupported IP version: z7Successfully brought up %s for ephemeral %s networking.)TNz2Failed to bring up %s for ephemeral %s networking.FN)r   enter_contextrw   r"   r   ri   r   r@   rA   r   r   )r%   r   r'   s      r(   r   3EphemeralIPNetwork._perform_ephemeral_network_setup  s    "	V#

((##{{"nn v%

((( !#;J<!HIIIII
 %'78 	IID
 !8O	s   B-B0 0C/ $C*$C/*C/c                 8    U R                   R                  5         g rz   )r   closers   s     r(   r1   EphemeralIPNetwork.__exit__  s    

r:   )rx   r"   r   r=   r   r   r   )FTN)r`   ra   rb   rc   rd   boolr	   r   r   r-   r   r)   r2   r   r
   	Exceptionr   r1   rf   rg   r:   r(   r   r     s~     AE= 	=
 = !)d38n)= >=62h/N+/ 
tXi((	)/br:   r   rx   r   c                 h  ^  U 4S jnT (       d  [         R                  S5        g[        S T  5       5      =(       d    Sn [        T  Vs/ s H  o3S   PM	     snUUSSS	9u  pEU(       d  [         R                  S
5        gU$ s  snf ! [         a   n[         R                  SU5         SnAgSnAff = f)ai  
Perform a connectivity check to the provided URLs to determine if the
ephemeral network setup is necessary.

This function attempts to reach one of the provided URLs and returns the
URL that was successfully reached. If none of the URLs can be reached,
it returns None.

The timeout for the request is determined by the highest timeout value
provided in the connectivity URLs data. If no timeout is provided, a
default timeout of 5 seconds is used.

Args:
    connectivity_urls_data: A list of dictionaries, each containing
        the following keys:
        - "url" (str): The URL to check connectivity for.
        - "headers" (dict, optional): Headers to include in the request.
        - "timeout" (int, optional): Timeout for the request in seconds.

Returns:
    Optional[str]: The URL that was successfully reached, or None if no
    connectivity was established.
c                 r   > T Vs/ s H  nUS   U :X  d  M  UR                  S5      PM!     snS   nU$ s  snf )zy
Helper function to get headers for a given URL from the connectivity
URLs data provided to _check_connectivity_to_imds.
urlheadersr   r$   )r   url_datar   rx   s      r(   _headers_cb0_check_connectivity_to_imds.<locals>._headers_cb3  sO     3
2#% $HLL#2
 	
 
s   44zZNo connectivity URLs provided. Skipping connectivity check before ephemeral network setup.Nc              3   D   #    U  H  oR                  S S5      v   M     g7f)timeoutr   Nr   ).0r   s     r(   	<genexpr>._check_connectivity_to_imds.<locals>.<genexpr>H  s     N7M8LLA&&7Ms       r   Fr   )urls
headers_cbr   connect_synchronouslymax_waitz5Failed to reach IMDS without ephemeral network setup.z8Failed to reach IMDS without ephemeral network setup: %s)r@   rA   maxr   r   )rx   r   r   r   url_that_worked_r'   s   `      r(   r   r     s    6
 "		J	
  	N7MNN 	 
)2HI2Hh5/2HI""'
. IIMN5 J  
		F	
 	
 
s*   	B 
BB B 
B1B,,B1)"rd   r   logging	functoolsr   typingr   r   r   r   r   r	   r
   cloudinit.netr   cloudinit.netinforB   cloudinit.net.dhcpr   r   cloudinit.subpr   cloudinit.url_helperr   r   	getLoggerr`   r@   r   ri   rw   r   r-   r   rg   r:   r(   <module>r      s       F F F  # M 0 7!b
 b
J4 4@pK pKfJ JZQ c3h0Qc]Qr:   