a 'Dg2 @s.ddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZmZddlmZmZddlmZdd lmZmZmZeeZd d d d dddddddddgdZGddde Z Gdddej!Z"Gddde"Z#e#ej$ffe"ej$ej%ffgZ&e ej!dddZ'dS)N) b64decode)suppress)Enum)AnyListTupleUnion)sources url_helperutil)find_fallback_nicget_interfaces_by_mac)EphemeralIPNetwork)get_dmi_configget_local_instance_id is_on_akamaizhttp://169.254.169.254zhttp://[fd00:a9fe:a9fe::1]ipv4ipv6z /v1/tokenz /v1/instancez /v1/user-data)tokenmetadatauserdataTzf2:3) base_urlspathsallow_local_stageallow_init_stage allow_dhcp allow_ipv4 allow_ipv6preferred_mac_prefixesc@seZdZdZdZdZdZdS)MetadataAvailabilityResultzj Used to indicate how this instance should behave based on the availability of metadata to it rN)__name__ __module__ __qualname____doc__ NOT_AVAILABLE AVAILABLEDEFERr*r*F/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceAkamai.pyr -sr c@seZdZdZdZddZdeeedddZe dd d Z e dd d Z e dd dZ e eeeefefdddZdeedddZedddZedddZdS)DataSourceAkamaiAkamaiFcCsJtdtj||||t|_tt t |ddgit g|_ dS)NzSetting up Akamai DataSourceZ datasourcer-) LOGdebugr DataSource__init__dictrr Z mergemanydictrZget_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistrorr*r*r+r1<s zDataSourceAkamai.__init__) path_nameuse_v6returncCsZ||jdvrtd|d}|s.|jds2d}|jd|}|jd|}d||S)z Looks up the path for a given name and returns a full url for it. If use_v6 is passed in, the IPv6 base url is used; otherwise the IPv4 url is used unless IPv4 is not allowed in ds_cfg rzUnknown path name {}rrrrz{}{})r4 ValueErrorformat)r5r8r9Z version_keyZbase_urlpathr*r*r+ _build_urlNszDataSourceAkamai._build_urlr:cCsR|jds|jdr(|jds8|jds8tdtjS|jrF|S|SdS)z Returns whether metadata should be retrieved at this stage, at the next stage, or never, in the form of a MetadataAvailabilityResult. rrrrz*Configuration prohibits fetching metadata.N)r4r.infor r' local_stage_should_fetch_data_local_should_fetch_data_networkr5r*r*r+_should_fetch_data`s z#DataSourceAkamai._should_fetch_datacCsD|jdstdtjS|jds>|jds>tdtjStjS)z Returns whether metadata should be retrieved during the local stage, or if it should wait for the init stage. rz)Configuration prohibits local stage setuprrz9Configuration does not allow for ephemeral network setup.r4r.r@r r)r(rDr*r*r+rBts  z)DataSourceAkamai._should_fetch_data_localcCs |jdstdtjStjS)zS Returns whether metadata should be fetched during the init stage. rz1Configuration does not allow for init stage setuprFrDr*r*r+rCs  z+DataSourceAkamai._should_fetch_data_networkcsg}|jrt}d}|jd}|D]&\}tfdd|Dr&|}qNq&|durftdt}g}|jdr|t |j |ddd df|jd r|jd r|t |j |dd dfn4|jdr|t df|jd r|t df|S) z Returns a list of context managers which should be tried when setting up a network context. If we're running in init mode, this return a noop since networking should already be configured. Nrcsg|]}|qSr*) startswith).0prefixZmacr*r+ zBDataSourceAkamai._get_network_context_managers..zGFailed to find default interface, attempting DHCP on fallback interfacerFTrrr)r) rAr r4itemsanyr.warningr appendrr7noop)r5network_context_managersZ interfacesZ interfaceZpreferred_prefixesinfr*rJr+_get_network_context_managerssh      z.DataSourceAkamai._get_network_context_managers)r9r:c Cs:ztj|jd|dddddddid }|jd krDtd |jWd St|}tj|jd |ddddd|dd}tt||_ tj|jd|ddddd|id}t||_ zt |j |_ Wn2t j y}ztd|WYd}~n d}~00WnBtjy4}z&td|rdnd|WYd}~dSd}~00d S)z Runs through the sequence of requests necessary to retrieve our metadata and user data, creating a token for use in doing so, capturing the results. rr9ZPUTr"zMetadata-Token-Expiry-SecondsZ300)Zrequest_methodtimeout sec_betweenretriesheadersz-Fetching token returned %s; not fetching dataTrzapplication/json)ZAcceptMetadata-Token)rXrYrZr[rr]z*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r Zreadurlr>coder.r@strjsonloadsrZ userdata_rawrbinasciiErrorrOZUrlError)r5r9Ztoken_responserrrer*r*r+_fetch_metadatas`      & z DataSourceAkamai._fetch_metadatac CstdtstddSt}d|i|_|}|tjkrf|tj krXtddStddS| }|D]`\}}|D|j |d}|r|j d ||jd<Wd qWd qr1s0Yqrt d dS) zW Overrides _get_data in the DataSource class to actually retrieve data z#Getting data from Akamai DataSourcez#Not running on Akamai, not running.Fz instance-idz5Metadata is not available, returning local data only.TzFConfigured not to fetch data at this stage; waiting for a later stage.rUidNzHFailed to contact metadata service, falling back to local metadata only.)r.r/rr@rrrEr r(r'rTrggetrO)r5Zlocal_instance_idZ availabilityrRZmanagerr9Zdoner*r*r+ _get_data's@       0zDataSourceAkamai._get_datacCst|dS)zj A local-only check to see if the instance id matches the id we see on the system zsystem-serial-number)r Zinstance_id_matches_system_uuidZget_instance_id)r5r6r*r*r+check_instance_id[sz"DataSourceAkamai.check_instance_idN)F)F)r#r$r%ZdsnamerAr1raboolr>r rErBrCrrrrrrTrgrjrkr*r*r*r+r,8s  OD4r,c@seZdZdZdZdS)DataSourceAkamaiLocalz A subclass of DataSourceAkamai that runs the same functions, but during the init-local stage. This allows configuring networking via cloud-init, as networking hasn't been configured yet. TN)r#r$r%r&rAr*r*r*r+rmesrmr?cCs t|tS)N)r Zlist_from_depends datasources)Zdependsr*r*r+get_datasource_listsro)(rdrbZloggingbase64r contextlibrrQenumrtypingrrrrZ cloudinitr r r Z cloudinit.netr r Zcloudinit.net.ephemeralrZ cloudinit.sources.helpers.akamairrrZ getLoggerr#r.r3r r0r,rmZDEP_FILESYSTEMZ DEP_NETWORKrnror*r*r*r+sN      /