Skip to content

Commit

Permalink
RELEASE/v1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
celic committed Jun 22, 2023
1 parent 917d50e commit e738c7e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
59 changes: 40 additions & 19 deletions client/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import sys
import json
import requests
from utilities.utils import clear_previous_run, get_ids, cert_prep, check_type, log, ref_payload, check_status, isTOTPExpired
from utilities.utils import clear_previous_run, get_ids, cert_prep, check_type, log, ref_payload, check_status, isTOTPExpired, didTOTPFail
import os
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed, thread
import argparse
import traceback
from threads.thread_runner import ThreadWrapper
from totp.totp import generate_pass, login_payload
from start.parsing import parse_config, parse_run
Expand All @@ -30,33 +31,36 @@ def new_getaddrinfo(*args, **kwargs):
#Checks TOTP window and refresh jwt if not in previous window
def eajwt_refresh(ea_jwt, sec = int(time.time() / 30)-1):
if sec == int(time.time() / 30): #if in same window, keep same token
print("inside if")
if(globalenv.verboseMode):
print("Same TOTP window, using previous token")
auth_header = {'Authorization': 'Bearer ' + ea_jwt}
return ea_jwt, auth_header
try: #if trying refresh without login
payload = ref_payload(generate_pass(seed_path), ea_jwt)

totpAlreadyUsed = False
retries = 0
if(globalenv.verboseMode):
print("New TOTP window, renewing previous token")
totpCheck = True
response = ""
while not(totpAlreadyUsed) and retries < 3:
retries+=1
while(totpCheck):
payload = ref_payload(generate_pass(seed_path), ea_jwt)
response = requests.post(server_url + '/login', cert=client_cert, json=payload, verify=False)
totpAlreadyUsed = not(isTOTPExpired(response))
if(not(totpAlreadyUsed)):
print("TOTP Window has already been used. Retrying...")
time.sleep(15)
check_status(response)
if(globalenv.verboseMode):
print(response.json())
if(globalenv.verboseMode):
print(response.json())
if isTOTPExpired(response) or didTOTPFail(response):
totpCheck = True
print("TOTP Window has already been used. Will retry...")
time.sleep(30)
else:
totpCheck = False

jwt_token = response.json()[1]['accessToken']
auth_header = {'Authorization': 'Bearer ' + jwt_token}

return jwt_token, auth_header
except:
except Exception as e:
print(e)
auth_header = {'Authorization': 'Bearer ' + ea_jwt}
return ea_jwt, auth_header


#Gets stats from previous run
def prev_run(server_url, ea_id, df_ids, jwt_token, client_cert):
try:
Expand Down Expand Up @@ -123,18 +127,35 @@ def prev_run(server_url, ea_id, df_ids, jwt_token, client_cert):

#Do a run from the log file
if run_type == "status":

#log_file = json.load(open('jsons\\log.json', 'r'))[0]
client_cert, seed_path, server_url, esv_version = parse_config(config_path)
assessment_reg, raw_noise, restart_test, conditioned, supporting_paths, comments, sdType, mod_id, vend_id, entropyId, oe_id, certify, single_mod, responses, itar = parse_run(run_path)

#print("Logging in...")

#self.login_jwt = login_jwt
#self.auth_header = auth_header
#print("\nLogin Success!")
if len(responses) > 1:
print("*** Multiple OE statuses, responses will be batched")
count = 1
for response in responses:
if len(responses) > 1:
print("*** OE Batch " + str(count))
count = count + 1
entr_jwt = response.entr_jwt
print("Refreshing Token"); jwt_token, _ = eajwt_refresh(entr_jwt)
print("\nUsing values from previous run...")
if(globalenv.verboseMode):
print("Refreshing Token")
jwt_token, _ = eajwt_refresh(entr_jwt)
if(globalenv.verboseMode):
print("\nUsing values from previous run...")

ea_id = response.ea_id
df_ids = response.df_ids
prev_run(server_url, ea_id, df_ids, jwt_token, client_cert)


#Send Registration and Data Files
if run_type == "submit":
clear_previous_run()
Expand Down
9 changes: 1 addition & 8 deletions client/start/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ def parse_run(run_path):
print("Error: Number of oeIDs provided must match numberOfOEs in Assessment Registration. numberOfOEs is ", numberOfOEs, " but provided number of oeIDs is ", len(oeId))
sys.exit(1)


#rawNoise = run_file[0]["DataFiles"]["rawNoisePath"]
#restartTest = run_file[0]["DataFiles"]["restartTestPath"]
#conditioned = run_file[0]["DataFiles"]["unvettedConditionedPaths"] #need to take sequence position into account

#supportingDocuments = run_file[0]["SupportingDocuments"]

supporting_paths = [] # run_file[0]["SupportingDocuments"]["filePaths"]
comments = [] # run_file[0]["SupportingDocuments"]["comments"]
sdType = [] # run_file[0]["SupportingDocuments"]["sdType"]
Expand All @@ -88,7 +81,7 @@ def parse_run(run_path):

run_checks(comments, sdType, supporting_paths)
singleMod = run_file[0]["Assessment"]['limitEntropyAssessmentToSingleModule']
modId = None; vendId = None; #oeId = None
entropyId = None; modId = None; vendId = None; itar = None

certify = run_file[0]["Certify"]['Certify']
if certify: #Certification requires module and vendor IDs
Expand Down
12 changes: 7 additions & 5 deletions client/threads/thread_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def get_status(server_url, ea_id, id, entrjwt, client_cert):
safe_print(errorMsg)
#break
sys.exit(1)
time.sleep(15)
if not any (x in status.lower() for x in substrings):
time.sleep(15)

st = list(response.json()[1].values())
stLabel = list(response.json()[1].keys())
dataFiles.append(id); dataLabels.append(stLabel[0]); statLabels.append(stLabel[1]); stats.append(st[1])
# Removed 3/28/2023: This is just printing out the status that was already printed out on the previous iteration
#st = list(response.json()[1].values())
#stLabel = list(response.json()[1].keys())
#dataFiles.append(id); dataLabels.append(stLabel[0]); statLabels.append(stLabel[1]); stats.append(st[1])

safe_print(dataLabels[0] + ": " + str(dataFiles[0]) + " | " + statLabels[0] + ": " + str(stats[0])) # + " | Entropy Estimate: "
#safe_print(dataLabels[0] + ": " + str(dataFiles[0]) + " | " + statLabels[0] + ": " + str(stats[0])) # + " | Entropy Estimate: "

return response

Expand Down
13 changes: 12 additions & 1 deletion client/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,22 @@ def isTOTPExpired(response):
responseJson = response.json()
errorMsg = responseJson[1]["error"]

print(errorMsg.lower())
#print(errorMsg.lower())
if "totp" in errorMsg.lower() and "window" in errorMsg.lower():
return True
return False

def didTOTPFail(response):
if int(response.status_code) != 403:
return False
responseJson = response.json()
errorMsg = responseJson[1]["error"]

#print(errorMsg.lower())
if "totp" in errorMsg.lower() and "failed" in errorMsg.lower():
return True
return False

def create_log_file():
f = open('jsons/log.json',"w")
f.write("[{\"entr_jwt\": \"placeholder\",\"df_ids\": [\"1\",\"2\"],\"ea_id\": \"1\",\"server_url\": \"https://demo.esvts.nist.gov:7443/esv/v1\",\"client_cert\": [\"ESVTest.cer\",\"ESVTest.key\"],\"config_path\": \"config.json\",\"run_path\": \"run.json\",\"cert_supp\": [[{\"sdId\": 1,\"accessToken\": \"placeholder\"}]]}]")
Expand Down

0 comments on commit e738c7e

Please sign in to comment.