eaiovnaovbqoebvqoeavibavo 3 fr @sddlZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZy ddlZWnek rYnXdZdZdZd ZGd d d eZGd d d eZGdddeZdS)N)datetime)Lock) find_spec)sos_get_command_outputfilelinkZnodedirc@seZdZdZeddZejdZdZ dZ e Z ddZ d d Zd d Zd dZddZddZd+ddZd,ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*ZdS)-Archivez!Abstract base class for archives.cCs|jS)z6Returns the archive class's name as a string. )__name__)clsr /usr/lib/python3.6/archive.py archive_type+szArchive.archive_typeZsosZunsetFcCsd|j|fS)Nz[archive:%s] %s)r)selfmsgr r r _format_msg8szArchive._format_msgcCs ||_dS)N)_debug)rdebugr r r set_debug;szArchive.set_debugcCs|jj|j|dS)N)logerrorr)rrr r r log_error>szArchive.log_errorcCs|jj|j|dS)N)rZwarningr)rrr r r log_warnAszArchive.log_warncCs|jj|j|dS)N)rinfor)rrr r r log_infoDszArchive.log_infocCs |js dS|jj|j|dS)N)rrrr)rrr r r log_debugGszArchive.log_debugNcCstdS)N)NotImplementedError)rsrcdestforcer r r add_fileOszArchive.add_filewcCstdS)N)r)rcontentrmoder r r add_stringRszArchive.add_stringcCstdS)N)r)rr"rr r r add_binaryUszArchive.add_binarycCstdS)N)r)rsource link_namer r r add_linkXszArchive.add_linkcCstdS)N)r)rpathr r r add_dir[szArchive.add_dircCstdS)N)r)rr)r#devicer r r add_node^szArchive.add_nodecCstdS)zReturn a temporary directory that clients of the archive may use to write content to. The content of the path is guaranteed to be included in the generated archive.N)r)rr r r get_tmp_diraszArchive.get_tmp_dircCstdS)zReturn the maximum file name length this archive can support. This is the lesser of the name length limit of the archive format and any temporary file system based cache.N)r)rr r r name_maxgszArchive.name_maxcCsdS)aTReturn a string representing the path to the temporary archive. For archive classes that implement in-line handling this will be the archive file itself. Archives that use a directory based cache prior to packaging should return the path to the temporary directory where the report content is locatedNr )rr r r get_archive_pathmszArchive.get_archive_pathcCsdS)z:Clean up any temporary resources used by an Archive class.Nr )rr r r cleanupvszArchive.cleanupcCs |jdS)aFinalize an archive object via method. This may involve creating An archive that is subsequently compressed or simply closing an archive that supports in-line handling. If method is automatic then the following methods are tried in order: xz, gzipN)close)rmethodr r r finalizezszArchive.finalize)NF)r!)r __module__ __qualname____doc__ classmethodrloggingZ getLoggerr_namerr _path_lockrrrrrrr r$r%r(r*r,r-r.r/r0r3r r r r r (s.     r c@seZdZdZdZdZdZd5ddZddZdd Z d6d d Z d7ddZ ddZ d8ddZ d9ddZddZddZddZddZdd Zd!d"Zd#d$Zd:d%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdS);FileCacheArchivezd Abstract superclass for archive types that use a temporary cache directory in the file system. Nc Cs||_tjj|j|g|kr,tjj|||_||_||_||_||_|pJd|_ ||_ tjj |||_ |j tj|j dWdQRX|jd|j fdS)N/iz*initialised empty FileCacheArchive at '%s')r9osr) commonprefixrelpath_tmp_dirZ_policyZ_threadsenc_optssysrootmanifestjoin _archive_rootr:makedirsr)rnametmpdirpolicythreadsrBrCrDr r r __init__s zFileCacheArchive.__init__cCs(tjj|r|jtj}tjj|j|S)N)r>r)isabslstripseprErF)rrHr r r dest_paths  zFileCacheArchive.dest_pathcCsB|j s|j|jr|S|dtjkr2|dd}tjj|j|S)Nr)rC startswithr>rOr)rE)rr)r r r join_sysroots  zFileCacheArchive.join_sysrootcs|jd||j|}fdd}|jds4|}n&tjj|j|rJ|ntjj|d}|}g}x2|dkr|dkrtjj|\}} |j| |}qdW|j } d} x|D]} tjj | | } || sqtjj | | } tjj | s|jd| tjj | rtjj| rtj | } tjj| d}tjj || }|j||d}tjj|}tjj| rntjj| |} |jd | | ftj| | q|jd | tj| || }qW|S) aCreate leading path components The standard python `os.makedirs` is insufficient for our needs: it will only create directories, and ignores the fact that some path components may be symbolic links. :param src: The source path in the host file system for which leading components should be created, or the path to an sos_* virtual directory inside the archive. Host paths must be absolute (initial '/'), and sos_* directory paths must be a path relative to the root of the archive. :param mode: An optional mode to be used when creating path components. :returns: A rewritten destination path in the case that one or more symbolic links in intermediate components of the path have altered the path destination. zMaking leading paths for %scs|jtjjdS)z>Test whether path ``path`` is inside the archive. r<)rRr>r)rE)r))rootr r in_archivesz8FileCacheArchive._make_leading_paths..in_archiver=rr<zMaking path %s)r#zMaking symlink '%s' -> '%s'zMaking directory %s)rrFrRr>r)isdirrSsplitappendreverserEexistsislinkreadlink_make_leading_pathsnormpathrMr@symlinkmkdir)rrr#rrVsrc_dirr)Z path_compsheadtailZabs_pathZsrc_pathcomptargetZ target_dirZ target_srcr )rUr r^sN        z$FileCacheArchive._make_leading_pathsFc Csb|p |j|}|tkr|}ntjj|d}|s4|Stjj|r\tjj| r\td|n2tjj|s|tkrt|ntjj|d}|j|dd}|r|Stjj|r^tj |}d} |t krt j |j  rt| |df|tkot j|j  rt| |df|tkr0||j  r0t| |df|tkrZt j|j  rZt| |d fd S|S) a4Check a new destination path in the archive. Since it is possible for multiple plugins to collect the same paths, and since plugins can now run concurrently, it is possible for two threads to race in archive methods: historically the archive class only needed to test for the actual presence of a path, since it was impossible for another `Archive` client to enter the class while another method invocation was being dispatched. Deal with this by implementing a locking scheme for operations that modify the path structure of the archive, and by testing explicitly for conflicts with any existing content at the specified destination path. It is not an error to attempt to create a path that already exists in the archive so long as the type of the object to be added matches the type of object already found at the path. It is an error to attempt to re-create an existing path with a different path type (for example, creating a symbolic link at a path already occupied by a regular file). :param src: the source path to be copied to the archive :param path_type: the type of object to be copied :param dest: an optional destination path :param force: force file creation even if the path exists :returns: An absolute destination path if the path should be copied now or `None` otherwise rz'path '%s' exists and is not a directorycSs(ttj|tj|tj|tj|gS)N)anystatS_ISBLKS_ISCHRS_ISFIFOS_ISSOCK)r#r r r is_special4s z/FileCacheArchive.check_path..is_specialz path '%s' exists and is not a %sz regular filez symbolic linkz special fileZ directoryN)rPP_DIRr>r)rXr[rW ValueErrorr^lstatP_FILErhS_ISREGst_modeP_LINKS_ISLNKP_NODES_ISDIR) rr path_typerrdest_dirrbrmstZve_msgr r r check_paths8    zFileCacheArchive.check_pathcCsydtj|}|jds |jdrDtj||tj||j|jfdn tj||tj ||j |j Wn4t k r}z|j d||fWYdd}~XnXdS)Nz/sys/z/proc/)nsz&caught '%s' setting attributes of '%s')r>rhrRshutilZcopymodeutime st_atime_ns st_mtime_nsZcopystatchownst_uidst_gid Exceptionr)rrrrher r r _copy_attributesPs   z!FileCacheArchive._copy_attributescCs|j|s|}|j|t|d}|s(dSt|ddsytj||WnJtk r}z.|jds~|jdrln|jd||fWYdd}~XnX|j ||d|}n<|j dt |d}x|D]}|j |qWWdQRXd }|j d ||jfWdQRXdS) N)rreadz/sys/z/proc/zFile %s not collected: '%s'z'%s'rr!z open filez!added %s to FileCacheArchive '%s')r:r{rqgetattrr}copyOSErrorrRrrseekopenwriterrF)rrrrr file_namefliner r r r ^s, $     zFileCacheArchive.add_filer!cCs|j|}|j|tdd}tj||ddR}t|trD|jdd}|j|t j j |rf|j |||j d||jfWdQRXWdQRXdS)NT)rzutf-8)encodingutf8ignorez-added string at '%s' to FileCacheArchive '%s')r:r{rqcodecsr isinstancebytesdecoderr>r)r[rrrF)rr"rr#rrr r r r$s     zFileCacheArchive.add_stringcCsd|jT|j|t}|sdStj|ddd}|j|WdQRX|jd||jfWdQRXdS)Nwb)rz,added binary content at '%s' to archive '%s')r:r{rqrrrrrF)rr"rrr r r r%s zFileCacheArchive.add_binaryc Cs|jd||f|jH|j|t}|s.dStjj|s\tj|||jd|||jfWdQRX|jd|||ftjj |}tjj tjj ||}|j |}dd}tjj |stjj|r>|}tjj |}tjj |tj|}tjj||}|||r|jd||fdS|jd||f|j||nbtjj|rf|jd||j|n:tjj|r|jd ||j|n|jd ||fdS) Nzadding symlink at '%s' -> '%s'z-added symlink at '%s' to '%s' in archive '%s'z.Link follow up: source=%s link_name=%s dest=%scSstjj|}tjj|s.tjjtjj||}tjj|}||krFdSytj|Wn2tk r}z|jdkrtdSWYdd}~XnXdS)zReturn ``True`` if the symbolic link ``link_name`` is part of a file system loop, or ``False`` otherwise. T(NF) r>r)dirnamerMrealpathrErhrerrno)r'r&Zlink_dirrr r r is_loops    z*FileCacheArchive.add_link..is_loopz#Link '%s' - '%s' loops: skipping...z'Adding link %s -> %s for link follow upz Adding dir %s for link follow upz!Adding file %s for link follow upz)No link follow up: source=%s link_name=%s)rr:r{rtr>r)lexistsr`rFrrrErPr[r\r]r@r(rWr*isfiler ) rr&r'rZ source_dirZhost_path_nameZdest_path_namerryr r r r(sH           zFileCacheArchive.add_linkc Cs"|j|j|tWdQRXdS)zmCreate a directory in the archive. :param path: the path in the host file system to add N)r:r{rn)rr)r r r r*szFileCacheArchive.add_dircCs|j|t}|sdStjj|sytj|||WnLtk r~}z0|jtjkrjd}|j d||fdS|WYdd}~XnX|j ||dS)NzOperation not permittedzadd_node: %s - mknod '%s') r{rvr>r)r[mknodrrZEPERMrr)rr)r#r+rrrr r r r,s   zFileCacheArchive.add_nodecCs*dtjkr"tjd}tj|j|SdSdS)N PC_NAME_MAX)r>pathconf_namespathconfrF)rZ pc_name_maxr r r r.s  zFileCacheArchive.name_maxcCs|jS)N)rF)rr r r r-szFileCacheArchive.get_tmp_dircCs|jS)N)rF)rr r r r/ sz!FileCacheArchive.get_archive_pathcCs2tjtjj|j||d|jd||jfdS)zsCreate path, including leading components. Used by sos.sosreport to set up sos_* directories. )r#z2created directory at '%s' in FileCacheArchive '%s'N)r>rGr)rErFr)rr)r#r r r rG szFileCacheArchive.makedirscCs|j|}tj|ddddS)Nrzutf-8r)rerrors)rPrr)rr)r r r open_files zFileCacheArchive.open_filecCstjj|jrtj|jdS)N)r>r)rWrFr}Zrmtree)rr r r r0szFileCacheArchive.cleanupcCsftj}|jj}||}|jjd||jjd||jjd||j|jjddtjj dddS) zAdds component-agnostic data to the manifest so that individual SoSComponents do not need to redundantly add these manually Zend_timerun_timeZ compression)indent sos_reportsz manifest.jsonN) rZnowrDZ start_timeZ add_fieldr$Zget_jsonr>r)rE)rr2endstartrr r r add_final_manifest_datasz(FileCacheArchive.add_final_manifest_datacCsN|j|j|_tjj|j|j}tj|j|||_tjj|j|j|_ dS)zeRename the archive to an obfuscated version using an initialized SoSCleaner instance N) Zobfuscate_stringr9r>r)rErArenamerFrH _archive_name)rZcleanerZ _new_rootr r r rename_archive_root+s z$FileCacheArchive.rename_archive_rootc CstjtjB}t|dr(|j}|j|B}n|}|}d}|j|}|j}WdQRXt|t sh|j dd}tj ||||d\} } | r|j | |nd} | S)aApply a regexp substitution to a file in the archive. :param path: Path in the archive where the file can be found :type path: ``str`` :param regexp: A regex to match the contents of the file :type regexp: ``str`` or compiled ``re`` object :param subst: The substitution string to be used to replace matches within the file :type subst: ``str`` :returns: Number of replacements made :rtype: ``int`` patternr<Nrr)flagsr) re IGNORECASE MULTILINEhasattrrrrrrstrrsubnr$) rr)ZregexpZsubstZ common_flagsrrr"readableresultZ replacementsr r r do_file_sub5s"        zFileCacheArchive.do_file_subcCs|jd|j|fy|j|}Wn2tk rT}z|jd||jSd}~XnX|j|jd|jtj |jj f|j dry |j |Stk r}zd}|jd||f|Sd}~XqXn|SdS)Nz)finalizing archive '%s' using method '%s'z-An error occurred compressing the archive: %szbuilt archive at '%s' (size=%d)Zencryptz)An error occurred encrypting the archive:z%s %s) rrF_build_archiverrrHr0rr>rhst_sizerB_encrypt)rr2reserrrZexp_msgr r r r3Zs&   zFileCacheArchive.finalizecCs|jdd}|d7}d|}d}|jdrD|d|jd7}||7}|jdrd |jdjd d }d |i}|d 7}d|}||7}t|d|d}|ddkr|S|ddkr|jdrd}qd}n d|d}t|dS)aEncrypts the compressed archive using GPG. If encryption fails for any reason, it should be logged by sos but not cause execution to stop. The assumption is that the unencrypted archive would still be of use to the user, and/or that the end user has another means of securing the archive. Returns the name of the encrypted archive, or raises an exception to signal that encryption failed and the unencrypted archive name should be used. z sosreport-zsecured-sosreport-z.gpgzgpg --batch -o %s Nkeyz--trust-model always -e -r %s Zpasswordz%sz'"r<Zsos_gpgz-c --passphrase-fd 0 z!/bin/bash -c "echo $sos_gpg | %s"r)ZtimeoutenvZstatuszSpecified key not in keyringzCould not read passphrasezgpg exited with code %s)replacerBrr)rarchiveZarc_nameZenc_cmdrZpasswdrrr r r rrs,       zFileCacheArchive._encrypt)N)rT)NF)NF)r!)rT)r r4r5r6rArFrrLrPrSr^r{rr r$r%r(r*r,r.r-r/rGrr0rrrr3rr r r r r;s6  \ K $  G    %r;cs`eZdZdZdZdZdfdd ZdddZdd Zd d Z d d Z fddZ ddZ Z S)TarFileArchivez: archive class using python TarFile to create tar archivesNFcs:tt|j|||||||d|_tjj||j|_dS)Ntar) superrrL_suffixr>r)rErHr)rrHrIrJrKrBrCrD) __class__r r rLs  zTarFileArchive.__init__cCsP|j|_d|j|jd<d|j|jd<|r4||_n|j|_|j|_|j |_ dS)Nz%.9fZatimeZctime) st_mtimemtimest_atime pax_headersst_ctimer#rsrZuidrgid)rZtar_infofstatr#r r r set_tarinfo_from_statsz$TarFileArchive.set_tarinfo_from_statc s|jttjj|jdds*|jdddg}tfdd|DrNdSytj}Wntk rp|SX|j r|j }|r||j d<|j |||S) NrQz /version.txt$z/sos_logs(/.*)?z/sos_reports(/.*)?c3s|]}tj|VqdS)N)rmatch).0skip) orig_pathr r sz9TarFileArchive.copy_permissions_filter..zRHT.security.selinux) rHlenr>r)rXrFrgrhr_with_selinux_contextget_selinux_contextrr)rZtarinfoZskipsrcontextr )rr copy_permissions_filters      z&TarFileArchive.copy_permissions_filterc Cs,ytj|\}}|Stk r&dSXdS)N)selinuxZ getfileconr)rr)Zrccr r r rs z"TarFileArchive.get_selinux_contextcCsd|j|jfS)Nz%s.%s)rFr)rr r r rHszTarFileArchive.namecstt|jS)N)rrr.)r)rr r r.szTarFileArchive.name_maxcCs|dkrtddk rdnd}|jd}|jd||_|dkrHddi}nd d i}tj|jfd d |i|}xLdD]D}tjjtjj|j |sqr|j tjj|j ||j d|dqrW|j |j |j |j d|j |jd|7_|jS)NautoZlzmaZxzZgzipZipz.%sZ compresslevelZpresetr#zw:%s version.txtrsos_logsr=)arcname)rfilter)rrr)rstriprtarfilerr>r)r[rErFaddr9rr1rrH)rr2Z _comp_modekwargsrZ_contentr r r rs(     zTarFileArchive._build_archive)N)N)r r4r5r6r2rrLrrrrHr.r __classcell__r r )rr rs  r)r>rr}r8rrrhrrZ threadingrimportlib.utilrZ sos.utilitiesrr ImportErrorrqrtrvrnobjectr r;rr r r r  s4     [