2019-10-15 19:14:25 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-10-31 13:59:24 +00:00
|
|
|
"encoding/json"
|
2019-10-15 19:14:25 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2019-10-25 14:51:18 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2019-10-31 13:59:24 +00:00
|
|
|
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
|
2019-10-15 19:14:25 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2019-12-19 16:41:55 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/pprof"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2019-10-25 14:51:18 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2019-10-31 13:59:24 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMonitor_Monitor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// start server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := nomad.TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-10-15 19:14:25 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-10-15 19:14:25 +00:00
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
req := cstructs.MonitorRequest{
|
|
|
|
LogLevel: "debug",
|
|
|
|
NodeID: c.NodeID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := c.StreamingRpcHandler("Agent.Monitor")
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// create pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
streamMsg := make(chan *cstructs.StreamErrWrapper)
|
|
|
|
|
|
|
|
go handler(p2)
|
|
|
|
|
|
|
|
// Start decoder
|
|
|
|
go func() {
|
|
|
|
decoder := codec.NewDecoder(p1, structs.MsgpackHandle)
|
|
|
|
for {
|
|
|
|
var msg cstructs.StreamErrWrapper
|
|
|
|
if err := decoder.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF || strings.Contains(err.Error(), "closed") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errCh <- fmt.Errorf("error decoding: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
streamMsg <- &msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send request
|
|
|
|
encoder := codec.NewEncoder(p1, structs.MsgpackHandle)
|
|
|
|
require.Nil(encoder.Encode(req))
|
|
|
|
|
2019-10-31 13:59:24 +00:00
|
|
|
timeout := time.After(5 * time.Second)
|
2019-10-15 19:14:25 +00:00
|
|
|
expected := "[DEBUG]"
|
|
|
|
received := ""
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout waiting for logs")
|
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatal(err)
|
|
|
|
case msg := <-streamMsg:
|
|
|
|
if msg.Error != nil {
|
|
|
|
t.Fatalf("Got error: %v", msg.Error.Error())
|
|
|
|
}
|
2019-10-31 13:59:24 +00:00
|
|
|
|
|
|
|
var frame sframer.StreamFrame
|
|
|
|
err := json.Unmarshal(msg.Payload, &frame)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
received += string(frame.Data)
|
2019-10-15 19:14:25 +00:00
|
|
|
if strings.Contains(received, expected) {
|
|
|
|
require.Nil(p2.Close())
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 14:51:18 +00:00
|
|
|
|
|
|
|
func TestMonitor_Monitor_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// start server
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-10-25 14:51:18 +00:00
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2019-10-25 18:25:19 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-10-25 18:25:19 +00:00
|
|
|
|
|
|
|
policyBad := mock.NodePolicy(acl.PolicyDeny)
|
2019-10-25 14:51:18 +00:00
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2019-10-25 18:25:19 +00:00
|
|
|
policyGood := mock.AgentPolicy(acl.PolicyRead)
|
2019-10-25 14:51:18 +00:00
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid", policyGood)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
|
|
|
ExpectedErr: structs.ErrPermissionDenied.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
|
|
|
ExpectedErr: "Unknown log level",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
ExpectedErr: "Unknown log level",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
req := &cstructs.MonitorRequest{
|
|
|
|
LogLevel: "unknown",
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: tc.Token,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-25 18:25:19 +00:00
|
|
|
handler, err := c.StreamingRpcHandler("Agent.Monitor")
|
2019-10-25 14:51:18 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// create pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
streamMsg := make(chan *cstructs.StreamErrWrapper)
|
|
|
|
|
|
|
|
go handler(p2)
|
|
|
|
|
|
|
|
// Start decoder
|
|
|
|
go func() {
|
|
|
|
decoder := codec.NewDecoder(p1, structs.MsgpackHandle)
|
|
|
|
for {
|
|
|
|
var msg cstructs.StreamErrWrapper
|
|
|
|
if err := decoder.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF || strings.Contains(err.Error(), "closed") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errCh <- fmt.Errorf("error decoding: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
streamMsg <- &msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send request
|
|
|
|
encoder := codec.NewEncoder(p1, structs.MsgpackHandle)
|
|
|
|
require.Nil(encoder.Encode(req))
|
|
|
|
|
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout")
|
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatal(err)
|
|
|
|
case msg := <-streamMsg:
|
|
|
|
if msg.Error == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(msg.Error.Error(), tc.ExpectedErr) {
|
|
|
|
break OUTER
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Bad error: %v", msg.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 21:52:13 +00:00
|
|
|
|
2019-12-13 18:41:55 +00:00
|
|
|
// Test that by default with no acl, endpoint is disabled
|
|
|
|
func TestAgentProfile_DefaultDisabled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// start server and client
|
|
|
|
s1, cleanup := nomad.TestServer(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
|
|
|
c.Servers = []string{s1.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
|
|
|
defer cleanupC()
|
|
|
|
|
|
|
|
req := structs.AgentPprofRequest{
|
2019-12-19 16:41:55 +00:00
|
|
|
ReqType: pprof.CPUReq,
|
2019-12-13 18:41:55 +00:00
|
|
|
NodeID: c.NodeID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := structs.AgentPprofResponse{}
|
|
|
|
|
|
|
|
err := c.ClientRPC("Agent.Profile", &req, &reply)
|
|
|
|
require.EqualError(err, structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:52:13 +00:00
|
|
|
func TestAgentProfile(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// start server and client
|
|
|
|
s1, cleanup := nomad.TestServer(t, nil)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
|
|
|
c.Servers = []string{s1.GetConfig().RPCAddr.String()}
|
2019-12-13 18:41:55 +00:00
|
|
|
c.EnableDebug = true
|
2019-12-12 21:52:13 +00:00
|
|
|
})
|
|
|
|
defer cleanupC()
|
|
|
|
|
|
|
|
// Successful request
|
|
|
|
{
|
|
|
|
req := structs.AgentPprofRequest{
|
2019-12-19 16:41:55 +00:00
|
|
|
ReqType: pprof.CPUReq,
|
2019-12-12 21:52:13 +00:00
|
|
|
NodeID: c.NodeID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := structs.AgentPprofResponse{}
|
|
|
|
|
|
|
|
err := c.ClientRPC("Agent.Profile", &req, &reply)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.NotNil(reply.Payload)
|
|
|
|
require.Equal(c.NodeID(), reply.AgentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown profile request
|
|
|
|
{
|
|
|
|
req := structs.AgentPprofRequest{
|
2019-12-19 16:41:55 +00:00
|
|
|
ReqType: pprof.LookupReq,
|
2019-12-12 21:52:13 +00:00
|
|
|
Profile: "unknown",
|
|
|
|
NodeID: c.NodeID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := structs.AgentPprofResponse{}
|
|
|
|
|
|
|
|
err := c.ClientRPC("Agent.Profile", &req, &reply)
|
|
|
|
require.EqualError(err, "RPC Error:: 404,Pprof profile not found profile: unknown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentProfile_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// start server
|
|
|
|
s, root, cleanupS := nomad.TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
|
|
|
c.ACLEnabled = true
|
|
|
|
c.Servers = []string{s.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
|
|
|
defer cleanupC()
|
|
|
|
|
|
|
|
policyBad := mock.AgentPolicy(acl.PolicyRead)
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
|
|
|
policyGood := mock.AgentPolicy(acl.PolicyWrite)
|
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid", policyGood)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
authErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
|
|
|
authErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
req := &structs.AgentPprofRequest{
|
2019-12-19 16:41:55 +00:00
|
|
|
ReqType: pprof.CmdReq,
|
2019-12-12 21:52:13 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: tc.Token,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := &structs.AgentPprofResponse{}
|
|
|
|
|
|
|
|
err := c.ClientRPC("Agent.Profile", req, reply)
|
|
|
|
if tc.authErr {
|
|
|
|
require.EqualError(err, structs.ErrPermissionDenied.Error())
|
|
|
|
} else {
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(reply.Payload)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|