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

Commit

Permalink
Update reserved policy to accept multiple hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorn committed Feb 7, 2019
1 parent c134bf9 commit 75a0bdf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions store/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

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 75a0bdf

Please sign in to comment.