Skip to content

Commit

Permalink
Yahcli : Add checks to validate port number (#1412)
Browse files Browse the repository at this point in the history
* add checks to validate port number in service endpoints when uploading an addressBook using yahcli

Signed-off-by: anighanta <[email protected]>

* add test addressbook for bad port in endpoint

Signed-off-by: anighanta <[email protected]>
  • Loading branch information
anighanta authored May 21, 2021
1 parent d10ed76 commit 584386d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class BookEntryPojo {
private static final String MISSING_CERT_HASH = "<N/A>";
private static final String SENTINEL_REPLACEMENT_VALUE = "!";

static class EndpointPojo {
public static class EndpointPojo {
private String ipAddressV4;
private Integer port;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public byte[] toRawFile(String styledFile) {
public byte[] toValidatedRawFile(String styledFile) {
var pojo = pojoFrom(styledFile);
for (var entry : pojo.getEntries()) {
try {
BookEntryPojo.asOctets(entry.getDeprecatedIp());
} catch (Exception e) {
throw new IllegalStateException(
"Deprecated IP field cannot be set to '" + entry.getDeprecatedIp() + "'", e);
}
validateIPandPort(entry.getDeprecatedIp(), entry.getDeprecatedPortNo(), "Deprecated");

try {
HapiPropertySource.asAccount(entry.getDeprecatedMemo());
Expand All @@ -70,14 +65,31 @@ public byte[] toValidatedRawFile(String styledFile) {
"Deprecated memo field cannot be set to '" + entry.getDeprecatedMemo() + "'", e);
}

if (entry.getDeprecatedPortNo() <= 0) {
throw new IllegalStateException(
"Deprecated portno field cannot be set to '" + entry.getDeprecatedPortNo() + "'");
for(BookEntryPojo.EndpointPojo endpointPojo : entry.getEndpoints()) {
validateIPandPort(endpointPojo.getIpAddressV4(), endpointPojo.getPort(), "Endpoint");
}
}
return grpcBytesFromPojo(pojo);
}

private void validateIPandPort(String IpV4Address, Integer portNo, String type) {
try {
BookEntryPojo.asOctets(IpV4Address);
} catch (Exception e) {
throw new IllegalStateException(
type + " IP field cannot be set to '" + IpV4Address + "'", e);
}

try {
if (portNo <= 0) {
throw new IllegalStateException(
type + " portno field cannot be set to '" + portNo + "'");
}
} catch (NullPointerException e) {
throw new IllegalStateException(type + " portno field is not set", e);
}
}

private AddressBookPojo pojoFrom(String styledFile) {
try {
return mapper.readValue(styledFile, AddressBookPojo.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"entries" : [ {
"deprecatedIp" : "127.0.0.1",
"deprecatedPortNo" : 50204,
"deprecatedMemo" : "0.0.3",
"nodeId" : 0,
"certHash" : "<N/A>",
"rsaPubKey" : "308201a2300d06092a864886f70d01010105000382018f003082018a02820181008db4664a62d562d8f478d859c50d9aa52c46475dd0f5b0552000d13f4cb6d4ac925e0cd9d9c0778e56124d423a243a618c7665d6033e79a29a9cc81a4cc8fcf2f8f18eaa76551f61eb536d93e322b9fedcf87acb72e54f777d1fa5c7daea34fb03442379d9223712f86152d30a7651b5435abfc53194c7a88f7c57e447256c300bc86f074f8cfa2e9f97805f2c409bd19b7e38513807895f0d04f54bc51967d7de4bdc98ce3e3b01a36a46edfa619c5b3264a715a6bc7265b4ff4c7a92e4dd4295dd8d8e974301ec3774b72aa4c1caa71a4fdf22ef8edc7edafc51beb10427fb21ad46bbad551fb39074278f529e4bdda2068b6821b6d3cc4c043171331cddee80e5f070c28ffc93b4824d3426ae01640463d04b4bff398496808cf76ca481686494412cf155b81e446fa0fb85a50e4a10d1c6870ef1f2503a8b6df7c030c35a960d47aa09267b527434fda2255def76fd2ea337b66c8a805d453f0a1cd6832d6e8ae5001a4f26747782ee60b4e7bc36ce97dd480fd2f4ffc9dc6b418261bda50203010001",
"nodeAccount" : "0.0.3",
"endpoints" : [ {
"ipAddressV4" : "127.0.0.1",
"port" : 0
} ]
} ]
}

0 comments on commit 584386d

Please sign in to comment.