smd/swarm, swarm/api: client.go supports toEncrypt parameter

This commit is contained in:
Balint Gabor 2018-04-11 13:40:16 +02:00
parent b74f35b078
commit f0dc791adf
4 changed files with 42 additions and 25 deletions

View file

@ -131,13 +131,13 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
longestPathEntry = api.ManifestEntry{} longestPathEntry = api.ManifestEntry{}
) )
mroot, err := client.DownloadManifest(mhash) mroot, isEncrypted, err := client.DownloadManifest(mhash)
if err != nil { if err != nil {
utils.Fatalf("Manifest download failed: %v", err) utils.Fatalf("Manifest download failed: %v", err)
} }
//TODO: check if the "hash" to add is valid and present in swarm //TODO: check if the "hash" to add is valid and present in swarm
_, err = client.DownloadManifest(hash) _, _, err = client.DownloadManifest(hash)
if err != nil { if err != nil {
utils.Fatalf("Hash to add is not present: %v", err) utils.Fatalf("Hash to add is not present: %v", err)
} }
@ -180,7 +180,7 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
mroot.Entries = append(mroot.Entries, newEntry) mroot.Entries = append(mroot.Entries, newEntry)
} }
newManifestHash, err := client.UploadManifest(mroot) newManifestHash, err := client.UploadManifest(mroot, isEncrypted)
if err != nil { if err != nil {
utils.Fatalf("Manifest upload failed: %v", err) utils.Fatalf("Manifest upload failed: %v", err)
} }
@ -197,7 +197,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
longestPathEntry = api.ManifestEntry{} longestPathEntry = api.ManifestEntry{}
) )
mroot, err := client.DownloadManifest(mhash) mroot, isEncrypted, err := client.DownloadManifest(mhash)
if err != nil { if err != nil {
utils.Fatalf("Manifest download failed: %v", err) utils.Fatalf("Manifest download failed: %v", err)
} }
@ -257,7 +257,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
mroot = newMRoot mroot = newMRoot
} }
newManifestHash, err := client.UploadManifest(mroot) newManifestHash, err := client.UploadManifest(mroot, isEncrypted)
if err != nil { if err != nil {
utils.Fatalf("Manifest upload failed: %v", err) utils.Fatalf("Manifest upload failed: %v", err)
} }
@ -273,7 +273,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
longestPathEntry = api.ManifestEntry{} longestPathEntry = api.ManifestEntry{}
) )
mroot, err := client.DownloadManifest(mhash) mroot, isEncrypted, err := client.DownloadManifest(mhash)
if err != nil { if err != nil {
utils.Fatalf("Manifest download failed: %v", err) utils.Fatalf("Manifest download failed: %v", err)
} }
@ -323,7 +323,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
mroot = newMRoot mroot = newMRoot
} }
newManifestHash, err := client.UploadManifest(mroot) newManifestHash, err := client.UploadManifest(mroot, isEncrypted)
if err != nil { if err != nil {
utils.Fatalf("Manifest upload failed: %v", err) utils.Fatalf("Manifest upload failed: %v", err)
} }

View file

@ -84,7 +84,7 @@ func upload(ctx *cli.Context, toEncrypt bool) {
utils.Fatalf("Error opening file: %s", err) utils.Fatalf("Error opening file: %s", err)
} }
defer f.Close() defer f.Close()
hash, err := client.UploadRaw(f, f.Size) hash, err := client.UploadRaw(f, f.Size, toEncrypt)
if err != nil { if err != nil {
utils.Fatalf("Upload failed: %s", err) utils.Fatalf("Upload failed: %s", err)
} }

View file

@ -52,12 +52,17 @@ type Client struct {
Gateway string Gateway string
} }
// UploadRaw uploads raw data to swarm and returns the resulting hash // UploadRaw uploads raw data to swarm and returns the resulting hash. If toEncrypt is true it
func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) { // uploads encrypted data
func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, error) {
if size <= 0 { if size <= 0 {
return "", errors.New("data size must be greater than zero") return "", errors.New("data size must be greater than zero")
} }
req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/", r) addr := ""
if toEncrypt {
addr = "encrypt"
}
req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/"+addr, r)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -77,18 +82,20 @@ func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) {
return string(data), nil return string(data), nil
} }
// DownloadRaw downloads raw data from swarm // DownloadRaw downloads raw data from swarm and it returns a ReadCloser and a bool whether the
func (c *Client) DownloadRaw(hash string) (io.ReadCloser, error) { // content was encrypted
func (c *Client) DownloadRaw(hash string) (io.ReadCloser, bool, error) {
uri := c.Gateway + "/bzz-raw:/" + hash uri := c.Gateway + "/bzz-raw:/" + hash
res, err := http.DefaultClient.Get(uri) res, err := http.DefaultClient.Get(uri)
if err != nil { if err != nil {
return nil, err return nil, false, err
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
res.Body.Close() res.Body.Close()
return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status) return nil, false, fmt.Errorf("unexpected HTTP status: %s", res.Status)
} }
return res.Body, nil isEncrypted := (res.Header.Get("X-Encrypted") == "true")
return res.Body, isEncrypted, nil
} }
// File represents a file in a swarm manifest and is used for uploading and // File represents a file in a swarm manifest and is used for uploading and
@ -229,26 +236,26 @@ func (c *Client) DownloadDirectory(hash, path, destDir string) error {
} }
// UploadManifest uploads the given manifest to swarm // UploadManifest uploads the given manifest to swarm
func (c *Client) UploadManifest(m *api.Manifest) (string, error) { func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool) (string, error) {
data, err := json.Marshal(m) data, err := json.Marshal(m)
if err != nil { if err != nil {
return "", err return "", err
} }
return c.UploadRaw(bytes.NewReader(data), int64(len(data))) return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
} }
// DownloadManifest downloads a swarm manifest // DownloadManifest downloads a swarm manifest
func (c *Client) DownloadManifest(hash string) (*api.Manifest, error) { func (c *Client) DownloadManifest(hash string) (*api.Manifest, bool, error) {
res, err := c.DownloadRaw(hash) res, isEncrypted, err := c.DownloadRaw(hash)
if err != nil { if err != nil {
return nil, err return nil, isEncrypted, err
} }
defer res.Close() defer res.Close()
var manifest api.Manifest var manifest api.Manifest
if err := json.NewDecoder(res).Decode(&manifest); err != nil { if err := json.NewDecoder(res).Decode(&manifest); err != nil {
return nil, err return nil, isEncrypted, err
} }
return &manifest, nil return &manifest, isEncrypted, nil
} }
// List list files in a swarm manifest which have the given prefix, grouping // List list files in a swarm manifest which have the given prefix, grouping

View file

@ -31,6 +31,13 @@ import (
// TestClientUploadDownloadRaw test uploading and downloading raw data to swarm // TestClientUploadDownloadRaw test uploading and downloading raw data to swarm
func TestClientUploadDownloadRaw(t *testing.T) { func TestClientUploadDownloadRaw(t *testing.T) {
testClientUploadDownloadRaw(false, t)
}
func TestClientUploadDownloadRawEncrypted(t *testing.T) {
testClientUploadDownloadRaw(true, t)
}
func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
srv := testutil.NewTestSwarmServer(t) srv := testutil.NewTestSwarmServer(t)
defer srv.Close() defer srv.Close()
@ -38,16 +45,19 @@ func TestClientUploadDownloadRaw(t *testing.T) {
// upload some raw data // upload some raw data
data := []byte("foo123") data := []byte("foo123")
hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data))) hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// check we can download the same data // check we can download the same data
res, err := client.DownloadRaw(hash) res, isEncrypted, err := client.DownloadRaw(hash)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if isEncrypted != toEncrypt {
t.Fatalf("Expected encyption status %v got %v", toEncrypt, isEncrypted)
}
defer res.Close() defer res.Close()
gotData, err := ioutil.ReadAll(res) gotData, err := ioutil.ReadAll(res)
if err != nil { if err != nil {