Skip to content

Commit

Permalink
Merge pull request #211 from dengyi9/master
Browse files Browse the repository at this point in the history
CA Management. #202
  • Loading branch information
dengyi9 authored Mar 28, 2019
2 parents a114ac3 + b75a37f commit 135c9ba
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 28 deletions.
1 change: 1 addition & 0 deletions fabric/v1.1/fabcar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hfc-key-store
8 changes: 8 additions & 0 deletions fabric/v1.1/fabcar/desktopConfigCAAdmin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
peerGrpcUrl: grpc://localhost:7051
peerEventUrl: grpc://localhost:7053
ordererUrl: grpc://localhost:7050
mspId: Org1MSP
# 私钥和证书,每次需手动执行node enrollAdmin.js生成,并手动hfc-key-store/admin复制出证书创建新文件,并修改私钥对应的文件
certificate: hfc-key-store/admin.crt
privateKey: hfc-key-store/474b19c5aff786e6db33b89b94c8daa6b3336183e843c1e58630450c53f5bd96-priv
caServerUrl: http://localhost:7054
3 changes: 3 additions & 0 deletions src/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ Common.REVOKE_USERNAME = 'User ID';
Common.REVOKE_OPTIONAL = 'Optional';
Common.REVOKE_CONFIRM = 'Revoke';

Common.GENERATE_CRL = 'GENERATE CERTIFICATE REVOKE LIST--CRL';
Common.GENERATE_CRL_OPTIONAL = 'Optional';
Common.GENERATE_CRL_CONFIRM = 'Get';

Common.WARN = {
chaincodeName: 'chaincode name can not be null!',
Expand Down
3 changes: 3 additions & 0 deletions src/common/common_cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Common.REVOKE_USERNAME = '用户ID';
Common.REVOKE_OPTIONAL = '其他参数';
Common.REVOKE_CONFIRM = '吊销';

Common.GENERATE_CRL = '证书吊销列表(CRL)获取';
Common.GENERATE_CRL_OPTIONAL = '其他参数';
Common.GENERATE_CRL_CONFIRM = '获取';

Common.WARN = {
chaincodeName: '链码名称不能为空!',
Expand Down
11 changes: 8 additions & 3 deletions src/components/content/CARegisterContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ export default class CARegisterContent extends React.Component {
};
const DivStyle = {
width: '560px',
marginBottom: '30px',
marginBottom: '10px',
};
const titleStyle = {
fontSize: '130%',
marginTop: '20px',
textAlign: 'center',
};
const ButtonStyle = {
marginLeft: '20px',
Expand All @@ -190,7 +195,7 @@ export default class CARegisterContent extends React.Component {
return (
<div style={outerDivStyle}>

<div>{this.state.Common.REGISTER}</div>
<div style={titleStyle}>{this.state.Common.REGISTER}</div>
<div style={DivStyle}>
<span style={asteriskStyle}>*</span>
<span style={spanStyle}>{this.state.Common.REGISTER_USERNAME}</span>
Expand Down Expand Up @@ -220,7 +225,7 @@ export default class CARegisterContent extends React.Component {
/>
</div>

<div >{this.state.Common.ENROLL}</div>
<div style={titleStyle}>{this.state.Common.ENROLL}</div>
<div style={DivStyle}>
<span style={asteriskStyle}>*</span>
<span style={spanStyle}>{this.state.Common.ENROLL_USERNAME}</span>
Expand Down
59 changes: 55 additions & 4 deletions src/components/content/CAUpdateContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ export default class CAUpdateContent extends React.Component {
revokeUserName: '',
revokeOptional: '',
revokeResult: '',

generateCrlOptional: '',
generateCrlResult: '',
};

this.onChangeReenrollOptional = this.onChangeReenrollOptional.bind(this);
this.onChangeRevokeUserName = this.onChangeRevokeUserName.bind(this);
this.onChangeRevokeOptional = this.onChangeRevokeOptional.bind(this);
this.onChangeRevokeOptional = this.onChangeRevokeOptional.bind(this);
this.handleReenroll = this.handleReenroll.bind(this);
this.handleRevoke = this.handleRevoke.bind(this);
this.handleGenerateCrl = this.handleGenerateCrl.bind(this);
}

onChangeReenrollOptional(event) {
Expand All @@ -38,6 +43,9 @@ export default class CAUpdateContent extends React.Component {
onChangeRevokeOptional(event) {
this.setState({ revokeOptional: event.target.value });
}
onChangeGenerateCrlOptional(event) {
this.setState({ generateCrlOptional: event.target.value });
}

handleReenroll() {
let req = null;
Expand Down Expand Up @@ -80,14 +88,37 @@ export default class CAUpdateContent extends React.Component {
})
.then((result) => {
logger.debug('revoke successfully, result: ', result);
self.setState({ revokeResult: result.toString() });
self.setState({ revokeResult: 'success' });
})
.catch((err) => {
self.setState({ revokeResult: 'Revocation fails.' });
logger.debug(err);
});
}

handleGenerateCrl() {
let req = null;
if (this.state.enrollOptional) {
req = JSON.parse(this.state.enrollOptional);
}

const self = this;

getFabricClientSingleton()
.then((client) => {
logger.debug('start to generateCRL, request: ', req);
return client.generateCRL(req);
})
.then((result) => {
logger.debug('crlResult: ', result);
self.setState({ generateCrlResult: result.toString() });
})
.catch((err) => {
self.setState({ generateCrlResult: 'Fail.' });
logger.debug(err);
});
}

render() {
const outerDivStyle = {
padding: '24px',
Expand Down Expand Up @@ -139,7 +170,12 @@ export default class CAUpdateContent extends React.Component {
};
const DivStyle = {
width: '560px',
marginBottom: '30px',
marginBottom: '10px',
};
const titleStyle = {
fontSize: '130%',
marginTop: '20px',
textAlign: 'center',
};
const ButtonStyle = {
marginLeft: '20px',
Expand All @@ -157,7 +193,7 @@ export default class CAUpdateContent extends React.Component {
return (
<div style={outerDivStyle}>

<div>{this.state.Common.REENROLL}</div>
<div style={titleStyle}>{this.state.Common.REENROLL}</div>
<div style={DivStyle}>
<span style={spanStyle}>{this.state.Common.REENROLL_OPTIONAL}</span>
<Input placeholder="Optional json parameters" style={configInputStyle} value={this.state.reenrollOptional} onChange={this.onChangeReenrollOptional} />
Expand All @@ -172,7 +208,7 @@ export default class CAUpdateContent extends React.Component {
/>
</div>

<div >{this.state.Common.REVOKE}</div>
<div style={titleStyle}>{this.state.Common.REVOKE}</div>
<div style={DivStyle}>
<span style={asteriskStyle}>*</span>
<span style={spanStyle}>{this.state.Common.REVOKE_USERNAME}</span>
Expand All @@ -191,6 +227,21 @@ export default class CAUpdateContent extends React.Component {
readOnly
/>
</div>

<div style={titleStyle}>{this.state.Common.GENERATE_CRL}</div>
<div style={DivStyle}>
<span style={spanStyle}>{this.state.Common.GENERATE_CRL_OPTIONAL}</span>
<Input placeholder="Optional json parameters" style={configInputStyle} value={this.state.generateCrlOptional} onChange={this.onChangeGenerateCrlOptional} />
</div>
<div style={DivStyle}>
<Button style={ButtonStyle} type="primary" onClick={this.handleGenerateCrl}>{this.state.Common.GENERATE_CRL_CONFIRM}</Button>
<TextArea
placeholder="GenerateCRL Result"
value={this.state.generateCrlResult}
autosize={{ minRows: 2, maxRows: 2 }}
readOnly
/>
</div>
</div>

);
Expand Down
46 changes: 31 additions & 15 deletions src/util/fabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,15 @@ class FabricClient {
self.fabricClient.setCryptoSuite(cryptoSuite);

logger.info('almost done');

return this.fabricClient.createUser({
mspid,
username: mspid,
cryptoContent: {
privateKey: keyPath,
signedCert: certPath,
},
})
.then((user) => {
if (keyPath) {
return this.fabricClient.createUser({
mspid,
username: mspid,
cryptoContent: {
privateKey: keyPath,
signedCert: certPath,
},
}).then((user) => {
if (user && user.isEnrolled()) {
logger.info('Successfully loaded user1 from persistence, user:', user.toString());
} else {
Expand All @@ -525,10 +524,11 @@ class FabricClient {
logger.info('create fabric client success');
return Promise.resolve('success');
})
.catch((err) => {
logger.error(`Fail to instantiate chaincode. Error message: ${err.stack}` ? err.stack : err);
return Promise.reject('fail');
});
.catch((err) => {
logger.error(`Fail to instantiate chaincode. Error message: ${err.stack}` ? err.stack : err);
return Promise.reject('fail');
});
}
});
// ---------------admin finish ---------------
}
Expand Down Expand Up @@ -820,6 +820,18 @@ class FabricClient {
return this.fabricCAClient.revoke(req, this.user);
}

/**
* 获取证书吊销列表 - 参考 https://fabric-sdk-node.github.io/release-1.4/FabricCAServices.html#generateCRL
* @param { FabricCAServices.IRestriction } req
* @returns {Promise<any>}
*/
generateCRL(req) {
if (!req) {
req = {};
}
return this.fabricCAClient.generateCRL(req, this.user);
}

// 关闭连接
close() {
this.peer.close();
Expand All @@ -838,7 +850,11 @@ export function getFabricClientSingletonHelper(dbConfig) {
.then(input => __fabricClient._config(input))
.then(() => __fabricClient.importCer())
.then(() => __fabricClient._loginUser())
.then(() => Promise.resolve(__fabricClient));
.then(() => Promise.resolve(__fabricClient))
.catch((err) => {
logger.debug('getFabricClientSingletonHelper error: ', err);
throw err;
});
}
return Promise.resolve(__fabricClient);
}
Expand Down
27 changes: 21 additions & 6 deletions test/util/fabric.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const path = require('path');
// 3. stop fabric network, clean up
describe('fabric v1.1 basic-network', () => {
function initFabricNetwork() {
logger.info('Initiating Fabric Network.');
const buf = execSync('cd fabric/v1.1/fabcar && ./startFabric.sh');
logger.debug(buf.toString());
// logger.info('Initiating Fabric Network.');
// const buf = execSync('cd fabric/v1.1/fabcar && ./startFabric.sh');
// logger.debug(buf.toString());
}

function clearFabricNetwork() {
logger.info('clearing Fabric Network.');
const buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
logger.debug(buf.toString());
// logger.info('clearing Fabric Network.');
// const buf = execSync('cd fabric/v1.1/basic-network && ./teardown.sh');
// logger.debug(buf.toString());
}

beforeAll(() => {
Expand Down Expand Up @@ -321,6 +321,21 @@ describe('fabric v1.1 basic-network', () => {
throw err;
});
});

it('get Certificate Revocation List (CRL)', () => {
return getFabricClientSingletonHelper(configDbForCATest)
.then((client) => {
const req = {};
return client.generateCRL(req);
})
.then((result) => {
logger.debug('get CRL result: ', result);
expect(result).not.toBeNull();
})
.catch((err) => {
throw err;
});
});
});
});

Expand Down

0 comments on commit 135c9ba

Please sign in to comment.