-
Notifications
You must be signed in to change notification settings - Fork 2
Sourcery refactored main branch #11
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
| parser = argparse.ArgumentParser(description='Get Certificate Chain v' + scriptVersion) | ||
| parser = argparse.ArgumentParser( | ||
| description=f'Get Certificate Chain 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)
| # Make sure the filename string is lower case | ||
| newNormalizedName = ''.join(commonName).lower() | ||
|
|
||
| # Return newNormalizedName | ||
| return newNormalizedName | ||
| return ''.join(commonName).lower() |
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 normalizeSubject refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# Make sure the filename string is lower case
# Return newNormalizedName
| 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 returnCertSKI refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| 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 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 <= maxDepth: | ||
| # Retrive the AKI from the certificate. | ||
| certAKI = returnCertAKI(__sslCertificate) | ||
| # Retrieve the SKI from the certificate. | ||
| certSKI = returnCertSKI(__sslCertificate) | ||
| if __depth > maxDepth: | ||
| return | ||
| # Retrive the AKI from the certificate. | ||
| certAKI = returnCertAKI(__sslCertificate) | ||
| # Retrieve the SKI from the certificate. | ||
| certSKI = 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 = returnCertAIAList(__sslCertificate) | ||
| if aiaUriList != []: | ||
| # Iterate through the aiaUriList list. | ||
| for item in aiaUriList: | ||
| # get the certificate for the item element. | ||
| nextCert = 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: | ||
| certChain.append(nextCert) | ||
| __depth += 1 | ||
| 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 = 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 = returnCertSKI(rootCACertificate) | ||
| rootCASKI_Value = rootCASKI._value.digest | ||
| if rootCASKI_Value == certAKIValue: | ||
| rootCACN = rootCA | ||
| print(f"Root CA Found - {rootCACN}") | ||
| 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 = returnCertAIAList(__sslCertificate) | ||
| if aiaUriList != []: | ||
| # Iterate through the aiaUriList list. | ||
| for item in aiaUriList: | ||
| # get the certificate for the item element. | ||
| nextCert = 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: | ||
| certChain.append(nextCert) | ||
| __depth += 1 | ||
| 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 = 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 = returnCertSKI(rootCACertificate) | ||
| rootCASKI_Value = rootCASKI._value.digest | ||
| if rootCASKI_Value == certAKIValue: | ||
| rootCACN = rootCA | ||
| print(f"Root CA Found - {rootCACN}") | ||
| 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 walkTheChain refactored with the following changes:
- Add guard clause (
last-if-guard) - Replace if statement with if expression (
assign-if-exp)
| sslCertificateFilename = str(len(__certificateChain) - 1 - counter) + '-' + normalizedSubject + '.crt' | ||
| sslCertificateFilename = f'{str(len(__certificateChain) - 1 - counter)}-{normalizedSubject}.crt' |
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 writeChainToFile refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation)
| # If the ':' is in the hostname argument, then we'll assume it's meant to be a port following the ':'. | ||
| if ":" in args.hostname: | ||
| tmpLine = args.hostname.split(':') | ||
| hostnameQuery = {"hostname": tmpLine[0], "port": int(tmpLine[1])} | ||
|
|
||
| else: | ||
| if ":" not in args.hostname: | ||
| # If no ':' is found, then set default port 443. | ||
| hostnameQuery = {"hostname": args.hostname, "port": 443} | ||
| return {"hostname": args.hostname, "port": 443} | ||
|
|
||
| return hostnameQuery | ||
| tmpLine = args.hostname.split(':') | ||
| return {"hostname": tmpLine[0], "port": int(tmpLine[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 checkHostname refactored with the following changes:
- Lift return into if (
lift-return-into-if) - Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
This removes the following comments ( why? ):
# If the ':' is in the hostname argument, then we'll assume it's meant to be a port following the ':'.
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!