2015-09-07 02:08:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-10-17 17:48:04 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-03-24 04:08:25 +00:00
|
|
|
"errors"
|
2015-09-07 02:08:05 +00:00
|
|
|
"fmt"
|
2019-10-15 14:33:07 +00:00
|
|
|
"io/ioutil"
|
2018-01-26 19:12:09 +00:00
|
|
|
"net"
|
2015-09-07 02:08:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-01-26 01:56:47 +00:00
|
|
|
"net/url"
|
2020-03-24 04:08:25 +00:00
|
|
|
"os"
|
2019-10-07 20:41:52 +00:00
|
|
|
"strings"
|
2020-03-24 04:08:25 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2015-09-07 02:08:05 +00:00
|
|
|
"testing"
|
2018-01-26 19:12:09 +00:00
|
|
|
"time"
|
2016-10-17 17:48:04 +00:00
|
|
|
|
2018-01-26 19:12:09 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2017-10-06 19:02:53 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2019-12-10 16:13:22 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-01-26 19:12:09 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pool"
|
2017-10-06 19:02:53 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2016-10-17 17:48:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-01-26 01:56:47 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2020-02-03 15:27:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-26 01:56:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-07 02:08:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_AgentSelf(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2019-09-22 12:35:53 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-07 02:08:05 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/self", nil)
|
2019-09-22 12:35:53 +00:00
|
|
|
require.NoError(err)
|
2015-09-07 02:08:05 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentSelfRequest(respW, req)
|
2019-09-22 12:35:53 +00:00
|
|
|
require.NoError(err)
|
2015-09-07 02:08:05 +00:00
|
|
|
|
|
|
|
// Check the job
|
|
|
|
self := obj.(agentSelf)
|
2019-09-22 12:35:53 +00:00
|
|
|
require.NotNil(self.Config)
|
|
|
|
require.NotNil(self.Config.ACL)
|
|
|
|
require.NotEmpty(self.Stats)
|
2017-08-08 04:33:18 +00:00
|
|
|
|
|
|
|
// Check the Vault config
|
2019-09-22 12:35:53 +00:00
|
|
|
require.Empty(self.Config.Vault.Token)
|
2017-08-08 04:33:18 +00:00
|
|
|
|
2018-01-26 01:56:47 +00:00
|
|
|
// Assign a Vault token and require it is redacted.
|
2017-08-08 04:33:18 +00:00
|
|
|
s.Config.Vault.Token = "badc0deb-adc0-deba-dc0d-ebadc0debadc"
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.AgentSelfRequest(respW, req)
|
2019-09-22 12:35:53 +00:00
|
|
|
require.NoError(err)
|
2017-08-08 04:33:18 +00:00
|
|
|
self = obj.(agentSelf)
|
2019-09-22 12:35:53 +00:00
|
|
|
require.Equal("<redacted>", self.Config.Vault.Token)
|
|
|
|
|
|
|
|
// Assign a ReplicationToken token and require it is redacted.
|
|
|
|
s.Config.ACL.ReplicationToken = "badc0deb-adc0-deba-dc0d-ebadc0debadc"
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.AgentSelfRequest(respW, req)
|
|
|
|
require.NoError(err)
|
|
|
|
self = obj.(agentSelf)
|
|
|
|
require.Equal("<redacted>", self.Config.ACL.ReplicationToken)
|
2019-09-23 17:07:27 +00:00
|
|
|
|
|
|
|
// Check the Consul config
|
|
|
|
require.Empty(self.Config.Consul.Token)
|
|
|
|
|
|
|
|
// Assign a Consul token and require it is redacted.
|
|
|
|
s.Config.Consul.Token = "badc0deb-adc0-deba-dc0d-ebadc0debadc"
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.AgentSelfRequest(respW, req)
|
|
|
|
require.NoError(err)
|
|
|
|
self = obj.(agentSelf)
|
|
|
|
require.Equal("<redacted>", self.Config.Consul.Token)
|
|
|
|
|
|
|
|
// Check the Circonus config
|
|
|
|
require.Empty(self.Config.Telemetry.CirconusAPIToken)
|
|
|
|
|
|
|
|
// Assign a Consul token and require it is redacted.
|
|
|
|
s.Config.Telemetry.CirconusAPIToken = "badc0deb-adc0-deba-dc0d-ebadc0debadc"
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
obj, err = s.Server.AgentSelfRequest(respW, req)
|
|
|
|
require.NoError(err)
|
|
|
|
self = obj.(agentSelf)
|
|
|
|
require.Equal("<redacted>", self.Config.Telemetry.CirconusAPIToken)
|
2015-09-07 02:08:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
func TestHTTP_AgentSelf_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/self", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.AgentSelfRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentSelfRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
obj, err := s.Server.AgentSelfRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
self := obj.(agentSelf)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(self.Config)
|
|
|
|
require.NotNil(self.Stats)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
obj, err := s.Server.AgentSelfRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
self := obj.(agentSelf)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(self.Config)
|
|
|
|
require.NotNil(self.Stats)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
func TestHTTP_AgentJoin(t *testing.T) {
|
2018-01-26 01:56:47 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-07 02:08:05 +00:00
|
|
|
// Determine the join address
|
|
|
|
member := s.Agent.Server().LocalMember()
|
|
|
|
addr := fmt.Sprintf("%s:%d", member.Addr, member.Port)
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("PUT",
|
|
|
|
fmt.Sprintf("/v1/agent/join?address=%s&address=%s", addr, addr), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentJoinRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
|
|
|
join := obj.(joinResult)
|
|
|
|
if join.NumJoined != 2 {
|
|
|
|
t.Fatalf("bad: %#v", join)
|
|
|
|
}
|
|
|
|
if join.Error != "" {
|
|
|
|
t.Fatalf("bad: %#v", join)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentMembers(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-07 02:08:05 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/members", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
obj, err := s.Server.AgentMembersRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the job
|
2016-11-08 21:01:56 +00:00
|
|
|
members := obj.(structs.ServerMembersResponse)
|
|
|
|
if len(members.Members) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", members.Members)
|
2015-09-07 02:08:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
func TestHTTP_AgentMembers_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/members", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.AgentMembersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentMembersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
obj, err := s.Server.AgentMembersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
members := obj.(structs.ServerMembersResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(members.Members, 1)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
obj, err := s.Server.AgentMembersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
members := obj.(structs.ServerMembersResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(members.Members, 1)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
func TestHTTP_AgentMonitor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("invalid log_json parameter", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2019-10-30 13:55:41 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_json=no", nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
httpErr := err.(HTTPCodedError).Code()
|
|
|
|
require.Equal(t, 400, httpErr)
|
|
|
|
})
|
|
|
|
})
|
2019-10-30 13:55:41 +00:00
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("unknown log_level", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2019-10-30 13:36:39 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_level=unknown", nil)
|
2019-10-07 20:41:52 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
httpErr := err.(HTTPCodedError).Code()
|
|
|
|
require.Equal(t, 400, httpErr)
|
|
|
|
})
|
|
|
|
})
|
2019-10-07 20:41:52 +00:00
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("check for specific log level", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2019-10-30 13:36:39 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_level=warn", nil)
|
2019-10-07 20:41:52 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-07 20:41:52 +00:00
|
|
|
}()
|
|
|
|
|
2019-10-25 14:58:54 +00:00
|
|
|
// send the same log until monitor sink is set up
|
2019-10-10 19:30:37 +00:00
|
|
|
maxLogAttempts := 10
|
|
|
|
tried := 0
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if tried < maxLogAttempts {
|
|
|
|
s.Server.logger.Warn("log that should be sent")
|
|
|
|
tried++
|
|
|
|
}
|
|
|
|
|
|
|
|
got := resp.Body.String()
|
2019-11-01 14:33:28 +00:00
|
|
|
want := `{"Data":"`
|
2019-10-10 19:30:37 +00:00
|
|
|
if strings.Contains(got, want) {
|
2019-11-05 15:34:02 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("missing expected log, got: %v, want: %v", got, want)
|
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
2020-02-03 15:27:29 +00:00
|
|
|
})
|
|
|
|
})
|
2019-11-05 15:34:02 +00:00
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("plain output", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2019-11-05 15:34:02 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_level=debug&plain=true", nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-05 15:34:02 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// send the same log until monitor sink is set up
|
|
|
|
maxLogAttempts := 10
|
|
|
|
tried := 0
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if tried < maxLogAttempts {
|
|
|
|
s.Server.logger.Debug("log that should be sent")
|
|
|
|
tried++
|
|
|
|
}
|
|
|
|
|
|
|
|
got := resp.Body.String()
|
|
|
|
want := `[DEBUG] http: log that should be sent`
|
|
|
|
if strings.Contains(got, want) {
|
2019-10-10 19:30:37 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("missing expected log, got: %v, want: %v", got, want)
|
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
2020-02-03 15:27:29 +00:00
|
|
|
})
|
|
|
|
})
|
2019-10-10 19:30:37 +00:00
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("logs for a specific node", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2019-10-30 13:36:39 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_level=warn&node_id="+s.client.NodeID(), nil)
|
2019-10-10 19:30:37 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
assert.NoError(t, err)
|
2019-10-10 19:30:37 +00:00
|
|
|
}()
|
|
|
|
|
2019-10-25 14:58:54 +00:00
|
|
|
// send the same log until monitor sink is set up
|
2019-10-09 15:17:29 +00:00
|
|
|
maxLogAttempts := 10
|
|
|
|
tried := 0
|
2019-10-15 14:33:07 +00:00
|
|
|
out := ""
|
2019-10-07 20:41:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2019-10-09 15:17:29 +00:00
|
|
|
if tried < maxLogAttempts {
|
|
|
|
s.Server.logger.Debug("log that should not be sent")
|
|
|
|
s.Server.logger.Warn("log that should be sent")
|
|
|
|
tried++
|
|
|
|
}
|
2019-10-15 14:33:07 +00:00
|
|
|
output, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-10-09 15:17:29 +00:00
|
|
|
|
2019-10-15 14:33:07 +00:00
|
|
|
out += string(output)
|
2019-11-01 14:33:28 +00:00
|
|
|
want := `{"Data":"`
|
2019-10-15 14:33:07 +00:00
|
|
|
if strings.Contains(out, want) {
|
2019-10-07 20:41:52 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2019-10-09 15:17:29 +00:00
|
|
|
|
2019-10-15 14:33:07 +00:00
|
|
|
return false, fmt.Errorf("missing expected log, got: %v, want: %v", out, want)
|
2019-10-07 20:41:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
2020-02-03 15:27:29 +00:00
|
|
|
})
|
|
|
|
})
|
2020-02-03 15:17:59 +00:00
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
t.Run("logs for a local client with no server running on agent", func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2020-02-03 15:17:59 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/monitor?log_level=warn", nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
resp := newClosableRecorder()
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// set server to nil to monitor as client
|
|
|
|
s.Agent.server = nil
|
|
|
|
_, err = s.Server.AgentMonitor(resp, req)
|
2020-02-03 15:27:29 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-03 15:17:59 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// send the same log until monitor sink is set up
|
|
|
|
maxLogAttempts := 10
|
|
|
|
tried := 0
|
|
|
|
out := ""
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if tried < maxLogAttempts {
|
|
|
|
s.Agent.logger.Warn("log that should be sent")
|
|
|
|
tried++
|
|
|
|
}
|
|
|
|
output, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out += string(output)
|
|
|
|
want := `{"Data":"`
|
|
|
|
if strings.Contains(out, want) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("missing expected log, got: %v, want: %v", out, want)
|
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
2020-02-03 15:27:29 +00:00
|
|
|
})
|
2019-10-07 20:41:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-16 18:42:18 +00:00
|
|
|
// Scenarios when Pprof requests should be available
|
2019-12-10 16:13:22 +00:00
|
|
|
// see https://github.com/hashicorp/nomad/issues/6496
|
|
|
|
// +---------------+------------------+--------+------------------+
|
|
|
|
// | Endpoint | `enable_debug` | ACLs | **Available?** |
|
|
|
|
// +---------------+------------------+--------+------------------+
|
|
|
|
// | /debug/pprof | unset | n/a | no |
|
|
|
|
// | /debug/pprof | `true` | n/a | yes |
|
|
|
|
// | /debug/pprof | `false` | n/a | no |
|
|
|
|
// | /agent/pprof | unset | off | no |
|
|
|
|
// | /agent/pprof | unset | on | **yes** |
|
|
|
|
// | /agent/pprof | `true` | off | yes |
|
2019-12-19 18:17:42 +00:00
|
|
|
// | /agent/pprof | `false` | on | **yes** |
|
2019-12-10 16:13:22 +00:00
|
|
|
// +---------------+------------------+--------+------------------+
|
|
|
|
func TestAgent_PprofRequest_Permissions(t *testing.T) {
|
2019-12-19 18:17:42 +00:00
|
|
|
trueP, falseP := helper.BoolToPtr(true), helper.BoolToPtr(false)
|
2019-12-10 16:13:22 +00:00
|
|
|
cases := []struct {
|
2019-12-19 18:17:42 +00:00
|
|
|
acl *bool
|
|
|
|
debug *bool
|
|
|
|
ok bool
|
2019-12-10 16:13:22 +00:00
|
|
|
}{
|
2019-12-19 18:17:42 +00:00
|
|
|
// manually set to false because test helpers
|
|
|
|
// enable to true by default
|
|
|
|
// enableDebug: helper.BoolToPtr(false),
|
|
|
|
{debug: nil, ok: false},
|
|
|
|
{debug: trueP, ok: true},
|
|
|
|
{debug: falseP, ok: false},
|
|
|
|
{debug: falseP, acl: falseP, ok: false},
|
|
|
|
{acl: trueP, ok: true},
|
|
|
|
{acl: falseP, debug: trueP, ok: true},
|
|
|
|
{debug: falseP, acl: trueP, ok: true},
|
2019-12-10 16:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-12-19 18:17:42 +00:00
|
|
|
ptrToStr := func(val *bool) string {
|
|
|
|
if val == nil {
|
|
|
|
return "unset"
|
|
|
|
} else if *val == true {
|
|
|
|
return "true"
|
|
|
|
} else {
|
|
|
|
return "false"
|
2019-12-10 16:13:22 +00:00
|
|
|
}
|
2019-12-19 18:17:42 +00:00
|
|
|
}
|
2019-12-10 16:13:22 +00:00
|
|
|
|
2019-12-19 18:17:42 +00:00
|
|
|
t.Run(
|
|
|
|
fmt.Sprintf("debug %s, acl %s",
|
|
|
|
ptrToStr(tc.debug),
|
|
|
|
ptrToStr(tc.acl)),
|
|
|
|
func(t *testing.T) {
|
|
|
|
cb := func(c *Config) {
|
|
|
|
if tc.acl != nil {
|
|
|
|
c.ACL.Enabled = *tc.acl
|
|
|
|
}
|
|
|
|
if tc.debug == nil {
|
|
|
|
var nodebug bool
|
|
|
|
c.EnableDebug = nodebug
|
|
|
|
} else {
|
|
|
|
c.EnableDebug = *tc.debug
|
|
|
|
}
|
2019-12-10 16:13:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:17:42 +00:00
|
|
|
httpTest(t, cb, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
url := "/v1/agent/pprof/cmdline"
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2019-12-10 16:13:22 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-19 18:17:42 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if tc.acl != nil && *tc.acl {
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.Server.AgentPprofRequest(respW, req)
|
|
|
|
if tc.ok {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, structs.ErrPermissionDenied.Error(), err.Error())
|
|
|
|
}
|
|
|
|
})
|
2019-12-10 16:13:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:36:12 +00:00
|
|
|
func TestAgent_PprofRequest(t *testing.T) {
|
|
|
|
cases := []struct {
|
2019-12-12 21:52:13 +00:00
|
|
|
desc string
|
|
|
|
url string
|
|
|
|
addNodeID bool
|
|
|
|
addServerID bool
|
|
|
|
expectedErr string
|
2020-02-03 15:27:29 +00:00
|
|
|
clientOnly bool
|
2019-12-04 13:36:12 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-02-03 15:27:29 +00:00
|
|
|
desc: "cmdline local server request",
|
2019-12-12 21:52:13 +00:00
|
|
|
url: "/v1/agent/pprof/cmdline",
|
2019-12-04 13:36:12 +00:00
|
|
|
},
|
2020-02-03 15:27:29 +00:00
|
|
|
{
|
|
|
|
desc: "cmdline local node request",
|
|
|
|
url: "/v1/agent/pprof/cmdline",
|
|
|
|
clientOnly: true,
|
|
|
|
},
|
2019-12-06 21:27:26 +00:00
|
|
|
{
|
2019-12-12 21:52:13 +00:00
|
|
|
desc: "cmdline node request",
|
|
|
|
url: "/v1/agent/pprof/cmdline",
|
|
|
|
addNodeID: true,
|
2019-12-06 21:27:26 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-12 21:52:13 +00:00
|
|
|
desc: "cmdline server request",
|
|
|
|
url: "/v1/agent/pprof/cmdline",
|
|
|
|
addServerID: true,
|
2019-12-06 21:27:26 +00:00
|
|
|
},
|
2019-12-13 19:44:02 +00:00
|
|
|
{
|
|
|
|
desc: "invalid server request",
|
|
|
|
url: "/v1/agent/pprof/unknown",
|
|
|
|
addServerID: true,
|
2019-12-13 20:06:19 +00:00
|
|
|
expectedErr: "RPC Error:: 404,Pprof profile not found profile: unknown",
|
2019-12-13 19:44:02 +00:00
|
|
|
},
|
2019-12-06 21:27:26 +00:00
|
|
|
{
|
2019-12-12 21:52:13 +00:00
|
|
|
desc: "cpu profile request",
|
|
|
|
url: "/v1/agent/pprof/profile",
|
|
|
|
addNodeID: true,
|
2019-12-06 21:27:26 +00:00
|
|
|
},
|
|
|
|
{
|
2019-12-12 21:52:13 +00:00
|
|
|
desc: "trace request",
|
|
|
|
url: "/v1/agent/pprof/trace",
|
|
|
|
addNodeID: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "pprof lookup request",
|
|
|
|
url: "/v1/agent/pprof/goroutine",
|
|
|
|
addNodeID: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "unknown pprof lookup request",
|
|
|
|
url: "/v1/agent/pprof/latency",
|
|
|
|
addNodeID: true,
|
|
|
|
expectedErr: "RPC Error:: 404,Pprof profile not found profile: latency",
|
2019-12-06 21:27:26 +00:00
|
|
|
},
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
|
|
|
|
// add node or server id query param
|
|
|
|
url := tc.url
|
|
|
|
if tc.addNodeID {
|
|
|
|
url = url + "?node_id=" + s.client.NodeID()
|
|
|
|
} else if tc.addServerID {
|
|
|
|
url = url + "?server_id=" + s.server.LocalMember().Name
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:27:29 +00:00
|
|
|
if tc.clientOnly {
|
|
|
|
s.Agent.server = nil
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:36:12 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
resp, err := s.Server.AgentPprofRequest(respW, req)
|
|
|
|
|
2019-12-06 21:27:26 +00:00
|
|
|
if tc.expectedErr != "" {
|
2019-12-04 13:36:12 +00:00
|
|
|
require.Error(t, err)
|
2019-12-12 21:52:13 +00:00
|
|
|
require.EqualError(t, err, tc.expectedErr)
|
2019-12-04 13:36:12 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
type closableRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
closer chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClosableRecorder() *closableRecorder {
|
|
|
|
r := httptest.NewRecorder()
|
|
|
|
closer := make(chan bool)
|
|
|
|
return &closableRecorder{r, closer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *closableRecorder) Close() {
|
|
|
|
close(r.closer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *closableRecorder) CloseNotify() <-chan bool {
|
|
|
|
return r.closer
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
func TestHTTP_AgentForceLeave(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2015-09-07 02:08:05 +00:00
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/agent/force-leave?node=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
_, err = s.Server.AgentForceLeaveRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-09-25 03:39:03 +00:00
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
func TestHTTP_AgentForceLeave_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/agent/force-leave?node=foo", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.AgentForceLeaveRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentForceLeaveRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentForceLeaveRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.AgentForceLeaveRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-25 03:39:03 +00:00
|
|
|
func TestHTTP_AgentSetServers(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2018-01-26 19:12:09 +00:00
|
|
|
addr := s.Config.AdvertiseAddrs.RPC
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2020-04-07 13:27:48 +00:00
|
|
|
// Write the Nomad RPC byte to set the mode
|
2018-01-26 19:12:09 +00:00
|
|
|
if _, err := conn.Write([]byte{byte(pool.RpcNomad)}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := pool.NewClientCodec(conn)
|
|
|
|
args := &structs.GenericRequest{}
|
|
|
|
var leader string
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Status.Leader", args, &leader)
|
|
|
|
return leader != "", err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
|
|
|
|
2015-09-25 03:39:03 +00:00
|
|
|
// Create the request
|
2017-09-26 22:26:33 +00:00
|
|
|
req, err := http.NewRequest("PUT", "/v1/agent/servers", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2015-09-25 03:39:03 +00:00
|
|
|
|
|
|
|
// Send the request
|
2017-09-26 22:26:33 +00:00
|
|
|
respW := httptest.NewRecorder()
|
2015-09-25 03:39:03 +00:00
|
|
|
_, err = s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "missing server address")
|
2015-09-25 03:39:03 +00:00
|
|
|
|
|
|
|
// Create a valid request
|
2016-09-26 18:57:06 +00:00
|
|
|
req, err = http.NewRequest("PUT", "/v1/agent/servers?address=127.0.0.1%3A4647&address=127.0.0.2%3A4647&address=127.0.0.3%3A4647", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2015-09-25 03:39:03 +00:00
|
|
|
|
2018-01-26 01:56:47 +00:00
|
|
|
// Send the request which should fail
|
2015-09-25 03:39:03 +00:00
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
_, err = s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
2015-09-25 03:39:03 +00:00
|
|
|
|
|
|
|
// Retrieve the servers again
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/servers", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2015-09-25 03:39:03 +00:00
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
// Make the request and check the result
|
2017-09-14 17:04:48 +00:00
|
|
|
expected := []string{
|
2018-01-26 01:56:47 +00:00
|
|
|
s.GetConfig().AdvertiseAddrs.RPC,
|
2016-06-01 07:34:46 +00:00
|
|
|
}
|
2016-09-26 18:57:06 +00:00
|
|
|
out, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2015-09-25 03:39:03 +00:00
|
|
|
servers := out.([]string)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(servers, len(expected))
|
|
|
|
require.Equal(expected, servers)
|
2015-09-25 03:39:03 +00:00
|
|
|
})
|
|
|
|
}
|
2016-10-17 17:48:04 +00:00
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
func TestHTTP_AgentSetServers_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
2018-01-26 19:12:09 +00:00
|
|
|
addr := s.Config.AdvertiseAddrs.RPC
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Write the Consul RPC byte to set the mode
|
|
|
|
if _, err := conn.Write([]byte{byte(pool.RpcNomad)}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := pool.NewClientCodec(conn)
|
|
|
|
args := &structs.GenericRequest{}
|
|
|
|
var leader string
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Status.Leader", args, &leader)
|
|
|
|
return leader != "", err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Make the HTTP request
|
2018-01-26 01:56:47 +00:00
|
|
|
path := fmt.Sprintf("/v1/agent/servers?address=%s", url.QueryEscape(s.GetConfig().AdvertiseAddrs.RPC))
|
|
|
|
req, err := http.NewRequest("PUT", path, nil)
|
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentListServers_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Create list request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/servers", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
expected := []string{
|
2018-01-26 01:56:47 +00:00
|
|
|
s.GetConfig().AdvertiseAddrs.RPC,
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 01:56:47 +00:00
|
|
|
// Wait for client to have a server
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
return len(s.client.GetServers()) != 0, fmt.Errorf("no servers")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatal(err)
|
|
|
|
})
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
out, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
servers := out.([]string)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(servers, len(expected))
|
|
|
|
require.Equal(expected, servers)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
out, err := s.Server.AgentServersRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
servers := out.([]string)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(servers, len(expected))
|
|
|
|
require.Equal(expected, servers)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
func TestHTTP_AgentListKeys(t *testing.T) {
|
2017-07-20 05:42:15 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
2017-07-20 05:14:36 +00:00
|
|
|
}, func(s *TestAgent) {
|
2016-10-17 17:48:04 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/list", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(t, err)
|
2016-10-17 17:48:04 +00:00
|
|
|
kresp := out.(structs.KeyringResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(t, kresp.Keys, 1)
|
2016-10-17 17:48:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
func TestHTTP_AgentListKeys_ACL(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
|
|
|
|
cb := func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
|
|
|
}
|
|
|
|
|
|
|
|
httpACLTest(t, cb, func(s *TestAgent) {
|
|
|
|
state := s.Agent.server.State()
|
|
|
|
|
|
|
|
// Make the HTTP request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/list", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err := s.Server.KeyringOperationRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1005, "invalid", mock.AgentPolicy(acl.PolicyRead))
|
|
|
|
setToken(req, token)
|
|
|
|
_, err := s.Server.KeyringOperationRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, 1007, "valid", mock.AgentPolicy(acl.PolicyWrite))
|
|
|
|
setToken(req, token)
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
kresp := out.(structs.KeyringResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(kresp.Keys, 1)
|
|
|
|
require.Contains(kresp.Keys, key1)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a root token
|
|
|
|
{
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
setToken(req, s.RootToken)
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-06 19:02:53 +00:00
|
|
|
kresp := out.(structs.KeyringResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Len(kresp.Keys, 1)
|
|
|
|
require.Contains(kresp.Keys, key1)
|
2017-10-06 19:02:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
func TestHTTP_AgentInstallKey(t *testing.T) {
|
2018-01-26 01:56:47 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:42:15 +00:00
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
key2 := "wH1Bn9hlJ0emgWB1JttVRA=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
2017-07-20 05:14:36 +00:00
|
|
|
}, func(s *TestAgent) {
|
2016-10-17 17:48:04 +00:00
|
|
|
b, err := json.Marshal(&structs.KeyringRequest{Key: key2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/install", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
|
|
|
|
_, err = s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/list", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
kresp := out.(structs.KeyringResponse)
|
|
|
|
if len(kresp.Keys) != 2 {
|
|
|
|
t.Fatalf("bad: %v", kresp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentRemoveKey(t *testing.T) {
|
2018-01-26 01:56:47 +00:00
|
|
|
t.Parallel()
|
2017-07-20 05:42:15 +00:00
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
key2 := "wH1Bn9hlJ0emgWB1JttVRA=="
|
|
|
|
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.EncryptKey = key1
|
2017-07-20 05:14:36 +00:00
|
|
|
}, func(s *TestAgent) {
|
2016-10-17 17:48:04 +00:00
|
|
|
b, err := json.Marshal(&structs.KeyringRequest{Key: key2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/keyring/install", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/remove", bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
if _, err = s.Server.KeyringOperationRequest(respW, req); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/v1/agent/keyring/list", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
respW = httptest.NewRecorder()
|
|
|
|
out, err := s.Server.KeyringOperationRequest(respW, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
kresp := out.(structs.KeyringResponse)
|
|
|
|
if len(kresp.Keys) != 1 {
|
|
|
|
t.Fatalf("bad: %v", kresp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
func TestHTTP_AgentHealth_Ok(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
// Enable ACLs to ensure they're not enforced
|
|
|
|
httpACLTest(t, nil, func(s *TestAgent) {
|
|
|
|
// No ?type=
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
2017-10-13 22:37:44 +00:00
|
|
|
health := healthI.(*healthResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(health.Client)
|
|
|
|
require.True(health.Client.Ok)
|
|
|
|
require.Equal("ok", health.Client.Message)
|
|
|
|
require.NotNil(health.Server)
|
|
|
|
require.True(health.Server.Ok)
|
|
|
|
require.Equal("ok", health.Server.Message)
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// type=client
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health?type=client", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
2017-10-13 22:37:44 +00:00
|
|
|
health := healthI.(*healthResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(health.Client)
|
|
|
|
require.True(health.Client.Ok)
|
|
|
|
require.Equal("ok", health.Client.Message)
|
|
|
|
require.Nil(health.Server)
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// type=server
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health?type=server", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
2017-10-13 22:37:44 +00:00
|
|
|
health := healthI.(*healthResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(health.Server)
|
|
|
|
require.True(health.Server.Ok)
|
|
|
|
require.Equal("ok", health.Server.Message)
|
|
|
|
require.Nil(health.Client)
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// type=client&type=server
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health?type=client&type=server", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
2017-10-13 22:37:44 +00:00
|
|
|
health := healthI.(*healthResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(health.Client)
|
|
|
|
require.True(health.Client.Ok)
|
|
|
|
require.Equal("ok", health.Client.Message)
|
|
|
|
require.NotNil(health.Server)
|
|
|
|
require.True(health.Server.Ok)
|
|
|
|
require.Equal("ok", health.Server.Message)
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentHealth_BadServer(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
serverAgent := NewTestAgent(t, "server", nil)
|
|
|
|
defer serverAgent.Shutdown()
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
s := makeHTTPServer(t, func(c *Config) {
|
|
|
|
// Disable server to make server health unhealthy if requested
|
|
|
|
c.Server.Enabled = false
|
|
|
|
c.Client.Servers = []string{fmt.Sprintf("localhost:%d", serverAgent.Config.Ports.RPC)}
|
|
|
|
})
|
|
|
|
defer s.Shutdown()
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
// No ?type= means server is just skipped
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health", nil)
|
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
|
|
|
health := healthI.(*healthResponse)
|
|
|
|
require.NotNil(health.Client)
|
|
|
|
require.True(health.Client.Ok)
|
|
|
|
require.Equal("ok", health.Client.Message)
|
|
|
|
require.Nil(health.Server)
|
|
|
|
}
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
// type=server means server is considered unhealthy
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health?type=server", nil)
|
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.HealthRequest(respW, req)
|
|
|
|
require.NotNil(err)
|
|
|
|
httpErr, ok := err.(HTTPCodedError)
|
|
|
|
require.True(ok)
|
|
|
|
require.Equal(500, httpErr.Code())
|
|
|
|
require.Equal(`{"server":{"ok":false,"message":"server not enabled"}}`, err.Error())
|
|
|
|
}
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_AgentHealth_BadClient(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-01-26 01:56:47 +00:00
|
|
|
require := require.New(t)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
// Disable client to make server unhealthy if requested
|
|
|
|
cb := func(c *Config) {
|
|
|
|
c.Client.Enabled = false
|
|
|
|
}
|
2017-10-13 22:37:44 +00:00
|
|
|
|
2020-02-06 21:40:02 +00:00
|
|
|
// Enable ACLs to ensure they're not enforced
|
|
|
|
httpACLTest(t, cb, func(s *TestAgent) {
|
2017-10-13 22:37:44 +00:00
|
|
|
// No ?type= means client is just skipped
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
|
|
|
healthI, err := s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(http.StatusOK, respW.Code)
|
|
|
|
require.NotNil(healthI)
|
2017-10-13 22:37:44 +00:00
|
|
|
health := healthI.(*healthResponse)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(health.Server)
|
|
|
|
require.True(health.Server.Ok)
|
|
|
|
require.Equal("ok", health.Server.Message)
|
|
|
|
require.Nil(health.Client)
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// type=client means client is considered unhealthy
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/agent/health?type=client", nil)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.Nil(err)
|
2017-10-13 22:37:44 +00:00
|
|
|
|
|
|
|
respW := httptest.NewRecorder()
|
2017-10-13 23:25:25 +00:00
|
|
|
_, err = s.Server.HealthRequest(respW, req)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.NotNil(err)
|
2017-10-13 23:25:25 +00:00
|
|
|
httpErr, ok := err.(HTTPCodedError)
|
2018-01-26 01:56:47 +00:00
|
|
|
require.True(ok)
|
|
|
|
require.Equal(500, httpErr.Code())
|
|
|
|
require.Equal(`{"client":{"ok":false,"message":"client not enabled"}}`, err.Error())
|
2017-10-13 22:37:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-24 04:08:25 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
errorPipe = &net.OpError{
|
|
|
|
Op: "write",
|
|
|
|
Net: "tcp",
|
|
|
|
Source: &net.TCPAddr{},
|
|
|
|
Addr: &net.TCPAddr{},
|
|
|
|
Err: &os.SyscallError{
|
|
|
|
Syscall: "write",
|
|
|
|
Err: syscall.EPIPE,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// fakeRW is a fake response writer to ease polling streaming responses in a
|
|
|
|
// data-race-free way.
|
|
|
|
type fakeRW struct {
|
|
|
|
Code int
|
|
|
|
HeaderMap http.Header
|
|
|
|
buf *bytes.Buffer
|
|
|
|
closed bool
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
// Written is ticked whenever a Write occurs and on WriteHeaders if it
|
|
|
|
// is explicitly called
|
|
|
|
Written chan int
|
|
|
|
|
|
|
|
// ClosedErr is the error Write will return once the writer is closed.
|
|
|
|
// Defaults to EPIPE. Must not be mutated concurrently with writes.
|
|
|
|
ClosedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header is for setting headers before writing a response. Tests should check
|
|
|
|
// the HeaderMap field directly.
|
|
|
|
func (f *fakeRW) Header() http.Header {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
if f.Code != 0 {
|
|
|
|
panic("cannot set headers after WriteHeader has been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.HeaderMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeRW) Write(p []byte) (int, error) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
if f.closed {
|
|
|
|
// Mimic an EPIPE error
|
|
|
|
return 0, f.ClosedErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Code == 0 {
|
|
|
|
f.Code = 200
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := f.buf.Write(p)
|
|
|
|
select {
|
|
|
|
case f.Written <- 1:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader sets Code and FinalHeaders
|
|
|
|
func (f *fakeRW) WriteHeader(statusCode int) {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
if f.Code != 0 {
|
|
|
|
panic("cannot call WriteHeader more than once")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Code = statusCode
|
|
|
|
select {
|
|
|
|
case f.Written <- 1:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns the body bytes written to the buffer. Safe for calling
|
|
|
|
// concurrent with writes.
|
|
|
|
func (f *fakeRW) Bytes() []byte {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
return f.buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the writer causing an EPIPE error on future writes. Safe to call
|
|
|
|
// concurrently with other methods. Safe to call more than once.
|
|
|
|
func (f *fakeRW) Close() {
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
f.closed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeRW() *fakeRW {
|
|
|
|
return &fakeRW{
|
|
|
|
HeaderMap: make(map[string][]string),
|
|
|
|
buf: &bytes.Buffer{},
|
|
|
|
Written: make(chan int, 1),
|
|
|
|
ClosedErr: errorPipe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHTTP_XSS_Monitor asserts /v1/agent/monitor is safe against XSS attacks
|
|
|
|
// even when log output contains HTML+Javascript.
|
|
|
|
func TestHTTP_XSS_Monitor(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Logline string
|
|
|
|
JSON bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Plain",
|
|
|
|
Logline: "--TEST 123--",
|
|
|
|
JSON: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "JSON",
|
|
|
|
Logline: "--TEST 123--",
|
|
|
|
JSON: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "XSSPlain",
|
|
|
|
Logline: "<script>alert(document.domain);</script>",
|
|
|
|
JSON: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "XSSJson",
|
|
|
|
Logline: "<script>alert(document.domain);</script>",
|
|
|
|
JSON: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cases {
|
|
|
|
tc := cases[i]
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
s := makeHTTPServer(t, nil)
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
path := fmt.Sprintf("%s/v1/agent/monitor?error_level=error&plain=%t", s.HTTPAddr(), !tc.JSON)
|
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp := NewFakeRW()
|
|
|
|
closedErr := errors.New("sentinel error")
|
|
|
|
resp.ClosedErr = closedErr
|
|
|
|
defer resp.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
_, err := s.Server.AgentMonitor(resp, req)
|
|
|
|
errCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
deadline := time.After(3 * time.Second)
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
// Log a needle and look for it in the response haystack
|
|
|
|
s.Server.logger.Error(tc.Logline)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(30 * time.Millisecond):
|
|
|
|
// Give AgentMonitor handler goroutine time to start
|
|
|
|
case <-resp.Written:
|
|
|
|
// Something was written, check it
|
|
|
|
case <-deadline:
|
|
|
|
t.Fatalf("timed out waiting for expected log line; body:\n%s", string(resp.Bytes()))
|
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatalf("AgentMonitor exited unexpectedly: err=%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.JSON {
|
|
|
|
if bytes.Contains(resp.Bytes(), []byte(tc.Logline)) {
|
|
|
|
// Found needle!
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
// Try again
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode JSON
|
|
|
|
r := bytes.NewReader(resp.Bytes())
|
|
|
|
dec := json.NewDecoder(r)
|
|
|
|
for {
|
|
|
|
data := struct{ Data []byte }{}
|
|
|
|
if err := dec.Decode(&data); err != nil {
|
|
|
|
// Probably a partial write, continue
|
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Contains(data.Data, []byte(tc.Logline)) {
|
|
|
|
// Found needle!
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert default logs are application/json
|
|
|
|
ct := "text/plain"
|
|
|
|
if tc.JSON {
|
|
|
|
ct = "application/json"
|
|
|
|
}
|
|
|
|
require.Equal(t, []string{ct}, resp.HeaderMap.Values("Content-Type"))
|
|
|
|
|
|
|
|
// Close response writer and log to make AgentMonitor exit
|
|
|
|
resp.Close()
|
|
|
|
s.Server.logger.Error("log again to force a write that detects the closed connection")
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.EqualError(t, closedErr, err.Error())
|
|
|
|
case <-deadline:
|
|
|
|
t.Fatalf("timed out waiting for closing error from handler")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|