Display nicely Networks (CIDR) in runtime configuration (#6029)

* Display nicely Networks (CIDR) in runtime configuration

CIDR mask is displayed in binary in configuration.
This add support for nicely displaying CIDR in runtime configuration.

Currently, if a configuration contains the following lines:

  "http_config": {
    "allow_write_http_from": [
      "127.0.0.0/8",
      "::1/128"
    ]
  }

A call to `/v1/agent/self?pretty` would display

  "AllowWriteHTTPFrom": [
            {
                "IP": "127.0.0.0",
                "Mask": "/wAAAA=="
            },
            {
                "IP": "::1",
                "Mask": "/////////////////////w=="
            }
  ]

This PR fixes it and it will now display:

   "AllowWriteHTTPFrom": [ "127.0.0.0/8", "::1/128" ]

* Added test for cidr nice rendering in `TestSanitize()`.
pull/6173/head
Pierre Souchay 5 years ago committed by Matt Keeler
parent 821bd2f972
commit b4590fb8e8

@ -1674,7 +1674,6 @@ func cleanRetryJoin(a string) string {
func sanitize(name string, v reflect.Value) reflect.Value { func sanitize(name string, v reflect.Value) reflect.Value {
typ := v.Type() typ := v.Type()
switch { switch {
// check before isStruct and isPtr // check before isStruct and isPtr
case isNetAddr(typ): case isNetAddr(typ):
if v.IsNil() { if v.IsNil() {
@ -1689,6 +1688,8 @@ func sanitize(name string, v reflect.Value) reflect.Value {
return reflect.ValueOf("unix://" + x.String()) return reflect.ValueOf("unix://" + x.String())
case *net.IPAddr: case *net.IPAddr:
return reflect.ValueOf(x.IP.String()) return reflect.ValueOf(x.IP.String())
case *net.IPNet:
return reflect.ValueOf(x.String())
default: default:
return v return v
} }

@ -5660,6 +5660,14 @@ func TestConfigDecodeBytes(t *testing.T) {
} }
} }
func parseCIDR(t *testing.T, cidr string) *net.IPNet {
_, x, err := net.ParseCIDR(cidr)
if err != nil {
t.Fatalf("CIDRParse: %v", err)
}
return x
}
func TestSanitize(t *testing.T) { func TestSanitize(t *testing.T) {
rt := RuntimeConfig{ rt := RuntimeConfig{
BindAddr: &net.IPAddr{IP: net.ParseIP("127.0.0.1")}, BindAddr: &net.IPAddr{IP: net.ParseIP("127.0.0.1")},
@ -5670,6 +5678,10 @@ func TestSanitize(t *testing.T) {
&net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 5678}, &net.UDPAddr{IP: net.ParseIP("1.2.3.4"), Port: 5678},
}, },
DNSSOA: RuntimeSOAConfig{Refresh: 3600, Retry: 600, Expire: 86400, Minttl: 0}, DNSSOA: RuntimeSOAConfig{Refresh: 3600, Retry: 600, Expire: 86400, Minttl: 0},
AllowWriteHTTPFrom: []*net.IPNet{
parseCIDR(t, "127.0.0.0/8"),
parseCIDR(t, "::1/128"),
},
HTTPAddrs: []net.Addr{ HTTPAddrs: []net.Addr{
&net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 5678}, &net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 5678},
&net.UnixAddr{Name: "/var/run/foo"}, &net.UnixAddr{Name: "/var/run/foo"},
@ -6009,7 +6021,10 @@ func TestSanitize(t *testing.T) {
"Version": "", "Version": "",
"VersionPrerelease": "", "VersionPrerelease": "",
"Watches": [], "Watches": [],
"AllowWriteHTTPFrom": [] "AllowWriteHTTPFrom": [
"127.0.0.0/8",
"::1/128"
]
}` }`
b, err := json.MarshalIndent(rt.Sanitized(), "", " ") b, err := json.MarshalIndent(rt.Sanitized(), "", " ")
if err != nil { if err != nil {

Loading…
Cancel
Save