-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-6628] 2. Support affiliation REST API
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
1 parent
192c42b
commit 097c1e0
Showing
7 changed files
with
478 additions
and
34 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
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
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; |
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 |
---|---|---|
@@ -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. | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
|
@@ -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 | ||
|
Oops, something went wrong.