-zh ddlZddlZddlZddlmZddlmZmZmZddl m Z m Z ddl m Z ddlmZddlmZddlmZejeZ dd edzd edzd eedzedzffd ZGd deZGddeZdS)N)ValidationError)CommonEndpoints RootEndpointsbind)Scope is_root_user) MessageType)get_wordpress_incidents)get_installed_sites_paginated)get_domain_pathsuser site_searchreturncxtj}trtd|d}|s t j|j}td||n<#t$r/t d|td|dwxYw||fS||fS)a Determine the user_id and site_path for filtering WordPress incidents. Three calling contexts: 1. Root user: Can query all incidents or filter by specific user 2. Non-root user: Can only query their own incidents (user/site_search ignored) 3. Proxy service: Both user and site_search must be set, restricted to that site Args: user: Username to filter by site_search: Site path to filter by Returns: Tuple of (user_id, site_path) to filter by, or (None, None) for all Raises: KeyError: If the specified user doesn't exist ValueError: If proxy service call is missing required parameters z-Root user querying incidents, user filter: %sNz(Filtering incidents for user %s (uid=%d)zUser not found: %szUser 'z ' not found) osgetuidrloggerdebugpwdgetpwnampw_uidKeyErrorwarning)r r current_uiduser_ids i/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/simple_rpc/wordpress_security_plugin.pyget_user_id_and_site_for_queryrs,)++K~~$ DdKKK   ;,t,,3 >g ; ; ;3T:::9999::: ; ##  ##s 5A889B1ceZdZejZedddZedddZedddZ eddd Z d S) WordpressEndpointswordpress-pluginzinstall-on-new-sitescpK|jtjdd{VdS)Ninstall_on_new_sitesaction_sinkprocess_messager WordpressPluginActionselfs rwordpress_plugin_installz+WordpressEndpoints.wordpress_plugin_installDsZj((  -5K L L L           ztidy-upcpK|jtjdd{VdS)Ntidy_upr#r%r)s rwordpress_plugin_tidy_upz+WordpressEndpoints.wordpress_plugin_tidy_upJsYj((  -Y ? ? ?           r,updatecpK|jtjdd{VdS)Nupdate_existingr#r%r)s rwordpress_plugin_updatez*WordpressEndpoints.wordpress_plugin_updatePsZj((  -5F G G G           r,zinstall-and-updatecpK|jtjdd{VdS)Ninstall_and_updater#r%r)s r#wordpress_plugin_install_and_updatez6WordpressEndpoints.wordpress_plugin_install_and_updateVsZj((  -5I J J J           r,N) __name__ __module__ __qualname__rAV_IM360SCOPErr+r/r3r6r,rrrAs NE T 455  65  T i((  )(  T h''  ('  T 233  43   r,rceZdZejZedd ddedzdedzded ed edzd edzd edzd edzdedzdedzde dzde e fdZ eddddZ dS)WordpressCommonEndpointsr zlist-incidentsN2rr rlimitoffset by_abuser_ipby_country_code by_domainsearchsincetoorder_byrc @K t||\} } n/#t$r"}tt||d}~wwxYwt ||| ||||| | | |  }|D]:}|d|d<|d}|d|ind|d<;|S)ac List WordPress security incidents. Three calling contexts: 1. Root user: Can query all incidents or filter by specific user 2. Non-root user: Can only query their own incidents 3. Proxy service: Both user and site_search must be set, restricted to that site Args: user: Username to filter by (root or proxy service) site_search: Site path to filter by (proxy service only) limit: Maximum number of incidents to return offset: Number of incidents to skip by_abuser_ip: Filter by attacker IP address by_country_code: Filter by country code by_domain: Filter by domain search: Search across multiple fields since: Filter by timestamp >= this value (unix timestamp) to: Filter by timestamp <= this value (unix timestamp) order_by: List of fields to order by (e.g., ['timestamp-', 'severity-']) Returns: List of incident dictionaries Raises: ValidationError: If the specified user doesn't exist N) r@rArrBrCrDrErrFrGrHretriestimescountrycode)rrrstrr pop)r*r rr@rArBrCrDrErFrGrHr site_pathe incidentsincidentrLs rwordpress_plugin_list_incidentsz8WordpressCommonEndpoints.wordpress_plugin_list_incidents`sT 1!?k"" GYY 1 1 1!#a&&))q 0 1,%+!    "  H ( Y 7 7HW ll9--G%,%8!!d Y  s A?Az list-sitescfKd}|r/ tj|j}n#t$rdgfcYSwxYwt |||\}}t d{V}g}|D]K} || jg} | r| dn| j} | | | jdL||fS)z List WordPress sites with Imunify plugin installed. For root users: returns all sites. For non-root users: returns only sites belonging to that user. Nr)uidr@rA)domaindocroot) rrrrr r getrXrWappend) r*r@rAr rV max_countsitesdocroot_domainsitemssitedomainsprimary_domains r list_sitesz#WordpressCommonEndpoints.list_sitess   l4((/   "u  95    5 !1 2 2222222  D%))$,;;G+2CWQZZ N LL,#|    %s "33) NNr?rNNNNNNN)r?rN) r7r8r9rr:r;rrNintlistdictrTrbr<r,rr>r>]sN NE T .// "&#'&* $!  $FFDjF4ZF F  F Dj FtF:Fd FTzF $JF+F dFFF0/FP T l++# # # ,+# # # r,r>)NN)loggingrrdefence360agent.rpc_toolsr defence360agent.rpc_tools.lookuprrrdefence360agent.utilsrr"defence360agent.contracts.messagesr (defence360agent.model.wordpress_incidentr )defence360agent.wordpress.site_repositoryr defence360agent.wordpress.utilsr getLoggerr7rrNtuplercrrr>r<r,rrps 555555 65555555::::::LLLLLL=<<<<<  8 $ $8<($($ *($*-*($ 3:sTz !"($($($($V        8p p p p p p p p p p r,