
    &`iA              
       &   d Z ddlZddlZddlZddlmZmZ ddlZddlZddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlZdee         fd	Zd
edee         deee         ee         f         fdZd
edeee         ee         f         fdZd
edeeeeee         ee         f         f                  fdZ ej                     ej        ddddd           ej        ddedd          e
ed                                                             ZdS )zA CLI utility for check open ports in the Ray cluster.

See https://www.anyscale.com/blog/update-on-ray-cve-2023-48022-new-verification-tooling-available # noqa: E501
for more details.
    N)ListTuple)add_click_logging_options
cli_logger)RAY_PROCESSES)	PublicAPI)NodeAffinitySchedulingStrategyreturnc                  l   t                      } g }t          j        ddg          D ]T}	 |                    ||                                |                                f           @# t          j        $ r Y Qw xY wt          D ]\  }}|D ]}|\  }}}|r|nt          j	        |          }||v rw	 |
                                D ]6}	|	j        t          j        k    r|                     |	j        j                   7q# t          j        $ r t#          j        d|           Y w xY wt'          |           S )NnamecmdlinezSAccess denied to process connections for process, worker process probably restarted)setpsutilprocess_iterappendr   r   Errorr   
subprocesslist2cmdlineconnectionsstatusCONN_LISTENaddladdrportAccessDeniedr   infosorted)
unique_portsprocess_infosprockeywordfilter_by_cmd	candidateproc_cmd	proc_argscorpus
connections
             m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/check_open_ports.py_get_ray_portsr)      s   55LM#VY$788  	  $		T\\^^!DEEEE| 	 	 	D	 #0  & 	 	I(1%D(I!.VXXJ4KI4V4VF&  	&*&6&6&8&8 D D
%,0BBB(,,Z-=-BCCCD *   O=     !	 ,s$   <A''A98A9/AC;;$D"!D"service_urlportsc           	         t           j                            d| dddt          j        d|i                              d                    }t           j                            |          }|j        dk    rt          d	|j                   t          j	        |          }|
                    d
g           }|
                    dg           }||fS )NPOSTzapplication/json1)zContent-TypezX-Ray-Open-Port-Checkr+   zutf-8)methodurlheadersdata   z,Failed to check with Ray Open Port Service: 
open_portschecked_ports)urllibrequestRequestjsondumpsencodeurlopenr   RuntimeErrorloadget)r*   r+   r7   responseresponse_bodypublicly_open_portsr5   s          r(   #_check_for_open_ports_from_internetrC   3   s     n$$.%(
 
 Z%())0099 %  G ~%%g..H#L8?LL
 
 	
 Ih''M'++L"==!%%or::M--    c                 :    t          | t                                S N)rC   r)   )r*   s    r(   _check_if_exposed_to_internetrG   M   s     /{N<L<LMMMrD   c                     t          j        d           t          j        d          d             d t          j                    D             }t	          j        dt          |           d            fd	|D             }g }|                                D ]^\  }}	 |                    t          j	        |                     .# t          $ r$}t	          j        d
| d|            Y d }~Wd }~ww xY w|S )NT)ignore_reinit_errorr   )num_cpusc                 $    | t          |          fS rF   )rG   )node_idr*   s     r(   checkz!_check_ray_cluster.<locals>.checkX   s    5kBBBBrD   c                 .    g | ]}|d          
|d         S )AliveNodeID ).0nodes     r(   
<listcomp>z&_check_ray_cluster.<locals>.<listcomp>\   s%    LLLtd7mLDNLLLrD   zCluster has z= node(s). Scheduling tasks on each to check for exposed portsc           	          i | ]<}|                     t          |d                                         |          =S )F)rL   soft)scheduling_strategy)optionsr	   remote)rR   rL   rM   r*   s     r(   
<dictcomp>z&_check_ray_cluster.<locals>.<dictcomp>b   si     	 	 	  	MM$B#%% % %    fWk**	 	 	rD   zFailed to check on node z: )rayinitrY   nodesr   r   lenitemsr   r?   	Exception)r*   ray_node_idsper_node_tasksresultsrL   per_node_taskerM   s   `      @r(   _check_ray_clusterrf   S   sh    H&&&&ZC C C MLsy{{LLLLO	?s<(( 	? 	? 	?  
	 	 	 	 	 $	 	 	N G"0"6"6"8"8 G G	GNN37=112222 	G 	G 	GOEwEE!EEFFFFFFFF	G Ns   #'C
C9C44C9z--yesz-yTFzDon't ask for confirmation.)is_flagdefaulthelpz--service-urlz>https://ray-open-port-checker.uc.r.appspot.com/open-port-checkz@The url of service that checks whether submitted ports are open.)requiredtyperh   ri   c                    t          j        | dd          st          j        d           dS t          |          }g }|D ]W\  }\  }}|r3t          j        d|d|           |                    |||f           =t          j        d|d|           Xt          j        d	           |r(t          j        d
                                           dS t          j        d           dS )z*Check open ports in the local Ray cluster.z_Do you want to check the local Ray cluster for any nodes with ports accessible to the internet?T)yesmsg_defaultz&Exiting without checking as instructedNu&   [🛑] open ports detected open_ports=z node=u,   [🟢] No open ports detected checked_ports=zCheck complete, results:u  
[🛑] An server on the internet was able to open a connection to one of this Ray
cluster's public IP on one of Ray's internal ports. If this is not a false
positive, this is an extremely unsafe configuration for Ray to be running in.
Ray is not meant to be exposed to untrusted clients and will allow them to run
arbitrary code on your machine.

You should take immediate action to validate this result and if confirmed shut
down your Ray cluster immediately and take appropriate action to remediate its
exposure. Anything either running on this Ray cluster or that this cluster has
had access to could be at risk.

For guidance on how to operate Ray safely, please review [Ray's security
documentation](https://docs.ray.io/en/latest/ray-security/index.html).
u0   [🟢] No open ports detected from any Ray nodes)r   confirmr   rf   r   strip)rm   r*   cluster_open_portspublic_nodesrL   r4   r5   s          r(   check_open_portsrt   w   s_    D    	 	@AAA+K88L0B 
 
,,*m 		OXXXWXX   *m DEEEEOD!.D D8?D D   
 O./// L 
EGG	
 	
 	
 	
 	
$ 	JKKKKKrD   )__doc__r9   r   r6   typingr   r   clickr[   "ray.autoscaler._private.cli_loggerr   r   !ray.autoscaler._private.constantsr   ray.util.annotationsr   ray.util.scheduling_strategiesr	   r   intr)   strrC   rG   rf   commandoptionrt   rQ   rD   r(   <module>r      s9   
                



 T T T T T T T T ; ; ; ; ; ; * * * * * * I I I I I I  S	        :..!#Y.
49d3i . . . .4NN
49d3i N N N N!!	%U49d3i/00
12! ! ! !H T45R   	L	K   
1L 1L     1L 1L 1LrD   