
    x[hLT                        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mZmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZmZmZ ddlmZ  ej8                  e      Zdd	iZd
Z dZ!dZ"e!dz   Z#e"dz   Z$ddiZ%dZ& G d de
      Z' G d dejP                        Z)deddfdZ* G d dejV                        Z, G d de,      Z-de.fdZ/dee.   fdZ0de1fdZ2d e.de3fd!Z4d e.deee.e.f      fd"Z5d e.de.fd#Z6d	e,jn                  e,jp                  e#gd$d%e1d&e	e.   dee'   fd'Z9e,ejt                  ffe-ejt                  ejv                  ffgZ<d( Z=ed)k(  r,d*Z> e? ej                   e9e$e#g+       e2       d,             yy)-a3  Datasource for Oracle (OCI/Oracle Cloud Infrastructure)

Notes:
 * This datasource does not support OCI Classic. OCI Classic provides an EC2
   lookalike metadata service.
 * The UUID provided in DMI data is not the same as the meta-data provided
   instance-id, but has an equivalent lifespan.
 * We do need to support upgrade from an instance that cloud-init
   identified as OpenStack.
 * Bare metal instances use iSCSI root, virtual machine instances do not.
 * Both bare metal and virtual machine instances provide a chassis-asset-tag of
   OracleCloud.com.
    N)AnyDictList
NamedTupleOptionalTuple)atomic_helperdminetsourcesutil)NetworkConfig)cmdline	ephemeralget_interfaces_by_macis_netfail_master)wait_for_urlconfigure_secondary_nicsFzOracleCloud.comz&http://169.254.169.254/opc/v{version}/z+http://[fd00:c1::a9fe:a9fe]/opc/v{version}/z{path}/AuthorizationzBearer Oraclei(#  c                   P    e Zd ZU eed<   eeef   ed<   eeeef      ed<   eed<   y)ReadOpcMetadataResponseversioninstance_data
vnics_dataimds_url_usedN)	__name__
__module____qualname__int__annotations__r   strr   r        D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceOracle.pyr   r   4   s.    LS>!c3h((r#   r   c                       e Zd ZdZdefdZy)KlibcOracleNetworkConfigSourcezOverride super class to lower the applicability conditions.

    If any `/run/net-*.cfg` files exist, then it is applicable. Even if
    `/run/initramfs/open-iscsi.interface` does not exist.
    returnc                 ,    t        | j                        S )zOverride is_applicable)bool_filesselfs    r$   is_applicablez,KlibcOracleNetworkConfigSource.is_applicableB   s    DKK  r#   N)r   r   r   __doc__r)   r-   r"   r#   r$   r&   r&   ;   s    !t !r#   r&   network_configr'   c                 <   d| vry| d   dvrt         j                  d| d          yt               }| d   dk(  rT| d   D cg c]	  }d|v s| c}D ]8  }|d   dk(  sd	|v s|d	   }|j                  |      }|s*t	        |      s6|d	= : y| d   d
k(  r| j                  di       j                         D ]_  \  }}d|v s|j                  di       j                  d      }|s/|j                  |      }|sCt	        |      sO|d   d= |d= ||d   d<   a yyc c}w )aP  
    Search network config physical interfaces to see if any of them are
    a netfailover master.  If found, we prevent matching by MAC as the other
    failover devices have the same MAC but need to be ignored.

    Note: we rely on cloudinit.net changes which prevent netfailover devices
    from being present in the provided network config.  For more details about
    netfailover devices, refer to cloudinit.net module.

    :param network_config
       A v1 or v2 network config dict with the primary NIC, and possibly
       secondary nic configured.  This dict will be mutated.

    r   N)      z+Ignoring unknown network config version: %sr1   configtypephysicalmac_addressr2   	ethernetsmatch
macaddresszset-namename)LOGdebugr   getr   items)r/   mac_to_nameccfgmaccur_name_macaddrs           r$   _ensure_netfailover_saferF   G   sY     &i .		99%	
 	')Ki A%-h7G!6Q;AG 	/C6{j( C'm,C*s3H# *84.	/ 
		"a	'$((b9??A 
	8FAs#~'''2.22<@*w7H# *84L6
O/7GV,
	8 
( Hs    	D
Dc                       e Zd ZU dZdZej                  j                  ej                  j                  ej                  j                  ej                  j                  fZeej                  df   ed<   dZdZdZ fdZd	ed
df fdZd
efdZed
efd       Zd Zd
efdZd Zd
efdZd
efdZed        ZddefdZ  xZ!S )DataSourceOracleOracleN.network_config_sourcesT      c                 Z   t        t        | 
  |g|i | d | _        t	        j
                  t	        j                  |d| j                  gi       t        g      | _	        t               | _        g dd| _        | j                         }|j                  | _        |j                   | _        y )N
datasourcer1   r3   r   )superrH   __init___vnics_datar   mergemanydictget_cfg_by_pathdsnameBUILTIN_DS_CONFIGds_cfgr&   _network_config_source_network_configget_url_paramsmax_wait_secondsurl_max_waittimeout_secondsurl_timeout)r,   sys_cfgargskwargs
url_params	__class__s        r$   rQ   zDataSourceOracle.__init__   s    .wHHH(($$W|T[[.I2N!
 'E&F#02q%A((*
&77%55r#   ci_pkl_versionr'   c                     t         |   |       t        | d      st        | dd        t        | d      st        | dt	                      t        | d      sg dd| _        y y )NrR   rX   rY   r1   rO   )rP   	_unpicklehasattrsetattrr&   rY   )r,   rd   rc   s     r$   rf   zDataSourceOracle._unpickle   sd    .)t]+D-.t56(.0
 t./.0Q#?D  0r#   c                 L    t        | j                  j                  dg             S )Nr3   )r)   rY   r=   r+   s    r$   _has_network_configz$DataSourceOracle._has_network_config   s     D((,,Xr:;;r#   c                      t               S )z@Check platform environment to report if this datasource may run.)_is_platform_viabler"   r#   r$   	ds_detectzDataSourceOracle.ds_detect   s     #$$r#   c                    t               | _        t        j                  dd      t        ddt        j                  dd      it
        j                  dd      t        ddt
        j                  dd      if}| j                  r9t        j                         }t        j                  | j                  |dd|      }nt        j                         }| j                          }| j                  j!                  d	t"        d	         }|5  t%        |xs || j&                  | j(                  t
        t        g
      }d d d        syt+        |j,                        | _        |j0                  x}| _        |j4                  | _        |d   |d   d|d   |d   d| _        d|v rQ|d   j!                  d      }|rt;        j<                  |      | _        |d   j!                  d      | j8                  d<   y# 1 sw Y   xY w)Nr2   instancer   path)urlheadersrr   r1   T)distro	interfaceipv6ipv4connectivity_urls_datar   fetch_vnics_datamax_waittimeoutmetadata_patternsFrr   	ociAdNameidr   hostnamedisplayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer:   metadata	user_datassh_authorized_keyspublic_keys) _read_system_uuidsystem_uuidIPV4_METADATA_PATTERNformat
V2_HEADERSIPV6_METADATA_PATTERNperform_dhcp_setupr   find_fallback_nicr   EphemeralIPNetworkrt   r   nullcontext_is_iscsi_rootrW   r=   rV   read_opc_metadatar\   r^    _get_versioned_metadata_base_urlr   metadata_addressr   _crawled_metadatar   rR   r   base64	b64decodeuserdata_raw)	r,   rx   nic_namenetwork_contextfetch_primary_nicfetch_secondary_nicsfetched_metadatadatar   s	            r$   	_get_datazDataSourceOracle._get_data   s6   ,. -33J 4  &	 ,33J 4  -33J 4  &	 ,33J 4 %"
2 "",,.H'::{{"'=O #..0O $ 3 3 55#{{&89 

  		0!2!J6J**(())#	 		   !A ..!
 )9(F(FFt%+66 "&k!2:":.'
 Z(,,[9I$*$4$4Y$?!+/
+;+?+?%,DMM-( M		 		s   2G66G?c                 @    t        j                  | j                        S )zquickly check (local only) if self.instance_id is still valid

        On Oracle, the dmi-provided system uuid differs from the instance-id
        but has the same life-span.)r   instance_id_matches_system_uuidr   )r,   r_   s     r$   check_instance_idz"DataSourceOracle.check_instance_id	  s    
 66t7G7GHHr#   c                 ^    t        j                  | j                  j                  d            S )Nr   )r   normalize_pubkey_datar   r=   r+   s    r$   get_public_ssh_keysz$DataSourceOracle.get_public_ssh_keys  s"    ,,T]]->->}-MNNr#   c                 6    | j                   j                         S )z)Return whether we are on a iscsi machine.)rX   r-   r+   s    r$   r   zDataSourceOracle._is_iscsi_root  s    **88::r#   c                 6    | j                   j                         S N)rX   render_configr+   s    r$   _get_iscsi_configz"DataSourceOracle._get_iscsi_config  s    **88::r#   c                    | j                         r| j                  S d}| j                         r| j                         | _        | j                         st        j                  d       d}| j                  j                  dt        d         }|s|r	 | j                  |       t        | j                         | j                  S # t        $ r t        j                  t        d       Y Fw xY w)zNetwork config is read from initramfs provided files

        Priority for primary network_config selection:
        - iscsi
        - imds

        If none is present, then we fall back to fallback configuration.
        FzLCould not obtain network configuration from initramfs. Falling back to IMDS.Tr   z+Failed to parse IMDS network configuration!)rj   rY   r   r   r;   warningrW   r=   rV   !_add_network_config_from_opc_imds	Exceptionr   logexcrF   )r,   set_primaryset_secondarys      r$   r/   zDataSourceOracle.network_config  s     ##%''' #'#9#9#;D '')KK( K&89
 - 66{C 	!!5!56###  As   C #C+*C+r   c                 b   | j                   t        j                  d       y|s'd| j                   d   v rt        j                  d       yt               }|r| j                   n| j                   dd }t	        |      D ].  \  }}|xr |dk(  }|d   j                         }|j                  dd	      xr |j                  d
d	       }||vrt        j                  d|       e||   }	|rt        j                  |d   d         }
nt        j                  |d         }
| j                  d   dk(  r|r|rddig}nyddig}nsg }|j                  d
      r&|j                  d|d
    d|
j                   d       |j                  d      r)|j                  d|d   d    d|
j                   d       |	d|t        |d}| j                  d   j                  |       a| j                  d   dk(  sut        d|id}|| j                  d   |	<   |xr ||d<   |xr | |d<   |svg |d<   |j                  d
      r&|d   j                  |d
    d|
j                          |j                  d      r)|d   j                  |d   d    d|
j                          || j                  d   |	<   1 y)a  Generate primary and/or secondary NIC config from IMDS and merge it.

        It will mutate the network config to include the secondary VNICs.

        :param set_primary: If True set primary interface.
        :raises:
        Exceptions are not handled within this function.  Likely
            exceptions are KeyError/IndexError
            (if the IMDS returns valid JSON with unexpected contents).
        Nz#NIC data is UNSET but should not benicIndexr   z\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r1   macAddripv6SubnetCidrBlockF	privateIpz)Interface with MAC %s not found; skippingipv6AddressessubnetCidrBlockr   r4   dhcp6dhcpstatic/)r4   addressr5   )r:   r4   r6   mtusubnetsr3   r2   r9   )r   r8   r7   dhcp4	addresses)rR   r;   r   r   	enumeratelowerr=   	ipaddress
ip_networkrY   append	prefixlenMTU)r,   r   interfaces_by_macr   index	vnic_dict
is_primaryr6   is_ipv6_onlyr:   networkr   interface_configs                r$   r   z2DataSourceOracle._add_network_config_from_opc_imdsH  s:    #KK=>
d.>.>q.A A KK9 13)4T%%$:J:J12:N
 !** 5 N	KE9$3!J#I.446K$==%u 8mmK77  "33? $[1D#..o.q1 $..y9J/KL##I.!3#$*G#4"5$*F#3"4 G }}[1(0'0'=&>a'.'8'8&9%; !}}_5(0'0'A!'D&EQ'.'8'8&9%; !&#.&$  $$X.556FG%%i0A5 *K8$  ;K$$[1$7,6,G< ),6,K|;K )!46$[1 }}[1(5<<(56a8I8I7JK !}}_5(5<<(9!<=Q&0013 ;K$$[1$7]N	Kr#   )F)"r   r   r   rU   r   r   NetworkConfigSourceCMD_LINE
SYSTEM_CFGDS	INITRAMFSrJ   r   r    r   r\   r^   rQ   r   rf   r)   rj   staticmethodrm   r   r   r   r   dictr   propertyr/   r   __classcell__)rc   s   @r$   rH   rH   {   s   FK##,,##..##&&##--	GE'"="=s"BC   LK6"@ @ @<T < %t % %TlID IO; ;;4 ; +$ +$ZqKT qKr#   rH   c                       e Zd ZdZy)DataSourceOracleNetFN)r   r   r   r   r"   r#   r$   r   r     s    r#   r   r   c                 V    | sy| j                  t        j                  d      d         S )NFopcr   )
startswithIPV4_METADATA_ROOTsplit)r   s    r$   _is_ipv4_metadata_urlr     s*    &&'9'?'?'Fq'IJJr#   c                  T    t        j                  d      } | d S | j                         S )Nzsystem-uuid)r
   read_dmi_datar   )sys_uuids    r$   r   r     s)      /H#49)99r#   c                  >    t        j                  d      } | t        k(  S )Nzchassis-asset-tag)r
   r   CHASSIS_ASSET_TAG)	asset_tags    r$   rl   rl     s    !!"56I)))r#   rr   c                     d| v rdS dS )Nz/opc/v2/r2   r1   r"   r~   s    r$   _url_versionr     s    c!1(q(r#   c                 .    t        |       dk(  rt        S d S )Nr2   )r   r   r~   s    r$   _headers_cbr     s    %c*a/:9T9r#   c                     | s| S d| v r| j                  d      d   dz   S d| v r| j                  d      d   dz   S t        d| z         )zQ
    Remove everything following the version number in the metadata address.
    v2r   zv2/v1zv1/zInvalid metadata address: )r   
ValueErrorr~   s    r$   r   r     s[     
s{yyq!E))	yyq!E))5;<<r#   ry   rz   r}   c           	         dD cg c]  }|D ]  }|j                  |d        }}}t        j                  d|       t        j                         }t        |||t        dd      \  }}	|s&t        j                  dd	j                  |             y
t        j                  d|       t        j                  |	j                  d            }
t        |      }d
}| rt        |j                  dd      g|t        j                         |z
  z
  |t        dd      \  }}|r;t        j                  |j                  d            }t        j                  d|       nt        j                  d       t        ||
||      S c c}}w )a  
    Fetch metadata from the /opc/ routes from the IMDS.

    Returns:
        Optional[ReadOpcMetadataResponse]: If fetching metadata fails, None.
            If fetching metadata succeeds, a namedtuple containing:
            - The metadata version as an integer
            - The JSON-decoded value of the instance data from the IMDS
            - The JSON-decoded value of the vnics data from the IMDS if
                `fetch_vnics_data` is True, else None. Alternatively,
                None if fetching metadata failed
            - The url that was used to fetch the metadata.
                This allows for later determining if v1 or v2 endppoint was
                used and whether the IMDS was reached via IPv4 or IPv6.
    )r2   r1   ro   rp   z*Attempting to fetch IMDS metadata from: %sg?T)urlsr{   r|   
headers_cb
sleep_timeconnect_synchronouslyz-Failed to fetch IMDS metadata from any of: %sz, Nz7Successfully fetched instance metadata from IMDS at: %szutf-8vnicsz4Successfully fetched vnics metadata from IMDS at: %sz+Failed to fetch IMDS network configuration!)r   r;   r<   time	monotonicr   r   r   joinjsonloadsdecoder   replacer   )rz   r{   r|   r}   r   metadata_patternr   
start_timeurl_that_workedinstance_responser   metadata_versionr   	vnics_urlvnics_responses                  r$   r   r     s   0  1  	jAAD  II:DA!J)5"*&O& ;IIdO	
 		E	
 JJ077@AM $O4J %1!))*g>?!1J!>?""&%
!	> N$9$9'$BCJIIF
 KKEF"	 ks   !E>c                 6    t        j                  | t              S r   )r   list_from_dependsdatasources)dependss    r$   get_datasource_listr  G  s    $$Wk::r#   __main__z
        Query Oracle Cloud metadata and emit a JSON object with two keys:
        `read_opc_metadata` and `_is_platform_viable`.  The values of each are
        the return values of the corresponding functions defined in
        DataSourceOracle.py.)r}   )r   rl   )Ar.   r   r   r   loggingr   typingr   r   r   r   r   r   	cloudinitr	   r
   r   r   r   cloudinit.distros.networkingr   cloudinit.netr   r   r   r   cloudinit.url_helperr   	getLoggerr   r;   rV   r   r   IPV6_METADATA_ROOTr   r   r   r   r   KlibcNetworkConfigSourcer&   rF   
DataSourcerH   r   r!   r   r   r)   rl   r   r   r   r   r\   r^   r   DEP_FILESYSTEMDEP_NETWORKr  r  descriptionprint
json_dumpsr"   r#   r$   <module>r     s4        ? ? < < 6  .g!   & = B *Y6 *Y6 /
 j 	!W%E%E 	!18] 18t 18h~Kw)) ~KB
* KC K:8C= :
*T *
)c )c ):S :Xd38n5 :=# =# =  #**(($9#:PP
 CyP %&Pj ..01""	
	; z K 
   %6.0EF&
 (;'<		
 r#   