open-nomad/client/config/testing.go
Seth Hoenig 51a2212d3d
client: sandbox go-getter subprocess with landlock (#15328)
* client: sandbox go-getter subprocess with landlock

This PR re-implements the getter package for artifact downloads as a subprocess.

Key changes include

On all platforms, run getter as a child process of the Nomad agent.
On Linux platforms running as root, run the child process as the nobody user.
On supporting Linux kernels, uses landlock for filesystem isolation (via go-landlock).
On all platforms, restrict environment variables of the child process to a static set.
notably TMP/TEMP now points within the allocation's task directory
kernel.landlock attribute is fingerprinted (version number or unavailable)
These changes make Nomad client more resilient against a faulty go-getter implementation that may panic, and more secure against bad actors attempting to use artifact downloads as a privilege escalation vector.

Adds new e2e/artifact suite for ensuring artifact downloading works.

TODO: Windows git test (need to modify the image, etc... followup PR)

* landlock: fixup items from cr

* cr: fixup tests and go.mod file
2022-12-07 16:02:25 -06:00

79 lines
2.1 KiB
Go

package config
import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
testing "github.com/mitchellh/go-testing-interface"
)
// TestClientConfig returns a default client configuration for test clients and
// a cleanup func to remove the state and alloc dirs when finished.
func TestClientConfig(t testing.T) (*Config, func()) {
conf := DefaultConfig()
conf.Node = mock.Node()
conf.Logger = testlog.HCLogger(t)
// On macOS, os.TempDir returns a symlinked path under /var which
// is outside of the directories shared into the VM used for Docker.
// Expand the symlink to get the real path in /private, which is ok.
dirName := os.TempDir()
tmpDir, err := filepath.EvalSymlinks(dirName)
if err != nil {
t.Fatalf("Could not resolve temporary directory links for %s: %v", tmpDir, err)
}
tmpDir = filepath.Clean(tmpDir)
// Create a tempdir to hold state and alloc subdirs
parent, err := ioutil.TempDir(tmpDir, "nomadtest")
if err != nil {
t.Fatalf("error creating client dir: %v", err)
}
cleanup := func() {
os.RemoveAll(parent)
}
// Fixup nomadtest dir permissions
if err = os.Chmod(parent, 0777); err != nil {
t.Fatalf("error updating permissions on nomadtest dir")
}
allocDir := filepath.Join(parent, "allocs")
if err := os.Mkdir(allocDir, 0777); err != nil {
cleanup()
t.Fatalf("error creating alloc dir: %v", err)
}
conf.AllocDir = allocDir
stateDir := filepath.Join(parent, "client")
if err := os.Mkdir(stateDir, 0777); err != nil {
cleanup()
t.Fatalf("error creating alloc dir: %v", err)
}
conf.StateDir = stateDir
// Use a minimal chroot environment
conf.ChrootEnv = ci.TinyChroot
// Helps make sure we are respecting configured parent
conf.CgroupParent = "testing.slice"
conf.VaultConfig.Enabled = pointer.Of(false)
conf.DevMode = true
// Loosen GC threshold
conf.GCDiskUsageThreshold = 98.0
conf.GCInodeUsageThreshold = 98.0
// Same as default; necessary for task Event messages
conf.MaxKillTimeout = 30 * time.Second
return conf, cleanup
}