diff --git a/agent/intentions_endpoint.go b/agent/intentions_endpoint.go index 7c9855a44d..ff720e9002 100644 --- a/agent/intentions_endpoint.go +++ b/agent/intentions_endpoint.go @@ -324,7 +324,7 @@ func (s *HTTPHandlers) IntentionGetExact(resp http.ResponseWriter, req *http.Req var reply structs.IndexedIntentions if err := s.agent.RPC("Intention.Get", &args, &reply); err != nil { // We have to check the string since the RPC sheds the error type - if err.Error() == consul.ErrIntentionNotFound.Error() { + if strings.Contains(err.Error(), consul.ErrIntentionNotFound.Error()) { return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: err.Error()} } diff --git a/agent/intentions_endpoint_test.go b/agent/intentions_endpoint_test.go index 1180a2c7dd..114d195956 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -1,6 +1,7 @@ package agent import ( + "errors" "fmt" "net/http" "net/http/httptest" @@ -314,6 +315,52 @@ func TestIntentionCheck(t *testing.T) { }) } +type testSrv struct { + delegate +} + +func (s *testSrv) RPC(method string, args interface{}, reply interface{}) error { + return fmt.Errorf("rpc error making call: %w", errors.New("Intention not found")) +} +func (s *testSrv) Shutdown() error { + return nil +} + +func TestIntentionGetExact(t *testing.T) { + if testing.Short() { + t.Skip("too slow for testing.Short") + } + + t.Parallel() + + a := NewTestAgent(t, "") + defer a.Shutdown() + testrpc.WaitForTestAgent(t, a.RPC, "dc1") + + notfound := func(t *testing.T) { + t.Helper() + req, err := http.NewRequest("GET", "/v1/connect/intentions/exact?source=foo&destination=bar", nil) + require.NoError(t, err) + + resp := httptest.NewRecorder() + obj, err := a.srv.IntentionExact(resp, req) + testutil.RequireErrorContains(t, err, "Intention not found") + httpErr, ok := err.(HTTPError) + require.True(t, ok) + require.Equal(t, http.StatusNotFound, httpErr.StatusCode) + require.Nil(t, obj) + } + + t.Run("not found locally", func(t *testing.T) { + notfound(t) + }) + + t.Run("not found by RPC", func(t *testing.T) { + a.delegate = &testSrv{} + notfound(t) + }) +} + func TestIntentionPutExact(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short")