Skip to content

Commit

Permalink
refactor: remove subdomains from dnslink fixtures
Browse files Browse the repository at this point in the history
these concepts should not mix, and dnslinks should be testable
independent of --subdomain-url

i also added more useful export format for them, compatible with
IPNS_NS_MAP used by Boxo/Kubo/Rainbow
  • Loading branch information
lidel committed May 26, 2024
1 parent 822698a commit 89e2a27
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
51 changes: 24 additions & 27 deletions cmd/gateway-conformance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func main() {
},
&cli.BoolFlag{
Name: "merged",
Usage: "Merge the fixtures into a single CAR file",
Usage: "Merge the CAR fixtures into a single CAR file",
Value: false,
Destination: &merged,
},
Expand All @@ -256,44 +256,41 @@ func main() {
return err
}

// IPNS Records
err = copyFiles(fxs.IPNSRecords, directory)
if err != nil {
return err
}

// DNSLink fixtures as YAML, JSON, and IPNS_NS_MAP env variable
err = copyFiles(fxs.ConfigFiles, directory)
if err != nil {
return err
}
err = dnslink.MergeJSON(fxs.ConfigFiles, filepath.Join(directory, "dnslinks.json"))
if err != nil {
return err
}
err = dnslink.MergeNsMapEnv(fxs.ConfigFiles, filepath.Join(directory, "dnslinks.IPFS_NS_MAP"))
if err != nil {
return err
}

merged := cCtx.Bool("merged")
if merged {
// All .car fixtures merged into a single .car file
err = car.Merge(fxs.CarFiles, filepath.Join(directory, "fixtures.car"))
if err != nil {
return err
}

err := dnslink.Merge(fxs.ConfigFiles, filepath.Join(directory, "dnslinks.json"))
if err != nil {
return err
}

// TODO: when https://github.com/ipfs/specs/issues/369 has been completed,
// merge the IPNS records into a car file.
err = copyFiles(fxs.IPNSRecords, directory)
if err != nil {
return err
}
// implement merge support to include the IPNS records in the car file.
} else {
// Copy .car fixtures as -is
err = copyFiles(fxs.CarFiles, directory)
if err != nil {
return err
}

err = copyFiles(fxs.ConfigFiles, directory)
if err != nil {
return err
}

err = copyFiles(fxs.IPNSRecords, directory)
if err != nil {
return err
}

err := dnslink.Merge(fxs.ConfigFiles, filepath.Join(directory, "dnslinks.json"))
if err != nil {
return err
}
}

return nil
Expand Down
17 changes: 5 additions & 12 deletions tooling/dnslink/dnslink.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ type ConfigFixture struct {
}

type DNSLink struct {
Subdomain string `yaml:"subdomain"`
Domain string `yaml:"domain"`
Path string `yaml:"path"`
Domain string `yaml:"domain"`
Path string `yaml:"path"`
}

func InlineDNS(s string) string {
Expand Down Expand Up @@ -57,19 +56,13 @@ func (d *ConfigFixture) MustGet(id string) string {
if !ok {
panic(fmt.Errorf("dnslink %s not found", id))
}
if dnsLink.Domain != "" && dnsLink.Subdomain != "" {
panic(fmt.Errorf("dnslink %s has both domain and subdomain", id))
}
if dnsLink.Domain == "" && dnsLink.Subdomain == "" {
panic(fmt.Errorf("dnslink %s has neither domain nor subdomain", id))
if dnsLink.Domain == "" {
panic(fmt.Errorf("dnslink %s has no domain", id))
}
if dnsLink.Path == "" {
panic(fmt.Errorf("dnslink %s has no path", id))
}

if dnsLink.Domain != "" {
return dnsLink.Domain
}
return dnsLink.Domain

return dnsLink.Subdomain
}
51 changes: 27 additions & 24 deletions tooling/dnslink/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
)

type DNSLinksAggregate struct {
Domains map[string]string `json:"domains"`
Subdomains map[string]string `json:"subdomains"`
Domains map[string]string `json:"domains"`
}

func Aggregate(inputPaths []string) (*DNSLinksAggregate, error) {
agg := DNSLinksAggregate{
Domains: make(map[string]string),
Subdomains: make(map[string]string),
Domains: make(map[string]string),
}

for _, file := range inputPaths {
Expand All @@ -24,34 +23,19 @@ func Aggregate(inputPaths []string) (*DNSLinksAggregate, error) {
}

for _, link := range dnsLinks.DNSLinks {
if link.Domain != "" && link.Subdomain != "" {
return nil, fmt.Errorf("dnslink %s has both domain and subdomain", link.Subdomain)
if _, ok := agg.Domains[link.Domain]; ok {
return nil, fmt.Errorf("collision detected for domain %s", link.Domain)
}

if link.Domain != "" {
if _, ok := agg.Domains[link.Domain]; ok {
return nil, fmt.Errorf("collision detected for domain %s", link.Domain)
}

agg.Domains[link.Domain] = link.Path
continue
}

if link.Subdomain != "" {
if _, ok := agg.Subdomains[link.Subdomain]; ok {
return nil, fmt.Errorf("collision detected for subdomain %s", link.Subdomain)
}

agg.Subdomains[link.Subdomain] = link.Path
continue
}
agg.Domains[link.Domain] = link.Path
continue
}
}

return &agg, nil
}

func Merge(inputPaths []string, outputPath string) error {
func MergeJSON(inputPaths []string, outputPath string) error {
kvs, err := Aggregate(inputPaths)
if err != nil {
return err
Expand All @@ -65,3 +49,22 @@ func Merge(inputPaths []string, outputPath string) error {
err = os.WriteFile(outputPath, j, 0644)
return err
}

// MergeEnv produces a string compatible with IPFS_NS_MAP env veriable syntax
// which can be used by tools to pre-populate namesys (IPNS, DNSLink) resolution
// results to facilitate tests based on static fixtures.
func MergeNsMapEnv(inputPaths []string, outputPath string) error {
kvs, err := Aggregate(inputPaths)
if err != nil {
return err
}

var result []string
for key, value := range kvs.Domains {
result = append(result, fmt.Sprintf("%s:%s", key, value))
}
nsMapValue := strings.Join(result, ",")

err = os.WriteFile(outputPath, []byte(nsMapValue), 0644)
return err
}

0 comments on commit 89e2a27

Please sign in to comment.