2018-02-06 00:16:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-04-28 21:22:53 +00:00
|
|
|
"encoding/json"
|
2018-02-06 00:16:20 +00:00
|
|
|
"fmt"
|
2019-04-28 21:22:53 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"runtime"
|
2019-04-01 12:56:02 +00:00
|
|
|
"strings"
|
2018-02-06 00:16:20 +00:00
|
|
|
"testing"
|
2019-04-28 21:22:53 +00:00
|
|
|
"time"
|
2018-02-06 00:16:20 +00:00
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2019-04-28 21:22:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
2019-04-28 21:22:53 +00:00
|
|
|
nconfig "github.com/hashicorp/nomad/nomad/structs/config"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2022-06-07 14:18:19 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-28 21:22:53 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2018-02-06 00:16:20 +00:00
|
|
|
)
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
func TestAllocations_Restart(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
require := require.New(t)
|
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
a := mock.Alloc()
|
|
|
|
a.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver"
|
|
|
|
a.Job.TaskGroups[0].RestartPolicy = &nstructs.RestartPolicy{
|
|
|
|
Attempts: 0,
|
|
|
|
Mode: nstructs.RestartPolicyModeFail,
|
|
|
|
}
|
|
|
|
a.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
2020-05-31 14:33:03 +00:00
|
|
|
"run_for": "10s",
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
require.Nil(client.addAlloc(a, ""))
|
|
|
|
|
|
|
|
// Try with bad alloc
|
|
|
|
req := &nstructs.AllocRestartRequest{}
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp)
|
|
|
|
require.Error(err)
|
|
|
|
|
|
|
|
// Try with good alloc
|
|
|
|
req.AllocID = a.ID
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var resp2 nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp2)
|
|
|
|
if err != nil && strings.Contains(err.Error(), "not running") {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_Restart_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-01 12:56:02 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-01 12:56:02 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-01 12:56:02 +00:00
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
alloc := testutil.WaitForRunningWithToken(t, server.RPC, job, root.SecretID)[0]
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocRestartRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-01 12:56:02 +00:00
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{}))
|
|
|
|
req := &nstructs.AllocRestartRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-01 12:56:02 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
policyHCL := mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilityAllocLifecycle})
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1007, "valid", policyHCL)
|
|
|
|
require.NotNil(token)
|
|
|
|
req := &nstructs.AllocRestartRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-01 12:56:02 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
req.Namespace = nstructs.DefaultNamespace
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
|
|
|
//require.True(nstructs.IsErrUnknownAllocation(err), "Expected unknown alloc, found: %v", err)
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocRestartRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-01 12:56:02 +00:00
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Restart", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
// Depending on how quickly the alloc restarts there may be no
|
|
|
|
// error *or* a task not running error; either is fine.
|
|
|
|
if err != nil {
|
|
|
|
require.Contains(err.Error(), "Task not running", err)
|
|
|
|
}
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
func TestAllocations_GarbageCollectAll(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-02-06 00:16:20 +00:00
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
require.Nil(client.ClientRPC("Allocations.GarbageCollectAll", &req, &resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_GarbageCollectAll_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-02-06 00:16:20 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-02-06 00:16:20 +00:00
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollectAll", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollectAll", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1007, "valid", mock.NodePolicy(acl.PolicyWrite))
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
require.Nil(client.ClientRPC("Allocations.GarbageCollectAll", &req, &resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
require.Nil(client.ClientRPC("Allocations.GarbageCollectAll", &req, &resp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_GarbageCollect(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
client, cleanup := TestClient(t, func(c *config.Config) {
|
2018-02-14 00:31:17 +00:00
|
|
|
c.GCDiskUsageThreshold = 100.0
|
|
|
|
})
|
2018-10-16 03:07:16 +00:00
|
|
|
defer cleanup()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
|
|
|
a := mock.Alloc()
|
2018-02-14 00:31:17 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver"
|
2020-03-24 21:38:59 +00:00
|
|
|
rp := &nstructs.RestartPolicy{
|
2018-02-14 00:31:17 +00:00
|
|
|
Attempts: 0,
|
|
|
|
Mode: nstructs.RestartPolicyModeFail,
|
|
|
|
}
|
2020-03-24 21:38:59 +00:00
|
|
|
a.Job.TaskGroups[0].RestartPolicy = rp
|
|
|
|
a.Job.TaskGroups[0].Tasks[0].RestartPolicy = rp
|
2018-02-14 00:31:17 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "10ms",
|
|
|
|
}
|
2018-02-06 00:16:20 +00:00
|
|
|
require.Nil(client.addAlloc(a, ""))
|
|
|
|
|
|
|
|
// Try with bad alloc
|
|
|
|
req := &nstructs.AllocSpecificRequest{}
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
|
|
|
|
// Try with good alloc
|
|
|
|
req.AllocID = a.ID
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2018-02-06 18:53:00 +00:00
|
|
|
// Check if has been removed first
|
2018-02-14 00:31:17 +00:00
|
|
|
if ar, ok := client.allocs[a.ID]; !ok || ar.IsDestroyed() {
|
2018-02-06 18:53:00 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
var resp2 nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp2)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_GarbageCollect_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-02-06 00:16:20 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:34:12 +00:00
|
|
|
noSuchAllocErr := fmt.Errorf("No such allocation on client or allocation not eligible for GC")
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Wait for client to be running job
|
|
|
|
alloc := testutil.WaitForRunningWithToken(t, server.RPC, job, root.SecretID)[0]
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocSpecificRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
|
|
|
req := &nstructs.AllocSpecificRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "test-valid",
|
|
|
|
mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilitySubmitJob}))
|
|
|
|
req := &nstructs.AllocSpecificRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
req.Namespace = nstructs.DefaultNamespace
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp)
|
2021-01-04 16:34:12 +00:00
|
|
|
require.Error(err, noSuchAllocErr)
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocSpecificRequest{}
|
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.GarbageCollect", &req, &resp)
|
2021-01-04 16:34:12 +00:00
|
|
|
require.Error(err, noSuchAllocErr)
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
func TestAllocations_Signal(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-03 10:46:15 +00:00
|
|
|
|
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
a := mock.Alloc()
|
|
|
|
require.Nil(t, client.addAlloc(a, ""))
|
|
|
|
|
|
|
|
// Try with bad alloc
|
|
|
|
req := &nstructs.AllocSignalRequest{}
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Signal", &req, &resp)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.True(t, nstructs.IsErrUnknownAllocation(err))
|
|
|
|
|
|
|
|
// Try with good alloc
|
|
|
|
req.AllocID = a.ID
|
|
|
|
|
|
|
|
var resp2 nstructs.GenericResponse
|
|
|
|
err = client.ClientRPC("Allocations.Signal", &req, &resp2)
|
|
|
|
|
|
|
|
require.Error(t, err, "Expected error, got: %s, resp: %#+v", err, resp2)
|
2019-12-13 16:01:20 +00:00
|
|
|
require.Contains(t, err.Error(), "Failed to signal task: web, err: Task not running")
|
2019-04-03 10:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_Signal_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-03 10:46:15 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-03 10:46:15 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-03 10:46:15 +00:00
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
alloc := testutil.WaitForRunningWithToken(t, server.RPC, job, root.SecretID)[0]
|
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocSignalRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-03 10:46:15 +00:00
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Signal", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
|
|
|
req := &nstructs.AllocSignalRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-03 10:46:15 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Signal", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "test-valid",
|
|
|
|
mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilityAllocLifecycle}))
|
|
|
|
req := &nstructs.AllocSignalRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-03 10:46:15 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
req.Namespace = nstructs.DefaultNamespace
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Signal", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
2019-04-03 10:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
req := &nstructs.AllocSignalRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2019-04-03 10:46:15 +00:00
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
|
|
|
|
var resp nstructs.GenericResponse
|
|
|
|
err := client.ClientRPC("Allocations.Signal", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
2019-04-03 10:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
func TestAllocations_Stats(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
|
|
|
a := mock.Alloc()
|
|
|
|
require.Nil(client.addAlloc(a, ""))
|
|
|
|
|
|
|
|
// Try with bad alloc
|
|
|
|
req := &cstructs.AllocStatsRequest{}
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
|
|
|
|
// Try with good alloc
|
|
|
|
req.AllocID = a.ID
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var resp2 cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp2)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if resp2.Stats == nil {
|
|
|
|
return false, fmt.Errorf("invalid stats object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocations_Stats_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 00:16:20 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-02-06 00:16:20 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 00:16:20 +00:00
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
alloc := testutil.WaitForRunningWithToken(t, server.RPC, job, root.SecretID)[0]
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
req := &cstructs.AllocStatsRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
|
|
|
req := &cstructs.AllocStatsRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "test-valid",
|
|
|
|
mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob}))
|
|
|
|
req := &cstructs.AllocStatsRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
req.Namespace = nstructs.DefaultNamespace
|
|
|
|
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
|
|
|
req := &cstructs.AllocStatsRequest{}
|
2019-10-01 20:06:24 +00:00
|
|
|
req.AllocID = alloc.ID
|
2018-02-06 00:16:20 +00:00
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := client.ClientRPC("Allocations.Stats", &req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-28 21:22:53 +00:00
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
func TestAlloc_Checks(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
must.NoError(t, cleanup())
|
|
|
|
})
|
|
|
|
|
|
|
|
now := time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC).Unix()
|
|
|
|
|
|
|
|
qr1 := &nstructs.CheckQueryResult{
|
|
|
|
ID: "abc123",
|
|
|
|
Mode: "healthiness",
|
|
|
|
Status: "passing",
|
|
|
|
Output: "nomad: http ok",
|
|
|
|
Timestamp: now,
|
|
|
|
Group: "group",
|
|
|
|
Task: "task",
|
|
|
|
Service: "service",
|
|
|
|
Check: "check",
|
|
|
|
}
|
|
|
|
|
|
|
|
qr2 := &nstructs.CheckQueryResult{
|
|
|
|
ID: "def456",
|
|
|
|
Mode: "readiness",
|
|
|
|
Status: "passing",
|
|
|
|
Output: "nomad: http ok",
|
|
|
|
Timestamp: now,
|
|
|
|
Group: "group",
|
|
|
|
Service: "service2",
|
|
|
|
Check: "check",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("alloc does not exist", func(t *testing.T) {
|
|
|
|
request := cstructs.AllocChecksRequest{AllocID: "d3e34248-4843-be75-d4fd-4899975cfb38"}
|
|
|
|
var response cstructs.AllocChecksResponse
|
|
|
|
err := client.ClientRPC("Allocations.Checks", &request, &response)
|
|
|
|
must.EqError(t, err, `Unknown allocation "d3e34248-4843-be75-d4fd-4899975cfb38"`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no checks for alloc", func(t *testing.T) {
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
must.NoError(t, client.addAlloc(alloc, ""))
|
|
|
|
|
|
|
|
request := cstructs.AllocChecksRequest{AllocID: alloc.ID}
|
|
|
|
var response cstructs.AllocChecksResponse
|
|
|
|
err := client.ClientRPC("Allocations.Checks", &request, &response)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.MapEmpty(t, response.Results)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("two in one alloc", func(t *testing.T) {
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
must.NoError(t, client.addAlloc(alloc, ""))
|
|
|
|
must.NoError(t, client.checkStore.Set(alloc.ID, qr1))
|
|
|
|
must.NoError(t, client.checkStore.Set(alloc.ID, qr2))
|
|
|
|
|
|
|
|
request := cstructs.AllocChecksRequest{AllocID: alloc.ID}
|
|
|
|
var response cstructs.AllocChecksResponse
|
|
|
|
err := client.ClientRPC("Allocations.Checks", &request, &response)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.MapEq(t, map[nstructs.CheckID]*nstructs.CheckQueryResult{
|
|
|
|
"abc123": qr1,
|
|
|
|
"def456": qr2,
|
|
|
|
}, response.Results)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ignore unrelated alloc", func(t *testing.T) {
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
must.NoError(t, client.addAlloc(alloc1, ""))
|
|
|
|
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
must.NoError(t, client.addAlloc(alloc2, ""))
|
|
|
|
must.NoError(t, client.checkStore.Set(alloc1.ID, qr1))
|
|
|
|
must.NoError(t, client.checkStore.Set(alloc2.ID, qr2))
|
|
|
|
|
|
|
|
request := cstructs.AllocChecksRequest{AllocID: alloc1.ID}
|
|
|
|
var response cstructs.AllocChecksResponse
|
|
|
|
err := client.ClientRPC("Allocations.Checks", &request, &response)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.MapEq(t, map[nstructs.CheckID]*nstructs.CheckQueryResult{
|
|
|
|
"abc123": qr1,
|
|
|
|
}, response.Results)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-28 21:22:53 +00:00
|
|
|
func TestAlloc_ExecStreaming(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := nomad.TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-28 21:22:53 +00:00
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
expectedStdout := "Hello from the other side\n"
|
|
|
|
expectedStderr := "Hello from the other side\n"
|
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"stdout_string": expectedStdout,
|
|
|
|
"stderr_string": expectedStderr,
|
|
|
|
"exit_code": 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
testutil.WaitForRunning(t, s.RPC, job)
|
|
|
|
|
|
|
|
// Get the allocation ID
|
|
|
|
args := nstructs.AllocListRequest{}
|
|
|
|
args.Region = "global"
|
|
|
|
resp := nstructs.AllocListResponse{}
|
|
|
|
require.NoError(s.RPC("Alloc.List", &args, &resp))
|
|
|
|
require.Len(resp.Allocations, 1)
|
|
|
|
allocID := resp.Allocations[0].ID
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: allocID,
|
|
|
|
Task: job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := c.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(encoder.Encode(req))
|
|
|
|
|
|
|
|
timeout := time.After(3 * time.Second)
|
|
|
|
|
|
|
|
exitCode := -1
|
|
|
|
receivedStdout := ""
|
|
|
|
receivedStderr := ""
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
// time out report
|
|
|
|
require.Equal(expectedStdout, receivedStderr, "didn't receive expected stdout")
|
|
|
|
require.Equal(expectedStderr, receivedStderr, "didn't receive expected stderr")
|
|
|
|
require.Equal(3, exitCode, "failed to get exit code")
|
|
|
|
require.FailNow("timed out")
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(err)
|
|
|
|
case f := <-frames:
|
|
|
|
switch {
|
|
|
|
case f.Stdout != nil && len(f.Stdout.Data) != 0:
|
|
|
|
receivedStdout += string(f.Stdout.Data)
|
|
|
|
case f.Stderr != nil && len(f.Stderr.Data) != 0:
|
|
|
|
receivedStderr += string(f.Stderr.Data)
|
|
|
|
case f.Exited && f.Result != nil:
|
|
|
|
exitCode = int(f.Result.ExitCode)
|
|
|
|
default:
|
|
|
|
t.Logf("received unrelevant frame: %v", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedStdout == receivedStdout && expectedStderr == receivedStderr && exitCode == 3 {
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlloc_ExecStreaming_NoAllocation(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := nomad.TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-28 21:22:53 +00:00
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Task: "testtask",
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := c.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(encoder.Encode(req))
|
|
|
|
|
|
|
|
timeout := time.After(3 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
require.FailNow("timed out")
|
|
|
|
case err := <-errCh:
|
|
|
|
require.True(nstructs.IsErrUnknownAllocation(err), "expected no allocation error but found: %v", err)
|
|
|
|
case f := <-frames:
|
|
|
|
require.Fail("received unexpected frame", "frame: %#v", f)
|
2019-06-03 19:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlloc_ExecStreaming_DisableRemoteExec(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-06-03 19:31:39 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := nomad.TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-06-03 19:31:39 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-06-03 19:31:39 +00:00
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
c.DisableRemoteExec = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-06-03 19:31:39 +00:00
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
Task: "testtask",
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := c.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(encoder.Encode(req))
|
|
|
|
|
|
|
|
timeout := time.After(3 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
require.FailNow("timed out")
|
|
|
|
case err := <-errCh:
|
|
|
|
require.True(nstructs.IsErrPermissionDenied(err), "expected permission denied error but found: %v", err)
|
|
|
|
case f := <-frames:
|
|
|
|
require.Fail("received unexpected frame", "frame: %#v", f)
|
2019-04-28 21:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlloc_ExecStreaming_ACL_Basic(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-28 21:22:53 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityDeny})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyGood := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec, acl.NamespaceCapabilityReadFS})
|
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
alloc := testutil.WaitForRunningWithToken(t, s.RPC, job, root.SecretID)[0]
|
|
|
|
|
2019-04-28 21:22:53 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-05-13 18:30:31 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
2019-10-01 20:06:24 +00:00
|
|
|
ExpectedError: "task not found",
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
2019-10-01 20:06:24 +00:00
|
|
|
ExpectedError: "task not found",
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
2019-10-01 20:06:24 +00:00
|
|
|
AllocID: alloc.ID,
|
2019-04-28 21:22:53 +00:00
|
|
|
Task: "testtask",
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: c.Token,
|
|
|
|
Namespace: nstructs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := client.StreamingRpcHandler("Allocations.Exec")
|
2019-10-01 20:06:24 +00:00
|
|
|
require.Nil(t, err)
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.Nil(t, encoder.Encode(req))
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(3 * time.Second):
|
2019-10-01 20:06:24 +00:00
|
|
|
require.FailNow(t, "timed out")
|
2019-04-28 21:22:53 +00:00
|
|
|
case err := <-errCh:
|
2019-10-01 20:06:24 +00:00
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
2019-04-28 21:22:53 +00:00
|
|
|
case f := <-frames:
|
2019-10-01 20:06:24 +00:00
|
|
|
require.Fail(t, "received unexpected frame", "frame: %#v", f)
|
2019-04-28 21:22:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAlloc_ExecStreaming_ACL_WithIsolation_Image asserts that token only needs
|
|
|
|
// alloc-exec acl policy when image isolation is used
|
|
|
|
func TestAlloc_ExecStreaming_ACL_WithIsolation_Image(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
isolation := drivers.FSIsolationImage
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-04-28 21:22:53 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
|
|
|
|
pluginConfig := []*nconfig.PluginConfig{
|
|
|
|
{
|
|
|
|
Name: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"fs_isolation": string(isolation),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.PluginLoader = catalog.TestPluginLoaderWithOptions(t, "", map[string]string{}, pluginConfig)
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityDeny})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec})
|
|
|
|
tokenAllocExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyAllocExec)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocNodeExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec, acl.NamespaceCapabilityAllocNodeExec})
|
|
|
|
tokenAllocNodeExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyAllocNodeExec)
|
|
|
|
|
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"stdout_string": "some output",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
testutil.WaitForRunningWithToken(t, s.RPC, job, root.SecretID)
|
|
|
|
|
|
|
|
// Get the allocation ID
|
|
|
|
args := nstructs.AllocListRequest{}
|
|
|
|
args.Region = "global"
|
|
|
|
args.AuthToken = root.SecretID
|
|
|
|
args.Namespace = nstructs.DefaultNamespace
|
|
|
|
resp := nstructs.AllocListResponse{}
|
|
|
|
require.NoError(t, s.RPC("Alloc.List", &args, &resp))
|
|
|
|
require.Len(t, resp.Allocations, 1)
|
|
|
|
allocID := resp.Allocations[0].ID
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-05-13 18:30:31 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-exec token",
|
|
|
|
Token: tokenAllocExec.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-node-exec token",
|
|
|
|
Token: tokenAllocNodeExec.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: allocID,
|
|
|
|
Task: job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: c.Token,
|
|
|
|
Namespace: nstructs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := client.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(t, encoder.Encode(req))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
case err := <-errCh:
|
|
|
|
if c.ExpectedError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
|
|
|
}
|
|
|
|
case f := <-frames:
|
|
|
|
// we are good if we don't expect an error
|
|
|
|
if c.ExpectedError != "" {
|
|
|
|
require.Fail(t, "unexpected frame", "frame: %#v", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAlloc_ExecStreaming_ACL_WithIsolation_Chroot asserts that token only needs
|
|
|
|
// alloc-exec acl policy when chroot isolation is used
|
|
|
|
func TestAlloc_ExecStreaming_ACL_WithIsolation_Chroot(t *testing.T) {
|
client: enable support for cgroups v2
This PR introduces support for using Nomad on systems with cgroups v2 [1]
enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux
distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems
for Nomad users.
Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer,
but not so for managing cpuset cgroups. Before, Nomad has been making use of
a feature in v1 where a PID could be a member of more than one cgroup. In v2
this is no longer possible, and so the logic around computing cpuset values
must be modified. When Nomad detects v2, it manages cpuset values in-process,
rather than making use of cgroup heirarchy inheritence via shared/reserved
parents.
Nomad will only activate the v2 logic when it detects cgroups2 is mounted at
/sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2
mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to
use the v1 logic, and should operate as before. Systems that do not support
cgroups v2 are also not affected.
When v2 is activated, Nomad will create a parent called nomad.slice (unless
otherwise configured in Client conifg), and create cgroups for tasks using
naming convention <allocID>-<task>.scope. These follow the naming convention
set by systemd and also used by Docker when cgroups v2 is detected.
Client nodes now export a new fingerprint attribute, unique.cgroups.version
which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by
Nomad.
The new cpuset management strategy fixes #11705, where docker tasks that
spawned processes on startup would "leak". In cgroups v2, the PIDs are
started in the cgroup they will always live in, and thus the cause of
the leak is eliminated.
[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
Closes #11289
Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
|
|
|
ci.SkipSlow(t, "flaky on GHA; too much disk IO")
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != "linux" || unix.Geteuid() != 0 {
|
|
|
|
t.Skip("chroot isolation requires linux root")
|
|
|
|
}
|
|
|
|
|
|
|
|
isolation := drivers.FSIsolationChroot
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
client, cleanup := TestClient(t, func(c *config.Config) {
|
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
|
|
|
|
pluginConfig := []*nconfig.PluginConfig{
|
|
|
|
{
|
|
|
|
Name: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"fs_isolation": string(isolation),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.PluginLoader = catalog.TestPluginLoaderWithOptions(t, "", map[string]string{}, pluginConfig)
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityDeny})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec})
|
|
|
|
tokenAllocExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "alloc-exec", policyAllocExec)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocNodeExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec, acl.NamespaceCapabilityAllocNodeExec})
|
|
|
|
tokenAllocNodeExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "alloc-node-exec", policyAllocNodeExec)
|
|
|
|
|
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"stdout_string": "some output",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
testutil.WaitForRunningWithToken(t, s.RPC, job, root.SecretID)
|
|
|
|
|
|
|
|
// Get the allocation ID
|
|
|
|
args := nstructs.AllocListRequest{}
|
|
|
|
args.Region = "global"
|
|
|
|
args.AuthToken = root.SecretID
|
|
|
|
args.Namespace = nstructs.DefaultNamespace
|
|
|
|
resp := nstructs.AllocListResponse{}
|
|
|
|
require.NoError(t, s.RPC("Alloc.List", &args, &resp))
|
|
|
|
require.Len(t, resp.Allocations, 1)
|
|
|
|
allocID := resp.Allocations[0].ID
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-05-13 18:30:31 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-exec token",
|
|
|
|
Token: tokenAllocExec.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-node-exec token",
|
|
|
|
Token: tokenAllocNodeExec.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: allocID,
|
|
|
|
Task: job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: c.Token,
|
|
|
|
Namespace: nstructs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := client.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(t, encoder.Encode(req))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
case err := <-errCh:
|
|
|
|
if c.ExpectedError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
|
|
|
}
|
|
|
|
case f := <-frames:
|
|
|
|
// we are good if we don't expect an error
|
|
|
|
if c.ExpectedError != "" {
|
|
|
|
require.Fail(t, "unexpected frame", "frame: %#v", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAlloc_ExecStreaming_ACL_WithIsolation_None asserts that token needs
|
|
|
|
// alloc-node-exec acl policy as well when no isolation is used
|
|
|
|
func TestAlloc_ExecStreaming_ACL_WithIsolation_None(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:22:53 +00:00
|
|
|
isolation := drivers.FSIsolationNone
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-28 21:22:53 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
client, cleanup := TestClient(t, func(c *config.Config) {
|
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
|
|
|
|
pluginConfig := []*nconfig.PluginConfig{
|
|
|
|
{
|
|
|
|
Name: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"fs_isolation": string(isolation),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.PluginLoader = catalog.TestPluginLoaderWithOptions(t, "", map[string]string{}, pluginConfig)
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityDeny})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec})
|
|
|
|
tokenAllocExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "alloc-exec", policyAllocExec)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyAllocNodeExec := mock.NamespacePolicy(nstructs.DefaultNamespace, "",
|
2019-04-28 21:22:53 +00:00
|
|
|
[]string{acl.NamespaceCapabilityAllocExec, acl.NamespaceCapabilityAllocNodeExec})
|
|
|
|
tokenAllocNodeExec := mock.CreatePolicyAndToken(t, s.State(), 1009, "alloc-node-exec", policyAllocNodeExec)
|
|
|
|
|
|
|
|
job := mock.BatchJob()
|
|
|
|
job.TaskGroups[0].Count = 1
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"stdout_string": "some output",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for client to be running job
|
|
|
|
testutil.WaitForRunningWithToken(t, s.RPC, job, root.SecretID)
|
|
|
|
|
|
|
|
// Get the allocation ID
|
|
|
|
args := nstructs.AllocListRequest{}
|
|
|
|
args.Region = "global"
|
|
|
|
args.AuthToken = root.SecretID
|
|
|
|
args.Namespace = nstructs.DefaultNamespace
|
|
|
|
resp := nstructs.AllocListResponse{}
|
|
|
|
require.NoError(t, s.RPC("Alloc.List", &args, &resp))
|
|
|
|
require.Len(t, resp.Allocations, 1)
|
|
|
|
allocID := resp.Allocations[0].ID
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-05-13 18:30:31 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-exec token",
|
|
|
|
Token: tokenAllocExec.SecretID,
|
2021-05-13 18:30:31 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-28 21:22:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "alloc-node-exec token",
|
|
|
|
Token: tokenAllocNodeExec.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
ExpectedError: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: allocID,
|
|
|
|
Task: job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: c.Token,
|
|
|
|
Namespace: nstructs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := client.StreamingRpcHandler("Allocations.Exec")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(t, encoder.Encode(req))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
case err := <-errCh:
|
|
|
|
if c.ExpectedError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
|
|
|
}
|
|
|
|
case f := <-frames:
|
|
|
|
// we are good if we don't expect an error
|
|
|
|
if c.ExpectedError != "" {
|
|
|
|
require.Fail(t, "unexpected frame", "frame: %#v", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeFrames(t *testing.T, p1 net.Conn, frames chan<- *drivers.ExecTaskStreamingResponseMsg, errCh chan<- error) {
|
|
|
|
// Start the decoder
|
|
|
|
decoder := codec.NewDecoder(p1, nstructs.MsgpackHandle)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var msg cstructs.StreamErrWrapper
|
|
|
|
if err := decoder.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF || strings.Contains(err.Error(), "closed") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("received error decoding: %#v", err)
|
|
|
|
|
|
|
|
errCh <- fmt.Errorf("error decoding: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Error != nil {
|
|
|
|
errCh <- msg.Error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var frame drivers.ExecTaskStreamingResponseMsg
|
|
|
|
if err := json.Unmarshal(msg.Payload, &frame); err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("received message: %#v", msg)
|
|
|
|
frames <- &frame
|
|
|
|
}
|
|
|
|
}
|