fix: non-leader agents return 404 on Get Intention exact api (#13179)

* fix: non-leader agents return 404 on Get Intention exact api

- rpc call method appends extra error message, so change == to
  "Strings.Contains"

Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
This commit is contained in:
cskh 2022-05-24 13:21:15 -04:00 committed by GitHub
parent 4c04d70fb6
commit 8712a088b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -324,7 +324,7 @@ func (s *HTTPHandlers) IntentionGetExact(resp http.ResponseWriter, req *http.Req
var reply structs.IndexedIntentions var reply structs.IndexedIntentions
if err := s.agent.RPC("Intention.Get", &args, &reply); err != nil { if err := s.agent.RPC("Intention.Get", &args, &reply); err != nil {
// We have to check the string since the RPC sheds the error type // 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()} return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: err.Error()}
} }

View File

@ -1,6 +1,7 @@
package agent package agent
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "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) { func TestIntentionPutExact(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("too slow for testing.Short") t.Skip("too slow for testing.Short")