Skip to content

Commit

Permalink
feat: Add IP lookup by interface to template funcs (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
abtreece authored Sep 28, 2021
1 parent fc07d09 commit 71ac3ef
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions integration/confdir/templates/basic.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ host={{ getv "/database/host" }}
password={{ getv "/database/password" }}
port={{ getv "/database/port" }}
username={{ getv "/database/username" }}

AccessAddress: {{ lookupIfaceIPV4 "lo" }}
2 changes: 2 additions & 0 deletions integration/expect/basic.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ host=127.0.0.1
password=p@sSw0rd
port=3306
username=confd

AccessAddress: 127.0.0.1
2 changes: 2 additions & 0 deletions integration/vault-v1/confdir/templates/basic.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ host={{ getv "/kv-v1/database/host" }}
password={{ getv "/kv-v1/database/password" }}
port={{ getv "/kv-v1/database/port" }}
username={{ getv "/kv-v1/database/username" }}

AccessAddress: {{ lookupIfaceIPV4 "lo" }}
2 changes: 2 additions & 0 deletions integration/vault-v2/confdir/templates/basic.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ host={{ getv "/kv-v2/data/database/host" }}
password={{ getv "/kv-v2/data/database/password" }}
port={{ getv "/kv-v2/data/database/port" }}
username={{ getv "/kv-v2/data/database/username" }}

AccessAddress: {{ lookupIfaceIPV4 "lo" }}
48 changes: 48 additions & 0 deletions resource/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func newFuncMap() map[string]interface{} {
m["lookupIPV4"] = LookupIPV4
m["lookupIPV6"] = LookupIPV6
m["lookupSRV"] = LookupSRV
m["lookupIfaceIPV4"] = LookupIfaceIPV4
m["lookupIfaceIPV6"] = LookupIfaceIPV6
m["fileExists"] = util.IsFileExist
m["base64Encode"] = Base64Encode
m["base64Decode"] = Base64Decode
Expand Down Expand Up @@ -210,6 +212,52 @@ func LookupIPV4(data string) []string {
return addresses
}

func LookupIfaceIPV4(data string) (addr string) {
var (
ief *net.Interface
addrs []net.Addr
ipv4Addr net.IP
)
ief, err := net.InterfaceByName(data)
if err != nil {
return
}
addrs, err = ief.Addrs()
if err != nil { // get addresses
return
}
for _, addr := range addrs { // get ipv4 address
ipv4Addr = addr.(*net.IPNet).IP.To4()
if ipv4Addr != nil {
break
}
}
return ipv4Addr.String()
}

func LookupIfaceIPV6(data string) (addr string) {
var (
ief *net.Interface
addrs []net.Addr
ipv6Addr net.IP
)
ief, err := net.InterfaceByName(data)
if err != nil {
return
}
addrs, err = ief.Addrs()
if err != nil { // get addresses
return
}
for _, addr := range addrs { // get ipv6 address
ipv6Addr = addr.(*net.IPNet).IP.To16()
if ipv6Addr != nil {
break
}
}
return ipv6Addr.String()
}

type sortSRV []*net.SRV

func (s sortSRV) Len() int {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

const Version = "0.18.1"
const Version = "0.18.2"

// We want to replace this variable at build time with "-ldflags -X main.GitSHA=xxx", where const is not supported.
var GitSHA = ""

0 comments on commit 71ac3ef

Please sign in to comment.