-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from RohanShrothrium/CouchDBWalelt
CouchDBWallet
- Loading branch information
Showing
5 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import couchdb | ||
|
||
from cryptography.hazmat.primitives import serialization | ||
|
||
from hfc.fabric_ca.caservice import Enrollment | ||
|
||
|
||
class CouchDBWalletStore(object): | ||
""" | ||
CouchDBWalletStore stores the identities of users and admins | ||
in a CouchDB with given config | ||
ie. it contains the Private Key and Enrollment Certificate | ||
""" | ||
def __init__(self, dbName, config='http://localhost:5984'): | ||
self.server = couchdb.Server(config) | ||
try: | ||
self.db = self.server[dbName] | ||
except Exception: | ||
self.db = self.server.create(dbName) | ||
|
||
def exists(self, enrollment_id): | ||
""" | ||
Returns whether or not the creds of a user with a given user_id | ||
exists in the wallet | ||
""" | ||
try: | ||
self.db[enrollment_id] | ||
return True | ||
except Exception: | ||
return False | ||
|
||
def remove(self, enrollment_id): | ||
""" | ||
deletes identities of user with given enrollment_id | ||
""" | ||
self.db.delete(self.db[enrollment_id]) | ||
|
||
def put(self, enrollment_id, user_enrollment): | ||
""" | ||
Saves the particular Identity in the wallet | ||
""" | ||
if not isinstance(user_enrollment, Enrollment): | ||
raise ValueError('"user_enrollment" is not a valid Enrollment object') | ||
PrivateKey = user_enrollment.private_key.private_bytes(encoding=serialization.Encoding.PEM, | ||
format=serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm=serialization.NoEncryption() | ||
).decode() | ||
EnrollmentCert = user_enrollment.cert.decode() | ||
doc = {'EnrollmentCert': EnrollmentCert, 'PrivateKey': PrivateKey} | ||
self.db[enrollment_id] = doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ requests == 2.20.0 | |
rx >= 3.0.1 | ||
six >= 1.4.0 | ||
protobuf >= 3.6.0 | ||
couchdb >= 1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import unittest | ||
import time | ||
|
||
from test.integration.utils import cli_call | ||
|
||
from hfc.fabric_ca.caservice import ca_service | ||
from hfc.fabric_network.couchdbwalletstore import CouchDBWalletStore | ||
|
||
ENROLLMENT_ID = "admin" | ||
ENROLLMENT_SECRET = "adminpw" | ||
DB_NAME = 'wallet' | ||
|
||
|
||
class WalletTest(unittest.TestCase): | ||
def setUp(self): | ||
self._enrollment_id = ENROLLMENT_ID | ||
self._enrollment_secret = ENROLLMENT_SECRET | ||
self._dbName = DB_NAME | ||
if os.getenv("CA_ADDR"): | ||
self._ca_server_address = os.getenv("CA_ADDR") | ||
else: | ||
self._ca_server_address = "localhost:7054" | ||
self._compose_file_path = os.path.normpath( | ||
os.path.join(os.path.dirname(__file__), | ||
"../fixtures/ca/docker-compose.yml") | ||
) | ||
self.start_test_env() | ||
|
||
def tearDown(self): | ||
self.shutdown_test_env() | ||
|
||
def start_test_env(self): | ||
cli_call(["docker-compose", "-f", self._compose_file_path, "up", "-d"]) | ||
time.sleep(5) | ||
|
||
def shutdown_test_env(self): | ||
cli_call(["docker-compose", "-f", self._compose_file_path, "down"]) | ||
|
||
def test_enroll(self): | ||
casvc = ca_service("http://" + self._ca_server_address) | ||
adminEnrollment = casvc.enroll(self._enrollment_id, | ||
self._enrollment_secret) | ||
|
||
server = CouchDBWalletStore(DB_NAME) | ||
server.put(self._enrollment_id, adminEnrollment) | ||
|
||
self.assertTrue(server.exists(self._enrollment_id)) | ||
|
||
server.remove(self._enrollment_id) | ||
|
||
self.assertFalse(server.exists(self._enrollment_id)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |