Generate Certificates for Hyperledger Fabric with Fabric CA

#/bin/bash

export ORG_NAME=org1
export ORG_NAME_2=org2

export WORK_DIR=/tmp/contoz-generate-certs
export TYPE=msp
export FABRIC_CA_CLIENT_HOME=$WORK_DIR/fabric-ca/clients/admin
export ADMIN_USER=admin
export ADMIN_PASSWORD=adminpw
export CA_HOST=localhost:444
export ORDERER_USER=orderer
export ORDERER_PASSWORD=ordererpw
export CRS_NAMES=”–csr.names C=DE,ST=BW,L=Stuttgart,O=Hyperledger,OU=Contoz”

rm -rf crypto-config
mkdir -p $FABRIC_CA_CLIENT_HOME

openssl s_client -showcerts -connect $CA_HOST /dev/null|openssl x509 -outform PEM > $FABRIC_CA_CLIENT_HOME/client.pem

fabric-ca-client enroll -u https://$ADMIN_USER:$ADMIN_PASSWORD@$CA_HOST –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem

function register() {
ID_TYPE=$1
AFFFI=””
ATTR=””

if [ -z “$3” ]
then
echo “\$ATTR is empty”
else
ATTR= “–id.attrs “$3
fi

if [ -z “$2” ]
then
echo “\$AFFFI is empty”
else
AFFFI= “–id.affiliation “$2
fi

fabric-ca-client register –id.name $USER –id.type $ID_TYPE –id.secret $PASSWORD $AFFFI $ATTR –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem
}

function enroll_adminuser (){

ADMIN_PATH=$1
rm -rf $ADMIN_PATH
mkdir -p $ADMIN_PATH/signcerts
mkdir -p $ADMIN_PATH/admincerts
mkdir -p $ADMIN_PATH/cacerts
mkdir -p $ADMIN_PATH/keystore

fabric-ca-client enroll -u https://$USER:$PASSWORD@$CA_HOST -M enroll_admin –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem –csr.hosts $HOST #–enrollment.attrs $ATTR
cp $FABRIC_CA_CLIENT_HOME/enroll_admin/signcerts/* $ADMIN_PATH/signcerts/
cp $FABRIC_CA_CLIENT_HOME/enroll_admin/signcerts/* $ADMIN_PATH/admincerts/
cp $FABRIC_CA_CLIENT_HOME/enroll_admin/cacerts/* $ADMIN_PATH/cacerts/
cp $FABRIC_CA_CLIENT_HOME/enroll_admin/keystore/* $ADMIN_PATH/keystore/
rm -rf $FABRIC_CA_CLIENT_HOME/enroll_admin
}

function enroll_admincert (){

SRC_ADMINCERT_PATH=$1
DEST_ADMINCERT_PATH=$2
rm -rf $DEST_ADMINCERT_PATH
mkdir -p $DEST_ADMINCERT_PATH
cp $SRC_ADMINCERT_PATH/* $DEST_ADMINCERT_PATH/
}

function enroll_cacert (){
CACERT_PATH=$1
rm -rf $CACERT_PATH
mkdir -p $CACERT_PATH
fabric-ca-client enroll -u https://$USER:$PASSWORD@$CA_HOST -M enroll_cacert –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem –csr.hosts $HOST
cp $FABRIC_CA_CLIENT_HOME/enroll_cacert/cacerts/* $CACERT_PATH/
rm -rf $FABRIC_CA_CLIENT_HOME/enroll_cacert
}

function enroll_tlscacert (){
TSLCACERT_PATH=$1
rm -rf $TSLCACERT_PATH
mkdir -p $TSLCACERT_PATH
fabric-ca-client enroll -u https://$USER:$PASSWORD@$CA_HOST -M enroll_ca_tls –enrollment.profile tls –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem –csr.hosts $HOST
cp $FABRIC_CA_CLIENT_HOME/enroll_ca_tls/tlscacerts/* $TSLCACERT_PATH/
rm -rf $FABRIC_CA_CLIENT_HOME/enroll_ca_tls
}

function copy (){
SRC_PATH=$1
DEST_PATH=$2
rm -rf $DEST_PATH
mkdir -p $DEST_PATH
cp $SRC_PATH/* $DEST_PATH/

}

function enroll_signcerts (){
SIGNCERT_PATH=$1
rm -rf $SIGNCERT_PATH
mkdir -p $SIGNCERT_PATH/signcerts
mkdir -p $SIGNCERT_PATH/keystore
fabric-ca-client enroll -u https://$USER:$PASSWORD@$CA_HOST -M enroll_signcerts –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem –csr.hosts $HOST
cp $FABRIC_CA_CLIENT_HOME/enroll_signcerts/signcerts/* $SIGNCERT_PATH/signcerts
cp $FABRIC_CA_CLIENT_HOME/enroll_signcerts/keystore/* $SIGNCERT_PATH/keystore/
rm -rf $FABRIC_CA_CLIENT_HOME/enroll_signcerts
}

function enroll_orderer (){

ORDERER=$1
USER=$ORDERER.contoz.com
PASSWORD=contoz_pw
HOST=cli,$ORDERER,$ORDERER.$ORG_NAME
register peer
enroll_signcerts crypto-config/ordererOrganizations/contoz.com/orderers/$ORDERER.contoz.com/msp
enroll_tls crypto-config/ordererOrganizations/contoz.com/orderers/$ORDERER.contoz.com/tls
copy crypto-config/ordererOrganizations/contoz.com/msp/admincerts crypto-config/ordererOrganizations/contoz.com/orderers/$ORDERER.contoz.com/msp/admincerts
copy crypto-config/ordererOrganizations/contoz.com/msp/cacerts crypto-config/ordererOrganizations/contoz.com/orderers/$ORDERER.contoz.com/msp/cacerts
copy crypto-config/ordererOrganizations/contoz.com/msp/tlscacerts crypto-config/ordererOrganizations/contoz.com/orderers/$ORDERER.contoz.com/msp/tlscacerts
}

function enroll_tls (){

export MSP_PATH=$1

rm -rf $MSP_PATH
mkdir -p $MSP_PATH
fabric-ca-client enroll -u https://$USER:$PASSWORD@$CA_HOST -M enroll_tls –enrollment.profile tls –tls.certfiles $FABRIC_CA_CLIENT_HOME/client.pem –csr.hosts $HOST

PRIV_KEY=$(ls $FABRIC_CA_CLIENT_HOME/enroll_tls/keystore/*_sk)
CA_PEM=$(ls $FABRIC_CA_CLIENT_HOME/enroll_tls/tlscacerts/*.pem)

cp $PRIV_KEY $MSP_PATH/server.key
cp $CA_PEM $MSP_PATH/ca.crt
cp $FABRIC_CA_CLIENT_HOME/enroll_tls/signcerts/cert.pem $MSP_PATH/server.crt

#openssl x509 -outform der -in $FABRIC_CA_CLIENT_HOME/enroll_tls/signcerts/cert.pem -out $MSP_PATH/server.crt
#openssl x509 -outform der -in $FABRIC_CA_CLIENT_HOME/enroll_tls/tlscacerts/ca.crt -out $MSP_PATH/ca.crt
rm -rf $FABRIC_CA_CLIENT_HOME/enroll_tls

}

create_config_yaml (){
CY_PATH=$1
echo “NodeOUs:
Enable: true
ClientOUIdentifier:
Certificate: cacerts/localhost-444.pem
OrganizationalUnitIdentifier: client
PeerOUIdentifier:
Certificate: cacerts/localhost-444.pem
OrganizationalUnitIdentifier: peer” >> $CY_PATH
}

function enroll_peer (){

PEER=$1
ORG=$2
USER=$PEER.$ORG.contoz.com
PASSWORD=contoz_pw
HOST=cli,$PEER,$PEER.$ORG
register peer
enroll_signcerts crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/msp
enroll_tls crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/tls
copy crypto-config/peerOrganizations/$ORG.contoz.com/msp/admincerts crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/msp/admincerts
copy crypto-config/peerOrganizations/$ORG.contoz.com/msp/cacerts crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/msp/cacerts
copy crypto-config/peerOrganizations/$ORG.contoz.com/msp/tlscacerts crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/msp/tlscacerts
create_config_yaml crypto-config/peerOrganizations/$ORG.contoz.com/peers/$PEER.$ORG.contoz.com/msp/config.yaml
}

enroll_user (){
USER=$1@$ORG_NAME.contoz.com
PASSWORD=$1_contoz_pw
register $4
HOST=$2
enroll_adminuser $3/$1@$ORG_NAME.contoz.com/msp
enroll_tlscacert $3/$1@$ORG_NAME.contoz.com/msp/tlscacerts
enroll_tls $3/$1@$ORG_NAME.contoz.com/tls
create_config_yaml $3/$1@$ORG_NAME.contoz.com/msp/config.yaml

}

USER=Admin@contoz.com
PASSWORD=admin_contoz_pw
HOST=cli,orderer1,orderer1.$ORG_NAME,orderer2,orderer2.$ORG_NAME,orderer3,orderer3.$ORG_NAME,orderer4,orderer4.$ORG_NAME,orderer5,orderer4.$ORG_NAME
register client
enroll_adminuser crypto-config/ordererOrganizations/contoz.com/users/Admin@contoz.com/msp
enroll_tlscacert crypto-config/ordererOrganizations/contoz.com/users/Admin@contoz.com/msp/tlscacerts
enroll_tls crypto-config/ordererOrganizations/contoz.com/users/Admin@contoz.com/tls

enroll_admincert crypto-config/ordererOrganizations/contoz.com/users/Admin@contoz.com/msp/admincerts crypto-config/ordererOrganizations/contoz.com/msp/admincerts
enroll_cacert crypto-config/ordererOrganizations/contoz.com/msp/cacerts
enroll_tlscacert crypto-config/ordererOrganizations/contoz.com/msp/tlscacerts

enroll_orderer orderer1
enroll_orderer orderer2
enroll_orderer orderer3
enroll_orderer orderer4
enroll_orderer orderer5

USER=Admin@$ORG_NAME.contoz.com
PASSWORD=admin_contoz_pw
register client
HOST=cli,peer1,peer1.$ORG_NAME,peer2,peer2.$ORG_NAME,peer3,peer3.$ORG_NAME,peer4,peer4.$ORG_NAME,peer5,peer4.$ORG_NAME
enroll_adminuser crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users/Admin@$ORG_NAME.contoz.com/msp
enroll_tlscacert crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users/Admin@$ORG_NAME.contoz.com/msp/tlscacerts
enroll_tls crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users/Admin@$ORG_NAME.contoz.com/tls

enroll_admincert crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users/Admin@$ORG_NAME.contoz.com/msp/admincerts crypto-config/peerOrganizations/$ORG_NAME.contoz.com/msp/admincerts
enroll_cacert crypto-config/peerOrganizations/$ORG_NAME.contoz.com/msp/cacerts
enroll_tlscacert crypto-config/peerOrganizations/$ORG_NAME.contoz.com/msp/tlscacerts
create_config_yaml crypto-config/peerOrganizations/$ORG_NAME.contoz.com/msp/config.yaml

enroll_peer peer0 $ORG_NAME
enroll_peer peer1 $ORG_NAME
enroll_peer peer2 $ORG_NAME
enroll_peer peer3 $ORG_NAME
enroll_peer peer4 $ORG_NAME

enroll_user User1 mwc1,mwc1.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users client
enroll_user User2 mwc2,mwc2.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users client
enroll_user User3 mwc3,mwc3.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users client
enroll_user User4 mwc4,mwc4.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users client
enroll_user User5 mwc5,mwc5.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users client

enroll_user User6 mwa1,mwa1.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users user
enroll_user User7 mwa2,mwa2.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users user
enroll_user User8 mwa3,mwa3.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users user
enroll_user User9 mwa4.mwa4.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users user
enroll_user User10 mwa5,mwa5.$ORG_NAME crypto-config/peerOrganizations/$ORG_NAME.contoz.com/users user

USER=Admin@$ORG_NAME_2.contoz.com
PASSWORD=admin_contoz_pw
register client
HOST=cli,peer1,peer1.$ORG_NAME_2,peer2,peer2.$ORG_NAME_2,peer3,peer3.$ORG_NAME_2,peer4,peer4.$ORG_NAME_2,peer5,peer4.$ORG_NAME
enroll_adminuser crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users/Admin@$ORG_NAME_2.contoz.com/msp
enroll_tlscacert crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users/Admin@$ORG_NAME_2.contoz.com/msp/tlscacerts
enroll_tls crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users/Admin@$ORG_NAME_2.contoz.com/tls

enroll_admincert crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users/Admin@$ORG_NAME_2.contoz.com/msp/admincerts crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/msp/admincerts
enroll_cacert crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/msp/cacerts
enroll_tlscacert crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/msp/tlscacerts
create_config_yaml crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/msp/config.yaml

enroll_peer peer0 $ORG_NAME_2
enroll_peer peer1 $ORG_NAME_2
enroll_peer peer2 $ORG_NAME_2
enroll_peer peer3 $ORG_NAME_2
enroll_peer peer4 $ORG_NAME_2

enroll_user User1 mwc1,mwc1.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users client
enroll_user User2 mwc2,mwc2.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users client
enroll_user User3 mwc3,mwc3.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users client
enroll_user User4 mwc4,mwc4.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users client
enroll_user User5 mwc5,mwc5.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users client

enroll_user User6 mwa1,mwa1.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users user
enroll_user User7 mwa2,mwa2.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users user
enroll_user User8 mwa3,mwa3.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users user
enroll_user User9 mwa4.mwa4.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users user
enroll_user User10 mwa5,mwa5.$ORG_NAME_2 crypto-config/peerOrganizations/$ORG_NAME_2.contoz.com/users user

tree crypto-config
#tree $FABRIC_CA_CLIENT_HOME

rm -rf $FABRIC_CA_CLIENT_HOME

Advertisements

Scala Parser for YAML File


import java.io.{File, FileInputStream}
import java.text.SimpleDateFormat
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor

object Main {
def main(args: Array[String]): Unit = {
val text = scala.io.Source.fromInputStream(getClass.getResourceAsStream(
"codes.yaml")).mkString
val yaml = new Yaml
val obj = yaml.load(text)
val codes = obj.asInstanceOf[java.util.LinkedHashMap[String,java.util.Map[String, Any]]]
var values = codes.get("duration").get("values").asInstanceOf[java.util.ArrayList
[java.util.Map[String,Any]]]
values.toArray.foreach(c=> println(c.asInstanceOf[java.util.LinkedHashMap[String,Any]]
.get("code")))
}
}

codes.yaml:

duration:
values:
-
code: 1
from: 0
to: 1
-
code: 2
from: 1
to: 3

build.sbt

libraryDependencies += "org.yaml" % "snakeyaml" % "1.8"

WrodCount MapReduce with Scalding

git clone https://github.com/scalding-io/ProgrammingWithScalding

cd ProgrammingWithScalding/chapter2/

mvn clean install

hadoop fs -mkdir -p /data/input

hadoop fs -mkdir -p /data/output

echo “This is a happy day. A day to remember” > /tmp/input.txt

hadoop fs -put /tmp/input.txt /data/input

hadoop jar /root/repo/ProgrammingWithScalding/chapter2/target/chapter2-0-jar-with-dependencies.jar com.twitter.scalding.Tool WordCountJob –local –input /data/input/input.txt –output /data/output/output.txt

cat /data/output/output.txt
a 2
day 1
day. 1
happy 1
is 1
remember 1
this 1
to 1

Wordcount Example With Apache Spark

mkdir wordcount-spark
cd wordcount-spark

mkdir -p src/main/scala

cat <<EOF > build.sbt
name := "wordcount"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "org.apache.spark" %% "spark-core" % "1.6.0"
EOF

touch src/main/scala/SparkWordCount.scala

import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object SparkWordCount {
def main(args: Array[String]) {
// create Spark context with Spark configuration
val sc = new SparkContext(new SparkConf().setAppName("Spark Count"))

// get threshold
val threshold = args(1).toInt

// read in text file and split each document into words
val tokenized = sc.textFile(args(0)).flatMap(_.split(" "))

// count the occurrence of each word
val wordCounts = tokenized.map((_, 1)).reduceByKey(_ + _)

// filter out words with fewer than threshold occurrences
val filtered = wordCounts.filter(_._2 >= threshold)

// count characters
val charCounts = filtered.flatMap(_._1.toCharArray).map((_, 1)).reduceByKey(_ + _)

System.out.println(charCounts.collect().mkString(", "))
}
}

sbt package

cat <<EOF > /tmp/wordcount.txt
Hello world, Hello
EOF

cp target/scala-2.11/workcount_2.11-1.0.jar /tmp/

cd $SPARK_HOME
./bin/spark-submit --master "local[*]" --class SparkWordCount /tmp/wordcount_2.11-1.0.jar /tmp/wordcount.txt 1