This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 288
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 #6 from MCLDG/feature/part5
Feature/part5: Adding a new member to a Fabric network on Amazon Managed Blockchain
- Loading branch information
Showing
13 changed files
with
1,138 additions
and
13 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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,90 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file is distributed | ||
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing | ||
# permissions and limitations under the License. | ||
|
||
function createConfigUpdate { | ||
echo "Creating config update payload for the new member '$MEMBERID'" | ||
cd /opt/home | ||
|
||
# Start the configtxlator | ||
configtxlator start & | ||
configtxlator_pid=$! | ||
echo "configtxlator_pid:$configtxlator_pid" | ||
echo "Sleeping 5 seconds for configtxlator to start..." | ||
sleep 5 | ||
|
||
pushd /tmp | ||
# Remove any previously generated config or protobuf files | ||
rm /tmp/${MEMBERID}_config*.* | ||
rm /tmp/${MEMBERID}_updated*.* | ||
|
||
CTLURL=http://127.0.0.1:7059 | ||
# Convert the config block protobuf to JSON | ||
curl -X POST --data-binary @$BLOCKDIR/$CHANNEL.config.block $CTLURL/protolator/decode/common.Block > ${MEMBERID}_config_block.json | ||
# Extract the config from the config block | ||
jq .data.data[0].payload.data.config ${MEMBERID}_config_block.json > ${MEMBERID}_config.json | ||
|
||
isMemberInChannelConfig ${MEMBERID}_config.json | ||
if [ $? -eq 0 ]; then | ||
echo "Member '$MEMBERID' already exists in the channel config. Config will not be updated. Exiting createConfigUpdate" | ||
return 1 | ||
fi | ||
|
||
# Append the new org configuration information | ||
jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"'$MEMBERID'":.[1]}}}}}' ${MEMBERID}_config.json ${MEMBERID}.json > ${MEMBERID}_updated_config.json | ||
|
||
# Create the config diff protobuf | ||
curl -X POST --data-binary @${MEMBERID}_config.json $CTLURL/protolator/encode/common.Config > ${MEMBERID}_config.pb | ||
curl -X POST --data-binary @${MEMBERID}_updated_config.json $CTLURL/protolator/encode/common.Config > ${MEMBERID}_updated_config.pb | ||
curl -X POST -F original=@${MEMBERID}_config.pb -F updated=@${MEMBERID}_updated_config.pb $CTLURL/configtxlator/compute/update-from-configs -F channel=$CHANNEL > ${MEMBERID}_config_update.pb | ||
|
||
# Convert the config diff protobuf to JSON | ||
curl -X POST --data-binary @${MEMBERID}_config_update.pb $CTLURL/protolator/decode/common.ConfigUpdate > ${MEMBERID}_config_update.json | ||
|
||
# Create envelope protobuf container config diff to be used in the "peer channel update" command to update the channel configuration block | ||
echo '{"payload":{"header":{"channel_header":{"channel_id":"'"${CHANNEL}"'", "type":2}},"data":{"config_update":'$(cat ${MEMBERID}_config_update.json)'}}}' > ${MEMBERID}_config_update_as_envelope.json | ||
curl -X POST --data-binary @${MEMBERID}_config_update_as_envelope.json $CTLURL/protolator/encode/common.Envelope > /tmp/${MEMBERID}_config_update_as_envelope.pb | ||
# copy to the /data directory so the file can be signed by other admins | ||
cp /tmp/${MEMBERID}_config_update_as_envelope.pb $BLOCKDIR | ||
|
||
# Stop configtxlator | ||
kill $configtxlator_pid | ||
echo "Created config update payload for the new organization '$MEMBERID', in file ${BLOCKDIR}/${MEMBERID}_config_update_as_envelope.pb" | ||
|
||
popd | ||
return 0 | ||
} | ||
|
||
# Checks whether the new member already exists in the channel config. This would be true if the member has already been added | ||
# to the channel config | ||
function isMemberInChannelConfig { | ||
if [ $# -ne 1 ]; then | ||
echo "Usage: isMemberInChannelConfig <Config JSON file>" | ||
exit 1 | ||
fi | ||
echo "Checking whether member '$MEMBERID' already exists in the channel config" | ||
local JSONFILE=$1 | ||
|
||
# check if the member exists in the channel config | ||
echo "About to execute jq '.channel_group.groups.Application.groups | contains({$MEMBERID})'" | ||
if cat ${JSONFILE} | jq -e ".channel_group.groups.Application.groups | contains({\"$MEMBERID\"})" > /dev/null; then | ||
echo "Member '$MEMBERID' already exists in the channel config" | ||
return 0 | ||
else | ||
echo "Member '$MEMBERID' does not exist in the channel config. This is expected as we are about to add the member" | ||
return 1 | ||
fi | ||
} | ||
|
||
createConfigUpdate |
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,121 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). | ||
# You may not use this file except in compliance with the License. | ||
# A copy of the License is located at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# or in the "license" file accompanying this file. This file is distributed | ||
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing | ||
# permissions and limitations under the License. | ||
|
||
set +e | ||
|
||
region=us-east-1 | ||
memberID=<your member ID, from the AWS Console> | ||
|
||
# convert memberID to lowercase. S3 buckets must be lower case | ||
memberID=$(echo "$memberID" | tr '[:upper:]' '[:lower:]') | ||
S3BucketNameCreator=${memberID}-creator | ||
S3BucketNameNewMember=${memberID}-newmember | ||
|
||
# copy the certificates for the new Fabric member to S3 | ||
function copyCertsToS3 { | ||
echo "Copying the certs for the new org to S3" | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
aws s3api put-object --bucket $S3BucketNameNewMember --key ${memberID}/admincerts --body /home/ec2-user/admin-msp/admincerts/cert.pem | ||
aws s3api put-object --bucket $S3BucketNameNewMember --key ${memberID}/cacerts --body /home/ec2-user/admin-msp/cacerts/*.pem | ||
aws s3api put-object-acl --bucket $S3BucketNameNewMember --key ${memberID}/admincerts --acl public-read | ||
aws s3api put-object-acl --bucket $S3BucketNameNewMember --key ${memberID}/cacerts --acl public-read | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Copying the certs for the new org to S3 complete" | ||
} | ||
|
||
# copy the certificates for the new Fabric member from S3 to the Fabric creator network | ||
function copyCertsFromS3 { | ||
echo "Copying the certs from S3" | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
mkdir -p /home/ec2-user/${memberID}-msp/admincerts | ||
mkdir -p /home/ec2-user/${memberID}-msp/cacerts | ||
aws s3api get-object --bucket $S3BucketNameNewMember --key ${memberID}/admincerts /home/ec2-user/${memberID}-msp/admincerts/cert.pem | ||
aws s3api get-object --bucket $S3BucketNameNewMember --key ${memberID}/cacerts /home/ec2-user/${memberID}-msp/cacerts/cacert.pem | ||
ls -lR /home/ec2-user/${memberID}-msp/ | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Copying the certs from S3 complete" | ||
} | ||
|
||
# copy the Channel Genesis block from the Fabric creator network to S3 | ||
function copyChannelGenesisToS3 { | ||
echo "Copying the Channel Genesis block to S3" | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
aws s3api put-object --bucket $S3BucketNameCreator --key org0/mychannel.block --body /home/ec2-user/fabric-samples/chaincode/hyperledger/fabric/peer/mychannel.block | ||
aws s3api put-object-acl --bucket $S3BucketNameCreator --key org0/mychannel.block --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers | ||
aws s3api put-object-acl --bucket $S3BucketNameCreator --key org0/mychannel.block --acl public-read | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Copying the Channel Genesis block to S3 complete" | ||
} | ||
|
||
# copy the Channel Genesis block from S3 to the new Fabric member | ||
function copyChannelGenesisFromS3 { | ||
echo "Copying the Channel Genesis block from S3" | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
sudo chown -R ec2-user /home/ec2-user/fabric-samples/chaincode/hyperledger/fabric/peer | ||
aws s3api get-object --bucket $S3BucketNameCreator --key org0/mychannel.block /home/ec2-user/fabric-samples/chaincode/hyperledger/fabric/peer/mychannel.block | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Copying the Channel Genesis block from S3 complete" | ||
} | ||
|
||
# create S3 bucket to copy files from the Fabric network creator. Bucket will be read-only to other members | ||
function createS3BucketForCreator { | ||
#create the s3 bucket | ||
echo -e "creating s3 bucket for network creator: $S3BucketNameCreator" | ||
#quick way of determining whether the AWS CLI is installed and a default profile exists | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
if [[ "$region" == "us-east-1" ]]; then | ||
aws s3api create-bucket --bucket $S3BucketNameCreator --region $region | ||
else | ||
aws s3api create-bucket --bucket $S3BucketNameCreator --region $region --create-bucket-configuration LocationConstraint=$region | ||
fi | ||
aws s3api put-bucket-acl --bucket $S3BucketNameCreator --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers | ||
aws s3api put-bucket-acl --bucket $S3BucketNameCreator --acl public-read | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Creating the S3 bucket complete" | ||
} | ||
|
||
# create S3 bucket to copy files from the new member. Bucket will be read-only to other members | ||
function createS3BucketForNewMember { | ||
#create the s3 bucket | ||
echo -e "creating s3 bucket for new member $NEW_ORG: $S3BucketNameNewMember" | ||
#quick way of determining whether the AWS CLI is installed and a default profile exists | ||
if [[ $(aws configure list) && $? -eq 0 ]]; then | ||
if [[ "$region" == "us-east-1" ]]; then | ||
aws s3api create-bucket --bucket $S3BucketNameNewMember --region $region | ||
else | ||
aws s3api create-bucket --bucket $S3BucketNameNewMember --region $region --create-bucket-configuration LocationConstraint=$region | ||
fi | ||
aws s3api put-bucket-acl --bucket $S3BucketNameNewMember --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers | ||
aws s3api put-bucket-acl --bucket $S3BucketNameNewMember --acl public-read | ||
else | ||
echo "AWS CLI is not configured on this node. To run this script install and configure the AWS CLI" | ||
fi | ||
echo "Creating the S3 bucket complete" | ||
} | ||
|
||
# This is a little hack I found here: https://stackoverflow.com/questions/8818119/how-can-i-run-a-function-from-a-script-in-command-line | ||
# that allows me to call this bash script and invoke a specific function from the command line | ||
"$@" | ||
|
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
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
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
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