diff --git a/platform/local/tpm.go b/platform/local/tpm.go index 1924dc9ba..7743739f6 100644 --- a/platform/local/tpm.go +++ b/platform/local/tpm.go @@ -13,23 +13,16 @@ import ( type SoftwareTPM struct { process *exec.ExecCmd dirFromKolaCwd string - dirFromTestDir string } func NewSwtpm(testDir string, tpmDir string) (*SoftwareTPM, error) { - dirFromKolaCwd := filepath.Join(testDir, tpmDir) - swtpm := &SoftwareTPM{dirFromKolaCwd: dirFromKolaCwd, dirFromTestDir: tpmDir} - - if err := os.Mkdir(swtpm.dirFromKolaCwd, 0700); err != nil { - return nil, fmt.Errorf("Failed to create TPM dir: %v", err) + dirFromKolaCwd, err := os.MkdirTemp("", "mantle-tpm-") + if err != nil { + return nil, fmt.Errorf("Failed to create TPM temp dir: %v", err) } + swtpm := &SoftwareTPM{dirFromKolaCwd: dirFromKolaCwd} - swtpm.process = exec.Command("swtpm", "socket", "--tpmstate", fmt.Sprintf("dir=./%v", swtpm.dirFromTestDir), "--ctrl", fmt.Sprintf("type=unixio,path=./%v", swtpm.SocketRelativePathFromTestDir()), "--tpm2") - // Use the test directory as current working directory - // so that we don't have a socket path argument that - // exceeds 108 chars which is the limit for UNIX sockets - // (Using ./ as prefix helps to know that these are relative - // path arguments). + swtpm.process = exec.Command("swtpm", "socket", "--tpmstate", fmt.Sprintf("dir=%v", swtpm.dirFromKolaCwd), "--ctrl", fmt.Sprintf("type=unixio,path=%v", swtpm.SocketPath()), "--tpm2") swtpm.process.Dir = testDir plog.Debugf("Prepared swtpm process %q with CWD %q", swtpm.process, swtpm.process.Dir) out, err := swtpm.process.StderrPipe() @@ -55,7 +48,6 @@ func (swtpm *SoftwareTPM) Stop() { os.RemoveAll(swtpm.dirFromKolaCwd) } -func (swtpm *SoftwareTPM) SocketRelativePathFromTestDir() string { - const socket string = "socket" - return filepath.Join(swtpm.dirFromTestDir, socket) +func (swtpm *SoftwareTPM) SocketPath() string { + return filepath.Join(swtpm.dirFromKolaCwd, "socket") } diff --git a/platform/machine/qemu/cluster.go b/platform/machine/qemu/cluster.go index 26c1bd366..606787eb4 100644 --- a/platform/machine/qemu/cluster.go +++ b/platform/machine/qemu/cluster.go @@ -124,7 +124,7 @@ ExecStartPost=/usr/bin/ln -fs /run/metadata/flatcar /run/metadata/coreos if err != nil { return nil, fmt.Errorf("starting swtpm: %v", err) } - options.SoftwareTPMSocket = swtpm.SocketRelativePathFromTestDir() + options.SoftwareTPMSocket = swtpm.SocketPath() defer func() { if swtpm != nil { swtpm.Stop() diff --git a/platform/machine/unprivqemu/cluster.go b/platform/machine/unprivqemu/cluster.go index dc5ebfcbe..0579f4f5a 100644 --- a/platform/machine/unprivqemu/cluster.go +++ b/platform/machine/unprivqemu/cluster.go @@ -137,7 +137,7 @@ LinkLocalAddressing=no if err != nil { return nil, fmt.Errorf("starting swtpm: %v", err) } - options.SoftwareTPMSocket = swtpm.SocketRelativePathFromTestDir() + options.SoftwareTPMSocket = swtpm.SocketPath() defer func() { if swtpm != nil { swtpm.Stop()