Skip to content

Commit

Permalink
Merge pull request #38 from RohanShrothrium/CouchDBWalelt
Browse files Browse the repository at this point in the history
CouchDBWallet
  • Loading branch information
dexhunter committed Mar 27, 2020
2 parents 566f7de + 8f89f71 commit bd05641
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ci/azure-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ steps:
versionSpec: '3.6'
- script: pip install virtualenv
displayName: Install VirtualEnv
- script: |
sudo snap install couchdb
sudo snap set couchdb admin=admin
sudo snap start couchdb
sudo snap connect couchdb:mount-observe
sudo snap connect couchdb:process-control
- script: sleep 5
- script: curl localhost:5984
displayName: Install CouchDB
- script: make check
displayName: Run Checks
50 changes: 50 additions & 0 deletions hfc/fabric_network/couchdbwalletstore.py
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
6 changes: 3 additions & 3 deletions hfc/fabric_network/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def create_user(self, enrollment_id, org, msp_id, state_store=None):
if not self.exists(enrollment_id):
raise AttributeError('"user" does not exist')
state_store = FileKeyValueStore(self._path)
cert_path = self._path + '/' + enrollment_id + '/' + 'private_sk'
key_path = self._path + '/' + enrollment_id + '/' + 'enrollmentCert.pem'
key_path = self._path + '/' + enrollment_id + '/' + 'private_sk'
cert_path = self._path + '/' + enrollment_id + '/' + 'enrollmentCert.pem'
user = create_user(enrollment_id, org, state_store, msp_id, key_path, cert_path)
return user

Expand Down Expand Up @@ -70,5 +70,5 @@ def CreateIdentity(self, Wallet):
f.close()

f = open(sub_directory+'enrollmentCert.pem', 'wb')
f.write(self._PrivateKey)
f.write(self._EnrollmentCert)
f.close()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ requests == 2.20.0
rx >= 3.0.1
six >= 1.4.0
protobuf >= 3.6.0
couchdb >= 1.2
58 changes: 58 additions & 0 deletions test/integration/couchdbwalletstore_test.py
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()

0 comments on commit bd05641

Please sign in to comment.