1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00

feat: remove writable gateway (#9743)

Co-authored-by: Marcin Rataj <lidel@lidel.org>
This commit is contained in:
Henrique Dias
2023-03-27 15:19:55 +02:00
committed by GitHub
parent bf7d0fc99d
commit 88d431c812
12 changed files with 18 additions and 464 deletions

View File

@ -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...)
}

View File

@ -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
@ -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)
}

View File

@ -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 {

View File

@ -1,265 +0,0 @@
package corehttp
import (
"context"
"fmt"
"net/http"
"os"
gopath "path"
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"
dag "github.com/ipfs/go-merkledag"
"github.com/ipfs/go-mfs"
path "github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver"
iface "github.com/ipfs/interface-go-ipfs-core"
routing "github.com/libp2p/go-libp2p/core/routing"
)
const (
ipfsPathPrefix = "/ipfs/"
)
type writableGatewayHandler struct {
api iface.CoreAPI
config *gateway.Config
}
func (i *writableGatewayHandler) addUserHeaders(w http.ResponseWriter) {
for k, v := range i.config.Headers {
w.Header()[http.CanonicalHeaderKey(k)] = v
}
}
func (i *writableGatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
p, err := i.api.Unixfs().Add(r.Context(), files.NewReaderFile(r.Body))
if err != nil {
internalWebError(w, err)
return
}
i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", p.Cid().String())
log.Debugw("CID created, http redirect", "from", r.URL, "to", p, "status", http.StatusCreated)
http.Redirect(w, r, p.String(), http.StatusCreated)
}
func (i *writableGatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ds := i.api.Dag()
// Parse the path
rootCid, newPath, err := parseIpfsPath(r.URL.Path)
if err != nil {
webError(w, "WritableGateway: failed to parse the path", err, http.StatusBadRequest)
return
}
if newPath == "" || newPath == "/" {
http.Error(w, "WritableGateway: empty path", http.StatusBadRequest)
return
}
newDirectory, newFileName := gopath.Split(newPath)
// Resolve the old root.
rnode, err := ds.Get(ctx, rootCid)
if err != nil {
webError(w, "WritableGateway: Could not create DAG from request", err, http.StatusInternalServerError)
return
}
pbnd, ok := rnode.(*dag.ProtoNode)
if !ok {
webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest)
return
}
// Create the new file.
newFilePath, err := i.api.Unixfs().Add(ctx, files.NewReaderFile(r.Body))
if err != nil {
webError(w, "WritableGateway: could not create DAG from request", err, http.StatusInternalServerError)
return
}
newFile, err := ds.Get(ctx, newFilePath.Cid())
if err != nil {
webError(w, "WritableGateway: failed to resolve new file", err, http.StatusInternalServerError)
return
}
// Patch the new file into the old root.
root, err := mfs.NewRoot(ctx, ds, pbnd, nil)
if err != nil {
webError(w, "WritableGateway: failed to create MFS root", err, http.StatusBadRequest)
return
}
if newDirectory != "" {
err := mfs.Mkdir(root, newDirectory, mfs.MkdirOpts{Mkparents: true, Flush: false})
if err != nil {
webError(w, "WritableGateway: failed to create MFS directory", err, http.StatusInternalServerError)
return
}
}
dirNode, err := mfs.Lookup(root, newDirectory)
if err != nil {
webError(w, "WritableGateway: failed to lookup directory", err, http.StatusInternalServerError)
return
}
dir, ok := dirNode.(*mfs.Directory)
if !ok {
http.Error(w, "WritableGateway: target directory is not a directory", http.StatusBadRequest)
return
}
err = dir.Unlink(newFileName)
switch err {
case os.ErrNotExist, nil:
default:
webError(w, "WritableGateway: failed to replace existing file", err, http.StatusBadRequest)
return
}
err = dir.AddChild(newFileName, newFile)
if err != nil {
webError(w, "WritableGateway: failed to link file into directory", err, http.StatusInternalServerError)
return
}
nnode, err := root.GetDirectory().GetNode()
if err != nil {
webError(w, "WritableGateway: failed to finalize", err, http.StatusInternalServerError)
return
}
newcid := nnode.Cid()
i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", newcid.String())
redirectURL := gopath.Join(ipfsPathPrefix, newcid.String(), newPath)
log.Debugw("CID replaced, redirect", "from", r.URL, "to", redirectURL, "status", http.StatusCreated)
http.Redirect(w, r, redirectURL, http.StatusCreated)
}
func (i *writableGatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// parse the path
rootCid, newPath, err := parseIpfsPath(r.URL.Path)
if err != nil {
webError(w, "WritableGateway: failed to parse the path", err, http.StatusBadRequest)
return
}
if newPath == "" || newPath == "/" {
http.Error(w, "WritableGateway: empty path", http.StatusBadRequest)
return
}
directory, filename := gopath.Split(newPath)
// lookup the root
rootNodeIPLD, err := i.api.Dag().Get(ctx, rootCid)
if err != nil {
webError(w, "WritableGateway: failed to resolve root CID", err, http.StatusInternalServerError)
return
}
rootNode, ok := rootNodeIPLD.(*dag.ProtoNode)
if !ok {
http.Error(w, "WritableGateway: empty path", http.StatusInternalServerError)
return
}
// construct the mfs root
root, err := mfs.NewRoot(ctx, i.api.Dag(), rootNode, nil)
if err != nil {
webError(w, "WritableGateway: failed to construct the MFS root", err, http.StatusBadRequest)
return
}
// lookup the parent directory
parentNode, err := mfs.Lookup(root, directory)
if err != nil {
webError(w, "WritableGateway: failed to look up parent", err, http.StatusInternalServerError)
return
}
parent, ok := parentNode.(*mfs.Directory)
if !ok {
http.Error(w, "WritableGateway: parent is not a directory", http.StatusInternalServerError)
return
}
// delete the file
switch parent.Unlink(filename) {
case nil, os.ErrNotExist:
default:
webError(w, "WritableGateway: failed to remove file", err, http.StatusInternalServerError)
return
}
nnode, err := root.GetDirectory().GetNode()
if err != nil {
webError(w, "WritableGateway: failed to finalize", err, http.StatusInternalServerError)
return
}
ncid := nnode.Cid()
i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", ncid.String())
redirectURL := gopath.Join(ipfsPathPrefix+ncid.String(), directory)
// note: StatusCreated is technically correct here as we created a new resource.
log.Debugw("CID deleted, redirect", "from", r.RequestURI, "to", redirectURL, "status", http.StatusCreated)
http.Redirect(w, r, redirectURL, http.StatusCreated)
}
func parseIpfsPath(p string) (cid.Cid, string, error) {
rootPath, err := path.ParsePath(p)
if err != nil {
return cid.Cid{}, "", err
}
// Check the path.
rsegs := rootPath.Segments()
if rsegs[0] != "ipfs" {
return cid.Cid{}, "", fmt.Errorf("WritableGateway: only ipfs paths supported")
}
rootCid, err := cid.Decode(rsegs[1])
if err != nil {
return cid.Cid{}, "", err
}
return rootCid, path.Join(rsegs[2:]), nil
}
func webError(w http.ResponseWriter, message string, err error, defaultCode int) {
if _, ok := err.(resolver.ErrNoLink); ok {
webErrorWithCode(w, message, err, http.StatusNotFound)
} else if err == routing.ErrNotFound {
webErrorWithCode(w, message, err, http.StatusNotFound)
} else if ipld.IsNotFound(err) {
webErrorWithCode(w, message, err, http.StatusNotFound)
} else if err == context.DeadlineExceeded {
webErrorWithCode(w, message, err, http.StatusRequestTimeout)
} else {
webErrorWithCode(w, message, err, defaultCode)
}
}
func webErrorWithCode(w http.ResponseWriter, message string, err error, code int) {
http.Error(w, fmt.Sprintf("%s: %s", message, err), code)
if code >= 500 {
log.Warnf("server error: %s: %s", message, err)
}
}
// return a 500 error and log
func internalWebError(w http.ResponseWriter, err error) {
webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError)
}