mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
* 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
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package artifact
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
"github.com/hashicorp/nomad/e2e/e2eutil"
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
"github.com/shoenig/test/must"
|
|
)
|
|
|
|
func TestArtifact(t *testing.T) {
|
|
nomad := e2eutil.NomadClient(t)
|
|
|
|
e2eutil.WaitForLeader(t, nomad)
|
|
e2eutil.WaitForNodesReady(t, nomad, 1)
|
|
|
|
t.Run("testLinux", testLinux)
|
|
t.Run("testWindows", testWindows)
|
|
}
|
|
|
|
// artifactCheckLogContents verifies expected logs for artifact downloader tests.
|
|
//
|
|
// each task is designed to download the artifact in some way then cat the go.mod
|
|
// file, so we just need to read the logs
|
|
//
|
|
// note: git requires the use of destination (hence no default form)
|
|
func artifactCheckLogContents(t *testing.T, nomad *api.Client, group, task string, allocations []map[string]string) {
|
|
var allocID string
|
|
for _, alloc := range allocations {
|
|
if alloc["Task Group"] == group {
|
|
allocID = alloc["ID"]
|
|
break
|
|
}
|
|
}
|
|
e2eutil.WaitForAllocStopped(t, nomad, allocID)
|
|
t.Run(task, func(t *testing.T) {
|
|
logs, err := e2eutil.AllocTaskLogs(allocID, task, e2eutil.LogsStdOut)
|
|
must.NoError(t, err)
|
|
must.StrContains(t, logs, "module github.com/hashicorp/go-set")
|
|
})
|
|
}
|
|
|
|
func testWindows(t *testing.T) {
|
|
nomad := e2eutil.NomadClient(t)
|
|
|
|
jobID := "artifact-linux-" + uuid.Short()
|
|
jobIDs := []string{jobID}
|
|
t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
|
|
|
|
// start job
|
|
e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_windows.nomad", jobID, "")
|
|
|
|
// get allocations
|
|
allocations, err := e2eutil.AllocsForJob(jobID, "")
|
|
must.NoError(t, err)
|
|
must.Len(t, 1, allocations)
|
|
|
|
// assert log contents for each task
|
|
check := func(group, task string) {
|
|
artifactCheckLogContents(t, nomad, group, task, allocations)
|
|
}
|
|
|
|
check("rawexec", "rawexec_file_default")
|
|
check("rawexec", "rawexec_file_custom")
|
|
check("rawexec", "rawexec_zip_default")
|
|
check("rawexec", "rawexec_zip_custom")
|
|
check("rawexec", "rawexec_git_custom")
|
|
}
|
|
|
|
func testLinux(t *testing.T) {
|
|
nomad := e2eutil.NomadClient(t)
|
|
|
|
jobID := "artifact-linux-" + uuid.Short()
|
|
jobIDs := []string{jobID}
|
|
t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
|
|
|
|
// start job
|
|
e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_linux.nomad", jobID, "")
|
|
|
|
// get allocations
|
|
allocations, err := e2eutil.AllocsForJob(jobID, "")
|
|
must.NoError(t, err)
|
|
must.Len(t, 3, allocations)
|
|
|
|
// assert log contents for each task
|
|
check := func(group, task string) {
|
|
artifactCheckLogContents(t, nomad, group, task, allocations)
|
|
}
|
|
|
|
check("rawexec", "rawexec_file_default")
|
|
check("rawexec", "rawexec_file_custom")
|
|
check("rawexec", "rawexec_zip_default")
|
|
check("rawexec", "rawexec_zip_custom")
|
|
check("rawexec", "rawexec_git_custom")
|
|
|
|
check("exec", "exec_file_default")
|
|
check("exec", "exec_file_custom")
|
|
check("exec", "exec_zip_default")
|
|
check("exec", "exec_zip_custom")
|
|
check("exec", "exec_git_custom")
|
|
|
|
check("docker", "docker_file_default")
|
|
check("docker", "docker_file_custom")
|
|
check("docker", "docker_zip_default")
|
|
check("docker", "docker_zip_custom")
|
|
check("docker", "docker_git_custom")
|
|
}
|