-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/linksharing/sharing: add static dns client
This allows to adjust DNSClient to use a local file for testing purposes, avoiding the need to use public DNS servers. Change-Id: If687eb5417a3763b62b32a277f619f80384fe76e
- Loading branch information
Showing
5 changed files
with
239 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (C) 2024 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package sharing | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/miekg/dns" | ||
"github.com/zeebo/errs" | ||
) | ||
|
||
// StaticDNSClient provides static responses to dns queries. | ||
type StaticDNSClient struct { | ||
txt map[string]*TXTRecordSet | ||
cname map[string][]string | ||
} | ||
|
||
// ParseStaticDNSClientFromZoneFile parses zone file for txt records. | ||
func ParseStaticDNSClientFromZoneFile(data []byte) (*StaticDNSClient, error) { | ||
client := &StaticDNSClient{ | ||
txt: map[string]*TXTRecordSet{}, | ||
cname: map[string][]string{}, | ||
} | ||
|
||
zp := dns.NewZoneParser(bytes.NewReader(data), "", "") | ||
for { | ||
rr, ok := zp.Next() | ||
if !ok { | ||
break | ||
} | ||
|
||
switch rec := rr.(type) { | ||
case *dns.TXT: | ||
domain := rec.Hdr.Name | ||
ttl := time.Duration(rec.Hdr.Ttl) * time.Second | ||
|
||
set, ok := client.txt[domain] | ||
if !ok { | ||
set = NewTXTRecordSet() | ||
client.txt[domain] = set | ||
} | ||
|
||
for _, txt := range rec.Txt { | ||
set.Add(txt, ttl) | ||
} | ||
case *dns.CNAME: | ||
domain := rec.Hdr.Name | ||
client.cname[domain] = append(client.cname[domain], rec.Target) | ||
} | ||
} | ||
if err := zp.Err(); err != nil { | ||
return nil, errDNS.Wrap(err) | ||
} | ||
|
||
for _, set := range client.txt { | ||
set.Finalize() | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
// LookupTXTRecordSet fetches record set from the specified host. | ||
func (cli *StaticDNSClient) LookupTXTRecordSet(ctx context.Context, host string) (_ *TXTRecordSet, err error) { | ||
defer mon.Task()(&ctx)(&err) | ||
|
||
if !strings.HasSuffix(host, ".") { | ||
host += "." | ||
} | ||
|
||
set, ok := cli.txt[host] | ||
if !ok { | ||
return nil, errDNS.New("not found") | ||
} | ||
|
||
return set, nil | ||
} | ||
|
||
// ValidateCNAME checks host has a CNAME record with a value of one of the public URL bases. | ||
func (cli *StaticDNSClient) ValidateCNAME(ctx context.Context, host string, bases []*url.URL) (err error) { | ||
defer mon.Task()(&ctx)(&err) | ||
|
||
if !strings.HasSuffix(host, ".") { | ||
host += "." | ||
} | ||
|
||
for _, url := range bases { | ||
for _, target := range cli.cname[host] { | ||
if target == url.Host || target == url.Host+"." { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return errs.New("domain %q does not contain a CNAME with any public host", host) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (C) 2024 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package sharing | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"storj.io/common/testcontext" | ||
) | ||
|
||
func TestStaticDNSClient(t *testing.T) { | ||
ctx := testcontext.New(t) | ||
|
||
client, err := ParseStaticDNSClientFromZoneFile([]byte(strings.Join( | ||
[]string{ | ||
"downloads.example.com. IN CNAME link.storjshare.io.", | ||
"txt-downloads.example.com. IN TXT storj-root:files", | ||
"txt-downloads.example.com. IN TXT storj-access:ju5umq3nrhaf6xo6srpb4xvldglq", | ||
}, | ||
"\n", | ||
))) | ||
require.NoError(t, err) | ||
|
||
t.Run("ValidateCNAME", func(t *testing.T) { | ||
base, err := parseURLBase("http://link.storjshare.io") | ||
require.NoError(t, err) | ||
|
||
err = client.ValidateCNAME(ctx, "downloads.example.com", []*url.URL{base}) | ||
require.NoError(t, err) | ||
|
||
err = client.ValidateCNAME(ctx, "downloads.example.local", []*url.URL{base}) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("LookupTXTRecordSet", func(t *testing.T) { | ||
set, err := client.LookupTXTRecordSet(ctx, "txt-downloads.example.com") | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "files", set.Lookup("storj-root")) | ||
require.Equal(t, "ju5umq3nrhaf6xo6srpb4xvldglq", set.Lookup("storj-access")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters