Skip to content

Commit

Permalink
SDK now properly adding a peer with an invalid URL
Browse files Browse the repository at this point in the history
Before this change, the SDK aborted when adding a peer with
an invalid URL.  This change correctly handles the case when the URL
does not begin with 'grpc' or 'grpcs' and returns an 'InvalidProtocol'
error message

Fixes FAB-256

Change-Id: I7791acc49b4c9b05511155d399d882b884128c4c
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Sep 2, 2016
1 parent 84e7cc1 commit 21a4a8a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
21 changes: 13 additions & 8 deletions sdk/node/src/hfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2205,15 +2205,24 @@ class Endpoint {

constructor(url:string, pem?:string) {
let purl = parseUrl(url);
let protocol = purl.protocol.toLowerCase();
var protocol;
if (purl.protocol) {
protocol = purl.protocol.toLowerCase().slice(0,-1);
}
if (protocol === 'grpc') {
this.addr = purl.host;
this.creds = grpc.credentials.createInsecure();
} else if (protocol === 'grpcs') {
}
else if (protocol === 'grpcs') {
this.addr = purl.host;
this.creds = grpc.credentials.createSsl(new Buffer(pem));
} else {
throw Error("invalid protocol: " + protocol);
}
else {
var error = new Error();
error.name = "InvalidProtocol";
error.message = "Invalid protocol: " + protocol +
". URLs must begin with grpc:// or grpcs://"
throw error;
}
}
}
Expand Down Expand Up @@ -2628,10 +2637,6 @@ function isFunction(fcn:any):boolean {
function parseUrl(url:string):any {
// TODO: find ambient definition for url
var purl = urlParser.parse(url, true);
var protocol = purl.protocol;
if (endsWith(protocol, ":")) {
purl.protocol = protocol.slice(0, -1);
}
return purl;
}

Expand Down
12 changes: 12 additions & 0 deletions sdk/node/test/fixtures/tlsca.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-----BEGIN CERTIFICATE-----
MIIBwTCCAUegAwIBAgIBATAKBggqhkjOPQQDAzApMQswCQYDVQQGEwJVUzEMMAoG
A1UEChMDSUJNMQwwCgYDVQQDEwNPQkMwHhcNMTYwMTIxMjI0OTUxWhcNMTYwNDIw
MjI0OTUxWjApMQswCQYDVQQGEwJVUzEMMAoGA1UEChMDSUJNMQwwCgYDVQQDEwNP
QkMwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAR6YAoPOwMzIVi+P83V79I6BeIyJeaM
meqWbmwQsTRlKD6g0L0YvczQO2vp+DbxRN11okGq3O/ctcPzvPXvm7Mcbb3whgXW
RjbsX6wn25tF2/hU6fQsyQLPiJuNj/yxknSjQzBBMA4GA1UdDwEB/wQEAwIChDAP
BgNVHRMBAf8EBTADAQH/MA0GA1UdDgQGBAQBAgMEMA8GA1UdIwQIMAaABAECAwQw
CgYIKoZIzj0EAwMDaAAwZQIxAITGmq+x5N7Q1jrLt3QFRtTKsuNIosnlV4LR54l3
yyDo17Ts0YLyC0pZQFd+GURSOQIwP/XAwoMcbJJtOVeW/UL2EOqmKA2ygmWX5kte
9Lngf550S6gPEWuDQOcY95B+x3eH
-----END CERTIFICATE-----
73 changes: 73 additions & 0 deletions sdk/node/test/unit/chain-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,79 @@ function fail(t, msg, err) {
t.end(err);
}

//
// Test adding an invalid peer (omit protocol or use invalid protocol in URL)
//
test('Add invalid peer URLs to the chain', function (t) {

//list of valid protocol prefixes to test
var prefixes = [
"",
"grpcio://",
"http://",
" "
];

t.plan(prefixes.length);

var chain_test;

//loop through the prefixes
prefixes.forEach(function (prefix, index) {

chain_test = hfc.newChain("chain_test" + index);

try {
chain_test.addPeer(prefix + "://localhost:7051", new Buffer(32));
t.fail("Should not be able to add peer with URL starting with " + prefix);
}
catch (err) {
if (err.name === 'InvalidProtocol') {
t.pass("Returned 'InvalidProtocol' error for URL starting with " + prefix);
}
else {
t.fail("Failed to return 'InvalidProtocol' error for URL starting with " + prefix);
}
}
})

});


//
// Test adding a valid peer (URL must start with grpc or grpcs)
//
test('Add valid peer URLs to the chain', function (t) {

//list of valid protocol prefixes to test
var prefixes = [
"grpc",
"GRPC",
"grpcs",
"GRPCS"
];

t.plan(prefixes.length);

var chain_test2;

//loop through the prefixes
prefixes.forEach(function (prefix, index) {

chain_test2 = hfc.newChain("chain_test2" + index);

try {
chain_test2.addPeer(prefix + "://localhost:7051",
fs.readFileSync(__dirname + "/../fixtures/tlsca.cert"));
t.pass("Successfully added peer with URL starting with " + prefix + "://");
}
catch (err) {
t.fail("Could not add peer to the chain with URL starting with " + prefix + "://");
}
})

});

//
// Set Invalid security level and hash algorithm.
//
Expand Down

0 comments on commit 21a4a8a

Please sign in to comment.