
    x[h2              	          S SK r S SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
JrJrJrJr  S SKJrJrJr  S SKJrJr  S SKJr  S S	KJrJrJr  \R8                  " \5      rS
SS.SSSS.SSSSSS/S.r " S S\	5      r  " S S\RB                  5      r" " S S\"5      r#\#\RH                  44\"\RH                  \RJ                  44/r&S\\RB                     4S jr'g)    N)	b64decode)suppress)Enum)AnyListTupleUnion)sources
url_helperutil)find_fallback_nicget_interfaces_by_mac)EphemeralIPNetwork)get_dmi_configget_local_instance_idis_on_akamaizhttp://169.254.169.254zhttp://[fd00:a9fe:a9fe::1]ipv4ipv6z	/v1/tokenz/v1/instancez/v1/user-data)tokenmetadatauserdataTzf2:3)	base_urlspathsallow_local_stageallow_init_stage
allow_dhcp
allow_ipv4
allow_ipv6preferred_mac_prefixesc                   $    \ rS rSrSrSrSrSrSrg)MetadataAvailabilityResult-   z^
Used to indicate how this instance should behave based on the availability
of metadata to it
r          N)	__name__
__module____qualname____firstlineno____doc__NOT_AVAILABLE	AVAILABLEDEFER__static_attributes__r&       D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAkamai.pyr"   r"   -   s    
 MIEr0   r"   c                       \ rS rSrSrSrS rSS\S\S\4S jjr	S\
4S	 jrS\
4S
 jrS\
4S jrS\\\\\4   \4      4S jrSS\S\4S jjrS\4S jrS\4S jrSrg)DataSourceAkamai8   AkamaiFc                    [         R                  S5        [        R                  R	                  XX#5        [        5       U l        [        R                  " [        5       [        R                  " USS/0 5      [        /5      U l        g )NzSetting up Akamai DataSource
datasourcer5   )LOGdebugr
   
DataSource__init__dictr   r   mergemanydictr   get_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistror   s       r1   r;   DataSourceAkamai.__init__<   sn    		01##D6A (( $$!8,
 "

r0   	path_nameuse_v6returnc                    XR                   S   ;  a  [        SR                  U5      5      eSnU(       d  U R                   S   (       d  SnU R                   S   U   nU R                   S   U   nSR                  XE5      $ )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
r   zUnknown path name {}r   r   r   r   z{}{})r@   
ValueErrorformat)rA   rE   rF   version_keybase_urlpaths         r1   
_build_urlDataSourceAkamai._build_urlN   sw     KK003::9EFF\2 K;;{+K8{{7#I.}}X,,r0   c                 N   U R                   S   (       d  U R                   S   (       a(  U R                   S   (       d9  U R                   S   (       d%  [        R                  S5        [        R                  $ U R
                  (       a  U R                  5       $ U R                  5       $ )z
Returns whether metadata should be retrieved at this stage, at the next
stage, or never, in the form of a MetadataAvailabilityResult.
r   r   r   r   z*Configuration prohibits fetching metadata.)r@   r8   infor"   r,   local_stage_should_fetch_data_local_should_fetch_data_networkrA   s    r1   _should_fetch_data#DataSourceAkamai._should_fetch_data`   sw     L)$++l2K/0KK 23 HHAB-;;;00222244r0   c                 .   U R                   S   (       d%  [        R                  S5        [        R                  $ U R                   S   (       d9  U R                   S   (       d%  [        R                  S5        [        R                  $ [        R
                  $ )zo
Returns whether metadata should be retrieved during the local stage, or
if it should wait for the init stage.
r   z)Configuration prohibits local stage setupr   r   z9Configuration does not allow for ephemeral network setup.r@   r8   rQ   r"   r.   r-   rU   s    r1   rS   )DataSourceAkamai._should_fetch_data_localt   sl    
 {{./HH@A-333{{<(\1J HHK .333)333r0   c                     U R                   S   (       d%  [        R                  S5        [        R                  $ [        R
                  $ )zC
Returns whether metadata should be fetched during the init stage.
r   z1Configuration does not allow for init stage setuprY   rU   s    r1   rT   +DataSourceAkamai._should_fetch_data_network   s6     {{-.HHHI-333)333r0   c           	          / nU R                   (       Ga  [        5       nSnU R                  S   nUR                  5        H8  u  pV[	        U Vs/ s H  ouR                  U5      PM     sn5      (       d  M6  Un  O   Uc  [        R                  S5        [        5       n/ nU R                  S   (       a'  UR                  [        U R                  USSS9S45        U R                  S   (       a:  U R                  S	   (       a&  UR                  [        U R                  USS
9S45        U$ U R                  S   (       a  UR                  [        5       S45        U R                  S   (       a  UR                  [        5       S45        U$ s  snf )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.
Nr    zGFailed to find default interface, attempting DHCP on fallback interfacer   FTr   r   r   )r   )rR   r   r@   itemsany
startswithr8   warningr   appendr   rC   noop)rA   network_context_managers
interfaces	interfacepreferred_prefixesmacinfprefixs           r1   _get_network_context_managers.DataSourceAkamai._get_network_context_managers   s     	! 
 /0JI!%-E!F&,,.:LM:L^^F+:LM  !$I /  ) ./	')${{<((//* KK%!&!%	 
 {{<(T[[-F(//* KK%!%
 	6 ('! {{<((// {{<((// ('o Ns   F
c           	           [         R                  " U R                  SUS9SSSSSS0S	9nUR                  S
:w  a!  [        R                  SUR                  5        g[        U5      n[         R                  " U R                  SUS9SSSSUS.S9n[        R                  " [        U5      5      U l	        [         R                  " U R                  SUS9SSSSU0S9n[        U5      U l
         [        U R                  5      U l
        g! [        R                   a   n[        R                  SU5         SnAgSnAff = f! [         R                   a*  n[        R                  SU(       a  SOSU5         SnAgSnAff = f)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.
r   rF   PUT   r%      zMetadata-Token-Expiry-Seconds300)request_methodtimeoutsec_betweenretriesheaders   z-Fetching token returned %s; not fetching dataTr   zapplication/json)AcceptMetadata-Token)rt   ru   rv   rw   r   rz   z*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r   readurlrN   coder8   rQ   strjsonloadsr   userdata_rawr   binasciiErrorra   UrlError)rA   rF   token_responser   r   r   es          r1   _fetch_metadata DataSourceAkamai._fetch_metadata   s   :	'//7$3U	N ""c)C"'' 'E "))
6:0&+	H !JJs8}5DM "))
6:$eH !$HDM$-d.?.?$@!  >> MH!LL M"" 		 KKC3
 		sC   AD? BD? -D D<D72D? 7D<<D? ?E= E88E=c                    [         R                  S5        [        5       (       d  [         R                  S5        g[	        5       nSU0U l        U R                  5       nU[        R                  :w  a@  U[        R                  :X  a  [         R                  S5        g[         R                  S5        gU R                  5       nU H[  u  pEU   U R                  US9nU(       a4  U R
                  R                  S	U5      U R
                  S'    S
S
S
5          g S
S
S
5        M]     [         R                  S5        g! , (       d  f       M  = f)zG
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.rn   idNzHFailed to contact metadata service, falling back to local metadata only.)r8   r9   r   rQ   r   r   rV   r"   r-   r,   rk   r   getra   )rA   local_instance_idavailabilityrd   managerrF   dones          r1   	_get_dataDataSourceAkamai._get_data'  s,    			78~~HH:;13,
 ..05???9GGGK HH! #'#E#E#G 7OG++6+:37==3D3D)4DMM-0  & #    8 KK!
 ' s   AD99
E	c                 L    [         R                  " U R                  5       S5      $ )zR
A local-only check to see if the instance id matches the id we see on
the system
zsystem-serial-number)r
   instance_id_matches_system_uuidget_instance_id)rA   rB   s     r1   check_instance_id"DataSourceAkamai.check_instance_id[  s&    
 66  "$:
 	
r0   )r@   r   r   N)F)r'   r(   r)   r*   dsnamerR   r;   r   boolrN   r"   rV   rS   rT   r   r   r	   r   r   rk   r   r   r   r/   r&   r0   r1   r3   r3   8   s    FK
$-C - -# -$5$> 5(4*D 4*	4,F 	4M(	eE#112D89	:M(^Bd Bt BH24 2h
D 
r0   r3   c                       \ rS rSrSrSrSrg)DataSourceAkamaiLocalie  z
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.
Tr&   N)r'   r(   r)   r*   r+   rR   r/   r&   r0   r1   r   r   e  s     Kr0   r   rG   c                 8    [         R                  " U [        5      $ )N)r
   list_from_dependsdatasources)dependss    r1   get_datasource_listr     s    $$Wk::r0   )(r   r   loggingbase64r   
contextlibr   rc   enumr   typingr   r   r   r	   	cloudinitr
   r   r   cloudinit.netr   r   cloudinit.net.ephemeralr    cloudinit.sources.helpers.akamair   r   r   	getLoggerr'   r8   r?   r"   r:   r3   r   DEP_FILESYSTEMDEP_NETWORKr   r   r&   r0   r1   <module>r      s       '  * * / / B 6  !
 ),
 "#  	% 0 j
w)) j
Z	,  W3356 	""	
	 ;D););$< ;r0   