Skip to content

Commit

Permalink
Merge pull request #2 from hyperledger/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
XuHugo authored Dec 29, 2020
2 parents 7fc5242 + 1aabaab commit b50383b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 3 deletions.
6 changes: 3 additions & 3 deletions build_image/dockerhub/v0.9.0/user-dashboard/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2966,9 +2966,9 @@ inherits@2.0.3:
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=

ini@^1.3.0, ini@^1.3.4, ini@~1.3.0:
version "1.3.5"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"
integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==
version "1.3.8"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==

inquirer@^0.12.0:
version "0.12.0"
Expand Down
5 changes: 5 additions & 0 deletions src/api-engine/api/lib/pki/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# SPDX-License-Identifier: Apache-2.0
#
from .cryptogen.cryptogen import CryptoGen
from .cryptogen.cryptocfg import CryptoConfig
3 changes: 3 additions & 0 deletions src/api-engine/api/lib/pki/cryptogen/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# SPDX-License-Identifier: Apache-2.0
#
105 changes: 105 additions & 0 deletions src/api-engine/api/lib/pki/cryptogen/cryptocfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#
# SPDX-License-Identifier: Apache-2.0
#
import yaml
import os
from api.config import CELLO_HOME


class CryptoConfig:
"""Class represents crypto-config yaml."""

def __init__(self, name, file="crypto-config.yaml", country="CN", locality="BJ", province="CP", enablenodeous=True, filepath=CELLO_HOME):
"""init CryptoConfig
param:
name: organization's name
file: crypto-config.yaml
country: country
locality: locality
province: province
enablenodeous: enablenodeous
filepath: cello's working directory
return:
"""
self.filepath = filepath
self.name = name
self.country = country
self.locality = locality
self.province = province
self.enablenodeous = enablenodeous
self.file = file

def create(self) -> None:
"""create the crypto-config.yaml
param
return:
"""
try:
network = {}
for item in ["Peer", "Orderer"]:
org = []
ca = dict(Country=self.country,
Locality=self.locality,
Province=self.province)
specs = []
# for host in org_info["Specs"]:
# specs.append(dict(Hostname=host))
if item == "Peer":
org.append(dict(Domain=self.name,
Name=self.name.split(".")[0].capitalize(),
CA=ca,
Specs=specs,
EnableNodeOUs=self.enablenodeous))
network = {'PeerOrgs': org}
else:
org.append(dict(Domain=self.name.split(".", 1)[1],
Name=self.name.split(".")[0].capitalize() + item,
CA=ca,
Specs=specs,
EnableNodeOUs=self.enablenodeous))
network['OrdererOrgs'] = org

os.system('mkdir -p {}/{}'.format(self.filepath, self.name))

with open('{}/{}/{}'.format(self.filepath, self.name, self.file), 'w', encoding='utf-8') as f:
yaml.dump(network, f)
except Exception as e:
err_msg = "CryptoConfig create failed for {}!".format(e)
raise Exception(err_msg)

def update(self, org_info: any) -> None:
"""update the crypto-config.yaml
param:
org_info: Node of type peer or orderer
return:
"""
try:
with open('{}/{}/{}'.format(self.filepath, self.name, self.file), 'r+', encoding='utf-8') as f:
network = yaml.load(f, Loader=yaml.FullLoader)
if org_info["type"] == "peer":
orgs = network['PeerOrgs']
else:
orgs = network['OrdererOrgs']

for org in orgs:
specs = org["Specs"]
for host in org_info["Specs"]:
specs.append(dict(Hostname=host))

with open('{}/{}/{}'.format(self.filepath, self.name, self.file), 'w', encoding='utf-8') as f:
yaml.dump(network, f)
except Exception as e:
err_msg = "CryptoConfig update failed for {}!".format(e)
raise Exception(err_msg)

def delete(self):
"""delete the crypto-config.yaml
param:
return:
"""
try:
os.system('rm -rf {}/{}'.format(self.filepath, self.name))
except Exception as e:
err_msg = "CryptoConfig delete failed for {}!".format(e)
raise Exception(err_msg)

52 changes: 52 additions & 0 deletions src/api-engine/api/lib/pki/cryptogen/cryptogen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# SPDX-License-Identifier: Apache-2.0
#
from subprocess import call
from api.config import CELLO_HOME, FABRIC_TOOL


class CryptoGen:
"""Class represents crypto-config tool."""

def __init__(self, name, filepath=CELLO_HOME, cryptogen=FABRIC_TOOL, version="2.2.0"):
"""init CryptoGen
param:
name: organization's name
cryptogen: tool path
version: version
filepath: cello's working directory
return:
"""
self.cryptogen = cryptogen + "/cryptogen"
self.filepath = filepath
self.version = version
self.name = name

def generate(self, output="crypto-config", config="crypto-config.yaml"):
"""Generate key material
param:
output: The output directory in which to place artifacts
config: The configuration template to use
return:
"""
try:
call([self.cryptogen, "generate", "--output={}/{}/{}".format(self.filepath, self.name, output),
"--config={}/{}/{}".format(self.filepath, self.name, config)])
except Exception as e:
err_msg = "cryptogen generate fail for {}!".format(e)
raise err_msg

def extend(self, input="crypto-config", config="crypto-config.yaml"):
"""Extend existing network
param:
input: The input directory in which existing network place
config: The configuration template to use
return:
"""
try:
call([self.cryptogen, "extend", "--input={}/{}/{}".format(self.filepath, self.name, input),
"--config={}/{}/{}".format(self.filepath, self.name, config)])
except Exception as e:
err_msg = "cryptogen extend fail for {}!".format(e)
raise err_msg

0 comments on commit b50383b

Please sign in to comment.