-
Notifications
You must be signed in to change notification settings - Fork 0
/
caget.sh
328 lines (285 loc) · 10.3 KB
/
caget.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
set -e
# SDIR=$(dirname "$0")
# 中间ca的url
ENROLLURL=localhost:7055
SDIR=$(
cd "$(dirname "$0")"
pwd
)
# 生成的ca的文件的相对根目录
HOME_DIR=$SDIR/fabric-ca-files
function finishMSPSetup() {
if [ $# -ne 1 ]; then
fatal "Usage: finishMSPSetup <targetMSPDIR>"
fi
if [ ! -d $1/tlscacerts ]; then
mkdir $1/tlscacerts
cp $1/cacerts/* $1/tlscacerts
if [ -d $1/intermediatecerts ]; then
mkdir $1/tlsintermediatecerts
cp $1/intermediatecerts/* $1/tlsintermediatecerts
fi
fi
}
# Copy the org's admin cert into some target MSP directory
# This is only required if ADMINCERTS is enabled.
function copyAdminCert() {
if [ $# -ne 2 ]; then
fatal "Usage: copyAdminCert <adminCertDir> <targetMSPDIR>"
fi
if $ADMINCERTS; then
dstDir=$2/admincerts
mkdir -p $dstDir
cp $1/signcerts/* $dstDir
fi
}
function copyTls() {
if [ $# -ne 1 ]; then
fatal "Usage: copyTls <targetMSPDIR>"
fi
cp $1/keystore/* $1/server.key
sleep 1
cp $1/signcerts/* $1/server.crt
}
function copyRootCA() {
if [ $# -ne 1 ]; then
fatal "Usage: copyTls <targetMSPDIR>"
fi
if [ -f $SDIR/ca/middleCA/ca-chain.pem ]; then
echo "=======copyrootca:路径是:$SDIR/ca/middleCA/ca-chain.pem======"
cp -rf $SDIR/ca/middleCA/ca-chain.pem $1/ca.crt
else
echo "=======copyrootca:没有rootca 需要手动复制======"
fi
}
# 判断路径是否存在
function mkdirPath() {
if [ -d $HOME_DIR ]; then
echo "=========================================="
echo "===============清空 $HOME_DIR/*============="
echo "==========================================="
rm -rf $HOME_DIR/*
fi
}
function initCAAdmin() {
mkdirPath
echo "==============initCAAdmin======="
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/caAdmin
fabric-ca-client enroll -d -u http://admin:vsaCNbZGpOtR@$ENROLLURL
}
function addOrg() {
echo "==============Add orgs======="
fabric-ca-client affiliation list
fabric-ca-client affiliation remove --force org1
fabric-ca-client affiliation remove --force org2
fabric-ca-client affiliation add org1
}
function ordererRegister() {
echo "==============ordererRegister======="
# id:
# name: orderer.fp.com
# type: orderer
# affiliation: org1
# maxenrollments: 0
# attributes:
# POST https://ica-fp.com:7054/register
# {"id":"orderer1.fp.com","type":"orderer","secret":"orderer1.fp.compw","affiliation":"org1"}
sleep 1
fabric-ca-client register -d --id.secret orderer.fp.compw --id.name orderer.fp.com --id.type orderer --id.affiliation org1
# id:
# name: Admin@fp.com
# type: client
# affiliation: org1
# maxenrollments: 0
# attributes:
# - name: admin
# value: true
# ecert: true
# POST https://ica-fp.com:7054/register
# {"id":"admin-fp.com","type":"client","secret":"admin-fp.compw","affiliation":"org1","attrs":[{"name":"admin","value":"true","ecert":true}]}
sleep 1
fabric-ca-client register -d --id.secret Admin.fp.compw --id.name Admin@fp.com --id.type client --id.affiliation org1 --id.attrs admin=true:ecert
}
function orderergetcacert() {
echo "==============orderergetcacert======="
# id:
# name:
# type: client
# affiliation: org1
# maxenrollments: 0
# attributes:
sleep 1
fabric-ca-client getcacert -d -u http://$ENROLLURL -M $HOME_DIR/fp.com/msp --id.type client --id.affiliation org1
# cp $HOME_DIR/fp.com/msp/cacerts/* $HOME_DIR/fp.com/msp/tlscacerts
# cp $HOME_DIR/fp.com/msp/intermediatecerts/* $HOME_DIR/fp.com/msp/tlsintermediatecerts
echo "============home_dir $HOME_DIR============"
sleep 1
finishMSPSetup $HOME_DIR/fp.com/msp
}
function ordererenrolladmin() {
echo "==============ordererenrolladmin======="
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/fp.com/admin
fabric-ca-client enroll -d -u http://Admin@fp.com:Admin.fp.compw@$ENROLLURL --id.affiliation org1
# cp $HOME_DIR/fp.com/admin/msp/signcerts/* $HOME_DIR/fp.com/msp/admincerts/cert.pem
# cp $HOME_DIR/fp.com/admin/msp/signcerts/* $HOME_DIR/fp.com/admin/msp/admincerts
sleep 1
copyAdminCert $HOME_DIR/fp.com/admin/msp $HOME_DIR/fp.com/msp
sleep 1
copyAdminCert $HOME_DIR/fp.com/admin/msp $HOME_DIR/fp.com/admin/msp
}
function orderertls() {
echo "==============orderertls======="
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/fp.com/orderer
fabric-ca-client enroll -d --enrollment.profile tls -u http://orderer.fp.com:orderer.fp.compw@$ENROLLURL -M $HOME_DIR/fp.com/orderer/tls --csr.hosts orderer.fp.com --id.affiliation org1
# cp $HOME_DIR/fp.com/orderer/tls/keystore/* $HOME_DIR/fp.com/orderer/tls/server.key
# cp $HOME_DIR/fp.com/orderer/tls/signcerts/* $HOME_DIR/fp.com/orderer/tls/server.crt
# cp $SDIR/ca/middleCA/ca-chain.pem $HOME_DIR/fp.com/orderer/tls/ca.crt
sleep 1
copyTls $HOME_DIR/fp.com/orderer/tls
sleep 1
copyRootCA $HOME_DIR/fp.com/orderer/tls
# rm -rf /tmp/tls
}
function ordererenroll() {
echo "==============ordererenroll======="
fabric-ca-client enroll -d -u http://orderer.fp.com:orderer.fp.compw@$ENROLLURL -M $HOME_DIR/fp.com/orderer/msp --id.affiliation org1
# cp $HOME_DIR/fp.com/orderer/msp/cacerts/* $HOME_DIR/fp.com/orderer/msp/tlscacerts
# cp $HOME_DIR/fp.com/orderer/msp/intermediatecerts/* $HOME_DIR/fp.com/orderer/msp/tlsintermediatecerts
sleep 1
finishMSPSetup $HOME_DIR/fp.com/orderer/msp
sleep 1
copyAdminCert $HOME_DIR/fp.com/admin/msp $HOME_DIR/fp.com/orderer/msp
}
function peerregister() {
# id:
# name: peer0.org1.fp.com
# type: peer
# affiliation: org1
# maxenrollments: 0
# attributes:
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/caAdmin
# POST https://ica-org1.fp.com:7054/register
# {"id":"peer3.org1.fp.com","type":"peer","secret":"peer3.org1.fp.compw","affiliation":"org1"}
sleep 1
fabric-ca-client register -d --id.secret peer0.org1.fp.compw --id.name peer0.org1.fp.com --id.type peer --id.affiliation org1
sleep 1
fabric-ca-client register -d --id.secret peer1.org1.fp.compw --id.name peer1.org1.fp.com --id.type peer --id.affiliation org1
sleep 1
fabric-ca-client register -d --id.secret peer2.org1.fp.compw --id.name peer2.org1.fp.com --id.type peer --id.affiliation org1
# POST https://ica-org1.fp.com:7054/register
# {"id":"admin-org1.fp.com","type":"client","secret":"admin-org1.fp.compw","affiliation":"org1","attrs":[{"name":"hf.Registrar.Roles","value":"client"},{"name":"hf.Registrar.Attributes","value":"*"},{"name":"hf.Revoker","value":"true"},{"name":"hf.GenCRL","value":"true"},{"name":"admin","value":"true","ecert":true},{"name":"abac.init","value":"true","ecert":true}]}
# name: Admin@fp.com
# type: client
# affiliation: org1
# maxenrollments: 0
# attributes:
# - name: admin
# value: true
# ecert: true
sleep 1
fabric-ca-client register -d --id.name Admin@org1.fp.com --id.secret Admin.org1.fp.compw --id.type client --id.attrs hf.Registrar.Roles=client,hf.Registrar.Attributes=*,hf.Revoker=true,hf.GenCRL=true,admin=true:ecert --id.affiliation org1
# POST https://ica-org1.fp.com:7054/register
# {"id":"user-org1.fp.com","type":"client","secret":"user-org1.fp.compw","affiliation":"org1"}
sleep 1
fabric-ca-client register -d --id.name user@org1.fp.com --id.secret user.org1.fp.compw --id.type client --id.affiliation org1
}
function peergetcacert() {
fabric-ca-client getcacert -d -u http://$ENROLLURL -M $HOME_DIR/org1.fp.com/msp
# cp $HOME_DIR/fp.com/msp/cacerts/* $HOME_DIR/fp.com/msp/tlscacerts
# cp $HOME_DIR/fp.com/msp/intermediatecerts/* $HOME_DIR/fp.com/msp/tlsintermediatecerts
sleep 1
finishMSPSetup $HOME_DIR/org1.fp.com/msp
}
function peergetenrolladmin() {
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/org1.fp.com/admin
fabric-ca-client enroll -d -u http://Admin@org1.fp.com:Admin.org1.fp.compw@$ENROLLURL --id.affiliation org1
# mkdir -p $HOME_DIR/org1.fp.com/msp/admincerts
# cp $HOME_DIR/org1.fp.com/admin/msp/signcerts/* $HOME_DIR/org1.fp.com/msp/admincerts/cert.pem
# mkdir $HOME_DIR/org1.fp.com/admin/msp/admincerts
# cp $HOME_DIR/org1.fp.com/admin/msp/signcerts/* $HOME_DIR/org1.fp.com/admin/msp/admincerts
sleep 1
copyAdminCert $HOME_DIR/org1.fp.com/admin/msp $HOME_DIR/org1.fp.com/admin/msp
sleep 1
copyAdminCert $HOME_DIR/org1.fp.com/admin/msp $HOME_DIR/org1.fp.com/msp
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/org1.fp.com/user
fabric-ca-client enroll -d -u http://user@org1.fp.com:user.org1.fp.compw@$ENROLLURL --id.affiliation org1
}
function peerTLS() {
for ((i = 0; i < 3; i++)); do
peer=peer$i
echo "==============start peerTLS $peer====="
sleep 1
export FABRIC_CA_CLIENT_HOME=$HOME_DIR/org1.fp.com/$peer
fabric-ca-client enroll -d --enrollment.profile tls -u http://$peer.org1.fp.com:$peer.org1.fp.compw@$ENROLLURL -M $HOME_DIR/org1.fp.com/$peer/tls --csr.hosts $peer.org1.fp.com --id.affiliation org1
sleep 1
copyTls $HOME_DIR/org1.fp.com/$peer/tls
sleep 1
copyRootCA $HOME_DIR/org1.fp.com/$peer/tls
sleep 1
fabric-ca-client enroll -d -u http://$peer.org1.fp.com:$peer.org1.fp.compw@$ENROLLURL -M $HOME_DIR/org1.fp.com/$peer/msp --id.affiliation org1
# cp$HOME_DIR/org1.fp.com/peer0/msp/cacerts/* $HOME_DIR/org1.fp.com/peer0/msp/tlscacerts
# cp $HOME_DIR/org1.fp.com/peer0/msp/intermediatecerts/* $HOME_DIR/org1.fp.com/peer0/tlsintermediatecerts
sleep 1
finishMSPSetup $HOME_DIR/org1.fp.com/$peer/msp
sleep 1
copyAdminCert $HOME_DIR/org1.fp.com/admin/msp $HOME_DIR/org1.fp.com/$peer/msp
done
}
# Ask user for confirmation to proceed
function askProceed() {
read -p "Continue? [Y/n] " ans
case "$ans" in
y | Y | "")
echo "proceeding ..."
;;
n | N)
echo "exiting..."
exit 1
;;
*)
echo "invalid response"
askProceed
;;
esac
}
function printHelp() {
echo "======./caget.sh url 例: ./caget.sh localhost:7055======"
}
# 生成ca的流程
function control() {
echo "#####################################################"
echo "####################get CA start#######################"
echo "#####################################################"
initCAAdmin
addOrg
ordererRegister
orderergetcacert
ordererenrolladmin
orderertls
ordererenroll
peerregister
peergetcacert
peergetenrolladmin
peerTLS
echo "#####################################################"
echo "####################get CA down#######################"
echo "#####################################################"
# 复制证书
scripts/cacopy.sh
}
MODE=$1
if [ "${MODE}" == "help" ]; then
printHelp
exit 1
elif [ "${MODE}" != "" ]; then
ENROLLURL=$MODE
fi
echo "==============use:ca--url:$ENROLLURL=========="
askProceed
control
exit 0