Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored main branch #22

Open
wants to merge 57 commits into
base: main
Choose a base branch
from
Open

Sourcery refactored main branch #22

wants to merge 57 commits into from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jul 30, 2023

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

deepsource-autofix bot and others added 30 commits July 17, 2022 22:25
Blank lines should not contain any tabs or spaces.
@sourcery-ai sourcery-ai bot requested a review from TheScriptGuy July 30, 2023 05:23
Comment on lines -33 to +35
parser = argparse.ArgumentParser(description='Certificate Checker v' + scriptVersion)
parser = argparse.ArgumentParser(
description=f'Certificate Checker v{scriptVersion}'
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parseArguments refactored with the following changes:

certCheck.py Outdated
Comment on lines 197 to 201
# Create the json script structure with all the meta data.
myData = myDetails.combineData(__certResults, __mySystemInfo, __scriptStartTime, __scriptEndTime)

return myData
return myDetails.combineData(
__certResults, __mySystemInfo, __scriptStartTime, __scriptEndTime
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function gatherData refactored with the following changes:

This removes the following comments ( why? ):

# Create the json script structure with all the meta data.

certCheck.py Outdated
Comment on lines 310 to 311
print(uploadTime + " - " + str(uploadResult))
print(f"{uploadTime} - {str(uploadResult)}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function processQueryFile refactored with the following changes:

certCheck.py Outdated
Comment on lines 321 to 322
if args.contextVariables:
contextVariables = 1
else:
contextVariables = 0

contextVariables = 1 if args.contextVariables else 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function processHostname refactored with the following changes:

"local_untrusted_allow" in __hostinfo['options']:
"local_untrusted_allow" in __hostinfo['options']:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function certificateModule.getCertificate refactored with the following changes:

Comment on lines 142 to 144
certSKI = __sslCertificate.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_KEY_IDENTIFIER)

return certSKI
return __sslCertificate.extensions.get_extension_for_oid(
ExtensionOID.SUBJECT_KEY_IDENTIFIER
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getCertificateChain.returnCertSKI refactored with the following changes:

Comment on lines 168 to 173
dataAIA = [x for x in certValue or []]
for item in dataAIA:
if item.access_method._name == "caIssuers":
aiaUriList.append(item.access_location._value)

dataAIA = list(certValue or [])
aiaUriList.extend(
item.access_location._value
for item in dataAIA
if item.access_method._name == "caIssuers"
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getCertificateChain.returnCertAIAList refactored with the following changes:

Comment on lines 185 to 243
certAKIValue = None

# Get the value of the SKI from certSKI
certSKIValue = certSKI._value.digest

# Sometimes the AKI can be none. Lets handle this accordingly.
if certAKIValue is not None:
aiaUriList = self.returnCertAIAList(__sslCertificate)
if aiaUriList != []:
# Iterate through the aiaUriList list.
for item in aiaUriList:
# get the certificate for the item element.
nextCert = self.getCertificateFromUri(item)

# If the certificate is not none (great), append it to the certChain, increase the __depth and run the walkTheChain subroutine again.
if nextCert is not None:
self.certChain.append(nextCert)
__depth += 1
self.walkTheChain(nextCert, __depth)
else:
print("Could not retrieve certificate.")
sys.exit(1)
else:
"""Now we have to go on a hunt to find the root from a standard root store."""
print("Certificate didn't have AIA...ruh roh.")

# Load the Root CA Cert Chain.
caRootStore = self.loadRootCACertChain("cacert.pem")

# Assume we cannot find a Root CA
rootCACN = None

# Iterate through the caRootStore object.
for rootCA in caRootStore:
try:
rootCACertificatePEM = caRootStore[rootCA]
rootCACertificate = x509.load_pem_x509_certificate(rootCACertificatePEM.encode('ascii'))
rootCASKI = self.returnCertSKI(rootCACertificate)
rootCASKI_Value = rootCASKI._value.digest
if rootCASKI_Value == certAKIValue:
rootCACN = rootCA
print(f"Root CA Found - {rootCACN}")
self.certChain.append(rootCACertificate)
break
except x509.extensions.ExtensionNotFound:
# Apparently some Root CA's don't have a SKI?
pass

if rootCACN is None:
print("ERROR - Root CA NOT found.")
certAKIValue = certAKI._value.key_identifier if certAKI is not None else None
# Get the value of the SKI from certSKI
certSKIValue = certSKI._value.digest

# Sometimes the AKI can be none. Lets handle this accordingly.
if certAKIValue is not None:
aiaUriList = self.returnCertAIAList(__sslCertificate)
if aiaUriList != []:
# Iterate through the aiaUriList list.
for item in aiaUriList:
# get the certificate for the item element.
nextCert = self.getCertificateFromUri(item)

# If the certificate is not none (great), append it to the certChain, increase the __depth and run the walkTheChain subroutine again.
if nextCert is not None:
self.certChain.append(nextCert)
__depth += 1
self.walkTheChain(nextCert, __depth)
else:
print("Could not retrieve certificate.")
sys.exit(1)
else:
"""Now we have to go on a hunt to find the root from a standard root store."""
print("Certificate didn't have AIA...ruh roh.")

# Load the Root CA Cert Chain.
caRootStore = self.loadRootCACertChain("cacert.pem")

# Assume we cannot find a Root CA
rootCACN = None

# Iterate through the caRootStore object.
for rootCA in caRootStore:
try:
rootCACertificatePEM = caRootStore[rootCA]
rootCACertificate = x509.load_pem_x509_certificate(rootCACertificatePEM.encode('ascii'))
rootCASKI = self.returnCertSKI(rootCACertificate)
rootCASKI_Value = rootCASKI._value.digest
if rootCASKI_Value == certAKIValue:
rootCACN = rootCA
print(f"Root CA Found - {rootCACN}")
self.certChain.append(rootCACertificate)
break
except x509.extensions.ExtensionNotFound:
# Apparently some Root CA's don't have a SKI?
pass

if rootCACN is None:
print("ERROR - Root CA NOT found.")
sys.exit(1)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getCertificateChain.walkTheChain refactored with the following changes:

Comment on lines 264 to 262
for counter, certificateItem in enumerate(myCertChain):
for certificateItem in myCertChain:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getCertificateChain.writeChainToFile refactored with the following changes:

Comment on lines 39 to 46
for field in myDateTime:
if myDateTime[field] > 1:
for field, value in myDateTime.items():
if value > 1:
humanReadable = f"{myDateTime[field]} {field}"
timeYMDHMS.append(humanReadable)
else:
if myDateTime[field] == 1:
humanReadable = f"{myDateTime[field]} {field[:-1]}"
timeYMDHMS.append(humanReadable)
myDateTimeString = ', '.join(timeYMDHMS)

# Return the human readable form string.
return myDateTimeString
elif myDateTime[field] == 1:
humanReadable = f"{myDateTime[field]} {field[:-1]}"
timeYMDHMS.append(humanReadable)
return ', '.join(timeYMDHMS)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculateStats.convertTimeIntoHumanReadable refactored with the following changes:

This removes the following comments ( why? ):

# Return the human readable form string.

Comment on lines 96 to 98
if lowestCertificateTemplateTime > item["certificateTemplateTime"]:
lowestCertificateTemplateTime = item["certificateTemplateTime"]

lowestCertificateTemplateTime = min(
lowestCertificateTemplateTime, item["certificateTemplateTime"]
)
# Calculate highest certificate template time.
if highestCertificateTemplateTime < item["certificateTemplateTime"]:
highestCertificateTemplateTime = item["certificateTemplateTime"]

highestCertificateTemplateTime = max(
highestCertificateTemplateTime, item["certificateTemplateTime"]
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculateStats.calculateStatistics refactored with the following changes:

Comment on lines -173 to +169
# Create the json script structure with all the meta data.
myData = {
return {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculateStats.combineData refactored with the following changes:

This removes the following comments ( why? ):

# Create the json script structure with all the meta data.

print('Could not connect to URL - ' + fileURL + '\n')
print(f'Could not connect to URL - {fileURL}' + '\n')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function certData.getFileFromURL refactored with the following changes:

data/certData.py Outdated
options = ast.literal_eval('[' + options)
options = ast.literal_eval(f'[{options}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function certData.parse_line refactored with the following changes:

data/certData.py Outdated
Comment on lines 98 to 109
elif path.exists(queriesFile) and not (queriesFile.startswith('http://') or queriesFile.startswith('https://')):
elif (
path.exists(queriesFile)
and not queriesFile.startswith('http://')
and not queriesFile.startswith('https://')
):
with open(queriesFile, "r", encoding="utf-8") as f_queryFile:
queryFile = f_queryFile.readlines()
for line in queryFile:
hostEntry = certData.parse_line(line)
queries.append(hostEntry)
else:
print('I cannot get file ' + queriesFile)
print(f'I cannot get file {queriesFile}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function certData.loadQueriesFile refactored with the following changes:

Comment on lines -34 to +38
__myDeviceId = ""
if "myDeviceId" in self.myConfigJson:
__myDeviceId = self.myConfigJson["myDeviceId"]
return __myDeviceId
return (
self.myConfigJson["myDeviceId"]
if "myDeviceId" in self.myConfigJson
else ""
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function systemInfo.getDeviceId refactored with the following changes:

Comment on lines -41 to +42
__myTags = []

# First check to see if the myTags element is in the myConfigJson variable.
if "myTags" in self.myConfigJson:
__myTags = self.myConfigJson["myTags"]

# Return the value of __myTags
return __myTags
return self.myConfigJson["myTags"] if "myTags" in self.myConfigJson else []
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function systemInfo.getTag refactored with the following changes:

This removes the following comments ( why? ):

# First check to see if the myTags element is in the myConfigJson variable.
# Return the value of __myTags

Comment on lines 98 to 92
result = False
if "myTenantId" in __myConfigJson and __myConfigJson["myTenantId"] != "":
result = True
return result
return "myTenantId" in __myConfigJson and __myConfigJson["myTenantId"] != ""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function systemInfo.checkMyTenantId refactored with the following changes:

Comment on lines -110 to +101
result = bool("myTags" in __myConfigJson)
return result
return "myTags" in __myConfigJson
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function systemInfo.checkMyTags refactored with the following changes:

Comment on lines 120 to 110
result = False
if "myDeviceId" in __myConfigJson and __myConfigJson["myDeviceId"] != "":
result = True
return result
return "myDeviceId" in __myConfigJson and __myConfigJson["myDeviceId"] != ""
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function systemInfo.checkMyDeviceId refactored with the following changes:

@TheScriptGuy TheScriptGuy force-pushed the main branch 2 times, most recently from 389d558 to da7c802 Compare January 7, 2024 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant