From 1927285a691c3df52bfe56a3c846555297c1bd5c Mon Sep 17 00:00:00 2001 From: Hereward Cooper Date: Sun, 5 Jun 2022 16:22:35 -0700 Subject: [PATCH] New `getHostsByAddr` function for rDNS lookups --- docs/index.md | 2 +- docs/network.md | 10 +++++++++- functions.go | 28 +++++++++++++++------------- network.go | 5 +++++ network_test.go | 8 ++++++++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/docs/index.md b/docs/index.md index 7e898fa5..2a8b95a1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -21,4 +21,4 @@ The Sprig library provides over 70 template functions for Go's template language - [Version Comparison Functions](semver.md): `semver`, `semverCompare` - [Reflection](reflection.md): `typeOf`, `kindIs`, `typeIsLike`, etc. - [Cryptographic and Security Functions](crypto.md): `derivePassword`, `sha256sum`, `genPrivateKey`, etc. - - [Network](network.md): `getHostByName` + - [Network](network.md): `getHostByName`, `getHostByAddr` diff --git a/docs/network.md b/docs/network.md index ea6cfd7f..2317d6a2 100644 --- a/docs/network.md +++ b/docs/network.md @@ -4,8 +4,16 @@ Sprig network manipulation functions. ## getHostByName -The `getHostByName` receives a domain name and returns the ip address. +The `getHostByName` receives a domain name, performs a forward DNS lookup using the local resolver, and returns an ip address. If multiple addresses are returned, one is picked at random. ``` getHostByName "www.google.com" would return the corresponding ip address of www.google.com ``` + +## getHostByAddr + +The `getHostByAddr` receives an IP address (as a string), performs a reverse DNS lookup using the local resolver, and returns a hostname. Note the response will be fully-qualified (i.e. includes a final `.`). + +``` +getHostByAddr "8.8.8.8" would return `dns.google.com.`. +``` \ No newline at end of file diff --git a/functions.go b/functions.go index 57fcec1d..b830cb3f 100644 --- a/functions.go +++ b/functions.go @@ -92,6 +92,7 @@ var nonhermeticFunctions = []string{ // Network "getHostByName", + "getHostByAddr", } var genericMap = map[string]interface{}{ @@ -269,6 +270,7 @@ var genericMap = map[string]interface{}{ // Network: "getHostByName": getHostByName, + "getHostByAddr": getHostByAddr, // Paths: "base": path.Base, @@ -336,20 +338,20 @@ var genericMap = map[string]interface{}{ "mustChunk": mustChunk, // Crypto: - "bcrypt": bcrypt, - "htpasswd": htpasswd, - "genPrivateKey": generatePrivateKey, - "derivePassword": derivePassword, - "buildCustomCert": buildCustomCertificate, - "genCA": generateCertificateAuthority, - "genCAWithKey": generateCertificateAuthorityWithPEMKey, - "genSelfSignedCert": generateSelfSignedCertificate, + "bcrypt": bcrypt, + "htpasswd": htpasswd, + "genPrivateKey": generatePrivateKey, + "derivePassword": derivePassword, + "buildCustomCert": buildCustomCertificate, + "genCA": generateCertificateAuthority, + "genCAWithKey": generateCertificateAuthorityWithPEMKey, + "genSelfSignedCert": generateSelfSignedCertificate, "genSelfSignedCertWithKey": generateSelfSignedCertificateWithPEMKey, - "genSignedCert": generateSignedCertificate, - "genSignedCertWithKey": generateSignedCertificateWithPEMKey, - "encryptAES": encryptAES, - "decryptAES": decryptAES, - "randBytes": randBytes, + "genSignedCert": generateSignedCertificate, + "genSignedCertWithKey": generateSignedCertificateWithPEMKey, + "encryptAES": encryptAES, + "decryptAES": decryptAES, + "randBytes": randBytes, // UUIDs: "uuidv4": uuidv4, diff --git a/network.go b/network.go index 108d78a9..26fe1b1f 100644 --- a/network.go +++ b/network.go @@ -10,3 +10,8 @@ func getHostByName(name string) string { //TODO: add error handing when release v3 comes out return addrs[rand.Intn(len(addrs))] } + +func getHostByAddr(addr string) string { + hosts, _ := net.LookupAddr(addr) + return hosts[0] +} diff --git a/network_test.go b/network_test.go index 9c153f0a..8a34bce0 100644 --- a/network_test.go +++ b/network_test.go @@ -16,3 +16,11 @@ func TestGetHostByName(t *testing.T) { assert.NotNil(t, ip) assert.NotEmpty(t, ip) } + +func TestGetHostByAddr(t *testing.T) { + tpl := `{{"1.1.1.1" | getHostByAddr}}` + + resolvedHost, _ := runRaw(tpl, nil) + + assert.Equal(t, resolvedHost, "one.one.one.one.") +}