Skip to content

Commit

Permalink
feat: remove writable gateway (#9743)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
hacdias and lidel authored Mar 27, 2023
1 parent bf7d0fc commit 88d431c
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 464 deletions.
19 changes: 7 additions & 12 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ 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 legacy Gateway.Writable (deprecated)"),
cmds.BoolOption(writableKwd, "Enable legacy Gateway.Writable (REMOVED)"),
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 @@ -679,7 +679,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error

for _, listener := range listeners {
// we might have listened to /tcp/0 - let's see what we are listing on
fmt.Printf("API server listening on %s\n", listener.Multiaddr())
fmt.Printf("RPC API server listening on %s\n", listener.Multiaddr())
// Browsers require TCP.
switch listener.Addr().Network() {
case "tcp", "tcp4", "tcp6":
Expand All @@ -692,9 +692,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 @@ -804,7 +804,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
}

if writable {
log.Error("serveHTTPGateway: legacy Gateway.Writable is DEPRECATED and will be removed or changed in future versions. If you are still using this, provide feedback in https://github.com/ipfs/specs/issues/375")
log.Fatalf("Support for Gateway.Writable and --writable has been REMOVED. Please remove it from your config file or CLI. Modern replacement tracked in https://github.com/ipfs/specs/issues/375")
}

listeners, err := sockets.TakeListeners("io.ipfs.gateway")
Expand Down Expand Up @@ -837,13 +837,8 @@ 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 server listening on %s\n", listener.Multiaddr())
}

cmdctx := *cctx
Expand All @@ -852,7 +847,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
3 changes: 1 addition & 2 deletions config/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ type Gateway struct {
// should be redirected.
RootRedirect string

// DEPRECATED: Enables legacy PUT/POST request handling.
// Modern replacement tracked in https://github.com/ipfs/specs/issues/375
// REMOVED: modern replacement tracked in https://github.com/ipfs/specs/issues/375
Writable Flag `json:",omitempty"`

// PathPrefixes was removed: https://github.com/ipfs/go-ipfs/issues/7702
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/corehttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv

// we might have listened to /tcp/0 - let's see what we are listing on
addr = list.Multiaddr()
fmt.Printf("API server listening on %s\n", addr)
fmt.Printf("RPC API server listening on %s\n", addr)

return Serve(n, manet.NetListener(list), options...)
}
Expand Down
29 changes: 1 addition & 28 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,13 @@ import (
"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))
if err != nil {
return nil, err
}

headers := make(map[string][]string, len(cfg.Gateway.HTTPHeaders))
for h, v := range cfg.Gateway.HTTPHeaders {
headers[http.CanonicalHeaderKey(h)] = v
Expand All @@ -58,28 +53,6 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
// By default, our HTTP handler is the gateway handler.
handler := gw.ServeHTTP

// If we have the writable gateway enabled, we have to replace our
// http handler by a handler that takes care of the different methods.
if writable {
writableGw := &writableGatewayHandler{
config: &gwConfig,
api: api,
}

handler = func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
writableGw.postHandler(w, r)
case http.MethodDelete:
writableGw.deleteHandler(w, r)
case http.MethodPut:
writableGw.putHandler(w, r)
default:
gw.ServeHTTP(w, r)
}
}
}

for _, p := range paths {
mux.HandleFunc(p+"/", handler)
}
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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
Loading

0 comments on commit 88d431c

Please sign in to comment.