o m[h@s$ddlZddlZddlZddlZddlZddlmZddlmZm Z m Z ddl m Z m Z mZddl mZddl mZmZddlmZddlmZmZdd lmZdd lmZdd lmZdd lmZm Z dd l!m"Z"e#e$Z%dZ&dZ'GdddZ(ddZ)e(j*gZ+dZ,Gdddej-Z.Gddde.Z/ddZ0ddZ1ddZ2dd Z3d!d"Z4d#d$Z5d%d&Z6d'd(Z7d)d*Z8d+d,Z9e j:fd-ee;efd.ee;e;fd/e d0ee;ed4e;d5e>d6ed7e?d8ed?ZBd@dAZCe/ejDffe.ejDejEffgZFdBdCZGdS)DN)suppress)DictListLiteral)dminetsources) url_helper)utilwarnings)Distro) EventScope EventType)netplan)NoDHCPLeaseError)EphemeralIPNetwork)HotplugRetrySettingsNicOrder)ec2) datasourceEc2 strict_idwarnc@s(eZdZdZdZdZdZdZdZdZ dS) CloudNamesaws brightboxzstacke24cloudoutscaleunknownzno-ec2-metadataN) __name__ __module__ __qualname__AWS BRIGHTBOXZSTACKE24CLOUDOUTSCALEUNKNOWNNO_EC2_METADATAr*r*A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceEc2.pyr$srcCs|jdko d|jvS)Nzmeta-data/tags/)codeurl) exceptionr*r*r+skip_404_tag_errors3sr0zS ENV{ID_NET_DRIVER}=="vif|ena|ixgbevf", GOTO="cloudinit_hook" GOTO="cloudinit_end" cseZdZUdZgdZdZgdZeee d<dZ dZ dZ e jZd ZejejejejejhiZeZed d d Zfd dZdeddffdd ZddZ ddZ!ddZ"e#ddZ$e#ddZ%e#ddZ&e#dd Z'e#d!d"Z(e#d#d$Z)e#d%d&Z*d'd(Z+d)d*Z,d+d,Z-d-d.Z.d/d0Z/e#d1d2Z0e#d3d4Z1d5d6Z2e#d7d8Z3d9d:Z4dGd;d<Z5d=e6j7de8fd>d?Z9d=e6j7de:d fd@dAZ;d=e6j7de8fdBdCZS)I DataSourceEc2r)zhttp://169.254.169.254zhttp://[fd00:ec2::254]zhttp://instance-data.:8773z 2009-04-04)z 2021-03-23z 2018-09-24z 2016-09-02extended_metadata_versions2NFTcs,tt||||d|_d|_tj|_dSN)superr1__init__metadata_addressidentityrMAC_fallback_nic_order)selfsys_cfgdistropaths __class__r*r+r9os zDataSourceEc2.__init__ci_pkl_versionreturncs,t|t|_tj|_tddd|_dS)NTr5r6) r8 _unpickle_EXTRA_HOTPLUG_UDEV_RULESextra_hotplug_udev_rulesrr<r=rhotplug_retry_settings)r>rDrBr*r+rFus zDataSourceEc2._unpicklecCstS)z5Return the cloud name as identified during _get_data.)identify_platformr>r*r*r+_get_cloud_name{szDataSourceEc2._get_cloud_namecCsBtt|jttd\}}td||j|j |dkr#|jt j kr#dS|jt j kr+dS|j rutr9tddSz1t|j|jjddd}||_td|jrWd |jnd Wdn1sdwYWntytYdSw||_|jsdS|jd d|_|jd d|_|jd ididi|_dS)N)rNz0strict_mode: %s, cloud_name=%s cloud_platform=%strueFz1FreeBSD doesn't support running dhclient with -sfT)ipv4ipv6zCrawled metadata service%s meta-data user-datadynamicinstance-identitydocument)read_strict_moder get_cfg_by_pathr?STRICT_ID_PATHSTRICT_ID_DEFAULTLOGdebug cloud_nameplatformrr(r)perform_dhcp_setup is_FreeBSDrr@fallback_interfacecrawl_metadata_crawled_metadata state_msgrgetmetadata userdata_rawr;)r> strict_mode_sleepnetwr*r*r+ _get_datas`      zDataSourceEc2._get_datacCsL|jsdS|jdidi}|diD] \}}d|vr#dSqdS)z6Report if this instance type is Ec2 Classic (non-vpc).Fnetwork interfacesmacszvpc-idT)rfreitems)r> ifaces_md_macmac_datar*r*r+is_classic_instancesz!DataSourceEc2.is_classic_instancecCs|jsdS|jdS)Nzami-launch-index)rfrerKr*r*r+ launch_indexs zDataSourceEc2.launch_indexcCs|js tj|_|jSr7)_platform_typer1dsnamelowerrKr*r*r+r^s zDataSourceEc2.platformcCdS)Nzlatest/api/tokenr*rKr*r*r+api_token_routezDataSourceEc2.api_token_routecCrx)N21600r*rKr*r*r+imdsv2_token_ttl_secondsrzz&DataSourceEc2.imdsv2_token_ttl_secondscCrx)NzX-aws-ec2-metadata-tokenr*rKr*r*r+imdsv2_token_put_headerrzz%DataSourceEc2.imdsv2_token_put_headercCs |jdS)Nz -ttl-seconds)r}rKr*r*r+imdsv2_token_req_headers z%DataSourceEc2.imdsv2_token_req_headercCs |j|jgSr7)r}r~rKr*r*r+imdsv2_token_redacts z!DataSourceEc2.imdsv2_token_redactc Csd}|}|jD]M}||j|}z tj|||jd}Wntjy7}z t d||WYd}~q d}~ww|j dkrGt d||S|j dkrVd}t |||j q |j S) aEGet the best supported api version from the metadata service. Loop through all extended support metadata versions in order and return the most-fully featured metadata api version discovered. If extended_metadata_versions aren't present, return the datasource's min_metadata_version. z{0}/{1}/meta-data/instance-id)r.headersheaders_redactzurl %s raised exception %sNz#Found preferred metadata version %sr,z0Metadata api version %s not present. Headers: %s) _get_headersr2formatr:uhelpreadurlrUrlErrorr[r\r-rmin_metadata_version)r>url_tmplrapi_verr.respemsgr*r*r+get_metadata_api_versions,      z&DataSourceEc2.get_metadata_api_versioncCsd|jtjkr-t|dds#|}tj||j|j|j |j d di|_ |j d|j dS|j dS)Nr; headers_cbr exception_cbrV instanceIdz instance-id)r]rr#getattrrrget_instance_identityr:rr_refresh_stale_aws_token_cbrer;rf)r> api_versionr*r*r+get_instance_ids    zDataSourceEc2.get_instance_idc Cs|jtvrdSg}i}|j}d}|D]}d||}|||||<qtdd}d}|} ztj || j | j tj |j |j||jdd \}}Wn tjySYnw|r_|r_||_||Std|dS)aGet an API token for EC2 Instance Metadata Service. On EC2. IMDS will always answer an API token, unless the instance owner has disabled the IMDS HTTP endpoint or the network topology conflicts with the configured hop-limit. NPUT{0}/{1}zFetching Ec2 IMDSv2 API TokenF) urlsmax_waittimeout status_cbrrrequest_methodrconnect_synchronouslyz$Unable to get response from urls: %s)r] IDMSV2_SUPPORTED_CLOUD_PLATFORMSryrappendr[r\get_url_paramsr wait_for_urlmax_wait_secondstimeout_secondswarningr_token_exception_cbrr _api_tokenerror) r>mdurlsrurl2baseurl_pathrr.curresponse url_paramsr*r*r+_maybe_fetch_api_tokensD       z$DataSourceEc2._maybe_fetch_api_tokenc Csxg}d}|j}|}|jdkrdS|d|j}dd|D}t|t|kr6tdtt|t|t |r=|}nt d|j}| |}|s|j t vri}dj|jd } d } |D]} d | | } || | || <q^t}tj||j|jtj |j|j| d \} } | r|| }|r||_td |jt|S|j t vrt dt|Std|tt|t|S)NrF metadata_urlscSsg|] }t|r|qSr*)r is_resolvable_url).0xr*r*r+ Qsz;DataSourceEc2.wait_for_metadata_service..z,Removed the following from metadata urls: %sz+Empty metadata url list! using default listz{ver}/meta-data/instance-id)verGETr)rrrrrrrzUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabledz(Giving up on md from %s after %s seconds)ds_cfgrrrersetr[r\listlenrrr]rrrrtime monotonicrrrrrr:criticalintbool)r>r start_timemcfgrrfilteredr:rrrr.r_r*r*r+wait_for_metadata_serviceFsn          z'DataSourceEc2.wait_for_metadata_servicecCsd|jvrdSd}|jd}t|tstd|dS|D]\}}||kr+|}n |dkr5|dkr5|}q|durBtd|dS|}|dsMd|}tj |rU|S| tj |}|ritd|||S|dkrodS|S) Nzblock-device-mappingz+block-device-mapping not a dictionary: '%s' ephemeral ephemeral0z Unable to convert %s to a device/z/dev/%szRemapped device name %s => %s) rf isinstancedictr[r\ro startswithospathexists _remap_devicebasename)r>namefoundbdmentnamedeviceofoundremappedr*r*r+device_name_to_devices:       z#DataSourceEc2.device_name_to_devicecCsLz|jtjkr|jd|jddWS|jddWSty%YdSw)NavailabilityZone placementzavailability-zone)r]rr#r;rerfKeyErrorrKr*r*r+availability_zones   zDataSourceEc2.availability_zonecCsR|jtjkr|jd}|jr|s|jdd}|S|j}|dur'|ddSdS)Nregion)r]rr#r;rer)r>razr*r*r+rs    zDataSourceEc2.regioncCs0|sdS|jtjkrtt|tt|dSdSr7)r]rr(warn_if_necessaryr rXrYrZ)r>cfgis_new_instancer*r*r+activates  zDataSourceEc2.activatec Cs|jtjkr |jS|jdurtddSd}|jj}|jd}t |t rWt ||j|t |jdd|jd}|rVt|j|_|jtjtj|jtjtjntd|||_|jS)z@Return a network config dict for rendering ENI or netplan files.Nz8Unexpected call to network_config when metadata is None.rlapply_full_imds_network_configT) fallback_nicfull_network_configfallback_nic_orderz%Metadata 'network' key not valid: %s.)_network_configrUNSETrfr[rr@rarerr#convert_ec2_metadata_network_configr get_cfg_option_boolrr=rscopydeepcopydefault_update_eventsr NETWORKaddrBOOT BOOT_LEGACY)r>resultifacenet_mdr*r*r+network_configsD        zDataSourceEc2.network_configc Cs |siS|}|j}i}|jtvr|j}|j}d}n|jtjkr*d}}t }nd}}}zrredactcrawled_metadataexc_cb exc_cb_udskip_cb raw_userdatar;r*r*r+rbsd      zDataSourceEc2.crawl_metadatac Cs|jtvrdS|dur|j}td|j|i}d|j|j}zt j |||j dd}W|jSt j yI}zt d||WYd}~dSd}~ww)zRequest new metadata API token. @param seconds: The lifetime of the token in seconds @return: The API token or None if unavailable. Nz!Refreshing Ec2 metadata API tokenz{}/{}r)rrrz/Unable to get API token: %s raised exception %s)r]rr|r[r\r~rr:ryrrrrrcontents)r>secondsrequest_header token_urlrrr*r*r+_refresh_api_tokenOs,    z DataSourceEc2._refresh_api_tokenr/cCs ttj|}|s dS||S)zSCallback will not retry on SKIP_USERDATA_CODES or if no token is available.F)rskip_retry_on_codesSKIP_USERDATA_CODESr)r>r/retryr*r*r+rls z1DataSourceEc2._skip_or_refresh_stale_aws_token_cbcCs|jdkr tdd|_dS)z=Exception handler for Ec2 to refresh token if token is stale.iz+Clearing cached Ec2 API token due to expiryNT)r-r[r\rr>r/r*r*r+rvs  z)DataSourceEc2._refresh_stale_aws_token_cbcCsH|jr"|jdkrtddS|jdkrdS|jdkr"tddSdS)a2Fail quickly on proper AWS if IMDSv2 rejects API token request Guidance from Amazon is that if IMDSv2 had disabled token requests by returning a 403, or cloud-init malformed requests resulting in other 40X errors, we want the datasource detection to fail quickly without retries as those symptoms will likely not be resolved by retries. Exceptions such as requests.ConnectionError due to IMDS being temporarily unroutable or unavailable will still retry due to the callsite wait_for_url. izLEc2 IMDS endpoint returned a 403 error. HTTP endpoint is disabled. Aborting.Fiiz2Fatal error while requesting Ec2 IMDSv2 API tokensT)r-r[rr r*r*r+rs   z!DataSourceEc2._token_exception_cbrQcCsN|jtvriS|j|ji}|j|vr|S|js!||_|js!iS|j|jiS)zReturn a dict of headers for accessing a url. If _api_token is unset on AWS, attempt to refresh the token via a PUT and then return the updated token header. )r]rr~r|ryrr r})r>r.request_token_headerr*r*r+rs    zDataSourceEc2._get_headersr7)rQ)?r r!r"rvrrr2rstr__annotations__ url_max_wait url_timeoutrrrrr_r rrBOOT_NEW_INSTANCErrHOTPLUGsupported_update_eventsrGrHrrIr9rrFrLrkrspropertyrtr^ryr|r}r~rrrrrrrrrrrbr rrrrrrrr __classcell__r*r*rBr+r1Bs   3       3J3    / 8    r1cs$eZdZdZdZfddZZS)DataSourceEc2LocalayDatasource run at init-local which sets up network to query metadata. In init-local, no network is available. This subclass sets up minimal networking with dhclient on a viable nic so that it can talk to the metadata service. If the metadata service provides network configuration then render the network configuration for that instance based on metadata. Tcs8tjtjf}|j|vrtd||jdStt|S)Nz+Local Ec2 mode only supported on %s, not %sF) rr#r'r]r[r\r8rget_data)r>supported_platformsrBr*r+rs  zDataSourceEc2Local.get_data)r r!r"__doc__r_rrr*r*rBr+rsrc Cs>zt|WSty}z t||WYd}~Sd}~wwr7)parse_strict_mode ValueErrorr[r)cfgvaldefaultrr*r*r+rWs  rWc Cs|durdS|dur dS|sdS|d\}}}|dvr$td||f|rEz t|}W||fStyD}z td ||f|d}~wwd}||fS) NT)rMNF)falseN)rr,)rMr rzUInvalid mode '%s' in strict_id setting '%s': Expected one of 'true', 'false', 'warn'.z4sz#identify_platform..z'calling %s with %s raised exception: %s) _collect_platform_datar-r2r5r7r9rr[r)r+checkscheckerrrr*r*r+rJ+s* rJcCsd}ttttd}Wdn1swY|p&tdp&d}tdp-d}tdp4d}tdp;d}tdpBd}|||||d S) aFReturns a dictionary of platform info from dmi or /sys/hypervisor. Keys in the dictionary are as follows: uuid: system-uuid from dmi or /sys/hypervisor serial: dmi 'system-serial-number' (/sys/.../product_serial) asset_tag: 'dmidecode -s chassis-asset-tag' vendor: dmi 'system-manufacturer' (/sys/.../sys_vendor) product_name: dmi 'system-product-name' (/sys/.../system-manufacturer) On Ec2 instances experimentation is that product_serial is upper case, and product_uuid is lower case. This returns lower case values for both. Nz/sys/hypervisor/uuidz system-uuidrQzsystem-serial-numberzchassis-asset-tagzsystem-manufacturerzsystem-product-namer'r/r4r6r8) rOSErrorUnicodeDecodeErrorr load_text_filestripr read_dmi_datarwr>r*r*r+r;As r; macs_metadata macs_to_nicsrrEcs^i}rs|Stddtfdd}ddddtt|fddd DS) aI Builds a dictionary containing macs as keys and nic orders as values, taking into account `network-card` and `device-number` if present. Note that the first NIC will be the primary NIC as it will be the one with [network-card] == 0 and device-number == 0 if present. @param macs_metadata: dictionary with mac address as key and contents like: {"device-number": "0", "interface-id": "...", "local-ipv4s": ...} @macs_to_nics: dictionary with mac address as key and nic name as value @return: Dictionary with macs as keys and nic orders as values. cSs |dduS)Nr*mmdr*r*r+r:ys z"_build_nic_order..cs|||fSr7)re)mac)rDrEr*r+r:|scSs ||d}|durt|S|Sr7)rer)diktkey alt_valuevaluer*r*r+_get_key_as_int_ors z,_build_nic_order.._get_key_as_int_orcSsi|] \}\}}}||qSr*r*)rirI _mac_metadata _nic_namer*r*r+ s z$_build_nic_order..csB|ddtd|ddtdtjkr|dfS|dfS)NrFz network-cardinfinity device-numberr)floatrNIC_NAMErG)rNrr*r+r:s  )rK)filtermapkeys enumeratesorted)rDrEr nic_ordervalid_macs_metadatar*)rNrrDrEr+_build_nic_orderas&    r_ dev_confignic_name nic_metadatar@is_ipv4tablec CsJ|r |d}|d}n |d}|d}|r|s*td|r%ddSddS|d s3g|d <|rhz|jj||d }|d } Wnty\} z td || WYd} ~ nd} ~ ww|d d | |dt|t rp|gn|}|D] } |d | |dqt|dsg|d<t|t r|gn|}|D] } |d| |dqdS)a Configure policy-based routing on secondary NICs / secondary IPs to ensure outgoing packets are routed via the correct interface. @param: dev_config: network cfg v2 to be updated inplace. @param: nic_name: nic name. Only used if ipv4. @param: nic_metadata: nic metadata from IMDS. @param: distro: Instance of Distro. Only used if ipv4. @param: is_ipv4: Boolean indicating if we are acting over ipv4 or not. @param: table: Routing table id. subnet-ipv4-cidr-block local-ipv4szsubnet-ipv6-cidr-blocksipv6szANot enough IMDS information to configure policy routing for IPv%s46Nroutes)r@routerszoCould not perform dhcp discovery on %s to find its gateway. Not adding default route via the gateway. Error: %sz 0.0.0.0/0)toviard)rlrdrouting-policy)fromrd) rer[r\ dhcp_clientdhcp_discoveryrrrrr) r`rarbr@rcrdsubnet_prefix_routesipsleasegatewayr prefix_routeipr*r*r+_configure_policy_routingsl         rxTc Csdid}|s t}|dd}|sC|D] \}} | |kr!nqddd|i| d} ||} | d r;d| d <| |d | <|St|jtj} t |||} t | }|D]{}||} ||} | sfqX| |}|d k}d |ddi}d|dd|i| d} d|}| r| dr|sd|d<t | || | d|d| d rd| d <|| d<| r| dr|st | || | d|dt | || d<| ds| d| |d | <qXt|d dkr |d  D])} |d | d|d | dd|d | dd|d | ddq|S)a8Convert ec2 metadata to network config version 2 data dict. @param: network_md: 'network' portion of EC2 metadata. generally formed as {"interfaces": {"macs": {}} where 'macs' is a dictionary with mac address as key and contents like: {"device-number": "0", "interface-id": "...", "local-ipv4s": ...} @param: distro: instance of Distro. @param: macs_to_nics: Optional dict of mac addresses and nic names. If not provided, get_interfaces_by_mac is called to get it from the OS. @param: fallback_nic: Optionally provide the primary nic interface name. This nic will be guaranteed to minimally have a dhcp4 configuration. @param: full_network_config: Boolean set True to configure all networking presented by IMDS. This includes rendering secondary IPv4 and IPv6 addresses on all NICs and rendering network config on secondary NICs. If False, only the primary nic will be configured and only with dhcp (IPv4/IPv6). @return A dict of network config version 2 based on the metadata and macs. rU)version ethernetsrmrnTF macaddress)dhcp4dhcp6matchset-namergr}rzrz route-metricrFd)r|dhcp4-overridesr}r~rrTz use-routes)r@rarbrcrdzdhcp6-overrides addressesrNrjrn)rget_interfaces_by_macrorwrernetwork_rendererrRendererr_r\rZrxget_secondary_addressespopr) network_mdr@rErrrnetcfgrDrIrar`rb is_netplanr]rnnic_idxis_primary_nic dhcp_overriderdr*r*r+rs           rcCs|d}|d}g}tt|tot|dkr$|t|d||dtt|to/t|dkr<|t|d||dt|S)zParse interface-specific nic metadata and return any secondary IPs :return: List of secondary IPv4 or IPv6 addresses to configure on the interface rfrgrFre24zsubnet-ipv6-cidr-block128)rerrrrextend_get_secondary_addressesr\)rbrIipv4srgrr*r*r+rus    rc Csg}||}|}|rt|ddkr'd|vrdnd}td|||||n|dd}|ddD] } |dj| |d q4|S) zReturn list of IP addresses as CIDRs for secondary IPs The CIDR prefix will be default_prefix if cidr_key is absent or not parseable in nic_metadata. rrUrNrOzJCould not parse %s %s for mac %s. %s network config prefix defaults to /%srFNz {ip}/{prefix})rwprefix)rersplitr[rrr) rbcidr_keyrIrsdefault_prefixrcidrrip_typerwr*r*r+rs"  rcCs t|tSr7)rlist_from_depends datasources)dependsr*r*r+get_datasource_lists r)Hrloggingrrr' contextlibrtypingrrr cloudinitrrrr rr r cloudinit.distrosr cloudinit.eventr r cloudinit.netrcloudinit.net.dhcprcloudinit.net.ephemeralrcloudinit.sourcesrrcloudinit.sources.helpersr getLoggerr r[rYrZrr0r#rrG DataSourcer1rrWrrr-r2r5r7r9rJr;r<rrr_rrrxrrrDEP_FILESYSTEM DEP_NETWORKrrr*r*r*r+s         y! #    C X |