HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/master.cpython-310.pyc
o

�N�g�v�@sZdZddlZddlZddlZddlmZmZddlZddl	Zddl
ZddlZddlZddl
ZddlZddlZddlZddlZddlZddlmZddlmZddlmZddlmZe�e�Zdd	�Zd
d�Z dd
�Z!Gdd�d�Z"Gdd�de�Z#Gdd�de�Z$Gdd�de�Z%dd�Z&ddd�Z'dd�Z(edkr�ej)�*d�Z+e%e+�Z,e,�-�dSdS) ze
    salt.utils.master
    -----------------

    Utilities that can only be used on a salt master.

�N)�Event�Thread)�
SaltException)�CacheCli)�Process)�zmqc	Cs�g}tj�|dd�}tj�|�s|St�|�D](}tj�||�}zt||�}|dur0|�|�Wqty@t�	d|�Yqw|S)z0
    Return the running jobs on this minion
    �cachedir�procNz.%s removed during processing by master process)
�os�path�join�isdir�listdir�_read_proc_file�append�OSError�logZtrace)�opts�retZproc_dirZfn_r�data�r�E/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/master.py�get_running_jobs!s

��rc	Cs<tjj�|d��9}|��}|��|rtj�|�}n zt�	|�Wnt
y0t�d|�Ynw	Wd�dSWd�n1sDwYt
|t�sPdStjj�|d�srzt�	|�WdSt
yqt�d|�YdSwt|�s�|�d�}|r�t�d|�zt�	|�WdSt
y�t�d|�YdSw|S)z0
    Return a dict of JID metadata, or None
    �rbzUnable to remove proc file %s.N�pidz7PID %s exists but does not appear to be a salt process.)�salt�utils�files�fopen�read�close�payload�loadsr
�removerr�debug�
isinstance�dict�processZ
os_is_running�_check_cmdline�get�warning)rr�fp_�bufrrrrrr8sH���
��
��rcCs�tjj��sdS|�d�}|sdStj�d�sdStj�d|�d��}tj�	|�s+dSz"tjj
�|d��}d|��vWd	�WS1sFwYWd	St
yWYdSw)
a9
    In some cases where there are an insane number of processes being created
    on a system a PID can get recycled or assigned to a non-Salt process.
    On Linux this fn checks to make sure the PID we are checking on is actually
    a Salt process.

    For non-Linux systems we punt and just return True
    TrFz/procz/proc/z/cmdlinerssaltN)rr�platformZis_linuxr)r
rr
r�isfilerrrr)rrrr+rrrr(`s"	

(��r(c@s�eZdZdZ								ddd�Zdd	�Zd
d�Zdd
�Zd dd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Z				d!dd�ZdS)"�MasterPillarUtila�
    Helper utility for easy access to targeted minion grain and
    pillar data, either from cached data on the master or retrieved
    on demand, or (by default) both.

    The minion pillar data returned in get_minion_pillar() is
    compiled directly from salt.pillar.Pillar on the master to
    avoid any possible 'pillar poisoning' from a compromised or
    untrusted minion.

    ** However, the minion grains are still possibly entirely
    supplied by the minion. **

    Example use case:
        For runner modules that need access minion pillar data,
        MasterPillarUtil.get_minion_pillar should be used instead
        of getting the pillar data by executing the "pillar" module
        on the minions:

        # my_runner.py
        tgt = 'web*'
        pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type='glob', opts=__opts__)
        pillar_data = pillar_util.get_minion_pillar()
    ��globNTc		
Cs�t�d|jj�|durt�d|jj�t|jj�d���||_||_||_||_	||_
||_||_||_
tj�|�|_t�d|||||||�dS)NzNew instance of %s created.z!%s: Missing master opts init arg.z: Missing master opts init arg.z�Init settings: tgt: '%s', tgt_type: '%s', saltenv: '%s', use_cached_grains: %s, use_cached_pillar: %s, grains_fallback: %s, pillar_fallback: %s)rr$�	__class__�__name__�errorrr�tgt�tgt_type�saltenv�use_cached_grains�use_cached_pillar�grains_fallback�pillar_fallbackr�cache�factory)	�selfr5r6r7r8r9r:r;rrrr�__init__�s2��zMasterPillarUtil.__init__cGs�dd�|D�}|j�dd�s|j�dd�st�d�|S|s$|j�d�}|D]}tjj�	|j|�s2q&|j�
d|��d	�}t|t�rE|||<q&|S)
NcS�i|]}|i�qSrr��.0�	minion_idrrr�
<dictcomp>��z:MasterPillarUtil._get_cached_mine_data.<locals>.<dictcomp>�minion_data_cacheFZenforce_mine_cachezSSkipping cached mine data minion_data_cacheand enfore_mine_cache are both disabled.�minions�minions/�mine)
rr)rr$r<�listrr�verify�valid_id�fetchr%r&)r>�
minion_ids�	mine_datarC�mdatarrr�_get_cached_mine_data�s$��
�z&MasterPillarUtil._get_cached_mine_datacGs�dd�|D�}|��}|j�dd�st�d�||fS|s#|j�d�}|D]:}tjj	�
|j|�s1q%|j�d|��d�}t|t
�sKt�d	t|�j|�q%d
|vrU|d
||<d|vr_|d||<q%||fS)NcSr@rrrArrrrD�rEz<MasterPillarUtil._get_cached_minion_data.<locals>.<dictcomp>rFFz>Skipping cached data because minion_data_cache is not enabled.rGrHrzGcache.fetch should always return a dict. ReturnedType: %s, MinionId: %s�grains�pillar)�copyrr)rr$r<rJrrrKrLrMr%r&r*�typer3)r>rNrR�pillarsrCrPrrr�_get_cached_minion_data�s0

��z(MasterPillarUtil._get_cached_minion_datacCs`t�d|�tj�|jd��}|jd�|�d|jddd�Wd�S1s)wYdS)Nz%Getting live grains for minions: "%s"Z	conf_file�,zgrains.items�timeoutrJ)rYr6)rr$r�clientZget_local_clientr�cmdr)r>rNrZrrr�_get_live_minion_grains�s�$�z(MasterPillarUtil._get_live_minion_grainscCsb|duriS|st�d|�iSt�d|�tj�|j|||j|jd�}t�d|�|��}|S)Nz2Cannot get pillar data for %s: no grains supplied.zGetting live pillar for %sZ
ext_pillarzCompiling pillar for %s)	rr*r$rrSZPillarrr7Zcompile_pillar)r>rC�
minion_grainsrSrrrr�_get_live_minion_pillar�s�z(MasterPillarUtil._get_live_minion_pillarcs
i}|�di�}i�i�|jrJdd�|��D���fdd�|D�}t�d|�|jr/|�|��ttdd�|D����t����t�����}|S|�|���fdd�|D�}t�d	|�|jrjd
d�|��D��ttdd�|D����t����t�����}|S)N�
cached_grainscS�i|]	\}}|r||�qSrr�rBrCZmcacherrrrD���z7MasterPillarUtil._get_minion_grains.<locals>.<dictcomp>c�g|]}|�vr|�qSrrrA��cretrr�
<listcomp>�z7MasterPillarUtil._get_minion_grains.<locals>.<listcomp>z#Missed cached minion grains for: %scSr@rrrArrrrDrEcrcrrrA��lretrrrf#rgz!Missed live minion grains for: %scSr`rrrarrrrD(rbcSr@rrrArrrrD.rE)	r)r8�itemsrr$r:r\r&rJ)r>rN�kwargsrr_�missed_minionsr)rerir�_get_minion_grainssN�
�

�
��
�
��
�
��z#MasterPillarUtil._get_minion_grainscs*i}|�di��|�di�}i�i��jrUdd�|��D���fdd�|D�}t�d|��jr:��fdd�|D��ttd	d�|D����t����t�����}|S��fd
d�|D���fdd�|D�}t�d|��jrzd
d�|��D��ttdd�|D����t����t�����}|S)NrR�
cached_pillarcSr`rrrarrrrD>rbz7MasterPillarUtil._get_minion_pillar.<locals>.<dictcomp>crcrrrArdrrrfCrgz7MasterPillarUtil._get_minion_pillar.<locals>.<listcomp>z$Missed cached minion pillars for: %sc
�"i|]
}|��|��|i���qSr�r^r)rA�rRr>rrrDH����cSr@rrrArrrrDOrEc
rorrprArqrrrDTrrcrcrrrArhrrrfZrgz"Missed live minion pillars for: %scSr`rrrarrrrD_rbcSr@rrrArrrrDerE)r)r9rjrr$r;r&rJ)r>rNrkrrnrlr)rerRrir>r�_get_minion_pillar4sX�
��
�
����
��
�
��z#MasterPillarUtil._get_minion_pillarcCs^g}tjj�|j�}|�|j|j�}|d}|s#t�	d|j|j�iSt�	d|j|j|�|S)NrGz1No minions matched for tgt="%s" and tgt_type="%s"z3Matching minions for tgt="%s" and tgt_type="%s": %s)
rrrG�	CkMinionsrZ
check_minionsr5r6rr$)r>rNZ	ckminionsZ_resrrr�_tgt_to_listks$��zMasterPillarUtil._tgt_to_listcCs�i}i}|��}|jr|siStdd�|j|j|j|jfD��r-t�d�|j	|�\}}ni}i}t�d|�|j
|d|i�}t�d|�|j|||d��}|S)a�
        Get pillar data for the targeted minions, either by fetching the
        cached minion data on the master, or by compiling the minion's
        pillar data on the master.

        For runner modules that need access minion pillar data, this
        function should be used instead of getting the pillar data by
        executing the pillar module on the minions.

        By default, this function tries hard to get the pillar data:
            - Try to get the cached minion grains and pillar if the
                master has minion_data_cache: True
            - If the pillar data for the minion is cached, use it.
            - If there is no cached grains/pillar data for a minion,
                then try to get the minion grains directly from the minion.
            - Use the minion grains to compile the pillar directly from the
                master using salt.pillar.Pillar
        cs��|]}|VqdS�Nr�rB�argrrr�	<genexpr>�s
��
�z5MasterPillarUtil.get_minion_pillar.<locals>.<genexpr>zGetting cached minion data�!Getting minion grain data for: %sr_z"Getting minion pillar data for: %s)rRrn)rur5�anyr8r9r:r;rr$rWrmrs)r>Zminion_pillarsr]rN�cached_minion_grains�cached_minion_pillarsrrr�get_minion_pillar�s:
��
	
����z"MasterPillarUtil.get_minion_pillarcCsli}|��}|s
iStdd�|j|jfD��r$t�d�|j|�\}}ni}t�d|�|j|d|i�}|S)a5
        Get grains data for the targeted minions, either by fetching the
        cached minion data on the master, or by fetching the grains
        directly on the minion.

        By default, this function tries hard to get the grains data:
            - Try to get the cached minion grains if the master
                has minion_data_cache: True
            - If the grains data for the minion is cached, use it.
            - If there is no cached grains data for a minion,
                then try to get the minion grains directly from the minion.
        csrvrwrrxrrrrz�s�z5MasterPillarUtil.get_minion_grains.<locals>.<genexpr>zGetting cached minion data.r{r_)rur|r8r:rr$rWrm)r>r]rNr}r~rrr�get_minion_grains�s"


���z"MasterPillarUtil.get_minion_grainscCs&i}|��}t�d|�|j|�}|S)z@
        Get cached mine data for the targeted minions.
        z Getting cached mine data for: %s)rurr$rQ)r>rOrNrrr�get_cached_mine_data�s

z%MasterPillarUtil.get_cached_mine_dataFcCs�g}|r	|�d�|r|�d�|r|�d�|dur$|�d|�d��|s-t�d�dS|��}t�d	d
�|�|�||krDi}i}n|j|�\}}z�|j�d�}	|D]{}
tj	j
�|j|
�s`qT|
|	vreqTd|
��}|�
|
d�}|�
|
d�}
|rz|s�|r~|
r�|r�|s�|j�|d
�n|r�|
r�|j�|d
d|
i�n|r�|r�|j�|d
d|i�|r�|j�|d�qT|dur�|j�|d�}t|t�r�|�
|d�r�|j�|d|�qTWdSty�YdSw)zG
        Clear the cached data/files for the targeted minions.
        rSrRrINzmine_func: '�'z,No cached data types specified for clearing.FzClearing cached %s data for: %sz, rGrHrT)rrr$rurrWr<rJrrrKrLr�pop�flush�storerMr%r&r)r>Zclear_pillarZclear_grainsZ
clear_mineZclear_mine_funcZ
clear_whatrNrRrVZ	c_minionsrCZbankZ
minion_pillarr]rOrrr�clear_cached_minion_data�st





������
�� ��z)MasterPillarUtil.clear_cached_minion_data)r0r1NTTTTN)NN)FFFN)r3�
__module__�__qualname__�__doc__r?rQrWr\r^rmrsrurr�r�r�rrrrr/zs4
�)
,72�r/c@s eZdZdZdd�Zdd�ZdS)�
CacheTimerzw
    A basic timer class the fires timer-events every second.
    This is used for cleanup by the ConnectedCache()
    cCs6t�|�||_||_d|_tj�|jdd�|_dS)NT�sock_dir�
con_timer.ipc)	rr?r�stopped�daemonr
rr�
timer_sock)r>r�eventrrrr?(s

zCacheTimer.__init__cCs�t��}|�tj�}|�tjd�|�d|j�d}t�	d�|j
�d�sA|�t
j�|��|d7}|dkr9d}|j
�d�r&dSdS)z=
        main loop that fires the event every second
        �d�ipc://rzConCache-Timer started��<N)r�Context�socketZPUB�
setsockopt�LINGER�bindr�rr$r��wait�sendrr!�dumps)r>�contextr��countrrr�run/s
�zCacheTimer.runN)r3r�r�r�r?r�rrrrr�"sr�cs(eZdZdZ�fdd�Zdd�Z�ZS)�CacheWorkerz�
    Worker for ConnectedCache which runs in its
    own process to prevent blocking of ConnectedCache
    main-loop when refreshing minion-list
    cst�jdi|��||_dS)z<
        Sets up the zmq-connection to the ConCache
        Nr)�superr?r�r>rrk�r2rrr?Js
zCacheWorker.__init__cCsDttjj�|j����}t|j�}|��|�	|g�t
�d�dS)zI
        Gather currently connected minions and update the cache
        z$ConCache CacheWorker update finishedN)rJrrrGrtr�
connected_ids�	cache_cliZ
get_cachedZ	put_cacherr$)r>Znew_minsZccrrrr�Qs

zCacheWorker.run)r3r�r�r�r?r��
__classcell__rrr�rr�Csr�csHeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	�Z
S)�ConnectedCachez�
    Provides access to all minions ids that the master has
    successfully authenticated. The cache is cleaned up regularly by
    comparing it to the IPs that have open connections to
    the master publisher port.
    cs�t�jdi|��t�d�||_g|_tj�|jdd�|_	tj�|jdd�|_
tj�|jdd�|_|��t
�|_t|j|j�|_|j��d|_dS)	z=
        starts the timer and inits the cache itself
        zConCache initializing...r�z
con_cache.ipczcon_upd.ipcr�TNr)r�r?rr$rrGr
rr�
cache_sock�update_sock�
upd_t_sock�cleanupr�
timer_stopr��timer�start�runningr�r�rrr?ds


zConnectedCache.__init__cCs|��dS)z-
        handle signals and shutdown
        N)�stop)r>�sig�framerrr�signal_handler~szConnectedCache.signal_handlercCs`t�d�tj�|j�rt�|j�tj�|j�rt�|j�tj�|j�r.t�|j�dSdS)z,
        remove sockets on shutdown
        zConCache cleaning upN)	rr$r
r�existsr�r#r�r��r>rrrr��s
�zConnectedCache.cleanupcCsft�d�tj�|j�rt�|jd�tj�|j�r!t�|jd�tj�|j�r1t�|jd�dSdS)z9
        secure the sockets for root-only access
        zConCache securing socketsi�N)	rr$r
rr�r��chmodr�r�r�rrr�secure�s
�zConnectedCache.securecCs0|��|jrd|_|j��|j��dSdS)z(
        shutdown cache process
        FN)r�r�r��setr�rr�rrrr��s
�zConnectedCache.stopc
Cs$t��}|�tj�}|�tjd�|�d|j�|�tj�}|�tj	d�|�tjd�|�d|j
�|�tj�}|�tj	d�|�tjd�|�d|j�t�
�}|�|tj�|�|tj�|�|tj�t�tj|j�|��t�d�|j�rwz	t|�d��}Wn-ty�|��Yn!tjy�}zt�d�t�|�|��WYd}~nd}~ww|�|�tjkr�tj �!|�"��}t�#d|�t$|t%�r�|d	kr�tj �&|j'�}	|�(|	�|�|�tjk�rQtj �!|�"��}
t$|
t)�s�t�d
�~
q{z9|
�s	t�#d�Wq{|
d}t$|t%��r(||j'v�r't�#d
|
d�|j'�*|�nt$|t)��r6t�#d�||_'Wnt+�yGt�#d�~
Ynwt�dt,|j'��|�|�tjk�rttj �!|�"��}t-|d�dk�rtt.|j/�}
|
�0�|js|��|�1�|�1�|�1�|�2�t�#d�dS)zw
        Main loop of the ConCache, starts updates in intervals and
        answers requests from the MWorkers
        r�r��zConCache startedr�zConCache ZeroMQ-Error occurredNzConCache Received request: %srGz(ConCache Worker returned unusable resultz%ConCache Got empty update from workerrz"ConCache Adding minion %s to cachez*ConCache Replacing minion list from workerz.ConCache Got malformed result dict from workerzConCache %s entries in cache�zConCache Shutting down)3rr�r�ZREPr�r�r�r�ZSUBZ	SUBSCRIBEr��connectr�ZPoller�register�POLLIN�signal�SIGINTr�r�r�infor�r&�poll�KeyboardInterruptr�ZZMQErrorr4�	exceptionr)rr!r"�recvr$r%�strr�rGr�rJr�
IndexError�len�intr�rr�r Zterm)r>r�Zcreq_inZcupd_inZtimer_inZpollerZsocksZzmq_err�msgZreplyZ
new_c_datarZ	sec_eventZcwrrrr��s�


��




��
�
�
�LzConnectedCache.run)r3r�r�r�r?r�r�r�r�r�r�rrr�rr�\sr�cCsl|drttjj�|����}d}nd}d}tj���}|j|d|d�Wd�dS1s/wYdS)z%
    Ping all connected minions.
    rFrJ�*r1z	test.ping)r6N)	rJrrrGrtr�rZZLocalClientZ	cmd_async)rr5ZformrZrrr�ping_all_connected_minionss"�r�FcCs�|dkr|�dd�dkr|�dd�}|�d�r|�dd�}tjj��r)|�dd�}tj�	|dd|�d��}tjj
�|d||�z tjj�
|d	��
}|��Wd
�WS1sZwYWd
StykYdSw)z 
    Return the master key.
    �root�userZsudo_�\�_r�.Z_key�rNr0)r)�
startswithrrr-Z
is_windows�replacer
rrrKZcheck_path_traversalrrrr)Zkey_userrZskip_perm_errors�keyfile�keyrrr�get_master_key-s 
(��r�cCs0g}|D]}tjj�||�r|�||�q|S)zL
    Check a whitelist and/or blacklist to see if the value matches it.
    )rrZstringutilsZ
expr_match�extend)Zpattern_dictZ	user_namer�exprrrr�get_values_of_matching_keysFs�r��__main__z/etc/salt/master)F).r��loggingr
r��	threadingrrZ
salt.cacherZsalt.clientZsalt.configZsalt.payloadZsalt.pillarZsalt.utils.atomicfileZsalt.utils.filesZsalt.utils.minionsZsalt.utils.platformZsalt.utils.stringutilsZsalt.utils.verifyZsalt.exceptionsrZsalt.utils.cacherr�Zsalt.utils.processrZsalt.utils.zeromqr�	getLoggerr3rrrr(r/r�r�r�r�r�r�ZconfigZ
master_configrZconcr�rrrr�<module>sP
(+!D
�