Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #35 from booster-proj/issue/34
Browse files Browse the repository at this point in the history
Issue/34
  • Loading branch information
dmorn authored Feb 7, 2019
2 parents c134bf9 + 21178c3 commit ded5bfe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
13 changes: 9 additions & 4 deletions remote/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
20 changes: 11 additions & 9 deletions store/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,28 @@ 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"`
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,
}
}

Expand Down
10 changes: 10 additions & 0 deletions store/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit ded5bfe

Please sign in to comment.