Repository navigation
test: add tpm_devid integration suite backed by a software TPM - #7203
amartinezfayo merged 4 commits into
Conversation
382a2e6 to
cd6641f
Compare
The tpm_devid plugin is only exercised by whitebox unit tests against the in-process simulator in test/tpmsimulator, so nothing covers it end to end against a real TPM implementation. Add a provisioner that drives a software TPM over a socket and writes the credentials the plugin expects: the DevID certificate and TPM key blobs for the agent, and the DevID and endorsement CA bundles for the server. It takes the key templates, the NV index and the TPM open call from SPIRE rather than restating them. The agent regenerates the endorsement key from DefaultEKTemplateRSA and loads the DevID key under SRKTemplateHighRSA at attestation time, so anything provisioned under different parameters would fail to attest. This is the setup half of the suite; the suite itself follows. Signed-off-by: samuraiXwandering <[email protected]>
Adds a suite that attests an agent with the tpm_devid node attestor against swtpm running in its own container. The agent reaches the TPM over a unix socket on a shared volume. go-tpm dials the socket and speaks the emulator protocol, so the suite needs no TPM device and no extra container privileges. The test derives the expected agent ID from the DevID certificate that was provisioned, so it fails if the agent attests with any other credential rather than merely reporting that an agent appeared. The in-process simulator in test/tpmsimulator continues to back the unit tests. Fixes spiffe#6466 Signed-off-by: samuraiXwandering <[email protected]>
cd6641f to
48416e2
Compare
amartinezfayo
left a comment
There was a problem hiding this comment.
This is a great contribution, thank you @samuraixwandering!
| done | ||
| chmod 0666 "${SOCKET_DIR}/swtpm.sock" | ||
|
|
||
| wait "${swtpm_pid}" |
There was a problem hiding this comment.
I think the shell stays PID 1 here with no TERM handler, so the SIGTERM from docker compose down would be ignored and each teardown would sit out the 10 second kill timeout before the SIGKILL. Would it be worth trapping TERM to kill swtpm, or moving the socket-wait and chmod into a backgrounded subshell and exec'ing swtpm so the signal reaches it directly?
There was a problem hiding this comment.
Good catch, and confirmed. The shell was PID 1 with SigCgt showing only SIGINT and SIGCHLD, so SIGTERM was discarded and docker stop took the full 10 seconds and ended in a SIGKILL (exit 137). I took your second suggestion: the socket wait and chmod moved into a backgrounded subshell and swtpm is now exec'd, so it is PID 1 and catches SIGTERM itself. Same check after the change: SigCgt has SIGTERM, docker stop returns in 0s with exit 0, and the socket still comes out srw-rw-rw-. The suite's teardown went from about 11 seconds to 1.
| } | ||
| defer func() { | ||
| if err := tpm2.FlushContext(rwc, ekHandle); err != nil { | ||
| log.Printf("warning: cannot flush endorsement key handle: %v", err) |
There was a problem hiding this comment.
nit: the two flush defers treat failure differently: this one logs a warning while the one in provisionDevID promotes the error into the named return. A flush failure doesn't invalidate the material either way, so would it make sense to give both the same treatment?
There was a problem hiding this comment.
Agreed. Both now log, since a flush failure does not invalidate the material either way. That also let provisionDevID drop its named results, which only existed so the defer could write the error out. The change landed on the storage root key side, which is why this comment still points at unchanged lines.
| return fmt.Errorf("cannot provision DevID: %w", err) | ||
| } | ||
|
|
||
| for dir, files := range map[string]map[string][]byte{ |
There was a problem hiding this comment.
nit: I wonder if two direct writeFiles calls, one for agentDir and one for serverDir, would read a bit more directly than iterating a map of maps here.
There was a problem hiding this comment.
Changed to two direct writeFiles calls. It reads better and it also makes the order deterministic, which the map iteration was not.
| } | ||
| }() | ||
|
|
||
| devIDTemplate := client.AKTemplateRSA() |
There was a problem hiding this comment.
The plugin also supports ECC DevID keys, and the in-process simulator generates both flavors. RSA is a great start for the suite, but ECC coverage would be worth a follow-up maybe as a flag on this provisioner. What do you think?
There was a problem hiding this comment.
Agreed that it is worth covering. I would rather do it as a follow-up than grow this one: a -key-type flag alone would be untested code, so it wants a suite step that re-provisions and re-attests with ECC. One thing I want to check before committing to a shape is that ECC works over the swtpm transport, since the RSA path needed a spike to prove the emulator protocol worked at all.
While addressing these I also looked at what else the suite does not cover, in case you want any of it folded into the same follow-up. Three things: it never asserts the selectors the plugin emits (subject:cn, issuer:cn, ca:fingerprint), there is no negative case proving attestation is rejected when the DevID certificate is not signed by the configured CA, and the three password options (devid_password, owner_hierarchy_password, endorsement_hierarchy_password) are unexercised. Happy to add any of them here instead if you would prefer.
There was a problem hiding this comment.
Thanks for laying this out. I agree ECC is better as a follow-up for the reason you gave, so let's keep this PR as is. Of the three gaps, the selector assertions and the negative case are the ones worth doing, since the suite proves only the agent ID today, and the password options seem lower value. Feel free to open an issue to track them.
Exec swtpm from the entrypoint so it becomes PID 1 and receives SIGTERM directly. As a child of the shell it never saw the signal, because the kernel does not apply default signal actions to PID 1, so every teardown waited out the kill timeout before the container was killed. The socket wait and chmod move to a background subshell. Log both key flush failures rather than failing provisioning for one of them, since a flush failure does not invalidate the material either way. That also removes the named results, which only existed to carry the error out of the defer. Write the agent and server files with two direct calls instead of iterating a map of maps, which also makes the order deterministic. Build all four certificates the same way; only the DevID leaf was setting NotBefore. Signed-off-by: samuraiXwandering <[email protected]>
|
The three failures on the last run are not from this PR. The failing suite is It looks like the post-release window: So I do not think there is anything to change here. Is a version bump on Edit: Seems fine now, will be reworking. |
Summary
Adds an integration suite that attests an agent with the
tpm_devidnodeattestor against a software TPM, so the plugin is covered end to end and not
only by the whitebox unit tests against the in-process simulator.
Two commits: the provisioner under
test/integration/setup/tpmdevid, then thesuite itself under
test/integration/suites/tpm-devid.How it works
swtpm runs in its own container and exposes the TPM as a unix socket on a
shared volume. go-tpm dials that socket and speaks the emulator protocol, so
the suite needs no TPM device, no kernel module and no extra container
privileges.
The provisioner takes the key templates and the NV index from SPIRE rather than
restating them. The agent regenerates the endorsement key from
client.DefaultEKTemplateRSA()and loads the DevID key undertpmutil.SRKTemplateHighRSA()at attestation time, so credentials provisionedunder different parameters would not attest. Taking them from the plugin means
the suite follows if those ever change.
The test derives the expected agent ID from the DevID certificate it
provisioned, so it fails if the agent attests with some other credential rather
than merely reporting that an agent appeared.
The in-process simulator in
test/tpmsimulatoris untouched and still backs theunit tests. This is additive coverage, per the discussion on the issue.
Verification
main.devid_ca_pathat a CA that did not sign theagent's DevID certificate fails the suite with
nodeattestor(tpm_devid): unable to verify DevID signature: verification failed: x509: certificate signed by unknown authority, so the test isactually exercising attestation.
tpmutil.NewSessionloads the DevID key and regenerates the EK,GetEKCertreads the certificate back from NV, and bothSolveDevIDChallengeandCertifyDevIDKeysucceed.Fingerprint()for the same certificate.Notes
Suites are discovered automatically by
split.sh, so no workflow change isneeded. The suite runs on both the x64 and arm64 integration runners, so it
needs no
IGNORE_SUITESentry.Coverage for the external
spire-tpm-pluginis deliberately left out. It adds across-repo dependency to the integration CI and that plugin currently builds
against
spire-plugin-sdkv1.14.4 while SPIRE is on 1.15, so it is worth amaintainer decision. Happy to follow up with it separately.
Closes #6466