mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
swarm/...: Integrate encryption into swarm API
This commit is contained in:
parent
2ee678dea4
commit
ca6b22ee7c
11 changed files with 551 additions and 485 deletions
|
|
@ -39,7 +39,9 @@ import (
|
|||
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||
)
|
||||
|
||||
var hashMatcher = regexp.MustCompile("^[0-9A-Fa-f]{64}")
|
||||
|
||||
// TODO: this is bad, it should not be hardcoded how long is a hash
|
||||
var hashMatcher = regexp.MustCompile("^([0-9A-Fa-f]{64})([0-9A-Fa-f]{64})?")
|
||||
|
||||
type ErrResourceReturn struct {
|
||||
key string
|
||||
|
|
@ -230,9 +232,9 @@ func NewApi(dpa *storage.DPA, dns Resolver, resourceHandler *storage.ResourceHan
|
|||
}
|
||||
|
||||
// to be used only in TEST
|
||||
func (self *Api) Upload(uploadDir, index string) (hash string, err error) {
|
||||
func (self *Api) Upload(uploadDir, index string, toEncrypt bool) (hash string, err error) {
|
||||
fs := NewFileSystem(self)
|
||||
hash, err = fs.Upload(uploadDir, index)
|
||||
hash, err = fs.Upload(uploadDir, index, toEncrypt)
|
||||
return hash, err
|
||||
}
|
||||
|
||||
|
|
@ -241,9 +243,9 @@ func (self *Api) Retrieve(key storage.Key) storage.LazySectionReader {
|
|||
return self.dpa.Retrieve(key)
|
||||
}
|
||||
|
||||
func (self *Api) Store(data io.Reader, size int64) (key storage.Key, wait func(), err error) {
|
||||
func (self *Api) Store(data io.Reader, size int64, toEncrypt bool) (key storage.Key, wait func(), err error) {
|
||||
log.Debug("api.store", "size", size)
|
||||
return self.dpa.Store(data, size, false)
|
||||
return self.dpa.Store(data, size, toEncrypt)
|
||||
}
|
||||
|
||||
type ErrResolve error
|
||||
|
|
@ -283,17 +285,17 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) {
|
|||
}
|
||||
|
||||
// Put provides singleton manifest creation on top of dpa store
|
||||
func (self *Api) Put(content, contentType string) (k storage.Key, wait func(), err error) {
|
||||
func (self *Api) Put(content, contentType string, toEncrypt bool) (k storage.Key, wait func(), err error) {
|
||||
apiPutCount.Inc(1)
|
||||
r := strings.NewReader(content)
|
||||
key, waitContent, err := self.dpa.Store(r, int64(len(content)), false)
|
||||
key, waitContent, err := self.dpa.Store(r, int64(len(content)), toEncrypt)
|
||||
if err != nil {
|
||||
apiPutFail.Inc(1)
|
||||
return nil, nil, err
|
||||
}
|
||||
manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
|
||||
r = strings.NewReader(manifest)
|
||||
key, waitManifest, err := self.dpa.Store(r, int64(len(manifest)), false)
|
||||
key, waitManifest, err := self.dpa.Store(r, int64(len(manifest)), toEncrypt)
|
||||
if err != nil {
|
||||
apiPutFail.Inc(1)
|
||||
return nil, nil, err
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import (
|
|||
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||
)
|
||||
|
||||
func testApi(t *testing.T, f func(*Api)) {
|
||||
func testApi(t *testing.T, f func(*Api, bool)) {
|
||||
datadir, err := ioutil.TempDir("", "bzz-test")
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create temp dir: %v", err)
|
||||
|
|
@ -43,7 +43,8 @@ func testApi(t *testing.T, f func(*Api)) {
|
|||
return
|
||||
}
|
||||
api := NewApi(dpa, nil, nil)
|
||||
f(api)
|
||||
f(api, false)
|
||||
f(api, true)
|
||||
}
|
||||
|
||||
type testResponse struct {
|
||||
|
|
@ -106,11 +107,11 @@ func testGet(t *testing.T, api *Api, bzzhash, path string) *testResponse {
|
|||
}
|
||||
|
||||
func TestApiPut(t *testing.T) {
|
||||
testApi(t, func(api *Api) {
|
||||
testApi(t, func(api *Api, toEncrypt bool) {
|
||||
content := "hello"
|
||||
exp := expResponse(content, "text/plain", 0)
|
||||
// exp := expResponse([]byte(content), "text/plain", 0)
|
||||
key, wait, err := api.Put(content, exp.MimeType)
|
||||
key, wait, err := api.Put(content, exp.MimeType, toEncrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ func NewFileSystem(api *Api) *FileSystem {
|
|||
// TODO: localpath should point to a manifest
|
||||
//
|
||||
// DEPRECATED: Use the HTTP API instead
|
||||
func (self *FileSystem) Upload(lpath, index string) (string, error) {
|
||||
func (self *FileSystem) Upload(lpath, index string, toEncrypt bool) (string, error) {
|
||||
var list []*manifestTrieEntry
|
||||
localpath, err := filepath.Abs(filepath.Clean(lpath))
|
||||
if err != nil {
|
||||
|
|
@ -114,7 +114,7 @@ func (self *FileSystem) Upload(lpath, index string) (string, error) {
|
|||
stat, _ := f.Stat()
|
||||
var hash storage.Key
|
||||
var wait func()
|
||||
hash, wait, err = self.api.dpa.Store(f, stat.Size(), false)
|
||||
hash, wait, err = self.api.dpa.Store(f, stat.Size(), toEncrypt)
|
||||
if hash != nil {
|
||||
list[i].Hash = hash.Hex()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@ import (
|
|||
|
||||
var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
|
||||
|
||||
func testFileSystem(t *testing.T, f func(*FileSystem)) {
|
||||
testApi(t, func(api *Api) {
|
||||
f(NewFileSystem(api))
|
||||
func testFileSystem(t *testing.T, f func(*FileSystem, bool)) {
|
||||
testApi(t, func(api *Api, toEncrypt bool) {
|
||||
f(NewFileSystem(api), toEncrypt)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -46,9 +46,9 @@ func readPath(t *testing.T, parts ...string) string {
|
|||
}
|
||||
|
||||
func TestApiDirUpload0(t *testing.T) {
|
||||
testFileSystem(t, func(fs *FileSystem) {
|
||||
testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
|
||||
api := fs.api
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "")
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -74,20 +74,21 @@ func TestApiDirUpload0(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
newbzzhash, err := fs.Upload(downloadDir, "")
|
||||
newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if bzzhash != newbzzhash {
|
||||
// TODO: currently the hash is not deterministic in the encrypted case
|
||||
if !toEncrypt && bzzhash != newbzzhash {
|
||||
t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestApiDirUploadModify(t *testing.T) {
|
||||
testFileSystem(t, func(fs *FileSystem) {
|
||||
testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
|
||||
api := fs.api
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "")
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
@ -104,7 +105,7 @@ func TestApiDirUploadModify(t *testing.T) {
|
|||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
hash, wait, err := api.Store(bytes.NewReader(index), int64(len(index)))
|
||||
hash, wait, err := api.Store(bytes.NewReader(index), int64(len(index)), toEncrypt)
|
||||
wait()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
|
|
@ -144,9 +145,9 @@ func TestApiDirUploadModify(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestApiDirUploadWithRootFile(t *testing.T) {
|
||||
testFileSystem(t, func(fs *FileSystem) {
|
||||
testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
|
||||
api := fs.api
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html")
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
@ -160,9 +161,9 @@ func TestApiDirUploadWithRootFile(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestApiFileUpload(t *testing.T) {
|
||||
testFileSystem(t, func(fs *FileSystem) {
|
||||
testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
|
||||
api := fs.api
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "")
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
@ -176,9 +177,9 @@ func TestApiFileUpload(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestApiFileUploadWithRootFile(t *testing.T) {
|
||||
testFileSystem(t, func(fs *FileSystem) {
|
||||
testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
|
||||
api := fs.api
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html")
|
||||
bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -124,19 +124,30 @@ func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) {
|
|||
log.Debug("handle.post.raw", "ruid", r.ruid)
|
||||
|
||||
postRawCount.Inc(1)
|
||||
|
||||
toEncrypt := false
|
||||
if r.uri.Addr == "encrypt" {
|
||||
toEncrypt = true
|
||||
}
|
||||
|
||||
if r.uri.Path != "" {
|
||||
postRawFail.Inc(1)
|
||||
Respond(w, r, "raw POST request cannot contain a path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if r.uri.Addr != "" && r.uri.Addr != "encrypt" {
|
||||
postRawFail.Inc(1)
|
||||
Respond(w, r, "raw POST request addr can only be empty or \"encrypt\"", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Header.Get("Content-Length") == "" {
|
||||
postRawFail.Inc(1)
|
||||
Respond(w, r, "missing Content-Length header in request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
key, _, err := s.api.Store(r.Body, r.ContentLength)
|
||||
key, _, err := s.api.Store(r.Body, r.ContentLength, toEncrypt)
|
||||
if err != nil {
|
||||
postRawFail.Inc(1)
|
||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||
|
|
@ -176,7 +187,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *Request) {
|
|||
}
|
||||
log.Debug("resolved key", "ruid", r.ruid, "key", key)
|
||||
} else {
|
||||
key, err = s.api.NewManifest()
|
||||
key, err = s.api.NewManifest(false)
|
||||
if err != nil {
|
||||
postFilesFail.Inc(1)
|
||||
Respond(w, r, err.Error(), http.StatusInternalServerError)
|
||||
|
|
@ -365,7 +376,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *Request) {
|
|||
Respond(w, r, err2.Error(), code)
|
||||
return
|
||||
}
|
||||
m, err := s.api.NewResourceManifest(r.uri.Addr)
|
||||
m, err := s.api.NewResourceManifest(r.uri.Addr, false)
|
||||
if err != nil {
|
||||
Respond(w, r, fmt.Sprintf("failed to create resource manifest: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -840,14 +851,18 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||
req.uri = uri
|
||||
|
||||
log.Debug("parsed request path", "ruid", req.ruid, "method", req.Method, "uri", req.uri)
|
||||
log.Debug("parsed request path", "uri.Addr", req.uri.Addr, "uri.path", req.uri.Path, "uri.Scheme", req.uri.Scheme)
|
||||
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
if uri.Raw() || uri.DeprecatedRaw() {
|
||||
log.Debug("handlePostRaw")
|
||||
s.HandlePostRaw(w, req)
|
||||
} else if uri.Resource() {
|
||||
log.Debug("handlePostResource")
|
||||
s.HandlePostResource(w, req)
|
||||
} else {
|
||||
log.Debug("handlePostFiles")
|
||||
s.HandlePostFiles(w, req)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -218,7 +218,11 @@ func TestBzzResource(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestBzzGetPath(t *testing.T) {
|
||||
// testBzzGetPath(false, t)
|
||||
testBzzGetPath(true, t)
|
||||
}
|
||||
|
||||
func testBzzGetPath(encrypted bool, t *testing.T) {
|
||||
var err error
|
||||
|
||||
testmanifest := []string{
|
||||
|
|
@ -246,7 +250,7 @@ func TestBzzGetPath(t *testing.T) {
|
|||
for i, mf := range testmanifest {
|
||||
reader[i] = bytes.NewReader([]byte(mf))
|
||||
var wait func()
|
||||
key[i], wait, err = srv.Dpa.Store(reader[i], int64(len(mf)), false)
|
||||
key[i], wait, err = srv.Dpa.Store(reader[i], int64(len(mf)), encrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,20 +59,20 @@ type ManifestList struct {
|
|||
}
|
||||
|
||||
// NewManifest creates and stores a new, empty manifest
|
||||
func (a *Api) NewManifest() (storage.Key, error) {
|
||||
func (a *Api) NewManifest(toEncrypt bool) (storage.Key, error) {
|
||||
var manifest Manifest
|
||||
data, err := json.Marshal(&manifest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, wait, err := a.Store(bytes.NewReader(data), int64(len(data)))
|
||||
key, wait, err := a.Store(bytes.NewReader(data), int64(len(data)), toEncrypt)
|
||||
wait()
|
||||
return key, err
|
||||
}
|
||||
|
||||
// Manifest hack for supporting Mutable Resource Updates from the bzz: scheme
|
||||
// see swarm/api/api.go:Api.Get() for more information
|
||||
func (a *Api) NewResourceManifest(resourceKey string) (storage.Key, error) {
|
||||
func (a *Api) NewResourceManifest(resourceKey string, toEncrypt bool) (storage.Key, error) {
|
||||
var manifest Manifest
|
||||
entry := ManifestEntry{
|
||||
Hash: resourceKey,
|
||||
|
|
@ -83,7 +83,7 @@ func (a *Api) NewResourceManifest(resourceKey string) (storage.Key, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key, _, err := a.Store(bytes.NewReader(data), int64(len(data)))
|
||||
key, _, err := a.Store(bytes.NewReader(data), int64(len(data)), toEncrypt)
|
||||
return key, err
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +104,10 @@ func (a *Api) NewManifestWriter(key storage.Key, quitC chan bool) (*ManifestWrit
|
|||
|
||||
// AddEntry stores the given data and adds the resulting key to the manifest
|
||||
func (m *ManifestWriter) AddEntry(data io.Reader, e *ManifestEntry) (storage.Key, error) {
|
||||
key, _, err := m.api.Store(data, e.Size)
|
||||
|
||||
toEncrypt := (len(m.trie.hash) > m.trie.dpa.HashSize())
|
||||
|
||||
key, _, err := m.api.Store(data, e.Size, toEncrypt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,8 +45,8 @@ func NewStorage(api *Api) *Storage {
|
|||
// its content type
|
||||
//
|
||||
// DEPRECATED: Use the HTTP API instead
|
||||
func (self *Storage) Put(content, contentType string) (storage.Key, func(), error) {
|
||||
return self.api.Put(content, contentType)
|
||||
func (self *Storage) Put(content, contentType string, toEncrypt bool) (storage.Key, func(), error) {
|
||||
return self.api.Put(content, contentType, toEncrypt)
|
||||
}
|
||||
|
||||
// Get retrieves the content from bzzpath and reads the response in full
|
||||
|
|
|
|||
|
|
@ -20,18 +20,18 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func testStorage(t *testing.T, f func(*Storage)) {
|
||||
testApi(t, func(api *Api) {
|
||||
f(NewStorage(api))
|
||||
func testStorage(t *testing.T, f func(*Storage, bool)) {
|
||||
testApi(t, func(api *Api, toEncrypt bool) {
|
||||
f(NewStorage(api), toEncrypt)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStoragePutGet(t *testing.T) {
|
||||
testStorage(t, func(api *Storage) {
|
||||
testStorage(t, func(api *Storage, toEncrypt bool) {
|
||||
content := "hello"
|
||||
exp := expResponse(content, "text/plain", 0)
|
||||
// exp := expResponse([]byte(content), "text/plain", 0)
|
||||
bzzkey, wait, err := api.Put(content, exp.MimeType)
|
||||
bzzkey, wait, err := api.Put(content, exp.MimeType, toEncrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -100,3 +100,7 @@ func (self *DPA) Store(data io.Reader, size int64, toEncrypt bool) (key Key, wai
|
|||
putter := NewHasherStore(self.ChunkStore, self.hashFunc, toEncrypt)
|
||||
return PyramidSplit(data, putter, putter)
|
||||
}
|
||||
|
||||
func (self *DPA) HashSize() int {
|
||||
return self.hashFunc().Size()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue