o
    fa                  
   @   s  U 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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mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ eeZ dZ!dZ"dZ#dZ$dZ%dddZ&ee'e'f e(d< dddgeg dZ)ee(d< dZ*dZ+dZ,dZ-dddZ.d d dZ/d!d"gZ0g d#Z1d$Z2de2iZ3	dxd%d&Z4d'e'd(ed)ed*e5d+df
d,d-Z6d.d/ Z7d0d1 Z8d2d3 Z9d4d5 Z:d6d7 Z;d8d9 Z<d:d; Z=d<d= Z>d>d? Z?d@e'd+e'fdAdBZ@d+e'fdCdDZAd+e'fdEdFZBdGdH ZCdIe'd+eDfdJdKZEdLdMdNdOZFdPZGd+e
e'e'f fdQdRZHdSdT ZIdydVdWZJdXdY ZKdzdZd[ZL	d{d\d]ZMd^d_ ZNd`da ZOdbdc ZPddde ZQdfdg ZRdhdi ZSdjdk ZTdldm ZUdndo ZVdxdpdqZWdrds ZX			U	td|dudvZYdwe<iZZdS )}z*Apt Configure: Configure apt for the user.    N)indent)DictIterableListMapping)features	lifecyclesubp	templaterutil)Cloud)Config)
MetaSchema)GPG)PER_INSTANCEz
^[\w-]+:\wz/etc/apt/trusted.gpgz/etc/apt/trusted.gpg.d/z/etc/apt/cloud-init.gpg.d/z&# cloud-init disable_suites redacted: zsoftware-properties-commongnupg)add-apt-repositorygpgPACKAGE_DEPENDENCY_BY_COMMANDcc_apt_configureubuntudebian)iddistros	frequencyactivate_by_schema_keysmetaz/var/lib/apt/listsz'/etc/apt/apt.conf.d/94cloud-init-configz)/etc/apt/apt.conf.d/90cloud-init-aptproxyzkeyserver.ubuntu.comz!http://archive.ubuntu.com/ubuntu/z"http://security.ubuntu.com/ubuntu/PRIMARYSECURITYz$http://ports.ubuntu.com/ubuntu-portsamd64i386)s390xarm64armhfpowerpcppc64elriscv64a  # Ubuntu sources have moved to the /etc/apt/sources.list.d/ubuntu.sources
# file, which uses the deb822 format. Use deb822-formatted .sources files
# to manage package sources in the /etc/apt/sources.list.d/ directory.
# See the sources.list(5) manual page for details.
c                 C   s<   | du rt  } | tv rt S | tv rt S td|  )zreturns the default mirrors for the target. These depend on the
    architecture, for more see:
    https://wiki.ubuntu.com/UbuntuDevelopment/PackageArchive#PortsNz#No default mirror known for arch %s)r   get_dpkg_architecturePRIMARY_ARCHESPRIMARY_ARCH_MIRRORScopyPORTS_ARCHESPORTS_MIRRORS
ValueErrorarch r1   C/usr/lib/python3/dist-packages/cloudinit/config/cc_apt_configure.pyget_default_mirrorsU   s   r3   namecfgcloudargsreturnc                 C   sr   t |}|di }t|tstdjt|dt| t }t	||| W d   dS 1 s2w   Y  dS )zprocess the config for apt_config. This can be called from
    curthooks if a global apt config was provided or via the "apt"
    standalone command.aptz9Expected dictionary for 'apt' config, found {config_type})config_typeN)
convert_to_v3_apt_formatget
isinstancedictr.   formattypeapply_debconf_selectionsr   	apply_apt)r4   r5   r6   r7   apt_cfggpg_contextr1   r1   r2   handled   s   
"rE   c                   C   s(   t  rdS tdstdsdS dS )N)Fzsystem is snappy.zapt-getr9   )Fzno apt commands.)TzApt is available.)r   system_is_snappyr	   whichr1   r1   r1   r2   _should_configure_on_empty_aptw   s
   rH   c              	   C   s.  | st  \}}|std| d S td|  t d }t }t| ||d}td| d }| dt}	|	r>t	
|	j}t| || t| ddr_t| || t| ||| t|| zt| tt W n ttfyw   td	 Y nw d
| v r|}
||
d< |d |
d< t| d
 |||
|d d S d S )Nz#Nothing to do: No apt config and %szhandling apt config: %scodenamer/   zApt Mirror info: %sadd_apt_repo_matchpreserve_sources_listFz)Failed to apply proxy or apt config info:sourcesRELEASEMIRROR)template_paramsaa_repo_match)rH   LOGdebugr   lsb_releaser(   find_apt_mirror_infor<   ADD_APT_REPO_MATCHrecompilesearch_ensure_dependenciesis_falseadd_mirror_keysgenerate_sources_listrename_apt_listsapply_apt_configAPT_PROXY_FNAPT_CONFIG_FNIOErrorOSError	exceptionadd_apt_sources)r5   r6   r   should_configmsgreleaser0   mirrorsmatchermatchcfgparamsr1   r1   r2   rB      sH   


rB   c                 C   s(   |  ds	| d7 } tjdg| dd d S )N   
zdebconf-set-selectionsTdatacapture)endswithr	   )
selectionsr1   r1   r2   debconf_set_selections   s   

rr   c                 C   s   g }g }| D ]}|t v rtd| t |   || q|| qt|r-td| t|rAtjddgt| d dd d S d S )Nzunconfiguring %szSThe following packages were installed and preseeded, but cannot be unconfigured: %szdpkg-reconfigurez--frontend=noninteractiveTrm   )CONFIG_CLEANERSrQ   rR   appendlenwarningr	   list)packages	unhandled	to_configpkgr1   r1   r2   dpkg_reconfigure   s,   

r|   c           	         s   |  d  std dS d fddt  D }t|  t } 	 D ]\}}|
 D ]}|dr;q3tdd	|}|| q3q+t }td
| ||}t|dkretd dS t| dS )z2apply_debconf_selections - push content to debconfdebconf_selectionsz(debconf_selections was not set in configN
c                    s   g | ]} | qS r1   r1   ).0keyselsetsr1   r2   
<listcomp>   s    z,apply_debconf_selections.<locals>.<listcomp>#z[:\s].* zpkgs_cfgd: %sr   zno need for reconfig)r<   rQ   rR   joinsortedkeysrr   encodesetitems
splitlines
startswithrV   subaddr   get_installed_packagesintersectionru   r|   )	r5   rq   	pkgs_cfgd_keycontentliner{   pkgs_installedneed_reconfigr1   r   r2   rA      s*   

 


rA   c                  C   s6   t  tjdd} td|  | D ]}t| qdS )z%clean out any local cloud-init configz/etc/cloud/cloud.cfg.d/*dpkg*pathz#cleaning cloud-init config from: %sN)globr	   target_pathrQ   rR   osunlink)flistdpkg_cfgr1   r1   r2   clean_cloud_init   s
   r   c                 C   sL   | }| dr|dd }|d}|dkr||d d }|dd}|S )zmirrorurl_to_apt_fileprefix
    Convert a mirror url to the file prefix used by apt on disk to
    store cache information for that mirror.
    To do so do:
    - take off ???://
    - drop tailing /
    - convert in string / to _/r   z://   N_)rp   findreplace)mirrorstringposr1   r1   r2   mirrorurl_to_apt_fileprefix  s   

r   c              
   C   s   t |}tt}| D ]\\}}| |}|sq|tjj t	| }|tjj t	| }||kr2qt
|}	td| D ]+}
d||
|	d f }td|
| zt|
| W q= tyh   tjddd Y q=w qdS )z>rename_apt_lists - rename apt lists to preserve old cache dataz%s_*z%s%sNzRenaming apt list %s to %szFailed to rename apt list:T)exc_info)r3   r	   r   	APT_LISTSr   r<   r   r   sepr   ru   r   rQ   rR   renamerb   rv   )new_mirrorsr0   default_mirrorsprer4   omirrornmirroroprefixnprefixolenfilenamenewnamer1   r1   r2   r]     s*   

r]   c                 C   s8   dddddd}z||  }W |S  t y   | }Y |S w )zthere are a few default names which will be auto-extended.
    This comes at the inability to use those names literally as suites,
    but on the other hand increases readability of the cfg quite a lotz$RELEASE-updatesz$RELEASE-backportsz$RELEASE-securityz$RELEASE-proposedz$RELEASE)updates	backportssecurityproposedrg   )KeyError)suitemappingretsuiter1   r1   r2   map_known_suites/  s   
r   deb822_entryc                 C   s<   t d| st dd| } t t d| } dt| d S | S )z0If no active Suites, disable this deb822 source.z\nSuites:[ \t]+([\w-]+)z\nSuites:.*r   z7## Entry disabled by cloud-init, due to disable_suites
z# disabled by cloud-init: )rV   findallr   DISABLE_SUITES_REDACT_PREFIXr   )r   r1   r1   r2   %disable_deb822_section_without_suitesA  s   r   c           	         s"  g }fdd| D  t d|   d}| D ]j}|dr/|r)|| d7 }n|| q|r5| rF|r@|t| d}|| q|}|dsT||d 7 }q r|| dd	 } fd
d|D }||kr||t | d7 }dd	| }||d 7 }q|r|t| d	|S )z:reads the deb822 format config and comment disabled suitesc                    s    g | ]}t t|d  iqS )rM   )r
   render_stringr   r   r   )rg   r1   r2   r   T  s    z)disable_suites_deb822.<locals>.<listcomp>zDisabling suites %s as %sr   r   r~   zSuites:   Nc                    s   g | ]}| vr|qS r1   r1   r   )disabled_suite_namesr1   r2   r   r  s
    zSuites:  )
rQ   rR   r   r   rt   isspacer   splitr   r   )	disabledsrcrg   new_srcnew_deb822_entryr   new_lineorig_suites
new_suitesr1   )r   rg   r2   disable_suites_deb822Q  sH   






r   c                 C   s   | s|S |}t |rt| ||S | D ]`}t|}t|d|i}td|| d}|dD ]A}|dr:||7 }q.|	 }t
|dkrkd}	|d dra|dd	 D ]}
|	d7 }	|
d
r` nqS||	 |krkd| }||7 }q.|}q|S )zRreads the config for suites to be disabled and removes those
    from the templaterM   zDisabling suite %s as %sr   Tr   r      [N]z"# suite disabled by cloud-init: %s)is_deb822_sources_formatr   r   r
   r   rQ   rR   r   r   r   ru   rp   )r   r   rg   retsrcr   releasesuitenewsrcr   colspcolcolr1   r1   r2   disable_suites  s8   


r   c                 C   s0   dD ]}|  |g D ]
}t||||d q
qdS )z=Adds any keys included in the primary/security mirror clausesprimaryr   )	file_nameN)r<   add_apt_key)r5   r6   r   r   r   r1   r1   r2   r[     s
   r[   apt_src_contentc                 C   s6   t d| t jr
dS t d| t jrdS td dS )a?  Simple check for deb822 format for apt source content

    Only validates that minimal required keys are present in the file, which
    indicates we are likely deb822 format.

    Doesn't handle if multiple sections all contain deb822 keys.

    Return True if content looks like it is deb822 formatted APT source.
    z^(deb |deb-src )Fz'^(Types: |Suites: |Components: |URIs: )Tzapt.sources_list value does not match either deb822 source keys or deb/deb-src list keys. Assuming APT deb/deb-src list format.)rV   r   MrQ   rv   )r   r1   r1   r2   r     s   r   zetc/aptsources.listzsources.list.d)Dir::EtcDir::Etc::sourcelistDir::Etc::sourcepartsz@(Dir::Etc|Dir::Etc::sourceparts|Dir::Etc::sourcelist) \"([^\"]+)c                  C   s2  z%ddl } |   | jdtd }| jdtd }| jdtd }W na ty   ztddg\}}W n* tjya   td }td }td }d| d| d| d| dd	 Y  Y S w t	t
|}t|}|dtd }|dtd }|dtd }Y nw d| d| d| d| dd	S )
a  Return a dict of applicable apt configuration or defaults.

    Prefer python apt_pkg if present.
    Fallback to apt-config dump command if present out output parsed
    Fallback to DEFAULT_APT_CFG if apt-config command absent or
    output unparsable.
    r   Nr   r   r   z
apt-configdumpr   )
sourcelistsourceparts)apt_pkginit_configconfigr<   DEFAULT_APT_CFGImportErrorr	   ProcessExecutionErrorrV   r   
APT_CFG_REr>   )r   etcr   r   apt_dumpr   matched_cfgapt_cmd_configr1   r1   r2   get_apt_cfg  sD   	r   c                 C   s  t  }|d }|d  |jj d}tjr|}n|}||d}|D ]}	||	 ||	< ||	 ||	 < q!| dd}
|
shtd tjrDdnd	}|	d
|jj | }|sY|	d}|sct
d| dS t|}
t|
|}|
rt|r||krtd| |}ntd| |}t| d||}tj||dd ||krtj|rt|jj}|r|t|krtd| t|t dS dS td| t| dS dS dS )zgenerate_sources_list
    create a source.list file based on a custom or default template
    by replacing mirrors and release in the templater   r   z.sources)rM   rI   sources_listNz1No custom template provided, fall back to builtinz.deb822r   zsources.list.r   z#No template found, not rendering %szAProvided 'sources_list' user-data is deb822 format, writing to %szFProvided 'sources_list' user-data is not deb822 format, fallback to %sr   i  )modez*Replacing %s to favor deb822 source formatz)Removing %s to favor deb822 source format)r   distror4   r   APT_DEB822_SOURCE_LIST_FILElowerr<   rQ   infoget_template_filenamerv   r   load_text_filer
   r   r   rR   r   
write_filer   r   existsDEB822_ALLOWED_APT_SOURCES_LISTUBUNTU_DEFAULT_APT_SOURCES_LISTdel_file)r5   rg   rh   r6   rC   apt_sources_listapt_sources_deb822aptsrc_filerk   ktmpltmpl_fmttemplate_fnrenderedr   expected_contentr1   r1   r2   r\     sv   



	r\   Fc                 C   sJ   t d|  zt|j}td||| |dW S  tjy$   t d  w )zM
    actual adding of a key as defined in key argument
    to the system
    zAdding key:
'%s'r   )output_filern   hardenedz(failed to add apt GPG Key to apt keyring)	rQ   rR   pathlibPathstemapt_keyr	   r   rc   )r   r   r   r  r4   r1   r1   r2   add_apt_key_rawL  s   

r  c           
      C   s   g }t  }t| ddr+dD ]}| |r*| | D ]}ddh|r)|d qq| di }| D ]}ddh|rC|d ||dd	rP|d
 q5|D ]}	t|	sa|	t
|	  qS|rn|jt| dS dS )a  Install missing package dependencies based on apt_sources config.

    Inspect the cloud config user-data provided. When user-data indicates
    conditions where add_apt_key or add-apt-repository will be called,
    ensure the required command dependencies are present installed.

    Perform this inspection upfront because it is very expensive to call
    distro.install_packages due to a preliminary 'apt update' called before
    package installation.
    rK   Fr   r   keyidr   rL   sourcer   r   N)r   r   rZ   r<   r   r   valuesshutilrG   rt   r   r   install_packagesr   )
r5   rP   r6   missing_packagesrequired_cmds
mirror_keymirror_itemapt_sources_dictentcommandr1   r1   r2   rY   \  s.   




rY   c                 C   s`   d| v rd| vrt }d| v r| d }|| d || d< d| v r.t| d |p)| d ||dS dS )z
    Add key to the system as defined in ent (if any).
    Supports raw keys or keyid's
    The latter will as a first step fetched to get the raw key
    r  r   	keyserverr   r  N)DEFAULT_KEYSERVER
getkeybyidr  )r   r6   r   r  r   r"  r1   r1   r2   r   ~  s   r   c                 C   s  |du ri }|du rt dt| tstd|  | D ]}| | }td| d|vr/||d< d|v rFd|d v rFt|||dd	}||d
< nt||| d|vrQq|d }t||}|d 	dsmt
jd|d |d< |d ds||d  d7  < ||rz
tdd|g W n tjy   td  w qtj|d d}	zd| }
d}d|v r|d sd}tj|	|
|d W q ty } ztd|	|  d}~ww |jjdd dS )a  
    install keys and repo source .list files defined in 'sources'

    for each 'source' entry in the config:
        1. expand template variables and write source .list file in
                /etc/apt/sources.list.d/
        2. install defined keys
        3. update packages via distro-specific method (i.e. apt-key update)


    @param srcdict: a dict containing elements required
    @param cloud: cloud instance object

    Example srcdict value:
    {
    'rio-grande-repo': {
        'source': 'deb [signed-by=$KEY_FILE] $MIRROR $RELEASE main',
        'keyid': 'B59D 5F15 97A5 04B7 E230  6DCA 0620 BBCF 0368 3F77',
        'keyserver': 'pgp.mit.edu'
        }
    }

    Note: Deb822 format is not supported
    Nz did not get a valid repo matcherzunknown apt format: %szadding source/key '%s'r   r  z	$KEY_FILETr#  KEY_FILEr   z/etc/apt/sources.list.d/z.listr   z--no-updatezadd-apt-repository failed.r   z%s
art   w)omodezfailed write to file %s: %s)force)r.   r=   r>   	TypeErrorrQ   rR   r   r
   r   r   r   r   r   rp   r	   r   rc   r   r   r  ra   r   update_package_sources)srcdictr6   r   rO   rP   r   r   key_filer  sourcefncontentsr)  detailr1   r1   r2   rd     sb   


rd   c                 C   s~   i }t jdddd t| tr2td | D ]}d|vr'd|d< t|d}n|d }|||< q|S t| tr;| }|S t	d)	z1convert v1 apt format to v2 (dict in apt_sources)zConfig key 'apt_sources'22.1zUse 'apt' instead)
deprecateddeprecated_versionextra_messagez9apt config: convert V1 to V2 format (source list to dict)r   zcloud_config_sources.listzunknown apt_sources format)
r   	deprecater=   rw   rQ   rR   r   rand_dict_keyr>   r.   )srclistr-  srcentr   r1   r1   r2   convert_v1_to_v2_apt_format  s&   



r:  c                 C   s,   |  |ddur|  |||< | |= dS dS )ziconvert an old key to the new one if the old one exists
    returns true if a key was found and convertedNTFr<   )oldcfgaptcfgoldkeynewkeyr1   r1   r2   convert_key  s
   r@  c                 C   sL   g d}d}ddgi}|D ]\}}t | |||rd}q|r$|g|d< dS dS )zBconvert old apt_mirror keys into the new more advanced mirror spec))
apt_mirroruri)apt_mirror_searchrX   )apt_mirror_search_dns
search_dnsFarchesdefaultTr   N)r@  )r<  r=  keymap	convertednewmcfgr>  r?  r1   r1   r2   convert_mirror  s   
rK  c                 C   sV  ddddddddddd	d
}g }|D ]}|| v r'| | dv r"| |= q| | q|s,| S tjd| dd | dd}|durwtjddd |D ]+}|| }| | }| |= |du sb||ddu rcqI||| krttd||| |f qI| S i }|D ]}|| durt| ||||  q{t| | |D ]}| |ddurtd| q|| d< | S )z:convert old to new keys and adapt restructured mirror specrL   Nproxy
http_proxyhttps_proxy	ftp_proxyrK   r   rJ   )apt_sourcesrA  rC  rD  	apt_proxyapt_http_proxyapt_ftp_proxyapt_https_proxyapt_preserve_sources_listapt_custom_sources_listrJ   )Nr   zThe following config key(s): r2  )r3  r4  r9   z0Support for combined old and new apt module keysz@Old and New apt format defined with unequal values %s vs %s @ %sz&old apt key '%s' left after conversion)rt   r   r6  r<   r.   r@  rK  )r<  
mapoldkeysneedtoconvertr>  	newaptcfgr?  verifyr=  r1   r1   r2   convert_v2_to_v3_apt_format!  sr   

r[  c                 C   s,   |  dd}|durt|| d< t| } | S )zconvert the old list based format to the new dict based one. After that
    convert the old dict keys/format to v3 a.k.a 'new apt config'rP  N)r<   r:  r[  )r5   rP  r1   r1   r2   r;   k  s
   r;   c                 C   s   d}| rZd}g }|dkrd}n|dkrd}nt dt||j}d|dd	d }|r6|d
|  |d g }	|jj	}
d|
|d|
f }|D ]	}|	||  qKt
|	}|S )zG
    Try to resolve a list of predefines DNS names to pick mirrors
    Nr   r   r   r   zsecurity-mirrorzunknown mirror type.r   z.%s)z.localdomainr   zhttp://%s-%s%s/%sz%s)r.   r   get_hostname_fqdnfqdnr   r   rt   extendr   r4   search_for_mirror)
configured
mirrortyper5   r6   r   mydomdoms	mirrordnsr^  mirror_listr   	mirrorfmtpostr1   r1   r2   search_for_mirror_dnsz  s.   
ri  c                 C   sX   | dur|du r
| }| |dS |j  }|r(| }|d |d< |d |d< |S t|S )z^sets security mirror to primary if not defined.
    returns defaults if no mirrors are definedNr   r   r   r   r   )
datasourceget_package_mirror_infor+   r3   )pmirrorsmirrorr0   r6   mirror_infomr1   r1   r2   update_mirror_info  s   

rp  c                 C   sT   |  |d}|du rdS d}|D ]}| dpg }||v r!|  S d|v r'|}q|S )zuout of a list of potential mirror configurations select
    and return the one matching the architecture (or default)NrF  rG  r;  )r5   rb  r0   mirror_cfg_listrG  mirror_cfg_elemrF  r1   r1   r2   get_arch_mirrorconfig  s   rs  c                 C   s`   t | ||}|du rdS |dd}|du rt|dd}|du r.t|dd|| |}|S )zpass the three potential stages of mirror specification
    returns None is neither of them found anything otherwise the first
    hit is returnedNrB  rX   rE  )rs  r<   r   r`  ri  )r5   rb  r0   r6   mcfgr   r1   r1   r2   
get_mirror  s   ru  c                 C   sn   |du rt  }td| t| d||}td| t| d||}td| t||||}|d |d< |S )	a  find_apt_mirror_info
    find an apt_mirror given the cfg provided.
    It can check for separate config of primary and security mirrors
    If only primary is given security is assumed to be equal to primary
    If the generic apt_mirror is given that is defining for both
    Nz!got arch for mirror selection: %sr   zgot primary mirror: %sr   zgot security mirror: %sr   rN   )r   r(   rQ   rR   ru  rp  )r5   r6   r0   rl  rm  rn  r1   r1   r2   rT     s   rT   c                    s   d} fdd|D }t |r!td| t|d|d  ntj|r2t	| td|  
ddrItd	| t| 
d dS tj|r\t	| td
| dS dS )zHapply_apt_config
    Applies any apt*proxy config from if specified
    ))rL  Acquire::http::Proxy "%s";)rM  rv  )rO  zAcquire::ftp::Proxy "%s";)rN  zAcquire::https::Proxy "%s";c                    s(   g | ]\}}  |r|  | qS r1   r;  )r   r4   fmtr5   r1   r2   r     s   ( z$apply_apt_config.<locals>.<listcomp>zwrite apt proxy info to %sr~   z#no apt proxy configured, removed %sconfNzwrite apt config info to %sz$no apt config configured, removed %s)ru   rQ   rR   r   r  r   r   r   isfiler  r<   )r5   proxy_fnameconfig_fnamecfgsproxiesr1   rx  r2   r^      s   

r^   Tc                    sV   dd  fdd} fdd}| dkr||S | dks#| d	kr'||S t d
)ad  apt-key replacement

    commands implemented: 'add', 'list', 'finger'

    @param output_file: name of output gpg file (without .gpg or .asc)
    @param data: key contents
    @param human_output: list keys formatted for human parsing
    @param hardened: write keys to to /etc/apt/cloud-init.gpg.d/ (referred to
    with [signed-by] in sources file)
    c                  S   sJ   t jtr	tgng } t tD ]}|dr| t|  q| r#| S dS )zreturn all apt keys

        /etc/apt/trusted.gpg (if it exists) and all keyfiles (and symlinks to
        keyfiles) in /etc/apt/trusted.gpg.d/ are returned

        based on apt-key implementation
        )z.gpgz.ascr   )r   r   rz  APT_LOCAL_KEYSlistdirAPT_TRUSTED_GPG_DIRrp   rt   )	key_filesfiler1   r1   r2   _get_key_files/  s   
zapt_key.<locals>._get_key_filesc              	      s   d}st td  |S zrtnt}|  }d|}t || W |S  tj	y=   t td  Y |S  t
yO   t td  Y |S w )ziapt-key add <file>

        returns filepath to new keyring, or '/dev/null' when an error occurs
        z	/dev/nullz)Unknown filename, failed to add key: "{}"z{}{}.gpgz Gpg error, failed to add key: {}z#Decode error, failed to add key: {})r   logexcrQ   r?   CLOUD_INIT_GPG_DIRr  dearmorr  r	   r   UnicodeDecodeError)rD   r   key_dirstdout)rn   r  r  r1   r2   apt_key_add>  s0   


	

zapt_key.<locals>.apt_key_addc                    sf   g }  D ](}z| | j|d W q tjy- } ztd|| W Y d}~qd}~ww d|S )zapt-key list

        returns string of all trusted keys (in /etc/apt/trusted.gpg and
        /etc/apt/trusted.gpg.d/)
        )human_outputzFailed to list key "%s": %sNr~   )rt   	list_keysr	   r   rQ   rv   r   )rD   key_listr.  error)r  r  r1   r2   apt_key_listZ  s   

zapt_key.<locals>.apt_key_listr   fingerrw   z@apt_key() commands add, list, and finger are currently supported)r.   )r!  r   r  rn   r  r  r  r  r1   )r  rn   r  r  r  r2   r    s   r  z
cloud-init)N)F)FN)NN)NNFT)[__doc__r   loggingr   r  rV   r  textwrapr   typingr   r   r   r   	cloudinitr   r   r	   r
   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.gpgr   cloudinit.settingsr   	getLogger__name__rQ   rU   r  r  r  r   r   str__annotations__r   r   r`   r_   r$  r*   r-   r)   r,   r  r  r3   rw   rE   rH   rB   rr   r|   rA   r   r   r]   r   r   r   r   r[   boolr   r   r   r   r\   r  rY   r   rd   r:  r@  rK  r[  r;   ri  rp  rs  ru  rT   r^   r  rs   r1   r1   r1   r2   <module>   s   
	

	.
 #	/(3
F
"
X
J)

Y