func (app *App) OpenKV(tx *db.Tx, storage *db.VolumeStorage) (kv.KV, error) { var kvstores []kv.KV c := storage.Cursor() for item := c.First(); item != nil; item = c.Next() { backend, err := item.Backend() if err != nil { return nil, err } s, err := app.openStorage(backend) if err != nil { return nil, err } sharingKeyName, err := item.SharingKeyName() if err != nil { return nil, err } sharingKey, err := tx.SharingKeys().Get(sharingKeyName) if err != nil { return nil, fmt.Errorf("getting sharing key %q: %v", sharingKeyName, err) } var secret [32]byte sharingKey.Secret(&secret) s = untrusted.New(s, &secret) kvstores = append(kvstores, s) } return kvmulti.New(kvstores...), nil }
func (app *App) openKV(conf *wire.KV) (kv.KV, error) { var kvstores []kv.KV if conf.Local != nil { kvpath := filepath.Join(app.DataDir, "chunks") var s kv.KV var err error s, err = kvfiles.Open(kvpath) if err != nil { return nil, err } if conf.Local.Secret != nil { var secret [32]byte copy(secret[:], conf.Local.Secret) s = untrusted.New(s, &secret) } kvstores = append(kvstores, s) } for _, ext := range conf.External { var s kv.KV var err error s, err = kvfiles.Open(ext.Path) if err != nil { return nil, err } if ext.Secret != nil { var secret [32]byte copy(secret[:], ext.Secret) s = untrusted.New(s, &secret) } kvstores = append(kvstores, s) } if len(conf.XXX_unrecognized) > 0 { return nil, fmt.Errorf("unknown storage: %v", conf) } return kvmulti.New(kvstores...), nil }
func TestWrongType(t *testing.T) { remote := &kvmock.InMemory{} secret := &[32]byte{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, } converg := untrusted.New(remote, secret) store := kvchunks.New(converg) phony := &chunks.Chunk{ Type: "evilchunk", Level: 3, Buf: []byte(GREETING), } _, err := store.Add(phony) if err != nil { t.Fatalf("store.Add failed: %v", err) } var phonyData string for k, v := range remote.Data { phonyData = v delete(remote.Data, k) } orig := &chunks.Chunk{ Type: "testchunk", Level: 3, Buf: []byte(GREETING), } key, err := store.Add(orig) if err != nil { t.Fatalf("store.Add failed: %v", err) } // replace it with the phony, preserving key for k, _ := range remote.Data { remote.Data[k] = phonyData } _, err = store.Get(key, "testchunk", 3) if err == nil { t.Fatalf("expected an error") } switch e := err.(type) { case untrusted.CorruptError: break default: t.Errorf("error is wrong type %T: %#v", e, e) } }
func (c *casCommand) Setup() (ok bool) { path := filepath.Join(cli.Bazil.Config.DataDir.String(), c.Config.Path) var kvstore kv.KV var err error kvstore, err = kvfiles.Open(path) if err != nil { log.Printf("cannot open CAS: %v", err) return false } if c.Config.Sharing != "" { var secret [32]byte if err := c.getSharingKey(c.Config.Sharing, &secret); err != nil { log.Printf("cannot get sharing key: %q: %v", c.Config.Sharing, err) return false } kvstore = untrusted.New(kvstore, &secret) } c.State.Store = kvchunks.New(kvstore) return true }
func TestSimple(t *testing.T) { remote := &kvmock.InMemory{} secret := &[32]byte{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, } converg := untrusted.New(remote, secret) store := kvchunks.New(converg) orig := &chunks.Chunk{ Type: "testchunk", Level: 3, Buf: []byte(GREETING), } key, err := store.Add(orig) if err != nil { t.Fatalf("store.Add failed: %v", err) } got, err := store.Get(key, "testchunk", 3) if err != nil { t.Fatalf("store.Get failed: %v", err) } if got == nil { t.Fatalf("store.Get gave nil chunk") } if g, e := got.Type, "testchunk"; g != e { t.Errorf("unexpected chunk data: %v != %v", g, e) } if g, e := got.Level, uint8(3); g != e { t.Errorf("unexpected chunk data: %v != %v", g, e) } if g, e := string(got.Buf), GREETING; g != e { t.Errorf("unexpected chunk data: %v != %v", g, e) } }