-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery refactored main branch #22
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
base: main
Are you sure you want to change the base?
Conversation
Version 0.39. See CHANGELOG.md
Version 0.43. See CHANGELOG.md
Version 0.44. See CHANGELOG.md
Version 0.44. See CHANGELOG.md
certCheck.py
Outdated
| parser = argparse.ArgumentParser(description='Certificate Checker v' + scriptVersion) | ||
| parser = argparse.ArgumentParser( | ||
| description=f'Certificate Checker v{scriptVersion}' | ||
| ) |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| # 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 | ||
| ) |
There was a problem hiding this comment.
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:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# Create the json script structure with all the meta data.
certCheck.py
Outdated
| print(uploadTime + " - " + str(uploadResult)) | ||
| print(f"{uploadTime} - {str(uploadResult)}") |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
certCheck.py
Outdated
| if args.contextVariables: | ||
| contextVariables = 1 | ||
| else: | ||
| contextVariables = 0 | ||
|
|
||
| contextVariables = 1 if args.contextVariables else 0 |
There was a problem hiding this comment.
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:
- Replace if statement with if expression (
assign-if-exp) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation)
| # Determine which context to create | ||
| if __hostinfo['options'] is not None and \ | ||
| "local_untrusted_allow" in __hostinfo['options']: | ||
| "local_untrusted_allow" in __hostinfo['options']: |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation [×5] (
use-fstring-for-concatenation)
| certSKI = __sslCertificate.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_KEY_IDENTIFIER) | ||
|
|
||
| return certSKI | ||
| return __sslCertificate.extensions.get_extension_for_oid( | ||
| ExtensionOID.SUBJECT_KEY_IDENTIFIER | ||
| ) |
There was a problem hiding this comment.
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:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
certificate/getCertificateChain.py
Outdated
| 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" | ||
| ) |
There was a problem hiding this comment.
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:
- Replace identity comprehension with call to collection constructor (
identity-comprehension) - Replace a for append loop with list extend (
for-append-to-extend)
| if __depth <= self.maxDepth: | ||
| # Retrive the AKI from the certificate. | ||
| certAKI = self.returnCertAKI(__sslCertificate) | ||
| # Retrieve the SKI from the certificate. | ||
| certSKI = self.returnCertSKI(__sslCertificate) | ||
| if __depth > self.maxDepth: | ||
| return | ||
| # Retrive the AKI from the certificate. | ||
| certAKI = self.returnCertAKI(__sslCertificate) | ||
| # Retrieve the SKI from the certificate. | ||
| certSKI = self.returnCertSKI(__sslCertificate) | ||
|
|
||
| # Sometimes the AKI can be none. Lets handle this accordingly. | ||
| if certAKI is not None: | ||
| certAKIValue = certAKI._value.key_identifier | ||
| else: | ||
| 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) |
There was a problem hiding this comment.
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:
- Add guard clause (
last-if-guard) - Replace if statement with if expression (
assign-if-exp)
certificate/getCertificateChain.py
Outdated
| for counter, certificateItem in enumerate(myCertChain): | ||
| for certificateItem in myCertChain: |
There was a problem hiding this comment.
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:
- Remove unnecessary calls to
enumeratewhen the index is not used (remove-unused-enumerate)
| 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) |
There was a problem hiding this comment.
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:
- Use items() to directly unpack dictionary values (
use-dict-items) - Inline variable that is immediately returned (
inline-immediately-returned-variable) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
This removes the following comments ( why? ):
# Return the human readable form string.
data/calculateStats.py
Outdated
| 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"] | ||
| ) |
There was a problem hiding this comment.
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:
- Replace comparison with min/max call [×2] (
min-max-identity)
| # Create the json script structure with all the meta data. | ||
| myData = { | ||
| return { |
There was a problem hiding this comment.
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:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# Create the json script structure with all the meta data.
data/certData.py
Outdated
| tmpData = ['URL Redirects too many'] | ||
| except requests.exceptions.ConnectionError: | ||
| print('Could not connect to URL - ' + fileURL + '\n') | ||
| print(f'Could not connect to URL - {fileURL}' + '\n') |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| line, options = line.split('[', 1) | ||
| # Remove the closing bracket and convert to a list | ||
| options = ast.literal_eval('[' + options) | ||
| options = ast.literal_eval(f'[{options}') |
There was a problem hiding this comment.
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:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| 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}') |
There was a problem hiding this comment.
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:
- Simplify logical expression using De Morgan identities (
de-morgan) - Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| __myDeviceId = "" | ||
| if "myDeviceId" in self.myConfigJson: | ||
| __myDeviceId = self.myConfigJson["myDeviceId"] | ||
| return __myDeviceId | ||
| return ( | ||
| self.myConfigJson["myDeviceId"] | ||
| if "myDeviceId" in self.myConfigJson | ||
| else "" | ||
| ) |
There was a problem hiding this comment.
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:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| __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 [] |
There was a problem hiding this comment.
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:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# First check to see if the myTags element is in the myConfigJson variable.
# Return the value of __myTags
| result = False | ||
| if "myTenantId" in __myConfigJson and __myConfigJson["myTenantId"] != "": | ||
| result = True | ||
| return result | ||
| return "myTenantId" in __myConfigJson and __myConfigJson["myTenantId"] != "" |
There was a problem hiding this comment.
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:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Simplify boolean if expression (
boolean-if-exp-identity) - Replace if statement with if expression (
assign-if-exp) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| result = bool("myTags" in __myConfigJson) | ||
| return result | ||
| return "myTags" in __myConfigJson |
There was a problem hiding this comment.
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:
- Inline variable that is immediately returned (
inline-immediately-returned-variable) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
| result = False | ||
| if "myDeviceId" in __myConfigJson and __myConfigJson["myDeviceId"] != "": | ||
| result = True | ||
| return result | ||
| return "myDeviceId" in __myConfigJson and __myConfigJson["myDeviceId"] != "" |
There was a problem hiding this comment.
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:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Simplify boolean if expression (
boolean-if-exp-identity) - Replace if statement with if expression (
assign-if-exp) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
389d558 to
da7c802
Compare
Branch
mainrefactored 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
mainbranch, then run:Help us improve this pull request!