Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
feat: Enforce EDV document ID format in EDVFormatProvider
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Trider <Derek.Trider@securekey.com>
  • Loading branch information
Derek Trider committed Oct 24, 2020
1 parent f8f6921 commit f69d2ff
Show file tree
Hide file tree
Showing 28 changed files with 455 additions and 219 deletions.
6 changes: 3 additions & 3 deletions component/storage/jsindexeddb/jsindexeddb.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func (s *store) Get(k string) ([]byte, error) {
return []byte(data.Get("value").String()), nil
}

// Iterator returns iterator for the latest snapshot of the underlying db.
func (s *store) Iterator(start, limit string) storage.StoreIterator {
// Range returns iterator for the latest snapshot of the underlying db.
func (s *store) Range(start, limit string) storage.StoreIterator {
if limit == "" {
return newIterator(nil, false, fmt.Errorf("limit key is mandatory"))
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (s *store) Delete(k string) error {
}

// TODO #2229 - implement query method.
func (s *store) Query(query string) (storage.StoreIterator, error) {
func (s *store) Query(name, value string) (storage.StoreIterator, error) {
return nil, storage.ErrQueryingNotSupported
}

Expand Down
8 changes: 4 additions & 4 deletions component/storage/jsindexeddb/jsindexeddb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ func TestStore(t *testing.T) {
require.NoError(t, err)
}

itr := store.Iterator("abc_", "abc_"+storage.EndKeySuffix)
itr := store.Range("abc_", "abc_"+storage.EndKeySuffix)
verifyItr(t, itr, 4, "abc_")

itr = store.Iterator("", "")
itr = store.Range("", "")
verifyItr(t, itr, 0, "")

itr = store.Iterator("abc_", "mno_"+storage.EndKeySuffix)
itr = store.Range("abc_", "mno_"+storage.EndKeySuffix)
verifyItr(t, itr, 7, "")

itr = store.Iterator("abc_", "mno_123")
itr = store.Range("abc_", "mno_123")
verifyItr(t, itr, 6, "")
})
}
Expand Down
4 changes: 2 additions & 2 deletions component/storage/leveldb/leveldb_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type leveldbStore struct {
}

// TODO #2227 - implement query method.
func (s *leveldbStore) Query(_ string) (storage.StoreIterator, error) {
func (s *leveldbStore) Query(name, value string) (storage.StoreIterator, error) {
return nil, storage.ErrQueryingNotSupported
}

Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *leveldbStore) Get(k string) ([]byte, error) {
}

// Iterator returns iterator for the latest snapshot of the underlying db.
func (s *leveldbStore) Iterator(start, limit string) storage.StoreIterator {
func (s *leveldbStore) Range(start, limit string) storage.StoreIterator {
return s.db.NewIterator(&util.Range{
Start: []byte(start),
Limit: []byte(strings.ReplaceAll(limit, storage.EndKeySuffix, "~")),
Expand Down
2 changes: 1 addition & 1 deletion component/storage/leveldb/leveldb_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestLeveldbStore(t *testing.T) {
require.NoError(t, err)
})

t.Run("Test Leveldb store iterator", func(t *testing.T) {
t.Run("Test Leveldb store range", func(t *testing.T) {
prov := NewProvider(path)
store, err := prov.OpenStore("test-iterator")
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/didcomm/protocol/didexchange/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ type mockStore struct {
put func(string, []byte) error
get func(string) ([]byte, error)
delete func(string) error
query func(string) (storage.StoreIterator, error)
query func(string, string) (storage.StoreIterator, error)
}

// Put stores the key and the record.
Expand All @@ -734,12 +734,12 @@ func (m *mockStore) Delete(k string) error {
return m.delete(k)
}

func (m *mockStore) Query(query string) (storage.StoreIterator, error) {
return m.query(query)
func (m *mockStore) Query(name, value string) (storage.StoreIterator, error) {
return m.query(name, value)
}

// Search returns storage iterator.
func (m *mockStore) Iterator(start, limit string) storage.StoreIterator {
func (m *mockStore) Range(start, limit string) storage.StoreIterator {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/didcomm/protocol/introduce/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (s *Service) currentStateName(piID string) (string, error) {

// Actions returns actions for the async usage.
func (s *Service) Actions() ([]Action, error) {
records := s.store.Iterator(
records := s.store.Range(
fmt.Sprintf(transitionalPayloadKey, ""),
fmt.Sprintf(transitionalPayloadKey, storage.EndKeySuffix),
)
Expand Down Expand Up @@ -708,7 +708,7 @@ func (s *Service) saveParticipant(piID string, p *participant) error {
}

func (s *Service) getParticipants(piID string) ([]*participant, error) {
records := s.store.Iterator(fmt.Sprintf(participantsKey, piID, ""),
records := s.store.Range(fmt.Sprintf(participantsKey, piID, ""),
fmt.Sprintf(participantsKey, piID, storage.EndKeySuffix))
defer records.Release()

Expand Down
2 changes: 1 addition & 1 deletion pkg/didcomm/protocol/issuecredential/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func (s *Service) ActionStop(piID string, cErr error) error {

// Actions returns actions for the async usage.
func (s *Service) Actions() ([]Action, error) {
records := s.store.Iterator(
records := s.store.Range(
fmt.Sprintf(transitionalPayloadKey, ""),
fmt.Sprintf(transitionalPayloadKey, storage.EndKeySuffix),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/didcomm/protocol/mediator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (s *Service) Unregister(connID string) error {

// GetConnections returns the connections of the router.
func (s *Service) GetConnections() ([]string, error) {
records := s.routeStore.Iterator(
records := s.routeStore.Range(
fmt.Sprintf(routeConnIDDataKey, ""),
fmt.Sprintf(routeConnIDDataKey, storage.EndKeySuffix),
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/didcomm/protocol/outofband/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (s *Service) HandleInbound(msg service.DIDCommMsg, myDID, theirDID string)

// Actions returns actions for the async usage.
func (s *Service) Actions() ([]Action, error) {
records := s.store.Iterator(
records := s.store.Range(
fmt.Sprintf(transitionalPayloadKey, ""),
fmt.Sprintf(transitionalPayloadKey, storage.EndKeySuffix),
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/didcomm/protocol/outofband/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,15 +1265,15 @@ func (s *stubStore) Get(k string) ([]byte, error) {
panic("implement me")
}

func (s *stubStore) Iterator(start, limit string) storage.StoreIterator {
func (s *stubStore) Range(start, limit string) storage.StoreIterator {
panic("implement me")
}

func (s *stubStore) Delete(k string) error {
panic("implement me")
}

func (s *stubStore) Query(query string) (storage.StoreIterator, error) {
func (s *stubStore) Query(name, value string) (storage.StoreIterator, error) {
panic("implement me")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/didcomm/protocol/presentproof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (s *Service) deleteTransitionalPayload(id string) error {

// Actions returns actions for the async usage.
func (s *Service) Actions() ([]Action, error) {
records := s.store.Iterator(
records := s.store.Range(
fmt.Sprintf(transitionalPayloadKey, ""),
fmt.Sprintf(transitionalPayloadKey, storage.EndKeySuffix),
)
Expand Down
36 changes: 18 additions & 18 deletions pkg/internal/gomocks/storage/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/mock/storage/mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (s *MockStore) Get(k string) ([]byte, error) {
return val, s.ErrGet
}

// Iterator returns an iterator for the underlying mockstore.
func (s *MockStore) Iterator(start, limit string) storage.StoreIterator {
// Range returns an iterator for the underlying mockstore.
func (s *MockStore) Range(start, limit string) storage.StoreIterator {
if s.ErrItr != nil {
return NewMockIteratorWithError(s.ErrItr)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *MockStore) Delete(k string) error {
}

// Query returns a mocked store iterator and error value.
func (s *MockStore) Query(_ string) (storage.StoreIterator, error) {
func (s *MockStore) Query(indexKey, indexValue string) (storage.StoreIterator, error) {
return s.QueryReturnValue, s.ErrQuery
}

Expand Down
16 changes: 9 additions & 7 deletions pkg/storage/edv/documentprocessor/documentprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ func New(jweEncrypter jose.Encrypter, jweDecrypter jose.Decrypter) *DocumentProc
}

// Encrypt creates a new encrypted document based off of the given structured document.
func (a *DocumentProcessor) Encrypt(structuredDocument *edv.StructuredDocument) (*edv.EncryptedDocument, error) {
structuredDocumentBytes, err := a.marshal(structuredDocument)
func (d *DocumentProcessor) Encrypt(structuredDocument *edv.StructuredDocument,
indexedAttributes []edv.IndexedAttributeCollection) (*edv.EncryptedDocument, error) {
structuredDocumentBytes, err := d.marshal(structuredDocument)
if err != nil {
return nil, fmt.Errorf(failMarshalStructuredDocument, err)
}

jwe, err := a.jweEncrypter.Encrypt(structuredDocumentBytes)
jwe, err := d.jweEncrypter.Encrypt(structuredDocumentBytes)
if err != nil {
return nil, fmt.Errorf(failEncryptStructuredDocument, err)
}
Expand All @@ -60,21 +61,22 @@ func (a *DocumentProcessor) Encrypt(structuredDocument *edv.StructuredDocument)
}

encryptedDoc := edv.EncryptedDocument{
ID: structuredDocument.ID,
JWE: []byte(serializedJWE),
ID: structuredDocument.ID,
IndexedAttributeCollections: indexedAttributes,
JWE: []byte(serializedJWE),
}

return &encryptedDoc, nil
}

// Decrypt decrypts the encrypted document into a structured document.
func (a *DocumentProcessor) Decrypt(encryptedDocument *edv.EncryptedDocument) (*edv.StructuredDocument, error) {
func (d *DocumentProcessor) Decrypt(encryptedDocument *edv.EncryptedDocument) (*edv.StructuredDocument, error) {
encryptedJWE, err := jose.Deserialize(string(encryptedDocument.JWE))
if err != nil {
return nil, fmt.Errorf(failDeserializeJWE, err)
}

structuredDocumentBytes, err := a.jweDecrypter.Decrypt(encryptedJWE)
structuredDocumentBytes, err := d.jweDecrypter.Decrypt(encryptedJWE)
if err != nil {
return nil, fmt.Errorf(failDecryptJWE, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/edv/documentprocessor/documentprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ func TestAriesDocumentProcessor_Encrypt(t *testing.T) {
documentProcessor := DocumentProcessor{marshal: failingMarshal}
require.NotNil(t, documentProcessor)

encryptedDocument, err := documentProcessor.Encrypt(nil)
encryptedDocument, err := documentProcessor.Encrypt(nil, nil)
require.EqualError(t, err, fmt.Errorf(failMarshalStructuredDocument, errFailingMarshal).Error())
require.Nil(t, encryptedDocument)
})
t.Run("Fail to encrypt structured document", func(t *testing.T) {
documentProcessor := New(&failingEncrypter{}, nil)
require.NotNil(t, documentProcessor)

encryptedDocument, err := documentProcessor.Encrypt(createStructuredDocument())
encryptedDocument, err := documentProcessor.Encrypt(createStructuredDocument(), nil)
require.EqualError(t, err, fmt.Errorf(failEncryptStructuredDocument, errFailingEncrypter).Error())
require.Nil(t, encryptedDocument)
})
Expand Down Expand Up @@ -145,7 +145,7 @@ func createStructuredDocument() *edv.StructuredDocument {
func createEncryptedDocument(t *testing.T, documentProcessor *DocumentProcessor) *edv.EncryptedDocument {
structuredDocument := createStructuredDocument()

encryptedDocument, err := documentProcessor.Encrypt(structuredDocument)
encryptedDocument, err := documentProcessor.Encrypt(structuredDocument, nil)
require.NoError(t, err)
require.NotNil(t, encryptedDocument)

Expand Down
Loading

0 comments on commit f69d2ff

Please sign in to comment.