From 70858598e411fadb6f9897042ee0b3f4c0233c3b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 3 Mar 2018 10:12:05 -0800 Subject: [PATCH] agent: use testing intention to get valid intentions --- agent/consul/intention_endpoint.go | 9 ++++++--- agent/intentions_endpoint.go | 8 +++++++- agent/intentions_endpoint_test.go | 28 ++++++++++++---------------- agent/structs/testing_intention.go | 1 + 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/agent/consul/intention_endpoint.go b/agent/consul/intention_endpoint.go index c3723d7081..825c89c593 100644 --- a/agent/consul/intention_endpoint.go +++ b/agent/consul/intention_endpoint.go @@ -99,9 +99,12 @@ func (s *Intention) Apply( args.Intention.DestinationNS = structs.IntentionDefaultNamespace } - // Validate - if err := args.Intention.Validate(); err != nil { - return err + // Validate. We do not validate on delete since it is valid to only + // send an ID in that case. + if args.Op != structs.IntentionOpDelete { + if err := args.Intention.Validate(); err != nil { + return err + } } // Commit diff --git a/agent/intentions_endpoint.go b/agent/intentions_endpoint.go index 39cf3e50b8..72a1cba670 100644 --- a/agent/intentions_endpoint.go +++ b/agent/intentions_endpoint.go @@ -173,7 +173,13 @@ func (s *HTTPServer) IntentionSpecificGet(id string, resp http.ResponseWriter, r return nil, err } - // TODO: validate length + // This shouldn't happen since the called API documents it shouldn't, + // but we check since the alternative if it happens is a panic. + if len(reply.Intentions) == 0 { + resp.WriteHeader(http.StatusNotFound) + return nil, nil + } + return reply.Intentions[0], nil } diff --git a/agent/intentions_endpoint_test.go b/agent/intentions_endpoint_test.go index a1c0413ed9..c236491e8b 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -43,8 +43,10 @@ func TestIntentionsList_values(t *testing.T) { req := structs.IntentionRequest{ Datacenter: "dc1", Op: structs.IntentionOpCreate, - Intention: &structs.Intention{SourceName: v}, + Intention: structs.TestIntention(t), } + req.Intention.SourceName = v + var reply string if err := a.RPC("Intention.Apply", &req, &reply); err != nil { t.Fatalf("err: %s", err) @@ -93,13 +95,10 @@ func TestIntentionsMatch_basic(t *testing.T) { ixn := structs.IntentionRequest{ Datacenter: "dc1", Op: structs.IntentionOpCreate, - Intention: &structs.Intention{ - SourceNS: "default", - SourceName: "test", - DestinationNS: v[0], - DestinationName: v[1], - }, + Intention: structs.TestIntention(t), } + ixn.Intention.DestinationNS = v[0] + ixn.Intention.DestinationName = v[1] // Create var reply string @@ -198,7 +197,8 @@ func TestIntentionsCreate_good(t *testing.T) { defer a.Shutdown() // Make sure an empty list is non-nil. - args := &structs.Intention{SourceName: "foo"} + args := structs.TestIntention(t) + args.SourceName = "foo" req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args)) resp := httptest.NewRecorder() obj, err := a.srv.IntentionCreate(resp, req) @@ -238,12 +238,7 @@ func TestIntentionsSpecificGet_good(t *testing.T) { defer a.Shutdown() // The intention - ixn := &structs.Intention{ - SourceNS: structs.IntentionDefaultNamespace, - SourceName: "foo", - DestinationNS: structs.IntentionDefaultNamespace, - DestinationName: "bar", - } + ixn := structs.TestIntention(t) // Create an intention directly var reply string @@ -286,7 +281,7 @@ func TestIntentionsSpecificUpdate_good(t *testing.T) { defer a.Shutdown() // The intention - ixn := &structs.Intention{SourceName: "foo"} + ixn := structs.TestIntention(t) // Create an intention directly var reply string @@ -341,7 +336,8 @@ func TestIntentionsSpecificDelete_good(t *testing.T) { defer a.Shutdown() // The intention - ixn := &structs.Intention{SourceName: "foo"} + ixn := structs.TestIntention(t) + ixn.SourceName = "foo" // Create an intention directly var reply string diff --git a/agent/structs/testing_intention.go b/agent/structs/testing_intention.go index 930e27869e..a946a3243c 100644 --- a/agent/structs/testing_intention.go +++ b/agent/structs/testing_intention.go @@ -13,5 +13,6 @@ func TestIntention(t testing.T) *Intention { DestinationName: "db", Action: IntentionActionAllow, SourceType: IntentionSourceConsul, + Meta: map[string]string{}, } }