Skip to content

Commit

Permalink
[FAB-6628] 2. Support affiliation REST API
Browse files Browse the repository at this point in the history
This change implements the different REST API call to the affiliation
endpoint on the fabric-ca server.

Change-Id: I9d14c21c0c1218b3e7c595c882396aafcb32c9c6
Signed-off-by: zhaochy <[email protected]>
  • Loading branch information
zhaochy1990 authored and harrisob committed Jan 26, 2018
1 parent 192c42b commit 097c1e0
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 34 deletions.
4 changes: 3 additions & 1 deletion build/tasks/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ gulp.task('doc', ['clean'], function () {
'!fabric-client/lib/hash.js',
'!fabric-client/lib/utils.js',
'fabric-ca-client/index.js',
'fabric-ca-client/lib/FabricCAClientImpl.js'
'fabric-ca-client/lib/FabricCAClientImpl.js',
'fabric-ca-client/lib/AffiliationService.js',
'fabric-ca-client/lib/IdentityService.js',
], { read: false })
.pipe(jsdoc({
opts: {
Expand Down
7 changes: 6 additions & 1 deletion build/tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ process.env.THIRDPARTY_IMG_TAG = thirdpartyImageTag;
gulp.task('pre-test', function() {
return gulp.src([
'fabric-client/lib/**/*.js',
'fabric-ca-client/lib/FabricCAClientImpl.js'])
'fabric-ca-client/lib/FabricCAClientImpl.js',
'fabric-ca-client/lib/helper.js',
'fabric-ca-client/lib/IdentityService.js',
'fabric-ca-client/lib/AffiliationService.js',
])
.pipe(istanbul())
.pipe(istanbul.hookRequire());
});
Expand Down Expand Up @@ -107,6 +111,7 @@ gulp.task('test', ['clean-up', 'lint', 'pre-test', 'docker-ready', 'ca'], functi
// channel: mychannel, chaincode: end2endnodesdk:v0/v1
'test/integration/e2e.js',
'test/integration/query.js',
'test/integration/fabric-ca-affiliation-service-tests.js',
'test/integration/fabric-ca-identity-service-tests.js',
'test/integration/fabric-ca-services-tests.js',
'test/integration/client.js',
Expand Down
202 changes: 202 additions & 0 deletions fabric-ca-client/lib/AffiliationService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
Copyright 2018 IBM 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.
*/

'use strict';

const logger = require('./utils').getLogger('AffiliationService');
const checkRegistrar = require('./helper').checkRegistrar;

/**
* This is an implementation of the Affiliation service which communicates with
* the Fabric CA server using the Fabric CA client {@link FabricCAClient}.
* @class
*/
class AffiliationService {
constructor(client) {
this.client = client;
}

/**
* @typedef {Object} AffiliationRequest
* @property {string} name - Required. The affiliation path to create
* @property {string} caname - Optional. Name of the CA to send the request to within the Fabric CA server
* @property {boolean} force - Optional.
* <ul>
* <li>
* For create affiliation request, if any of the parent affiliations do not exist and 'force' is true,
* create all parent affiliations also.
* </li>
* <li>
* For delete affiliation request, if force is true and there are any child affiliations or any identities
* are associated with this affiliation or child affiliations, these identities and child affiliations
* will be deleted; otherwise, an error is returned.
* </li>
* <li>
* For update affiliation request, if any identities are associated with this affiliation, 'force' is true
* causes these identities' affiliations to be renamed; otherwise, an error is returned.
* </li>
* </ul>
*/

/**
* Create a new affiliation.
* The caller must have hf.AffiliationMgr authority.
*
* @param {AffiliationRequest} req - Required. The {@link AffiliationRequest}
* @param {User} registrar - Required. The identity of the registrar (i.e. who is performing the registration).
* @return {Promise} {@link ServiceResponse}
*/
create(req, registrar) {
if (typeof req === 'undefined' || req === null) {
throw new Error('Missing required argument "req"');
}

if (!req.name) {
throw new Error('Missing required parameters. "req.name" is required.');
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}

let url = 'affiliations';
if (req.force === true) {
url = url + '?force=true';
}
logger.debug('create new affiliation with url ' + url);
const request = {
name: req.name,
caname: req.caname
};
return this.client.post(url, request, signingIdentity);
}

/**
* List a specific affiliation at or below the caller's affinity.
* The caller must have hf.AffiliationMgr authority.
*
* @param {string} affiliation - The affiliation path to be queried.
* @param {User} registrar - Required. The identity of the registrar (i.e. who is performing the registration).
* @return {Promise} {@link ServiceResponse}
*/
getOne(affiliation, registrar) {
if (!affiliation || typeof affiliation !== 'string') {
throw new Error('Missing required argument "affiliation", or argument "affiliation" is not a valid string');
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}

const url = 'affiliations/' + affiliation;
return this.client.get(url, signingIdentity);
}

/**
* List all affiliations equal to and below the caller's affiliation.
* The caller must have hf.AffiliationMgr authority.
*
* @param {User} registrar - Required. The identity of the registrar (i.e. who is performing the registration).
* @return {Promise} {@link ServiceResponse}
*/
getAll(registrar) {
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}

return this.client.get('affiliations', signingIdentity);
}

/**
* Delete an affiliation.
* The caller must have hf.AffiliationMgr authority.
*
* @param {AffiliationRequest} req - Required. The {@link AffiliationRequest}
* @param {User} registrar - Required. The identity of the registrar (i.e. who is performing the registration).
* @return {Promise} {@link ServiceResponse}
*/
delete(req, registrar) {
if (typeof req === 'undefined' || req === null) {
throw new Error('Missing required argument "req"');
}

if (!req.name || typeof req.name !== 'string') {
throw new Error('Missing required argument "req.name", or argument "req.name" is not a valid string');
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}

let url = 'affiliations/' + req.name;
if (req.force === true) {
url = url + '?force=true';
}
return this.client.delete(url, signingIdentity);
}

/**
* Rename an affiliation.
* The caller must have hf.AffiliationMgr authority.
*
* @param {string} affiliation - The affiliation path to be updated
* @param {AffiliationRequest} req - Required. The {@link AffiliationRequest}
* @param {User} registrar
* @return {Promise} {@link ServiceResponse}
*/
update(affiliation, req, registrar) {
if (!affiliation || typeof affiliation !== 'string') {
throw new Error('Missing required argument "affiliation", or argument "affiliation" is not a valid string');
}
if (typeof req === 'undefined' || req === null) {
throw new Error('Missing required argument "req"');
}

if (!req.name || typeof req.name !== 'string') {
throw new Error('Missing required argument "req.name", or argument "req.name" is not a valid string');
}
checkRegistrar(registrar);

let signingIdentity = registrar.getSigningIdentity();
if (!signingIdentity) {
throw new Error('Can not get signingIdentity from registrar');
}

let url = 'affiliations/' + affiliation;
if (req.force === true) {
url = url + '?force=true';
}
const request = {
name: req.name
};
if (req.caname && typeof req.caname === 'string') {
request.caname = req.caname;
}

return this.client.put(url, request, signingIdentity);
}
}

module.exports = AffiliationService;
32 changes: 21 additions & 11 deletions fabric-ca-client/lib/FabricCAClientImpl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016 IBM All Rights Reserved.
Copyright 2018 IBM 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.
Expand All @@ -26,6 +26,8 @@ var https = require('https');
var urlParser = require('url');
var jsrsasign = require('jsrsasign');
const IdentityService = require('./IdentityService');
const AffiliationService = require('./AffiliationService');
const checkRegistrar = require('./helper').checkRegistrar;

var x509 = jsrsasign.X509;
var ASN1HEX = jsrsasign.ASN1HEX;
Expand Down Expand Up @@ -394,6 +396,15 @@ var FabricCAServices = class extends BaseClient {
return this._fabricCAClient.newIdentityService();
}

/**
* Create a new {@link AffiliationService} object
*
* @returns {AffiliationService} object
*/
newAffiliationService() {
return this._fabricCAClient.newAffiliationService();
}

/**
* @typedef {Object} HTTPEndpoint
* @property {string} hostname
Expand Down Expand Up @@ -659,6 +670,15 @@ var FabricCAClient = class {
return new IdentityService(this);
}

/**
* Create a new {@link AffiliationService} object
*
* @returns {AffiliationService} object
*/
newAffiliationService() {
return new AffiliationService(this);
}

post(api_method, requestObj, signingIdentity) {
return this.request('POST', api_method, signingIdentity, requestObj);
}
Expand Down Expand Up @@ -958,16 +978,6 @@ var FabricCAClient = class {
}
};

function checkRegistrar(registrar) {
if (typeof registrar === 'undefined' || registrar === null) {
throw new Error('Missing required argument "registrar"');
}

if (typeof registrar.getSigningIdentity !== 'function') {
throw new Error('Argument "registrar" must be an instance of the class "User", but is found to be missing a method "getSigningIdentity()"');
}
}

// This utility is based on jsrsasign.X509.getSubjectString() implementation
// we can not use that method directly because it requires calling readCertPEM()
// first which as of [email protected] always assumes RSA based certificates and
Expand Down
Loading

0 comments on commit 097c1e0

Please sign in to comment.