Пример #1
0
func (s *CheckersSuite) TestClientIPAddrCaveat(c *gc.C) {
	cav := checkers.ClientIPAddrCaveat(net.IP{127, 0, 0, 1})
	c.Assert(cav, gc.Equals, checkers.Caveat{
		Condition: "client-ip-addr 127.0.0.1",
	})
	cav = checkers.ClientIPAddrCaveat(net.ParseIP("2001:4860:0:2001::68"))
	c.Assert(cav, gc.Equals, checkers.Caveat{
		Condition: "client-ip-addr 2001:4860:0:2001::68",
	})
	cav = checkers.ClientIPAddrCaveat(nil)
	c.Assert(cav, gc.Equals, checkers.Caveat{
		Condition: "error bad IP address []",
	})
	cav = checkers.ClientIPAddrCaveat(net.IP{123, 3})
	c.Assert(cav, gc.Equals, checkers.Caveat{
		Condition: "error bad IP address [123 3]",
	})
}
Пример #2
0
// SameClientIPAddrCaveat returns a caveat that will check that
// the remote IP address is the same as that in the given HTTP request.
func SameClientIPAddrCaveat(req *http.Request) checkers.Caveat {
	if req.RemoteAddr == "" {
		return checkers.ErrorCaveatf("client has no remote IP address")
	}
	ip, err := requestIPAddr(req)
	if err != nil {
		return checkers.ErrorCaveatf("%v", err)
	}
	return checkers.ClientIPAddrCaveat(ip)
}
Пример #3
0
	caveat      string
	expectError string
	expectCause func(err error) bool
}

var isCaveatNotRecognized = errgo.Is(checkers.ErrCaveatNotRecognized)

var checkerTests = []struct {
	about   string
	checker checkers.Checker
	checks  []checkTest
}{{
	about:   "no host name declared",
	checker: checkers.New(httpbakery.Checkers(&http.Request{})),
	checks: []checkTest{{
		caveat:      checkers.ClientIPAddrCaveat(net.IP{0, 0, 0, 0}).Condition,
		expectError: `caveat "client-ip-addr 0.0.0.0" not satisfied: client has no remote address`,
	}, {
		caveat:      checkers.ClientIPAddrCaveat(net.IP{127, 0, 0, 1}).Condition,
		expectError: `caveat "client-ip-addr 127.0.0.1" not satisfied: client has no remote address`,
	}, {
		caveat:      "client-ip-addr badip",
		expectError: `caveat "client-ip-addr badip" not satisfied: cannot parse IP address in caveat`,
	}},
}, {
	about: "IPv4 host name declared",
	checker: checkers.New(httpbakery.Checkers(&http.Request{
		RemoteAddr: "127.0.0.1:1234",
	})),
	checks: []checkTest{{
		caveat: checkers.ClientIPAddrCaveat(net.IP{127, 0, 0, 1}).Condition,