Skip to content

How do you create a new MemoryCache and populate it with an embedded copy of the dns.json file to prevent downloading the file? #54

Description

@atc0005

My goal is to add RDAP query support to a monitoring plugin which will use this library to query the expiration status of a domain. Prototyping indicates that this library does the job well (thank you for providing & maintaining it).

While the check for a single domain won't be run often (e.g., once a day), there may be many executions of the monitoring plugin to cover all domains that are being monitored.

For my use case I wish to embed a copy of the dns.json file at build time and load it into a new MemoryCache. I'd then like to specify that MemoryCache when configuring a client to perform the query. I thought I did that, but the attempt does not appear to be working.

I downloaded a copy of the dns.json file to the same path as a Go file containing this content:

package main

import (
	_ "embed"
	"fmt"
	"log"

	"github.com/openrdap/rdap"
	"github.com/openrdap/rdap/bootstrap"
	"github.com/openrdap/rdap/bootstrap/cache"
)

//go:embed dns.json
var bootstrapRegistryFile []byte

func debug(msg string) {
	fmt.Printf("DEBUG: %s\n", msg)
}

func main() {
	bootstrapCache := cache.NewMemoryCache()
	bootstrapCache.Save("dns.json", bootstrapRegistryFile)

	client := &rdap.Client{
		Bootstrap: &bootstrap.Client{
			Cache: bootstrapCache,
		},

		Verbose: debug,
	}

	req := &rdap.Request{
		Type:  rdap.DomainRequest,
		Query: "example.com",
	}

	resp, err := client.Do(req)
	if err != nil {
		log.Fatalf("failed to request RDAP data: %v", err)
	}

	_ = resp

	// ...
}

The output that I'm seeing:

$ go run ./cmd/bugreport/
DEBUG:
DEBUG: client: Running...
DEBUG: client: Request type  : domain
DEBUG: client: Request query : example.com
DEBUG: client: Request URL   : TBD, bootstrap required
DEBUG:   bootstrap: Looking up...
DEBUG:   bootstrap: Question type : dns
DEBUG:   bootstrap: Question query: example.com
DEBUG:   bootstrap: Cache state: dns.json: good
DEBUG:   bootstrap: Downloading dns.json
DEBUG:   bootstrap: Looked up 'example.com'
DEBUG:   bootstrap: Matching entry 'com'
DEBUG:   bootstrap: Service URL #1: 'https://rdap.verisign.com/com/v1/'
DEBUG: client: RDAP URL #0 is https://rdap.verisign.com/com/v1/domain/example.com
DEBUG: client: GET https://rdap.verisign.com/com/v1/domain/example.com
DEBUG: client: status-code=200, content-type=application/rdap+json, length=2440 bytes, duration=235.0072ms
DEBUG: client: Successfully decoded response

For these two lines:

DEBUG:   bootstrap: Cache state: dns.json: good
DEBUG:   bootstrap: Downloading dns.json

The first line is expected: I've just freshly loaded the embedded file into the memory cache.

The second line is not expected: Why isn't the cached copy of the file being used?

This looks to be the relevant portion of the code:

rdap/bootstrap/client.go

Lines 324 to 333 in c9246cb

if c.registries[registry] == nil || forceDownload {
c.Verbose(fmt.Sprintf(" bootstrap: Downloading %s", registry.Filename()))
err := c.DownloadWithContext(question.Context(), registry)
if err != nil {
return nil, err
}
} else {
c.Verbose(" bootstrap: Using cached Service Registry file")
}

I'm struggling to find a way to directly set c.registries[registry] or indirectly do so via exposed functions.

Am I overlooking something obvious?

Thanks in advance for your help.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions