From 2ff6f1a80d6a070465e8c157a750ecf17687642b Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 6 Aug 2020 14:00:16 +0200 Subject: [PATCH] test(gateway): IPNS cleanup and implicit defaults fix This ensures implicit defaults are always present, even when Gateway.PublicGateways is defined in the config. User still can disable them, but needs to do it per hostname. License: MIT Signed-off-by: Marcin Rataj --- core/corehttp/hostname.go | 20 ++++++------ test/sharness/t0114-gateway-subdomains.sh | 38 +++++++++++++++++------ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/core/corehttp/hostname.go b/core/corehttp/hostname.go index 04f640e2fa4..e94be2c82df 100644 --- a/core/corehttp/hostname.go +++ b/core/corehttp/hostname.go @@ -233,21 +233,19 @@ type wildcardHost struct { func prepareKnownGateways(publicGateways map[string]*config.GatewaySpec) gatewayHosts { var hosts gatewayHosts - if len(publicGateways) == 0 { - hosts.exact = make( - map[string]*config.GatewaySpec, - len(defaultKnownGateways), - ) - for hostname, gw := range defaultKnownGateways { - hosts.exact[hostname] = gw - } - return hosts - } + hosts.exact = make(map[string]*config.GatewaySpec, len(publicGateways)+len(defaultKnownGateways)) - hosts.exact = make(map[string]*config.GatewaySpec, len(publicGateways)) + // First, implicit defaults such as subdomain gateway on localhost + for hostname, gw := range defaultKnownGateways { + hosts.exact[hostname] = gw + } + // Then apply values from Gateway.PublicGateways, if present in the config for hostname, gw := range publicGateways { if gw == nil { + // Remove any implicit defaults, if present. This is useful when one + // wants to disable subdomain gateway on localhost etc. + delete(hosts.exact, hostname) continue } if strings.Contains(hostname, "*") { diff --git a/test/sharness/t0114-gateway-subdomains.sh b/test/sharness/t0114-gateway-subdomains.sh index 5c9d459e681..eb82a68b346 100755 --- a/test/sharness/t0114-gateway-subdomains.sh +++ b/test/sharness/t0114-gateway-subdomains.sh @@ -534,18 +534,18 @@ test_hostname_gateway_response_should_contain \ ## https://github.com/ipfs/go-ipfs/issues/7318 ## ============================================================================ -# TODO: replace with cidv1 # ed25519 fits under 63 char limit when represented in base36 -CIDv1_ED25519_RAW="12D3KooWP3ggTJV8LGckDHc4bVyXGhEWuBskoFyE6Rn2BJBqJtpa" -CIDv1_ED25519_DNSSAFE="k51qzi5uqu5dl2yn0d6xu8q5aqa61jh8zeyixz9tsju80n15ssiyew48912c63" +IPNS_KEY="test_key_ed25519" +IPNS_ED25519_B58MH=$(ipfs key list -l -f b58mh | grep $IPNS_KEY | cut -d " " -f1 | tr -d "\n") +IPNS_ED25519_B36CID=$(ipfs key list -l -f b36cid | grep $IPNS_KEY | cut -d " " -f1 | tr -d "\n") # sha512 will be over 63char limit, even when represented in Base36 CIDv1_TOO_LONG=$(echo $CID_VAL | ipfs add --cid-version 1 --hash sha2-512 -Q) # local: *.localhost test_localhost_gateway_response_should_contain \ - "request for a ED25519 CID at localhost/ipfs/{CIDv1} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \ - "http://localhost:$GWAY_PORT/ipns/$CIDv1_ED25519_RAW" \ - "Location: http://${CIDv1_ED25519_DNSSAFE}.ipns.localhost:$GWAY_PORT/" + "request for a ED25519 libp2p-key at localhost/ipns/{b58mh} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \ + "http://localhost:$GWAY_PORT/ipns/$IPNS_ED25519_B58MH" \ + "Location: http://${IPNS_ED25519_B36CID}.ipns.localhost:$GWAY_PORT/" # router should not redirect to hostnames that could fail due to DNS limits test_localhost_gateway_response_should_contain \ @@ -567,10 +567,10 @@ test_localhost_gateway_response_should_contain \ # public subdomain gateway: *.example.com test_hostname_gateway_response_should_contain \ - "request for a ED25519 CID at example.com/ipfs/{CIDv1} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \ + "request for a ED25519 libp2p-key at example.com/ipns/{b58mh} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \ "example.com" \ - "http://127.0.0.1:$GWAY_PORT/ipns/$CIDv1_ED25519_RAW" \ - "Location: http://${CIDv1_ED25519_DNSSAFE}.ipns.example.com" + "http://127.0.0.1:$GWAY_PORT/ipns/$IPNS_ED25519_B58MH" \ + "Location: http://${IPNS_ED25519_B36CID}.ipns.example.com" test_hostname_gateway_response_should_contain \ "request for a too long CID at example.com/ipfs/{CIDv1} returns human readable error" \ @@ -621,7 +621,7 @@ test_hostname_gateway_response_should_contain \ ## Test path-based requests with a custom hostname config ## ============================================================================ -# set explicit subdomain gateway config for the hostname +# set explicit no-subdomain gateway config for the hostname ipfs config --json Gateway.PublicGateways '{ "example.com": { "UseSubdomains": false, @@ -904,6 +904,24 @@ test_hostname_gateway_response_should_contain \ "http://127.0.0.1:$GWAY_PORT/" \ "$CID_VAL" +## ============================================================================ +## Test support for overriding implicit defaults +## ============================================================================ + +# disable subdomain gateway at localhost by removing implicit config +ipfs config --json Gateway.PublicGateways '{ + "localhost": null +}' || exit 1 + +# restart daemon to apply config changes +test_kill_ipfs_daemon +test_launch_ipfs_daemon --offline + +test_localhost_gateway_response_should_contain \ + "request for localhost/ipfs/{CID} stays on path when subdomain gw is explicitly disabled" \ + "http://localhost:$GWAY_PORT/ipfs/$CIDv1" \ + "$CID_VAL" + # ============================================================================= # ensure we end with empty Gateway.PublicGateways ipfs config --json Gateway.PublicGateways '{}'