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

Add more explicit auto id tests #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

dastansam
Copy link
Member

@dastansam dastansam commented May 13, 2024

PR Type

enhancement, tests


Description

  • Added a new test test_auto_id_deterministic to ensure deterministic auto ID generation for certificates using the blake2b_256 hashing function.
  • Verified the auto ID for both issuer and child certificates, ensuring the correct implementation of the hashing and encoding processes.

Changes walkthrough 📝

Relevant files
Tests
test_certificate_manager.py
Add Deterministic Auto ID Generation Tests for Certificates

tests/auto_identity_tests/test_certificate_manager.py

  • Added a new import for blake2b_256 hashing function.
  • Added a new test test_auto_id_deterministic to verify deterministic
    auto ID generation for certificates.
  • Ensured the PEM certificate is in bytes and verified the auto ID of
    both issuer and child certificates using blake2b_256.
  • +23/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @dastansam dastansam changed the title Add more auto-id tests Add more explicit auto id tests May 13, 2024
    @github-actions github-actions bot added enhancement New feature or request tests labels May 13, 2024
    Copy link

    PR Description updated to latest commit (9f1e74d)

    Copy link

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the PR is focused on adding a specific test for deterministic auto ID generation, which is a relatively straightforward task. The changes are well-documented and localized to a single test function.

    🧪 Relevant tests

    Yes

    ⚡ Possible issues

    Hardcoded Values: The test test_auto_id_deterministic uses hardcoded values for expected auto IDs. This could lead to brittle tests that might fail if the underlying implementation changes.

    🔒 Security concerns

    No

    Code feedback:
    relevant filetests/auto_identity_tests/test_certificate_manager.py
    suggestion      

    Consider using a dynamic calculation for the expected auto ID values instead of hardcoding them. This will make the tests more robust against changes in the hashing function or its implementation. [important]

    relevant lineassert issuer_auto_id == "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba"

    relevant filetests/auto_identity_tests/test_certificate_manager.py
    suggestion      

    Add cleanup code or use a fixture to handle the creation and disposal of keys and certificates to prevent any side effects between tests. [medium]

    relevant lineprivate_key, _ = key_management.generate_ed25519_key_pair()

    relevant filetests/auto_identity_tests/test_certificate_manager.py
    suggestion      

    Consider verifying the entire certificate chain's auto ID, not just the issuer and child, to ensure complete coverage and integrity of the certificate hierarchy. [medium]

    relevant linechild_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child"))

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Replace hardcoded hash value with dynamic calculation in tests

    Replace the hardcoded hash value with a dynamic calculation to ensure the test remains
    valid even if the underlying data or hash function changes.

    tests/auto_identity_tests/test_certificate_manager.py [143]

    -assert issuer_auto_id == "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba"
    +expected_issuer_auto_id = blake2b_256(self_issuer.get_subject_common_name(certificate.subject).encode()).hex()
    +assert issuer_auto_id == expected_issuer_auto_id
     
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly identifies a hardcoded value in a test assertion, which can lead to brittle tests if underlying data changes. Replacing it with a dynamic calculation improves the test's robustness and adaptability.

    8
    Enhancement
    Ensure the blake2b_256 function is correctly imported and functional

    Add a test to verify that the blake2b_256 function is correctly imported and functional,
    as it is critical for generating deterministic IDs.

    tests/auto_identity_tests/test_certificate_manager.py [4]

     from auto_identity import blake2b_256
    +def test_blake2b_256_functionality():
    +    test_input = "test".encode()
    +    expected_output = "..."  # Expected hash result
    +    assert blake2b_256(test_input).hex() == expected_output
     
    Suggestion importance[1-10]: 7

    Why: Testing the functionality of blake2b_256 is important since it's critical for generating deterministic IDs. This suggestion ensures that any issues with the function are caught early in the testing phase.

    7
    Best practice
    Add error handling for cryptographic operations to manage potential exceptions

    Consider adding error handling for the cryptographic operations to manage exceptions that
    might occur during key generation, certificate issuance, or CSR creation.

    tests/auto_identity_tests/test_certificate_manager.py [131-146]

    -private_key, _ = key_management.generate_ed25519_key_pair()
    -certificate = self_issuer.self_issue_certificate(subject_name)
    -child_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child"))
    +try:
    +    private_key, _ = key_management.generate_ed25519_key_pair()
    +    certificate = self_issuer.self_issue_certificate(subject_name)
    +    child_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child"))
    +except Exception as e:
    +    # Handle exceptions appropriately
    +    print(f"An error occurred: {e}")
     
    Suggestion importance[1-10]: 7

    Why: Adding error handling for cryptographic operations is a best practice that can prevent unhandled exceptions from causing runtime failures, especially in security-sensitive operations like key generation and certificate handling.

    7
    Maintainability
    Use descriptive variable names for clarity

    Use a more descriptive variable name than subject_name to enhance code readability and
    maintainability.

    tests/auto_identity_tests/test_certificate_manager.py [132]

    -subject_name = "Test"
    +test_subject_name = "Test"
     
    Suggestion importance[1-10]: 5

    Why: While the suggestion to rename subject_name to test_subject_name enhances clarity, it is a minor improvement and does not significantly impact the overall maintainability or readability of the code.

    5

    Copy link
    Collaborator

    @EmilFattakhov EmilFattakhov left a comment

    Choose a reason for hiding this comment

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

    Left a few comments, nothing blocking!

    # for child certificate, auto_id of the issuer is included in the data
    child_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child"))

    assert CertificateManager.get_certificate_auto_id(child_certificate) == blake2b_256(CertificateManager.get_certificate_auto_id(
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    These assertions are a bit long, consider a slight refactor

    Suggested change
    assert CertificateManager.get_certificate_auto_id(child_certificate) == blake2b_256(CertificateManager.get_certificate_auto_id(
    computed_auto_id = blake2b_256(CertificateManager.get_certificate_auto_id(certificate).encode() + self_issuer.get_subject_common_name(child_certificate.subject).encode()).hex()
    assert CertificateManager.get_certificate_auto_id(child_certificate) == computed_auto_id

    child_certificate = self_issuer.issue_certificate(self_issuer.create_and_sign_csr("child"))

    assert CertificateManager.get_certificate_auto_id(child_certificate) == blake2b_256(CertificateManager.get_certificate_auto_id(
    certificate).encode() + self_issuer.get_subject_common_name(child_certificate.subject).encode()).hex()
    Copy link
    Collaborator

    Choose a reason for hiding this comment

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

    Suggested change
    certificate).encode() + self_issuer.get_subject_common_name(child_certificate.subject).encode()).hex()


    issuer_auto_id = CertificateManager.get_certificate_auto_id(certificate)
    assert issuer_auto_id == blake2b_256(self_issuer.get_subject_common_name(certificate.subject).encode()).hex()
    assert issuer_auto_id == "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba"
    Copy link
    Collaborator

    @EmilFattakhov EmilFattakhov May 14, 2024

    Choose a reason for hiding this comment

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

    I wasn't familiar where the hardcoded hash was coming from, until I've done a quick Googling. While this is not a huge problem now, may be confusing in a long run.

    I would suggest to introduce two constants "8d2143d76615c515b5cc88fa7806aef268edeea87571c8f8b21a19f77b9993ba" and "d1575259a7e413f51e24ece5e353f5015fb0e39e6738b2044a3d7a8c3bc11114" closer to beginning of the file or function, and highlight that they're coming from the blake2b_256 library.
    Maybe something like EXPECTED_SELF_ISSUED_CERT_BLAKE2B_HASH and EXPECTED_CHILD_CERT_BLAKE2B_HASH could work good, wdyt?

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants