agent: 400 error on invalid UUID format, api handles errors properly

This commit is contained in:
Mitchell Hashimoto 2018-06-27 07:40:06 +02:00
parent db351dc9bb
commit 1c3e9af316
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 47 additions and 1 deletions

View File

@ -212,6 +212,14 @@ func (s *HTTPServer) IntentionSpecificGet(id string, resp http.ResponseWriter, r
return nil, nil return nil, nil
} }
// Not ideal, but there are a number of error scenarios that are not
// user error (400). We look for a specific case of invalid UUID
// to detect a parameter error and return a 400 response. The error
// is not a constant type or message, so we have to use strings.Contains
if strings.Contains(err.Error(), "UUID") {
return nil, BadRequestError{Reason: err.Error()}
}
return nil, err return nil, err
} }

View File

@ -355,6 +355,23 @@ func TestIntentionsSpecificGet_good(t *testing.T) {
assert.Equal(ixn, value) assert.Equal(ixn, value)
} }
func TestIntentionsSpecificGet_invalidId(t *testing.T) {
t.Parallel()
require := require.New(t)
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
// Read intention with bad ID
req, _ := http.NewRequest("GET", "/v1/connect/intentions/hello", nil)
resp := httptest.NewRecorder()
obj, err := a.srv.IntentionSpecific(resp, req)
require.Nil(obj)
require.Error(err)
require.IsType(BadRequestError{}, err)
require.Contains(err.Error(), "UUID")
}
func TestIntentionsSpecificUpdate_good(t *testing.T) { func TestIntentionsSpecificUpdate_good(t *testing.T) {
t.Parallel() t.Parallel()

View File

@ -1,7 +1,9 @@
package api package api
import ( import (
"bytes"
"fmt" "fmt"
"io"
"time" "time"
) )
@ -172,7 +174,10 @@ func (h *Connect) IntentionGet(id string, q *QueryOptions) (*Intention, *QueryMe
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
return nil, qm, nil return nil, qm, nil
} else if resp.StatusCode != 200 { } else if resp.StatusCode != 200 {
return nil, nil, fmt.Errorf("Unexpected response code: %d", resp.StatusCode) var buf bytes.Buffer
io.Copy(&buf, resp.Body)
return nil, nil, fmt.Errorf(
"Unexpected response %d: %s", resp.StatusCode, buf.String())
} }
var out Intention var out Intention

View File

@ -61,6 +61,22 @@ func TestAPI_ConnectIntentionCreateListGetUpdateDelete(t *testing.T) {
require.Nil(actual) require.Nil(actual)
} }
func TestAPI_ConnectIntentionGet_invalidId(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t)
defer s.Stop()
connect := c.Connect()
// Get it
actual, _, err := connect.IntentionGet("hello", nil)
require.Nil(actual)
require.Error(err)
require.Contains(err.Error(), "UUID") // verify it contains the message
}
func TestAPI_ConnectIntentionMatch(t *testing.T) { func TestAPI_ConnectIntentionMatch(t *testing.T) {
t.Parallel() t.Parallel()