Fixed so that hash adding for local approval should happen sooner if device is moved out of OTP early. Also minor fixes with timestamping of hash additon to use local time instead of epoch, and str matching for local approval allow list finding
This commit is contained in:
@@ -43,7 +43,12 @@ def getDestAllowlistFromClientID(url, clientid):
|
|||||||
|
|
||||||
allowlists = getPolicyAllowlists(url,data.loc[0, "groupid"])
|
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
|
return app_id
|
||||||
|
|
||||||
|
|||||||
+12
-7
@@ -3,13 +3,12 @@ import requests
|
|||||||
import os
|
import os
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import utils.pretty as ct
|
import utils.pretty as ct
|
||||||
import shutil
|
|
||||||
import math
|
import math
|
||||||
import time
|
import time
|
||||||
import utils.clientfunctions as clientf
|
import utils.clientfunctions as clientf
|
||||||
import utils.pathfunctions as pathf
|
import utils.pathfunctions as pathf
|
||||||
import utils.policyfunctions as policyf
|
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):
|
def getActiveOTP(url):
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ def getActiveOTP(url):
|
|||||||
os.remove("OTP\\PARQ\\newest_active_OTP.parquet")
|
os.remove("OTP\\PARQ\\newest_active_OTP.parquet")
|
||||||
otp.to_parquet("OTP\\PARQ\\newest_active_OTP.parquet", index=False)
|
otp.to_parquet("OTP\\PARQ\\newest_active_OTP.parquet", index=False)
|
||||||
if not otp.empty:
|
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):
|
def getOTPActivities(url, otpid):
|
||||||
endpoint = url + f'/v1/otp/activities'
|
endpoint = url + f'/v1/otp/activities'
|
||||||
@@ -92,9 +91,9 @@ def monitorOTP(url, pups):
|
|||||||
hostname = row['hostname']
|
hostname = row['hostname']
|
||||||
purpose = row ['purpose']
|
purpose = row ['purpose']
|
||||||
pid = row['otpid']
|
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.
|
#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}")
|
print(f"Processing: {pid} with other data: {row}")
|
||||||
|
|
||||||
|
|
||||||
@@ -110,14 +109,20 @@ def monitorOTP(url, pups):
|
|||||||
pid = row['otpid']
|
pid = row['otpid']
|
||||||
allowlist = clientf.getDestAllowlistFromClientID(url,clientid)
|
allowlist = clientf.getDestAllowlistFromClientID(url,clientid)
|
||||||
policy = clientf.getPolicyFromClientID(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)
|
addOTPHashes(url, clientid,pid, pups)
|
||||||
|
|
||||||
finalhashesadded = pd.read_parquet(f"OTP\\PARQ\\otp_activities_{pid}.parquet")
|
finalhashesadded = pd.read_parquet(f"OTP\\PARQ\\otp_activities_{pid}.parquet")
|
||||||
finalhashesadded['policy'] = policy
|
finalhashesadded['policy'] = policy
|
||||||
finalhashesadded['allowlist'] = allowlist
|
finalhashesadded['allowlist'] = allowlist
|
||||||
finalhashesadded['added_at'] = time.time()
|
finalhashesadded['added_at'] = time.localtime()
|
||||||
|
|
||||||
if not os.path.exists(f"OTP\\PARQ\\localapprovalhistory.parquet"):
|
if not os.path.exists(f"OTP\\PARQ\\localapprovalhistory.parquet"):
|
||||||
df = pd.DataFrame()
|
df = pd.DataFrame()
|
||||||
|
|||||||
+48
-1
@@ -219,6 +219,53 @@ def recurring_job(
|
|||||||
})
|
})
|
||||||
save_jobs(jobs)
|
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
|
# Reload Saved Jobs
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
@@ -248,7 +295,7 @@ def reload_jobs():
|
|||||||
def start_scheduler():
|
def start_scheduler():
|
||||||
"""
|
"""
|
||||||
Start the scheduler loop (blocking).
|
Start the scheduler loop (blocking).
|
||||||
Call this once in your main program to begin.
|
Call this once in main to begin.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user