added missing data encryption/decryption and validation

This commit is contained in:
Jarek Kowalski
2016-04-03 10:16:07 -07:00
parent bdd1f9e886
commit 0cd6ebabb4
9 changed files with 417 additions and 149 deletions

100
session/session.go Normal file
View File

@@ -0,0 +1,100 @@
package session
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"github.com/kopia/kopia/auth"
"github.com/kopia/kopia/blob"
"github.com/kopia/kopia/cas"
)
type Session interface {
io.Closer
InitObjectManager(f cas.Format) (cas.ObjectManager, error)
OpenObjectManager() (cas.ObjectManager, error)
}
type session struct {
storage blob.Storage
creds auth.Credentials
format cas.Format
}
func (s *session) Close() error {
return nil
}
func (s *session) getPrivateBlock(blkID blob.BlockID) ([]byte, error) {
b, err := s.storage.GetBlock(blkID)
if err != nil {
return nil, fmt.Errorf("unable to read block %v: %v", blkID, err)
}
return b, err
}
func (s *session) encryptBlockWithPublicKey(blkID blob.BlockID, data io.ReadCloser, options blob.PutOptions) error {
err := s.storage.PutBlock(blkID, data, options)
if err != nil {
return fmt.Errorf("unable to write block %v: %v", blkID, err)
}
return err
}
func (s *session) getConfigBlockID() blob.BlockID {
if s.creds == nil {
return blob.BlockID("config.json")
}
return blob.BlockID("users." + s.creds.Username() + ".config.json")
}
func (s *session) InitObjectManager(format cas.Format) (cas.ObjectManager, error) {
mgr, err := cas.NewObjectManager(s.storage, format)
if err != nil {
return nil, err
}
b, err := json.Marshal(format)
if err != nil {
return nil, err
}
if err := s.encryptBlockWithPublicKey(
s.getConfigBlockID(),
ioutil.NopCloser(bytes.NewBuffer(b)),
blob.PutOptions{}); err != nil {
return nil, err
}
return mgr, nil
}
func (s *session) OpenObjectManager() (cas.ObjectManager, error) {
b, err := s.getPrivateBlock(s.getConfigBlockID())
if err != nil {
return nil, err
}
var format cas.Format
err = json.Unmarshal(b, &format)
if err != nil {
return nil, err
}
return cas.NewObjectManager(s.storage, format)
}
func New(storage blob.Storage, creds auth.Credentials) (Session, error) {
sess := &session{
storage: storage,
creds: creds,
}
return sess, nil
}

51
session/session_test.go Normal file
View File

@@ -0,0 +1,51 @@
package session
import (
"io/ioutil"
"github.com/kopia/kopia/cas"
"github.com/kopia/kopia/blob"
"testing"
)
func TestA(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "kopia")
if err != nil {
t.Errorf("can't create temp directory: %v", err)
return
}
// cfg := LoadConfig("kopia.config")
sc := blob.StorageConfiguration{
Type: "fs",
Config: &blob.FSStorageOptions{
Path: tmpDir,
},
}
storage, err := blob.NewStorage(sc)
if err != nil {
t.Errorf("cannot create storage: %v", err)
return
}
sess, err := New(storage, nil)
defer sess.Close()
om, err := sess.InitObjectManager(cas.Format{
Version: "1",
Hash: "sha1",
})
if err != nil {
t.Errorf("unable to init object manager: %v", err)
return
}
w := om.NewWriter()
w.Write([]byte{1, 2, 3})
x, err := w.Result(true)
t.Logf("%v x: %v %v", tmpDir, x, err)
}