Skip to content

Commit

Permalink
🐛 Fix dns.fqdn when connection via IP (#2090)
Browse files Browse the repository at this point in the history
Fixes #1958

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Oct 9, 2023
1 parent 6ef8773 commit d85c10c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
12 changes: 11 additions & 1 deletion providers/network/resources/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"net"
"strconv"
"strings"

Expand Down Expand Up @@ -58,7 +59,16 @@ func initDns(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]
_, ok := args["fqdn"]
if !ok {
conn := runtime.Connection.(*connection.HostConnection)
args["fqdn"] = llx.StringData(conn.FQDN())
fqdn := llx.StringData(conn.FQDN())

// Check whether the fqdn is valid
// In case of ssh connections, this could also be an ip address
ip := net.ParseIP(fqdn.Value.(string))
if ip == nil {
args["fqdn"] = fqdn
} else {
args["fqdn"] = llx.StringData("")
}
}

return args, nil, nil
Expand Down
47 changes: 47 additions & 0 deletions providers/network/resources/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/llx"

Check failure on line 11 in providers/network/resources/dns_test.go

View workflow job for this annotation

GitHub Actions / go-test

no required module provides package go.mondoo.com/cnquery/llx; to add it:

Check failure on line 11 in providers/network/resources/dns_test.go

View workflow job for this annotation

GitHub Actions / go-test

no required module provides package go.mondoo.com/cnquery/llx; to add it:
"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/network/connection"
"go.mondoo.com/cnquery/providers/network/resources"
)

func TestResource_DNS(t *testing.T) {
Expand All @@ -20,3 +26,44 @@ func TestResource_DomainName(t *testing.T) {
res = x.TestQuery(t, "domainName(\"mondoo.com\").tld")
assert.Equal(t, "com", string(res[0].Result().Data.Value))
}

func TestResource_DnsFqdn(t *testing.T) {
testCases := []struct {
hostName string
expectedId string
}{
{
hostName: "127.0.0.1",
expectedId: "dns/",
},
{
hostName: "3.127.139.132",
expectedId: "dns/",
},
{
hostName: "www.mondoo.com",
expectedId: "dns/www.mondoo.com",
},
{
hostName: "ec2-3-127-139-132.eu-central-1.compute.amazonaws.com",
expectedId: "dns/ec2-3-127-139-132.eu-central-1.compute.amazonaws.com",
},
}

runtime := &plugin.Runtime{}

for _, tc := range testCases {
conf := &inventory.Config{
Host: tc.hostName,
}
runtime.Connection = connection.NewHostConnection(1, nil, conf)

dns, err := resources.NewResource(
runtime,
"dns",
map[string]*llx.RawData{},
)
require.NoError(t, err)
require.Equal(t, tc.expectedId, dns.MqlID())
}
}

0 comments on commit d85c10c

Please sign in to comment.