Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New getHostsByAddr function for rDNS lookups #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
10 changes: 9 additions & 1 deletion docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.`.
```
28 changes: 15 additions & 13 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var nonhermeticFunctions = []string{

// Network
"getHostByName",
"getHostByAddr",
}

var genericMap = map[string]interface{}{
Expand Down Expand Up @@ -269,6 +270,7 @@ var genericMap = map[string]interface{}{

// Network:
"getHostByName": getHostByName,
"getHostByAddr": getHostByAddr,

// Paths:
"base": path.Base,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
8 changes: 8 additions & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}