summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/hashicorp/go-sockaddr/template
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/go-sockaddr/template')
-rw-r--r--vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile (renamed from vendor/github.com/hashicorp/go-sockaddr/template/Makefile)0
-rw-r--r--vendor/github.com/hashicorp/go-sockaddr/template/doc.go96
-rw-r--r--vendor/github.com/hashicorp/go-sockaddr/template/template.go32
-rw-r--r--vendor/github.com/hashicorp/go-sockaddr/template/template_test.go75
4 files changed, 186 insertions, 17 deletions
diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/Makefile b/vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile
index ce1e274e4..ce1e274e4 100644
--- a/vendor/github.com/hashicorp/go-sockaddr/template/Makefile
+++ b/vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile
diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go b/vendor/github.com/hashicorp/go-sockaddr/template/doc.go
index 59945d7be..90c8784a3 100644
--- a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go
+++ b/vendor/github.com/hashicorp/go-sockaddr/template/doc.go
@@ -53,23 +53,22 @@ Example:
{{ GetDefaultInterfaces }}
`GetPrivateInterfaces` - Returns one IfAddr for every forwardable IP address
-that is included in RFC 6890, is attached to the interface with the default
-route, and whose interface is marked as up. NOTE: RFC 6890 is a more exhaustive
-version of RFC1918 because it spans IPv4 and IPv6, however it does permit the
+that is included in RFC 6890 and whose interface is marked as up. NOTE: RFC 6890 is a more exhaustive
+version of RFC1918 because it spans IPv4 and IPv6, however, RFC6890 does permit the
inclusion of likely undesired addresses such as multicast, therefore our version
of "private" also filters out non-forwardable addresses.
Example:
- {{ GetPrivateInterfaces | include "flags" "up" }}
+ {{ GetPrivateInterfaces | sort "default" | join "address" " " }}
-`GetPublicInterfaces` - Returns a list of IfAddr that do not match RFC 6890, is
-attached to the default route, and whose interface is marked as up.
+`GetPublicInterfaces` - Returns a list of IfAddr structs whos IPs are
+forwardable, do not match RFC 6890, and whose interface is marked up.
Example:
- {{ GetPublicInterfaces | include "flags" "up" }}
+ {{ GetPublicInterfaces | sort "default" | join "name" " " }}
`GetPrivateIP` - Helper function that returns a string of the first IP address
@@ -80,6 +79,14 @@ Example:
{{ GetPrivateIP }}
+`GetPrivateIPs` - Helper function that returns a string of the all private IP
+addresses on the host.
+
+Example:
+
+ {{ GetPrivateIPs }}
+
+
`GetPublicIP` - Helper function that returns a string of the first IP from
GetPublicInterfaces.
@@ -87,12 +94,29 @@ Example:
{{ GetPublicIP }}
+`GetPublicIPs` - Helper function that returns a space-delimited string of the
+all public IP addresses on the host.
+
+Example:
+
+ {{ GetPrivateIPs }}
+
+
`GetInterfaceIP` - Helper function that returns a string of the first IP from
the named interface.
Example:
- {{ GetInterfaceIP }}
+ {{ GetInterfaceIP "en0" }}
+
+
+
+`GetInterfaceIPs` - Helper function that returns a space-delimited list of all
+IPs on a given interface.
+
+Example:
+
+ {{ GetInterfaceIPs "en0" }}
`sort` - Sorts the IfAddrs result based on its arguments. `sort` takes one
@@ -100,6 +124,8 @@ argument, a list of ways to sort its IfAddrs argument. The list of sort
criteria is comma separated (`,`):
- `address`, `+address`: Ascending sort of IfAddrs by Address
- `-address`: Descending sort of IfAddrs by Address
+ - `default`, `+default`: Ascending sort of IfAddrs, IfAddr with a default route first
+ - `-default`: Descending sort of IfAddrs, IfAttr with default route last
- `name`, `+name`: Ascending sort of IfAddrs by lexical ordering of interface name
- `-name`: Descending sort of IfAddrs by lexical ordering of interface name
- `port`, `+port`: Ascending sort of IfAddrs by port number
@@ -116,7 +142,7 @@ criteria is comma separated (`,`):
Example:
- {{ GetPrivateInterfaces | sort "type,size,address" }}
+ {{ GetPrivateInterfaces | sort "default,-type,size,+address" }}
`exclude` and `include`: Filters IfAddrs based on the selector criteria and its
@@ -142,7 +168,7 @@ available filtering criteria is:
Example:
- {{ GetPrivateInterfaces | exclude "type" "IPv6" | include "flag" "up|forwardable" }}
+ {{ GetPrivateInterfaces | exclude "type" "IPv6" }}
`unique`: Removes duplicate entries from the IfAddrs list, assuming the list has
@@ -152,14 +178,14 @@ already been sorted. `unique` only takes one argument:
Example:
- {{ GetPrivateInterfaces | sort "type,address" | unique "name" }}
+ {{ GetAllInterfaces | sort "default,-type,address" | unique "name" }}
`limit`: Reduces the size of the list to the specified value.
Example:
- {{ GetPrivateInterfaces | include "flags" "forwardable|up" | limit 1 }}
+ {{ GetPrivateInterfaces | limit 1 }}
`offset`: Seeks into the list by the specified value. A negative value can be
@@ -167,7 +193,33 @@ used to seek from the end of the list.
Example:
- {{ GetPrivateInterfaces | include "flags" "forwardable|up" | offset "-2" | limit 1 }}
+ {{ GetPrivateInterfaces | offset "-2" | limit 1 }}
+
+
+`math`: Perform a "math" operation on each member of the list and return new
+values. `math` takes two arguments, the attribute to operate on and the
+operation's value.
+
+Supported operations include:
+
+ - `address`: Adds the value, a positive or negative value expressed as a
+ decimal string, to the address. The sign is required. This value is
+ allowed to over or underflow networks (e.g. 127.255.255.255 `"address" "+1"`
+ will return "128.0.0.0"). Addresses will wrap at IPv4 or IPv6 boundaries.
+ - `network`: Add the value, a positive or negative value expressed as a
+ decimal string, to the network address. The sign is required. Positive
+ values are added to the network address. Negative values are subtracted
+ from the network's broadcast address (e.g. 127.0.0.1 `"network" "-1"` will
+ return "127.255.255.255"). Values that overflow the network size will
+ safely wrap.
+
+Example:
+
+ {{ GetPrivateInterfaces | include "type" "IP" | math "address" "+256" | attr "address" }}
+ {{ GetPrivateInterfaces | include "type" "IP" | math "address" "-256" | attr "address" }}
+ {{ GetPrivateInterfaces | include "type" "IP" | math "network" "+2" | attr "address" }}
+ {{ GetPrivateInterfaces | include "type" "IP" | math "network" "-2" | attr "address" }}
+ {{ GetPrivateInterfaces | include "flags" "forwardable|up" | include "type" "IPv4" | math "network" "+2" | attr "address" }}
`attr`: Extracts a single attribute of the first member of the list and returns
@@ -177,7 +229,19 @@ supported attributes.
Example:
- {{ GetPrivateInterfaces | include "flags" "forwardable|up" | attr "address" }}
+ {{ GetAllInterfaces | exclude "flags" "up" | attr "address" }}
+
+
+`Attr`: Extracts a single attribute from an `IfAttr` and in every other way
+performs the same as the `attr`.
+
+Example:
+
+ {{ with $ifAddrs := GetAllInterfaces | include "type" "IP" | sort "+type,+address" -}}
+ {{- range $ifAddrs -}}
+ {{- Attr "address" . }} -- {{ Attr "network" . }}/{{ Attr "size" . -}}
+ {{- end -}}
+ {{- end }}
`join`: Similar to `attr`, `join` extracts all matching attributes of the list
@@ -187,7 +251,7 @@ and returns them as a string joined by the separator, the second argument to
Example:
- {{ GetPrivateInterfaces | include "flags" "forwardable|up" | join "address" " " }}
+ {{ GetAllInterfaces | include "flags" "forwardable" | join "address" " " }}
`exclude` and `include` flags:
@@ -205,7 +269,7 @@ Example:
- `up`: Is the interface up?
-Attributes for `attr` and `join`:
+Attributes for `attr`, `Attr`, and `join`:
SockAddr Type:
- `string`
diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/template.go b/vendor/github.com/hashicorp/go-sockaddr/template/template.go
index ffe467b7f..bbed51361 100644
--- a/vendor/github.com/hashicorp/go-sockaddr/template/template.go
+++ b/vendor/github.com/hashicorp/go-sockaddr/template/template.go
@@ -64,23 +64,53 @@ func init() {
HelperFuncs = template.FuncMap{
// Misc functions that operate on IfAddrs inputs
- "attr": sockaddr.IfAttr,
+ "attr": Attr,
"join": sockaddr.JoinIfAddrs,
"limit": sockaddr.LimitIfAddrs,
"offset": sockaddr.OffsetIfAddrs,
"unique": sockaddr.UniqueIfAddrsBy,
+ // Misc math functions that operate on a single IfAddr input
+ "math": sockaddr.IfAddrsMath,
+
// Return a Private RFC 6890 IP address string that is attached
// to the default route and a forwardable address.
"GetPrivateIP": sockaddr.GetPrivateIP,
+ // Return all Private RFC 6890 IP addresses as a space-delimited string of
+ // IP addresses. Addresses returned do not have to be on the interface with
+ // a default route.
+ "GetPrivateIPs": sockaddr.GetPrivateIPs,
+
// Return a Public RFC 6890 IP address string that is attached
// to the default route and a forwardable address.
"GetPublicIP": sockaddr.GetPublicIP,
+ // Return allPublic RFC 6890 IP addresses as a space-delimited string of IP
+ // addresses. Addresses returned do not have to be on the interface with a
+ // default route.
+ "GetPublicIPs": sockaddr.GetPublicIPs,
+
// Return the first IP address of the named interface, sorted by
// the largest network size.
"GetInterfaceIP": sockaddr.GetInterfaceIP,
+
+ // Return all IP addresses on the named interface, sorted by the largest
+ // network size.
+ "GetInterfaceIPs": sockaddr.GetInterfaceIPs,
+ }
+}
+
+// Attr returns the attribute from the ifAddrRaw argument. If the argument is
+// an IfAddrs, only the first element will be evaluated for resolution.
+func Attr(selectorName string, ifAddrsRaw interface{}) (string, error) {
+ switch v := ifAddrsRaw.(type) {
+ case sockaddr.IfAddr:
+ return sockaddr.IfAttr(selectorName, v)
+ case sockaddr.IfAddrs:
+ return sockaddr.IfAttrs(selectorName, v)
+ default:
+ return "", fmt.Errorf("unable to obtain attribute %s from type %T (%v)", selectorName, ifAddrsRaw, ifAddrsRaw)
}
}
diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go b/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go
index 6f2b47828..ec9822e77 100644
--- a/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go
+++ b/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go
@@ -181,6 +181,81 @@ func TestSockAddr_Parse(t *testing.T) {
{{- end -}}`,
output: `true`,
},
+ {
+ name: "math address +",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+2" | sort "+type,+address" | join "address" " " }}`,
+ output: `127.0.0.3 ::3 fe80::3`,
+ },
+ {
+ name: "math address + overflow",
+ input: `|{{- with $ifAddrs := GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+16777217" | sort "+type,+address" -}}
+ {{- range $ifAddrs -}}
+ {{- attr "address" . }} -- {{ attr "network" . }}/{{ attr "size" . }}|{{ end -}}
+{{- end -}}`,
+ output: `|128.0.0.2 -- 128.0.0.0/16777216|::100:2 -- ::100:2/1|fe80::100:2 -- fe80::/18446744073709551616|`,
+ },
+ {
+ name: "math address + overflow+wrap",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+4294967294" | sort "+type,+address" | join "address" " " }}`,
+ output: `126.255.255.255 ::ffff:ffff fe80::ffff:ffff`,
+ },
+ {
+ name: "math address -",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-256" | sort "+type,+address" | join "address" " " }}`,
+ output: `126.255.255.1 fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ff01 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff01`,
+ },
+ {
+ name: "math address - underflow",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-4278190082" | sort "+type,+address" | join "address" " " }}`,
+ output: `127.255.255.255 fe7f:ffff:ffff:ffff:ffff:ffff:ff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ff:ffff`,
+ },
+ {
+ // Note to readers: lo0's link-local address (::1) address has a mask of
+ // /128 which means its value never changes and this is expected. lo0's
+ // site-local address has a /64 address and is expected to change.
+ name: "math network",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+2" | sort "+type,+address" | join "address" " " }}`,
+ output: `127.0.0.2 ::1 fe80::2`,
+ },
+ {
+ // Assume an IPv4 input of 127.0.0.1. With a value of 0xff00ff01, we wrap once on purpose.
+ name: "math network + wrap",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+4278255368" | sort "+type,+address" | join "address" " " }}`,
+ output: `127.0.255.8 ::1 fe80::ff00:ff08`,
+ },
+ {
+ name: "math network -",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "-2" | sort "+type,+address" | join "address" " " }}`,
+ output: `127.255.255.254 ::1 fe80::ffff:ffff:ffff:fffe`,
+ },
+ {
+ // Assume an IPv4 input of 127.0.0.1. With a value of 0xff000008 it
+ // should wrap and underflow by 8. Assume an IPv6 input of ::1. With a
+ // value of -0xff000008 the value underflows and wraps.
+ name: "math network - underflow+wrap",
+ input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | sort "+type,+address" | math "network" "-4278190088" | join "address" " " }}`,
+ output: `127.255.255.248 ::1 fe80::ffff:ffff:ff:fff8`,
+ },
+ {
+ // Assume the private IPs available on the host are: 10.1.2.3
+ // fe80::1025:f732:1001:203
+ name: "GetPrivateIPs",
+ input: `{{GetPrivateIPs}}`,
+ output: `10.1.2.3 fe80::1025:f732:1001:203`,
+ },
+ {
+ // Assume the public IPs available on the host are: 1.2.3.4 6.7.8.9
+ name: "GetPublicIPs",
+ input: `{{GetPublicIPs}}`,
+ output: `1.2.3.4 6.7.8.9`,
+ },
+ {
+ // Assume the private IPs on this host are just the IPv4 addresses:
+ // 10.1.2.3 and 172.16.4.6
+ name: "GetInterfaceIPs",
+ input: `{{GetInterfaceIPs "en0"}}`,
+ output: `10.1.2.3 and 172.16.4.6`,
+ },
}
for i, test := range tests {