
    }f2              	          d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d d	lmZmZmZ  ej8                  e      Zd
ddddddddddddgdZ G d de	      Z  G d dejB                        Z" G d de"      Z#e#ejH                  ffe"ejH                  ejJ                  ffgZ&deejB                     fdZ'y)    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                       e Zd ZdZdZdZdZy)MetadataAvailabilityResultzj
    Used to indicate how this instance should behave based on the availability
    of metadata to it
    r         N)__name__
__module____qualname____doc__NOT_AVAILABLE	AVAILABLEDEFER     D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAkamai.pyr"   r"   -   s    
 MIEr-   r"   c                       e Zd ZdZdZd ZddededefdZde	fdZ
de	fd	Zde	fd
Zdeeeeef   ef      fdZddedefdZdefdZdefdZy)DataSourceAkamaiAkamaiFc                    t         j                  d       t        j                  j	                  | |||       t               | _        t        j                  t               t        j                  |ddgi       t        g      | _        y )NzSetting up Akamai DataSource
datasourcer1   )LOGdebugr
   
DataSource__init__dictr   r   mergemanydictr   get_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistror   s       r.   r7   zDataSourceAkamai.__init__<   sr    		01##D'65A (( $$!8,
 "

r-   	path_nameuse_v6returnc                     || j                   d   vrt        dj                  |            d}|s| j                   d   sd}| j                   d   |   }| j                   d   |   }dj                  ||      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
        r   zUnknown path name {}r   r   r   r   z{}{})r<   
ValueErrorformat)r=   r@   rA   version_keybase_urlpaths         r.   
_build_urlzDataSourceAkamai._build_urlN   s{     DKK003::9EFF\2 K;;{+K8{{7#I.}}Xt,,r-   c                    | j                   d   s| j                   d   r| j                   d   s4| j                   d   s%t        j                  d       t        j                  S | j
                  r| j                         S | j                         S )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<   r4   infor"   r)   local_stage_should_fetch_data_local_should_fetch_data_networkr=   s    r.   _should_fetch_dataz#DataSourceAkamai._should_fetch_data`   sw     L)$++l2K/0KK 23 HHAB-;;;00222244r-   c                    | j                   d   s%t        j                  d       t        j                  S | j                   d   s4| j                   d   s%t        j                  d       t        j                  S t        j
                  S )z
        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<   r4   rK   r"   r+   r*   rO   s    r.   rM   z)DataSourceAkamai._should_fetch_data_localt   sl    
 {{./HH@A-333{{<(\1J HHK .333)333r-   c                     | j                   d   s%t        j                  d       t        j                  S t        j
                  S )zS
        Returns whether metadata should be fetched during the init stage.
        r   z1Configuration does not allow for init stage setuprR   rO   s    r.   rN   z+DataSourceAkamai._should_fetch_data_network   s6     {{-.HHHI-333)333r-   c           	         g }| j                   rt               }d}| j                  d   }|j                         D ]1  \  }}t	        |D cg c]  }|j                  |       c}      s/|} n |t        j                  d       t               }g }| j                  d   r*|j                  t        | j                  |dd      df       | j                  d   r8| j                  d	   r)|j                  t        | j                  |d
      df       |S | j                  d   r|j                  t               df       | j                  d   r|j                  t               df       |S c c}w )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   )rL   r   r<   itemsany
startswithr4   warningr   appendr   r?   noop)r=   network_context_managers
interfaces	interfacepreferred_prefixesmacinfprefixs           r.   _get_network_context_managersz.DataSourceAkamai._get_network_context_managers   s     	! 
 /0JI!%-E!F&,,. S:LMS^^F+M !$I  ) ./	')${{<((//* KK%!&!%	 
 {{<(T[[-F(//* KK%!%
 	6 ('! {{<((// {{<((// ('o Ns   
E*
c           	      (   	 t        j                  | j                  d|      ddddddi	      }|j                  d
k7  r!t        j                  d|j                         yt        |      }t        j                  | j                  d|      dddd|d      }t        j                  t        |            | _	        t        j                  | j                  d|      dddd|i      }t        |      | _
        	 t        | j                        j                         | _
        y# t        j                  $ r }t        j                  d|       Y d}~yd}~ww xY w# t         j                   $ r%}t        j                  d|rdnd|       Y d}~yd}~ww xY w)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   rA   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)rj   rk   rl   rm   r   rp   z*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r   readurlrI   coder4   rK   strjsonloadsr   userdata_rawr   decodebinasciiErrorrX   UrlError)r=   rA   token_responser   r   r   es          r.   _fetch_metadataz DataSourceAkamai._fetch_metadata   s   :	'//7$3U	N ""c)C"'' 'E "))
6:0&+	H !JJs8}5DM "))
6:$eH !$HDM$-d.?.?$@$G$G$I!  >> MH!LL M"" 		 KKC3
 		sC   AE  BE :(D# #E6EE EE F,FFc                 v   t         j                  d       t               st         j                  d       yt	               }d|i| _        | j                         }|t        j                  k7  r?|t        j                  k(  rt         j                  d       yt         j                  d       y| j                         }|D ]Y  \  }}|5  | j                  |      }|r4| j
                  j                  d	|      | j
                  d<   	 d
d
d
        y	 d
d
d
       [ t         j                  d       y# 1 sw Y   {xY w)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.rd   idNzHFailed to contact metadata service, falling back to local metadata only.)r4   r5   r   rK   r   r   rP   r"   r*   r)   rb   r   getrX   )r=   local_instance_idavailabilityr[   managerrA   dones          r.   	_get_datazDataSourceAkamai._get_data'  s?    			78~HH:;13,
 ..05???9GGGK HH! #'#E#E#G 7 	OGV ++6+:37==3D3D)4DMM-0  & #  	 KK!
 ' s   ?D//D8	c                 J    t        j                  | j                         d      S )zj
        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)r=   r>   s     r.   check_instance_idz"DataSourceAkamai.check_instance_id[  s&    
 66  "$:
 	
r-   N)F)r%   r&   r'   dsnamerL   r7   ru   boolrI   r"   rP   rM   rN   r   r   r	   r   r   rb   r   r   r   r,   r-   r.   r0   r0   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 
r-   r0   c                       e Zd ZdZdZy)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(   rL   r,   r-   r.   r   r   e  s     Kr-   r   rB   c                 6    t        j                  | t              S )N)r
   list_from_dependsdatasources)dependss    r.   get_datasource_listr     s    $$Wk::r-   )(rz   rv   loggingbase64r   
contextlibr   rZ   enumr   typingr   r   r   r	   	cloudinitr
   r   r   cloudinit.netr   r   cloudinit.net.ephemeralr    cloudinit.sources.helpers.akamair   r   r   	getLoggerr%   r4   r;   r"   r6   r0   r   DEP_FILESYSTEMDEP_NETWORKr   r   r,   r-   r.   <module>r      s       '  * * / / B 6  g!
 ),
 "#  	% 0 j
w)) j
Z	,  W3356 	""	
	 ;D););$< ;r-   