Skip to content

Commit

Permalink
feat: use new gateway api from go-libipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 31, 2023
1 parent 8d3b315 commit 6e8f94d
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 174 deletions.
19 changes: 6 additions & 13 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const (
routingOptionAutoKwd = "auto"
unencryptTransportKwd = "disable-transport-encryption"
unrestrictedAPIAccessKwd = "unrestricted-api"
writableKwd = "writable"
enablePubSubKwd = "enable-pubsub-experiment"
enableIPNSPubSubKwd = "enable-namesys-pubsub"
enableMultiplexKwd = "enable-mplex-experiment"
Expand Down Expand Up @@ -162,7 +161,6 @@ Headers.
cmds.StringOption(initProfileOptionKwd, "Configuration profiles to apply for --init. See ipfs init --help for more"),
cmds.StringOption(routingOptionKwd, "Overrides the routing option").WithDefault(routingOptionDefaultKwd),
cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem using FUSE (experimental)"),
cmds.BoolOption(writableKwd, "Enable writing objects (with POST, PUT and DELETE)"),
cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount). Defaults to config setting."),
cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount). Defaults to config setting."),
cmds.BoolOption(unrestrictedAPIAccessKwd, "Allow API access to unlisted hashes"),
Expand Down Expand Up @@ -683,9 +681,9 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
// only the webui objects are allowed.
// if you know what you're doing, go ahead and pass --unrestricted-api.
unrestricted, _ := req.Options[unrestrictedAPIAccessKwd].(bool)
gatewayOpt := corehttp.GatewayOption(false, corehttp.WebUIPaths...)
gatewayOpt := corehttp.GatewayOption(corehttp.WebUIPaths...)
if unrestricted {
gatewayOpt = corehttp.GatewayOption(true, "/ipfs", "/ipns")
gatewayOpt = corehttp.GatewayOption("/ipfs", "/ipns")
}

var opts = []corehttp.ServeOption{
Expand Down Expand Up @@ -789,9 +787,8 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
return nil, fmt.Errorf("serveHTTPGateway: GetConfig() failed: %s", err)
}

writable, writableOptionFound := req.Options[writableKwd].(bool)
if !writableOptionFound {
writable = cfg.Gateway.Writable
if cfg.Gateway.Writable {
return nil, fmt.Errorf("serveHTTPGateway: writable gateways are no longer supported, please remove .Gateway.Writable from your configuration file")
}

listeners, err := sockets.TakeListeners("io.ipfs.gateway")
Expand Down Expand Up @@ -824,13 +821,9 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
}

// we might have listened to /tcp/0 - let's see what we are listing on
gwType := "readonly"
if writable {
gwType = "writable"
}

for _, listener := range listeners {
fmt.Printf("Gateway (%s) server listening on %s\n", gwType, listener.Multiaddr())
fmt.Printf("Gateway (readonly) server listening on %s\n", listener.Multiaddr())
}

cmdctx := *cctx
Expand All @@ -839,7 +832,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
var opts = []corehttp.ServeOption{
corehttp.MetricsCollectionOption("gateway"),
corehttp.HostnameOption(),
corehttp.GatewayOption(writable, "/ipfs", "/ipns"),
corehttp.GatewayOption("/ipfs", "/ipns"),
corehttp.VersionOption(),
corehttp.CheckVersionOption(),
corehttp.CommandsROOption(cmdctx),
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func run(ipfsPath, watchPath string) error {
if *http {
addr := "/ip4/127.0.0.1/tcp/5001"
var opts = []corehttp.ServeOption{
corehttp.GatewayOption(true, "/ipfs", "/ipns"),
corehttp.GatewayOption("/ipfs", "/ipns"),
corehttp.WebUIOption,
corehttp.CommandsOption(cmdCtx(node, ipfsPath)),
}
Expand Down
4 changes: 2 additions & 2 deletions config/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type Gateway struct {
// should be redirected.
RootRedirect string

// Writable enables PUT/POST request handling by this gateway. Usually,
// writing is done through the API, not the gateway.
// REMOVED: Writable enables PUT/POST request handling by this gateway.
// Usually, writing is done through the API, not the gateway.
Writable bool

// PathPrefixes was removed: https://github.com/ipfs/go-ipfs/issues/7702
Expand Down
59 changes: 52 additions & 7 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package corehttp

import (
"context"
"fmt"
"io"
"net"
"net/http"

cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-libipfs/files"
"github.com/ipfs/go-libipfs/gateway"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
version "github.com/ipfs/kubo"
core "github.com/ipfs/kubo/core"
coreapi "github.com/ipfs/kubo/core/coreapi"
id "github.com/libp2p/go-libp2p/p2p/protocol/identify"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func GatewayOption(writable bool, paths ...string) ServeOption {
func GatewayOption(paths ...string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
cfg, err := n.Repo.Config()
if err != nil {
return nil, err
}

api, err := coreapi.NewCoreAPI(n, options.Api.FetchBlocks(!cfg.Gateway.NoFetch))
nodeAPI, err := coreapi.NewCoreAPI(n, options.Api.FetchBlocks(!cfg.Gateway.NoFetch))
if err != nil {
return nil, err
}
Expand All @@ -33,16 +40,21 @@ func GatewayOption(writable bool, paths ...string) ServeOption {

gateway.AddAccessControlHeaders(headers)

offlineAPI, err := api.WithOptions(options.Api.Offline(true))
offlineNodeAPI, err := nodeAPI.WithOptions(options.Api.Offline(true))
if err != nil {
return nil, err
}

gateway := gateway.NewHandler(gateway.Config{
Headers: headers,
Writable: writable,
}, api, offlineAPI)
gatewayCfg := gateway.Config{
Headers: headers,
}

gwAPI := &gatewayAPI{
node: nodeAPI,
offlineNode: offlineNodeAPI,
}

gateway := gateway.NewHandler(gatewayCfg, gwAPI)
gateway = otelhttp.NewHandler(gateway, "Gateway.Request")

for _, p := range paths {
Expand All @@ -62,3 +74,36 @@ func VersionOption() ServeOption {
return mux, nil
}
}

type gatewayAPI struct {
node iface.CoreAPI
offlineNode iface.CoreAPI
}

func (gw *gatewayAPI) GetUnixFs(ctx context.Context, pth path.Path) (files.Node, error) {
return gw.node.Unixfs().Get(ctx, pth)
}

func (gw *gatewayAPI) LsUnixFs(ctx context.Context, pth path.Path, opts ...options.UnixfsLsOption) (<-chan iface.DirEntry, error) {
return gw.node.Unixfs().Ls(ctx, pth, opts...)
}

func (gw *gatewayAPI) GetBlock(ctx context.Context, pth path.Path) (io.Reader, error) {
return gw.node.Block().Get(ctx, pth)
}

func (gw *gatewayAPI) StatBlockOffline(ctx context.Context, pth path.Path) (iface.BlockStat, error) {
return gw.offlineNode.Block().Stat(ctx, pth)
}

func (gw *gatewayAPI) GetNode(ctx context.Context, cid cid.Cid) (ipld.Node, error) {
return gw.node.Dag().Get(ctx, cid)
}

func (gw *gatewayAPI) GetRouting(ctx context.Context, k string) ([]byte, error) {
return gw.node.Routing().Get(ctx, k)
}

func (gw *gatewayAPI) ResolvePath(ctx context.Context, pth path.Path) (path.Resolved, error) {
return gw.node.ResolvePath(ctx, pth)
}
2 changes: 1 addition & 1 deletion core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, iface
dh.Handler, err = makeHandler(n,
ts.Listener,
HostnameOption(),
GatewayOption(false, "/ipfs", "/ipns"),
GatewayOption("/ipfs", "/ipns"),
VersionOption(),
)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,7 @@ Type: `string` (url)

### `Gateway.Writable`

A boolean to configure whether the gateway is writeable or not.

Default: `false`

Type: `bool`
**REMOVED**: Kubo no longer provides a Writable Gateway.

### `Gateway.PathPrefixes`

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.18
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08
github.com/ipfs/interface-go-ipfs-core v0.10.0
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.24.2
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ github.com/ipfs/go-ipld-legacy v0.1.1 h1:BvD8PEuqwBHLTKqlGFTHSwrwFOMkVESEvwIYwR2
github.com/ipfs/go-ipld-legacy v0.1.1/go.mod h1:8AyKFCjgRPsQFf15ZQgDB8Din4DML/fOmKZkkFkrIEg=
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496 h1:RVI31GQCFODREpasIFyVFkS6PjJT2bMwr/Bgr9Ryql4=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08 h1:nOCkGmyfl4m77P+lWR83d3jsFS0ek+kYxmUuONGMEe8=
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08/go.mod h1:P6YEhRwAljcifKAGL01RobOTfSfyFacr0F8wiJNz2uE=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk=
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ require (
github.com/ipfs/go-ipld-git v0.1.1
github.com/ipfs/go-ipld-legacy v0.1.1
github.com/ipfs/go-ipns v0.3.0
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08
github.com/ipfs/go-log v1.0.5
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-merkledag v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ github.com/ipfs/go-ipld-legacy v0.1.1 h1:BvD8PEuqwBHLTKqlGFTHSwrwFOMkVESEvwIYwR2
github.com/ipfs/go-ipld-legacy v0.1.1/go.mod h1:8AyKFCjgRPsQFf15ZQgDB8Din4DML/fOmKZkkFkrIEg=
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496 h1:RVI31GQCFODREpasIFyVFkS6PjJT2bMwr/Bgr9Ryql4=
github.com/ipfs/go-libipfs v0.4.1-0.20230130233950-a005a5006496/go.mod h1:AAPvZADZ80i+QhGCWNWCsx8IGY0t9C+IBEngLeYtySY=
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08 h1:nOCkGmyfl4m77P+lWR83d3jsFS0ek+kYxmUuONGMEe8=
github.com/ipfs/go-libipfs v0.4.1-0.20230131164115-b4a01af26d08/go.mod h1:P6YEhRwAljcifKAGL01RobOTfSfyFacr0F8wiJNz2uE=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/ipfs/go-log v1.0.2/go.mod h1:1MNjMxe0u6xvJZgeqbJ8vdo2TKaGwZ1a0Bpza+sr2Sk=
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=
Expand Down
7 changes: 0 additions & 7 deletions test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@ test_init_ipfs() {

}

test_config_ipfs_gateway_writable() {
test_expect_success "prepare config -- gateway writable" '
test_config_set --bool Gateway.Writable true ||
test_fsh cat "\"$IPFS_PATH/config\""
'
}

test_wait_for_file() {
loops=$1
delay=$2
Expand Down
132 changes: 0 additions & 132 deletions test/sharness/t0111-gateway-writeable.sh

This file was deleted.

0 comments on commit 6e8f94d

Please sign in to comment.