Skip to content

Added integration test for TPM. - #31315

Merged
getvictor merged 8 commits into
mainfrom
victor/31048-tpm-test
Jul 29, 2025
Merged

Added integration test for TPM.#31315
getvictor merged 8 commits into
mainfrom
victor/31048-tpm-test

Conversation

@getvictor

@getvictor getvictor commented Jul 28, 2025

Copy link
Copy Markdown
Member

For #31048

This change includes some refactoring of orbit code. No functional changes. Moved non-Linux-specific code from securehw_linux.go to securehw_tpm.go so that tests on any platform can use it.

There are no server changes impacting the upcoming 4.72 release. Just tests.

Checklist for submitter

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

fleetd/orbit/Fleet Desktop

  • If the change applies to only one platform, confirmed that runtime.GOOS is used as needed to isolate changes
  • Verified that fleetd runs on macOS, Linux and Windows

Summary by CodeRabbit

  • New Features

    • Introduced a new TPM 2.0-based secure hardware interface, enabling creation, loading, and management of ECC keys within a TPM device.
    • Added support for both standard and RFC 9421-compatible HTTP signatures using TPM-backed keys.
  • Bug Fixes

    • Improved error handling and resource management for TPM operations.
  • Tests

    • Added comprehensive unit tests for TPM key file loading scenarios.
    • Introduced integration tests using a simulated TPM device to validate end-to-end secure hardware and SCEP workflows.
  • Chores

    • Updated dependencies for enhanced compatibility and security.
    • Modernized build constraints for improved maintainability.

@getvictor
getvictor requested a review from a team as a code owner July 28, 2025 10:19
@coderabbitai

coderabbitai Bot commented Jul 28, 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 TPM 2.0 Secure Hardware (SecureHW) implementation for Linux was refactored. The old implementation was removed and replaced with a new file that provides TPM key management and signing. Tests for TPM key file loading were added, and an integration test using a TPM simulator was introduced. Some dependencies were updated.

Changes

Cohort / File(s) Change Summary
TPM 2.0 SecureHW implementation refactor
ee/orbit/pkg/securehw/securehw_linux.go, ee/orbit/pkg/securehw/securehw_tpm.go
The old TPM 2.0 SecureHW implementation was deleted from securehw_linux.go and a new, modular implementation was added in securehw_tpm.go. The new version manages TPM key creation, loading, and signing, with improved structure and logging.
TPM key file loading tests
ee/orbit/pkg/securehw/securehw_test.go
New unit tests were added for the loadTPMKeyFile method, covering error cases and using a TPM simulator for testing.
Integration test with TPM simulator
ee/server/integrationtest/hostidentity/hostidentity_test.go
An integration test was added that exercises the SecureHW and SCEP packages using a simulated TPM device, verifying key creation, certificate enrollment, and signing.
Go module dependencies
go.mod
Added github.com/google/go-tpm-tools as an indirect dependency and upgraded go.uber.org/multierr.
Go build tag modernization
ee/orbit/pkg/securehw/securehw_stub.go
The legacy build constraint was removed, leaving only the modern Go build tag.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Runner
    participant TPM as TPM Simulator
    participant SecureHW as tpm2SecureHW
    participant Key as tpm2Key
    participant SCEP as SCEP Client
    participant Server as Enrollment Server

    Test->>TPM: Open simulator
    Test->>SecureHW: Create SecureHW with TPM
    SecureHW->>TPM: Create parent key
    SecureHW->>TPM: Create ECC key
    SecureHW->>Key: Return tpm2Key
    Test->>Key: Get public key
    Test->>SCEP: Create client with key and enrollment secret
    SCEP->>Server: Fetch certificate
    Server-->>SCEP: Return certificate
    SCEP-->>Test: Certificate and public key
    Test->>Key: Create HTTP signer
    Test->>Server: Send signed enrollment request
    Server-->>Test: Return node key
    Test->>Key: Close
    Test->>SecureHW: Load key from file
    SecureHW->>TPM: Load parent key
    SecureHW->>TPM: Load ECC key
    SecureHW->>Key: Return loaded tpm2Key
    Test->>Key: Create HTTP signer
    Test->>Server: Send signed config request
    Server-->>Test: Return config
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch victor/31048-tpm-test

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 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
getvictor marked this pull request as draft July 28, 2025 10:20
@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 28, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Full review triggered.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
ee/orbit/pkg/securehw/securehw_linux.go (1)

16-42: Fix the no-op logger issue.

The code creates a no-op logger on line 39, which disables all logging from the TPM implementation. This appears to be a bug.

 	return &tpm2SecureHW{
 		device:      device,
-		logger:      zerolog.Nop(),
+		logger:      logger.With().Str("component", "securehw-tpm").Logger(),
 		keyFilePath: filepath.Join(metadataDir, "host_identity_tpm.pem"),
 	}, nil
🧹 Nitpick comments (2)
ee/orbit/pkg/securehw/securehw_test.go (1)

1-2: Consider documenting why Windows is excluded.

The build constraint excludes Windows, but it would be helpful to add a comment explaining why these tests don't run on Windows.

 //go:build !windows
+// Tests use TPM simulator which may have platform-specific behavior
ee/orbit/pkg/securehw/securehw_tpm.go (1)

279-279: Consider adding a comment about deterministic parent keys.

While the comment on line 278 mentions this, it might be helpful to reference the createParentKey method documentation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4726d4 and acc01a0.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (6)
  • ee/orbit/pkg/securehw/securehw_linux.go (2 hunks)
  • ee/orbit/pkg/securehw/securehw_stub.go (0 hunks)
  • ee/orbit/pkg/securehw/securehw_test.go (1 hunks)
  • ee/orbit/pkg/securehw/securehw_tpm.go (1 hunks)
  • ee/server/integrationtest/hostidentity/hostidentity_test.go (5 hunks)
  • go.mod (2 hunks)
💤 Files with no reviewable changes (1)
  • ee/orbit/pkg/securehw/securehw_stub.go
🧰 Additional context used
🧠 Learnings (3)
ee/orbit/pkg/securehw/securehw_tpm.go (2)

Learnt from: getvictor
PR: #30825
File: ee/server/service/hostidentity/httpsig/httpsig.go:86-88
Timestamp: 2025-07-15T07:26:38.930Z
Learning: In ee/server/service/hostidentity/httpsig/httpsig.go, the Fetch method stub returning "not implemented" is safe because the verification profile requires MetaKeyID in RequiredMetadata, ensuring the httpsig library always calls FetchByKeyID instead of Fetch for valid signatures.

Learnt from: getvictor
PR: #30820
File: third_party/httpsig-go/base.go:241-248
Timestamp: 2025-07-13T10:10:47.226Z
Learning: In the vendored httpsig-go library at third_party/httpsig-go/, the @target-uri derived component is not being used, so bugs in the deriveTargetURI function are not a concern for the current implementation.

ee/server/integrationtest/hostidentity/hostidentity_test.go (4)

Learnt from: getvictor
PR: #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.

Learnt from: getvictor
PR: #30589
File: server/fleet/datastore.go:13-14
Timestamp: 2025-07-08T16:12:48.182Z
Learning: The Fleet team is not currently testing or enforcing OSS build compatibility, so imports of enterprise-only packages (like ee/server/service/hostidentity/types) into OSS code are acceptable for now.

Learnt from: getvictor
PR: #30825
File: ee/server/service/hostidentity/httpsig/httpsig.go:86-88
Timestamp: 2025-07-15T07:26:38.930Z
Learning: In ee/server/service/hostidentity/httpsig/httpsig.go, the Fetch method stub returning "not implemented" is safe because the verification profile requires MetaKeyID in RequiredMetadata, ensuring the httpsig library always calls FetchByKeyID instead of Fetch for valid signatures.

Learnt from: getvictor
PR: #30820
File: third_party/httpsig-go/base.go:241-248
Timestamp: 2025-07-13T10:10:47.226Z
Learning: In the vendored httpsig-go library at third_party/httpsig-go/, the @target-uri derived component is not being used, so bugs in the deriveTargetURI function are not a concern for the current implementation.

ee/orbit/pkg/securehw/securehw_linux.go (2)

Learnt from: getvictor
PR: #30825
File: ee/server/service/hostidentity/httpsig/httpsig.go:86-88
Timestamp: 2025-07-15T07:26:38.930Z
Learning: In ee/server/service/hostidentity/httpsig/httpsig.go, the Fetch method stub returning "not implemented" is safe because the verification profile requires MetaKeyID in RequiredMetadata, ensuring the httpsig library always calls FetchByKeyID instead of Fetch for valid signatures.

Learnt from: getvictor
PR: #30820
File: third_party/httpsig-go/base.go:241-248
Timestamp: 2025-07-13T10:10:47.226Z
Learning: In the vendored httpsig-go library at third_party/httpsig-go/, the @target-uri derived component is not being used, so bugs in the deriveTargetURI function are not a concern for the current implementation.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: test-go (vuln, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (mysql, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (vuln, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (main, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (mysql, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (integration-mdm, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (main, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (integration-mdm, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (integration-enterprise, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (fleetctl, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (fleetctl, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (integration-core, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: test-go (integration-core, ubuntu-latest, mysql:8.0.36, false)
  • GitHub Check: test-go (integration-enterprise, ubuntu-latest, mysql:9.3.0, false)
  • GitHub Check: lint (windows-latest)
  • GitHub Check: lint (ubuntu-latest)
  • GitHub Check: lint (macos-latest)
  • GitHub Check: build-binaries
  • GitHub Check: Analyze (go)
  • GitHub Check: publish
🔇 Additional comments (12)
go.mod (2)

251-251: Good addition for TPM support.

The github.com/google/go-tpm-tools dependency provides TPM key file handling functionality that's used in the new TPM implementation.


335-335: Minor version update looks good.

The update of go.uber.org/multierr from v1.10.0 to v1.11.0 is a minor version bump that should be backward compatible.

ee/orbit/pkg/securehw/securehw_test.go (1)

15-117: Excellent test coverage for TPM key file loading.

The test cases comprehensively cover the different failure scenarios for loading TPM key files. Good use of table-driven tests would make this even cleaner, but the current structure is clear and effective.

ee/server/integrationtest/hostidentity/hostidentity_test.go (3)

15-32: Good organization of new imports.

The new imports for TPM simulator, orbit SCEP client, and secure hardware support are properly organized and necessary for the integration tests.


211-217: Smart refactoring to use the wrapper.

Using fleethttpsig.Signer wrapper instead of direct httpsig.NewSigner provides better abstraction and consistency across the codebase.


989-1165: Comprehensive integration test for TPM functionality.

This test thoroughly exercises the TPM-based secure hardware implementation:

  • Creates and uses TPM keys
  • Tests SCEP certificate enrollment
  • Verifies HTTP signature generation
  • Tests key persistence and reloading

The test provides excellent coverage of the end-to-end flow.

ee/orbit/pkg/securehw/securehw_tpm.go (6)

29-51: Good design for test helper function.

The NewTestSecureHW function provides a clean way to inject a TPM simulator for testing. The validation and logging are appropriate.


113-119: Proper error handling with resource cleanup.

Good use of deferred cleanup function to ensure TPM resources are freed on error paths.


138-194: Smart implementation of deterministic parent key.

Creating the parent key deterministically using CreatePrimary ensures consistent key hierarchies across load operations. This is a clever approach that avoids the need to persist the parent key.


196-239: Practical curve detection method.

Testing P-384 support by attempting to create a key is more reliable than querying capabilities. Good fallback to P-256 when P-384 isn't supported.


248-248: Secure file permissions.

Using 0600 permissions ensures only the owner can read the key file, which is appropriate for sensitive key material.


499-523: Correct implementation of RFC 9421 signature format.

The fixed-width r||s format for HTTP signatures is properly implemented with appropriate padding. Good error checking for oversized components.

@codecov

codecov Bot commented Jul 28, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.43424% with 99 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.16%. Comparing base (e4726d4) to head (4e33c58).
⚠️ Report is 24 commits behind head on main.

Files with missing lines Patch % Lines
ee/orbit/pkg/securehw/securehw_tpm.go 75.81% 71 Missing and 26 partials ⚠️
ee/orbit/pkg/securehw/securehw_linux.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #31315      +/-   ##
==========================================
+ Coverage   64.01%   64.16%   +0.15%     
==========================================
  Files        1913     1914       +1     
  Lines      188293   188305      +12     
  Branches     5629     5629              
==========================================
+ Hits       120534   120825     +291     
+ Misses      58311    57998     -313     
- Partials     9448     9482      +34     
Flag Coverage Δ
backend 65.04% <75.43%> (+0.16%) ⬆️

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
getvictor marked this pull request as ready for review July 28, 2025 13:10
@getvictor getvictor linked an issue Jul 28, 2025 that may be closed by this pull request
@getvictor
getvictor merged commit d1992aa into main Jul 29, 2025
37 of 39 checks passed
@getvictor
getvictor deleted the victor/31048-tpm-test branch July 29, 2025 18:21
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.

Add integration tests that use a TPM simulator

2 participants