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 <lidel@lidel.org>
Marcin Rataj committed
Aug 6, 2020 at 14:00 UTC
2ff6f1a80d6a070465e8c157a750ecf17687642b
2 files changed
+37
-21
core/corehttp/hostname.go
+9
-11
@@ -233,21 +233,19 @@ type wildcardHost struct {
233
func prepareKnownGateways(publicGateways map[string]*config.GatewaySpec) gatewayHosts {
234
var hosts gatewayHosts
235
236
- if len(publicGateways) == 0 {
237
- hosts.exact = make(
238
- map[string]*config.GatewaySpec,
239
- len(defaultKnownGateways),
240
- )
241
- for hostname, gw := range defaultKnownGateways {
242
- hosts.exact[hostname] = gw
243
- }
244
- return hosts
245
- }
236
+ hosts.exact = make(map[string]*config.GatewaySpec, len(publicGateways)+len(defaultKnownGateways))
237
247
- hosts.exact = make(map[string]*config.GatewaySpec, len(publicGateways))
238
+ // First, implicit defaults such as subdomain gateway on localhost
239
+ for hostname, gw := range defaultKnownGateways {
240
+ hosts.exact[hostname] = gw
241
+ }
242
243
+ // Then apply values from Gateway.PublicGateways, if present in the config
244
for hostname, gw := range publicGateways {
245
if gw == nil {
246
+ // Remove any implicit defaults, if present. This is useful when one
247
+ // wants to disable subdomain gateway on localhost etc.
248
+ delete(hosts.exact, hostname)
249
continue
250
}
251
if strings.Contains(hostname, "*") {
test/sharness/t0114-gateway-subdomains.sh
+28
-10
@@ -534,18 +534,18 @@ test_hostname_gateway_response_should_contain \
534
## https://github.com/ipfs/go-ipfs/issues/7318
535
## ============================================================================
536
537
-# TODO: replace with cidv1
537
# ed25519 fits under 63 char limit when represented in base36
539
-CIDv1_ED25519_RAW="12D3KooWP3ggTJV8LGckDHc4bVyXGhEWuBskoFyE6Rn2BJBqJtpa"
540
-CIDv1_ED25519_DNSSAFE="k51qzi5uqu5dl2yn0d6xu8q5aqa61jh8zeyixz9tsju80n15ssiyew48912c63"
538
+IPNS_KEY="test_key_ed25519"
539
+IPNS_ED25519_B58MH=$(ipfs key list -l -f b58mh | grep $IPNS_KEY | cut -d " " -f1 | tr -d "\n")
540
+IPNS_ED25519_B36CID=$(ipfs key list -l -f b36cid | grep $IPNS_KEY | cut -d " " -f1 | tr -d "\n")
541
# sha512 will be over 63char limit, even when represented in Base36
542
CIDv1_TOO_LONG=$(echo $CID_VAL | ipfs add --cid-version 1 --hash sha2-512 -Q)
543
544
# local: *.localhost
545
test_localhost_gateway_response_should_contain \
546
- "request for a ED25519 CID at localhost/ipfs/{CIDv1} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \
547
- "http://localhost:$GWAY_PORT/ipns/$CIDv1_ED25519_RAW" \
548
- "Location: http://${CIDv1_ED25519_DNSSAFE}.ipns.localhost:$GWAY_PORT/"
546
+ "request for a ED25519 libp2p-key at localhost/ipns/{b58mh} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \
547
+ "http://localhost:$GWAY_PORT/ipns/$IPNS_ED25519_B58MH" \
548
+ "Location: http://${IPNS_ED25519_B36CID}.ipns.localhost:$GWAY_PORT/"
549
550
# router should not redirect to hostnames that could fail due to DNS limits
551
test_localhost_gateway_response_should_contain \
@@ -567,10 +567,10 @@ test_localhost_gateway_response_should_contain \
567
# public subdomain gateway: *.example.com
568
569
test_hostname_gateway_response_should_contain \
570
- "request for a ED25519 CID at example.com/ipfs/{CIDv1} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \
570
+ "request for a ED25519 libp2p-key at example.com/ipns/{b58mh} returns Location HTTP header for DNS-safe subdomain redirect in browsers" \
571
"example.com" \
572
- "http://127.0.0.1:$GWAY_PORT/ipns/$CIDv1_ED25519_RAW" \
573
- "Location: http://${CIDv1_ED25519_DNSSAFE}.ipns.example.com"
572
+ "http://127.0.0.1:$GWAY_PORT/ipns/$IPNS_ED25519_B58MH" \
573
+ "Location: http://${IPNS_ED25519_B36CID}.ipns.example.com"
574
575
test_hostname_gateway_response_should_contain \
576
"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 \
621
## Test path-based requests with a custom hostname config
622
## ============================================================================
623
624
-# set explicit subdomain gateway config for the hostname
624
+# set explicit no-subdomain gateway config for the hostname
625
ipfs config --json Gateway.PublicGateways '{
626
"example.com": {
627
"UseSubdomains": false,
@@ -904,6 +904,24 @@ test_hostname_gateway_response_should_contain \
904
"http://127.0.0.1:$GWAY_PORT/" \
905
"$CID_VAL"
906
907
+## ============================================================================
908
+## Test support for overriding implicit defaults
909
+## ============================================================================
910
+
911
+# disable subdomain gateway at localhost by removing implicit config
912
+ipfs config --json Gateway.PublicGateways '{
913
+ "localhost": null
914
+}' || exit 1
915
+
916
+# restart daemon to apply config changes
917
+test_kill_ipfs_daemon
918
+test_launch_ipfs_daemon --offline
919
+
920
+test_localhost_gateway_response_should_contain \
921
+ "request for localhost/ipfs/{CID} stays on path when subdomain gw is explicitly disabled" \
922
+ "http://localhost:$GWAY_PORT/ipfs/$CIDv1" \
923
+ "$CID_VAL"
924
+
925
# =============================================================================
926
# ensure we end with empty Gateway.PublicGateways
927
ipfs config --json Gateway.PublicGateways '{}'