forked from hyperledger-archives/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance ACL validation (hyperledger-archives#3914)
Add file and location information to ACL validation errors and improve messages Closes #382 Signed-off-by: James Taylor <[email protected]>
- Loading branch information
Showing
12 changed files
with
422 additions
and
53 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,70 @@ | ||
/* | ||
* 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 BaseFileException = require('../basefileexception'); | ||
|
||
/** | ||
* Exception throws when a composer acl file is semantically invalid | ||
* @extends BaseFileException | ||
* @see See {@link BaseFileException} | ||
* @class | ||
* @memberof module:composer-common | ||
* @private | ||
*/ | ||
class IllegalAclException extends BaseFileException { | ||
|
||
/** | ||
* Create an IllegalAclException. | ||
* @param {String} message - the message for the exception | ||
* @param {AclFile} [aclFile] - the optional aclFile associated with the exception | ||
* @param {Object} [fileLocation] - location details of the error within the model file. | ||
* @param {String} fileLocation.start.line - start line of the error location. | ||
* @param {String} fileLocation.start.column - start column of the error location. | ||
* @param {String} fileLocation.end.line - end line of the error location. | ||
* @param {String} fileLocation.end.column - end column of the error location. | ||
*/ | ||
constructor(message, aclFile, fileLocation) { | ||
|
||
let messageSuffix = ''; | ||
if(aclFile && aclFile.getIdentifier()) { | ||
messageSuffix = 'File \'' + aclFile.getIdentifier() + '\': ' ; | ||
} | ||
|
||
if(fileLocation) { | ||
messageSuffix = messageSuffix + 'line ' + fileLocation.start.line + ' column ' + | ||
fileLocation.start.column + ', to line ' + fileLocation.end.line + ' column ' + | ||
fileLocation.end.column + '. '; | ||
} | ||
|
||
// First character to be uppercase, and prepended with a space | ||
if (messageSuffix) { | ||
messageSuffix = ' ' + messageSuffix.charAt(0).toUpperCase() + messageSuffix.slice(1); | ||
} | ||
|
||
super(message, fileLocation, message + messageSuffix); | ||
this.aclFile = aclFile; | ||
} | ||
|
||
/** | ||
* Returns the aclFile associated with the exception or null | ||
* @return {AclFile} the optional acl file associated with the exception | ||
*/ | ||
getAclFile() { | ||
return this.aclFile; | ||
} | ||
} | ||
|
||
module.exports = IllegalAclException; |
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,104 @@ | ||
/* | ||
* 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 IllegalAclException = require('./illegalaclexception'); | ||
|
||
/** | ||
* Operation captures the array ofaction verbs that the ACL rule | ||
* governs. | ||
* | ||
* @private | ||
* @class | ||
* @memberof module:composer-common | ||
*/ | ||
class Operation { | ||
|
||
/** | ||
* Create an Operation from an Abstract Syntax Tree. The AST is the | ||
* result of parsing. | ||
* | ||
* @param {AclRule} aclRule - the AclRule for this Operation | ||
* @param {Object} ast - the AST created by the parser | ||
* @throws {IllegalAclException} | ||
*/ | ||
constructor(aclRule, ast) { | ||
if(!aclRule || !ast) { | ||
throw new IllegalAclException('Invalid AclRule or AST'); | ||
} | ||
|
||
this.ast = ast; | ||
this.aclRule = aclRule; | ||
this.process(); | ||
} | ||
|
||
/** | ||
* Visitor design pattern | ||
* @param {Object} visitor - the visitor | ||
* @param {Object} parameters - the parameter | ||
* @return {Object} the result of visiting or null | ||
* @private | ||
*/ | ||
accept(visitor,parameters) { | ||
return visitor.visit(this, parameters); | ||
} | ||
|
||
/** | ||
* Returns the AclRule that owns this Operation. | ||
* | ||
* @return {AclRule} the owning AclRule | ||
*/ | ||
getAclRule() { | ||
return this.aclRule; | ||
} | ||
|
||
/** | ||
* Returns the expression as a text string. | ||
* | ||
* @return {string} the verbs for the operation | ||
*/ | ||
getVerbs() { | ||
return this.verbs; | ||
} | ||
|
||
/** | ||
* Process the AST and build the model | ||
* | ||
* @throws {IllegalAclException} | ||
* @private | ||
*/ | ||
process() { | ||
this.verbs = this.ast.verbs; | ||
} | ||
|
||
/** | ||
* Semantic validation of the structure of this Operation. | ||
* | ||
* @throws {IllegalAclException} | ||
* @private | ||
*/ | ||
validate() { | ||
const foundVerbs = {}; | ||
this.verbs.forEach((verb) => { | ||
if (foundVerbs[verb]) { | ||
throw new IllegalAclException(`The verb '${verb}' has been specified more than once in the ACL rule '${this.aclRule.getName()}'`, this.aclRule.getAclFile(), this.ast.location); | ||
} | ||
foundVerbs[verb] = true; | ||
}); | ||
} | ||
|
||
} | ||
|
||
module.exports = Operation; |
Oops, something went wrong.