Skip to content

Deleting host deletes its host identity cert. - #30984

Merged
getvictor merged 4 commits into
mainfrom
victor/30474-delete-host
Jul 17, 2025
Merged

Deleting host deletes its host identity cert.#30984
getvictor merged 4 commits into
mainfrom
victor/30474-delete-host

Conversation

@getvictor

@getvictor getvictor commented Jul 17, 2025

Copy link
Copy Markdown
Member

Fixes #30474

Checklist for submitter

  • Added/updated automated tests

Summary by CodeRabbit

  • Tests
    • Added new test cases to verify that re-enrollment with a deleted host's certificate is correctly rejected.
  • Bug Fixes
    • Improved host deletion to ensure related certificate records are properly cleared.

@coderabbitai

coderabbitai Bot commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes add new subtests to existing integration tests to check that, after deleting a host, attempts to re-enroll with the deleted host's certificate are rejected. Additionally, the host deletion logic is updated to clear certificates from the host_identity_scep_certificates table when a host is deleted.

Changes

Files/Paths Change Summary
ee/server/integrationtest/hostidentity/hostidentity_test.go Added subtests to verify that re-enrollment with a deleted host's certificate is rejected.
server/datastore/mysql/hosts.go Added host_identity_scep_certificates table to the list of tables cleared when deleting a host.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant Datastore
    participant API

    Test->>API: Enroll host (with certificate)
    API->>Datastore: Store host and certificate
    Test->>Datastore: Retrieve host by node key
    Test->>API: Delete host
    API->>Datastore: Remove host and related certificate
    Test->>API: Attempt to re-enroll with deleted certificate
    API-->>Test: Respond with 401 Unauthorized
Loading

Assessment against linked issues

Objective Addressed Explanation
Deleting a host should remove the certificate from DB (#30474)
If enrollment secret unchanged, deleted host can get new cert and re-enroll (#30474) The new tests only check that re-enrollment with the deleted cert is rejected, not that a new cert can be issued and re-enrollment succeeds.

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
None No out-of-scope changes were found. All changes are related to the objectives in the issue.
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch victor/30474-delete-host

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 17, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Full review triggered.

@codecov

codecov Bot commented Jul 17, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.33%. Comparing base (f79fed9) to head (1c0e6b0).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #30984      +/-   ##
==========================================
- Coverage   64.33%   64.33%   -0.01%     
==========================================
  Files        1892     1892              
  Lines      185742   185742              
  Branches     5316     5316              
==========================================
- Hits       119495   119488       -7     
- Misses      56881    56885       +4     
- Partials     9366     9369       +3     
Flag Coverage Δ
backend 65.23% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai are you still working on the code review? How can I tell if you're still working or if you're done?

@coderabbitai

coderabbitai Bot commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

@getvictor, I'm ready to provide the full review now! When I respond with "Full review triggered," it means I'm acknowledging your request, but the actual review comes in a subsequent comment (like this one).

Here's how you can tell my status:

  • Still working: You'll see "Full review triggered" but no detailed review yet
  • Done: You'll see my comprehensive review with specific feedback, code analysis, and recommendations

Let me provide the full review of your PR now:


🏁 Script executed:

#!/bin/bash
# Let's examine the key files that were changed
echo "=== Examining the host deletion logic ==="
cat server/datastore/mysql/hosts.go | grep -A 20 -B 5 "host_identity_scep_certificates"

echo -e "\n=== Examining the integration tests ==="
cat ee/server/integrationtest/hostidentity/hostidentity_test.go | grep -A 30 -B 5 "deleted host"

echo -e "\n=== Let's look at the hostRefs array context ==="
cat server/datastore/mysql/hosts.go | grep -A 10 -B 10 "hostRefs"

Length of output: 8166


🔍 Full Review

This PR effectively addresses the security concern from issue #30474 by ensuring host identity certificates are properly cleaned up when hosts are deleted. Here's my detailed analysis:

Core Changes Analysis

1. Database Cleanup Logic (server/datastore/mysql/hosts.go)

var hostRefs = []string{
    // ... existing tables ...
    "host_identity_scep_certificates",  // ← New addition
}

This change is excellent because it:

  • Follows the established pattern for host-related table cleanup
  • Ensures certificates are automatically deleted when hosts are deleted
  • Leverages the existing delHostRef function in the transaction loop

2. Security Validation Tests (ee/server/integrationtest/hostidentity/hostidentity_test.go)

The new test subtests are comprehensive and verify:

  • Orbit enrollment: Confirms deleted host certificates are rejected (HTTP 401)
  • Osquery enrollment: Same validation for osquery enrollment flow
  • Integration testing: Tests the complete flow from enrollment → deletion → re-enrollment attempt

🔒 Security Benefits

This change provides important security improvements:

  1. Prevents certificate reuse: Deleted host certificates can no longer be used for re-enrollment
  2. Enforces fresh authentication: Forces hosts to obtain new certificates after deletion
  3. Maintains audit trail integrity: Prevents confusion about which certificates are valid

📊 Test Coverage Assessment

The test coverage is thorough:

  • ✅ Tests both enrollment methods (Orbit and Osquery)
  • ✅ Verifies proper HTTP 401 response for deleted certificates
  • ✅ Uses integration tests to validate the complete flow
  • ✅ Tests run in the correct order (enrollment → deletion → re-enrollment)

🎯 Code Quality

  • Consistency: Uses the same cleanup pattern as other host-related tables
  • Maintainability: Changes are minimal and focused
  • Readability: Clear test descriptions and comments

💡 Potential Considerations

  1. Database Performance: The cleanup operation is already batched (hostsDeleteBatchSize = 5000), so performance impact should be minimal

  2. Certificate Lifecycle: The change correctly handles certificate deletion, but legitimate re-enrollment scenarios (with new certificates) should still work - which appears to be the intended behavior

🏆 Overall Assessment

This is a well-implemented security fix that:

  • Addresses the specific vulnerability described in the issue
  • Follows established code patterns
  • Includes comprehensive test coverage
  • Maintains backward compatibility for legitimate use cases

The implementation is clean, focused, and properly tested. Great work on addressing this security concern!


🧠 Learnings used
Learnt from: getvictor
PR: fleetdm/fleet#30589
File: ee/server/service/hostidentity/depot/depot.go:104-119
Timestamp: 2025-07-08T16:06:54.576Z
Learning: In ee/server/service/hostidentity/depot/depot.go, the security concern where shared challenges allow certificate revocation (lines 104-119) is a known issue that will be addressed in a later feature, not an immediate concern to fix.

@getvictor
getvictor marked this pull request as ready for review July 17, 2025 12:40
@getvictor
getvictor requested a review from a team as a code owner July 17, 2025 12:40
@getvictor
getvictor merged commit d70f5fd into main Jul 17, 2025
@getvictor
getvictor deleted the victor/30474-delete-host branch July 17, 2025 13:40
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.

TPM: delete host

2 participants