From 75a0bdf9c52291b54dc6b2d9549ad309903be7ae Mon Sep 17 00:00:00 2001 From: Daniel Morandini Date: Thu, 7 Feb 2019 12:00:14 +0100 Subject: [PATCH 1/3] Update reserved policy to accept multiple hosts --- store/policies.go | 16 +++++++++------- store/policies_test.go | 10 ++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/store/policies.go b/store/policies.go index a605075..bf83116 100644 --- a/store/policies.go +++ b/store/policies.go @@ -117,21 +117,23 @@ func (p *BlockPolicy) Accept(id, address string) bool { type ReservedPolicy struct { basePolicy SourceID string `json:"reserved_source_id"` - Address string `json:"address"` } -func NewReservedPolicy(issuer, sourceID, address string) *ReservedPolicy { - address = TrimPort(address) +func NewReservedPolicy(issuer, sourceID string, hosts ...string) *ReservedPolicy { + addrs := []string{} + for _, v := range hosts { + address := TrimPort(v) + addrs = append(addrs, LookupAddress(address)...) + } return &ReservedPolicy{ basePolicy: basePolicy{ - Name: fmt.Sprintf("reserve_%s_for_%s", sourceID, address), + Name: fmt.Sprintf("reserve_%s", sourceID), Issuer: issuer, Code: PolicyCodeReserve, - Desc: fmt.Sprintf("source %v will only be used for connections to %s", sourceID, address), - Addrs: LookupAddress(address), + Desc: fmt.Sprintf("source %v will only be used for connections to %v", sourceID, addrs), + Addrs: addrs, }, SourceID: sourceID, - Address: address, } } diff --git a/store/policies_test.go b/store/policies_test.go index ce99f1b..43d6aed 100644 --- a/store/policies_test.go +++ b/store/policies_test.go @@ -76,6 +76,7 @@ func TestReservedPolicy(t *testing.T) { s1 := &mock{id: "bar"} t0 := "host0" t1 := "host1" + t2 := "host2" p := store.NewReservedPolicy("T", s0.ID(), t0) if ok := p.Accept(s0.ID(), t0); !ok { @@ -90,6 +91,15 @@ func TestReservedPolicy(t *testing.T) { if ok := p.Accept(s1.ID(), t1); !ok { t.Fatalf("Policy %s did not accept source %v for address %s", p.ID(), s1.ID(), t1) } + + // reserved policy with multiple addresses + p = store.NewReservedPolicy("T", s0.ID(), t0, t1) + if ok := p.Accept(s0.ID(), t0); !ok { + t.Fatalf("Policy %s did not accept source %v for address %s", p.ID(), s0.ID(), t0) + } + if ok := p.Accept(s0.ID(), t2); ok { + t.Fatalf("Policy %s accepted source %v for address %s", p.ID(), s0.ID(), t2) + } } func TestAvoidPolicy(t *testing.T) { From 84a5b78a53cb519621f93e01661ac21f1e18a14b Mon Sep 17 00:00:00 2001 From: Daniel Morandini Date: Thu, 7 Feb 2019 12:21:10 +0100 Subject: [PATCH 2/3] Update reserve endpoint to accept a list of hosts --- remote/endpoints.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/remote/endpoints.go b/remote/endpoints.go index 30ad409..7034f13 100644 --- a/remote/endpoints.go +++ b/remote/endpoints.go @@ -121,10 +121,15 @@ func makePoliciesStickyHandler(s *store.SourceStore) http.HandlerFunc { } } +type ReservedPolicyInput struct { + PoliciesInput + Hosts []string `json:"hosts"` +} + func makePoliciesReserveHandler(s *store.SourceStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - var payload PoliciesInput + var payload ReservedPolicyInput if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { writeError(w, err, http.StatusBadRequest) return @@ -133,12 +138,12 @@ func makePoliciesReserveHandler(s *store.SourceStore) http.HandlerFunc { writeError(w, fmt.Errorf("validation error: source_id cannot be empty"), http.StatusBadRequest) return } - if payload.Target == "" { - writeError(w, fmt.Errorf("validation error: target cannot be empty"), http.StatusBadRequest) + if len(payload.Hosts) == 0 { + writeError(w, fmt.Errorf("validation error: hosts cannot be empty list"), http.StatusBadRequest) return } - p := store.NewReservedPolicy(payload.Issuer, payload.SourceID, payload.Target) + p := store.NewReservedPolicy(payload.Issuer, payload.SourceID, payload.Hosts...) p.Reason = payload.Reason handlePolicy(s, p, w, r) } From 21178c3bac935411800a399f249118335670d427 Mon Sep 17 00:00:00 2001 From: Daniel Morandini Date: Thu, 7 Feb 2019 12:21:24 +0100 Subject: [PATCH 3/3] Fix documentation. Close #34 --- store/policies.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/store/policies.go b/store/policies.go index bf83116..728caec 100644 --- a/store/policies.go +++ b/store/policies.go @@ -112,8 +112,8 @@ func (p *BlockPolicy) Accept(id, address string) bool { } // ReservedPolicy is a Policy implementation. It is used to reserve a source -// to be used only for connections to a defined address, and those connections -// will not be assigned to any other source. +// to be used only for connections to a defined list of addresses, and those +// connections will not be assigned to any other source. type ReservedPolicy struct { basePolicy SourceID string `json:"reserved_source_id"`