RustImplementation #23

Merged
mysticmomba merged 118 commits from RustImplementation into master 2025-11-04 18:13:24 -05:00
3 changed files with 66 additions and 9 deletions
Showing only changes of commit df52a4f0b0 - Show all commits
+6 -1
View File
@@ -43,7 +43,12 @@ def getDestAllowlistFromClientID(url, clientid):
allowlists = getPolicyAllowlists(url,data.loc[0, "groupid"])
app_id = allowlists.loc[allowlists['name'].str.contains('localapproval', case=False, na=False), 'applicationid'].values[0]
matches = allowlists.loc[
allowlists['name'].str.contains('local', case=False, na=False) &
allowlists['name'].str.contains('approval', case=False, na=False),
'applicationid'
].values
app_id = matches[0] if len(matches) > 0 else None
return app_id
+12 -7
View File
@@ -3,13 +3,12 @@ import requests
import os
import pandas as pd
import utils.pretty as ct
import shutil
import math
import time
import utils.clientfunctions as clientf
import utils.pathfunctions as pathf
import utils.policyfunctions as policyf
from utils.perstscheduler import register_function, run_once_job, recurring_job, reload_jobs, start_scheduler
from utils.perstscheduler import register_function, run_once_job, recurring_job, reload_jobs, start_scheduler, find_and_prioritize_jobs_by_pid
def getActiveOTP(url):
@@ -30,7 +29,7 @@ def getActiveOTP(url):
os.remove("OTP\\PARQ\\newest_active_OTP.parquet")
otp.to_parquet("OTP\\PARQ\\newest_active_OTP.parquet", index=False)
if not otp.empty:
ct.style_dataframe_dark(otp, f"newest_active_OTP.html")
ct.style_dataframe_dark(otp, f"OTP\\HTML\\newest_active_OTP.html")
def getOTPActivities(url, otpid):
endpoint = url + f'/v1/otp/activities'
@@ -92,9 +91,9 @@ def monitorOTP(url, pups):
hostname = row['hostname']
purpose = row ['purpose']
pid = row['otpid']
early = math.floor(duration * .9)
early = math.floor(duration * .95)
#If newly added to the list - schedule adding the majority of the executions prior to the expiration of OTP period.
run_once_job(f"Add activity hashes for {pid}, for {hostname} for the purpose: {purpose}, early run", "addhash", time.time() + early, [url, clientid, pid, pups], None)
run_once_job(f"Add activity hashes for {pid}, for {hostname} for the purpose: {purpose}", "addhash", time.time() + early, [url, clientid, pid, pups], None)
print(f"Processing: {pid} with other data: {row}")
@@ -110,14 +109,20 @@ def monitorOTP(url, pups):
pid = row['otpid']
allowlist = clientf.getDestAllowlistFromClientID(url,clientid)
policy = clientf.getPolicyFromClientID(url,clientid)
#When duration completes -
"""
Devices can come out of OTP either by timeout, or by early move out of OTP. If they are manually moved out prior to the job to add hashes can run, we want to accelerate the job.
But first, we want to update the OTP activities one final time for the pid, then move up any jobs if they exist, then add the hashes to the local approval allowlist
"""
getOTPActivities(url,pid)
find_and_prioritize_jobs_by_pid(pid, 1)
addOTPHashes(url, clientid,pid, pups)
finalhashesadded = pd.read_parquet(f"OTP\\PARQ\\otp_activities_{pid}.parquet")
finalhashesadded['policy'] = policy
finalhashesadded['allowlist'] = allowlist
finalhashesadded['added_at'] = time.time()
finalhashesadded['added_at'] = time.localtime()
if not os.path.exists(f"OTP\\PARQ\\localapprovalhistory.parquet"):
df = pd.DataFrame()
+48 -1
View File
@@ -219,6 +219,53 @@ def recurring_job(
})
save_jobs(jobs)
def find_and_prioritize_jobs_by_pid(pid_substring: str, new_delay_seconds: float = 1.0):
"""
Find all jobs whose ID contains the given PID substring and reschedule them to run sooner.
"""
jobs = load_jobs()
matched_jobs = [job for job in jobs if pid_substring in job.get("id", "")]
if not matched_jobs:
print(f"[INFO] No jobs found containing PID substring '{pid_substring}'.")
return
print(f"[INFO] Found {len(matched_jobs)} job(s) containing '{pid_substring}':")
for job in matched_jobs:
job_id = job["id"]
print(f" - Prioritizing job: {job_id}")
# Clear existing job from scheduler
schedule.clear(job_id)
# Reschedule based on job type
if job["type"] == "once":
run_once_job(
job_id,
job["function"],
time.time() + new_delay_seconds,
job.get("args"),
job.get("kwargs"),
replace=True,
persist=True
)
elif job["type"] == "recurring":
recurring_job(
job_id,
job["function"],
job["interval"],
job["unit"],
job.get("args"),
job.get("kwargs"),
replace=True,
persist=True
)
else:
print(f"[WARN] Unknown job type for job '{job_id}'")
# -------------------------------
# Reload Saved Jobs
# -------------------------------
@@ -248,7 +295,7 @@ def reload_jobs():
def start_scheduler():
"""
Start the scheduler loop (blocking).
Call this once in your main program to begin.
Call this once in main to begin.
"""
try:
while True: