Skip to content

Commit 16fb4a3

Browse files
author
platfowner
authored
Merge pull request #1141 from ainblockchain/release/v1.0.12
Release/v1.0.12
2 parents f251a5c + 6dade7c commit 16fb4a3

16 files changed

+414
-22
lines changed

client/middleware.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
const logger = new (require('../logger'))('MIDDLEWARE');
2+
13
const _ = require('lodash');
24
const express = require('express');
35
const cors = require('cors');
46
const ipWhitelist = require('ip-whitelist');
57
const rateLimit = require('express-rate-limit');
6-
const matchUrl = require('match-url-wildcard');
78

89
const { NodeConfigs } = require('../common/constants');
9-
const {
10-
getRegexpList,
11-
isWildcard
12-
} = require('../common/common-util');
10+
const CommonUtil = require('../common/common-util');
1311
const { JSON_RPC_SET_METHOD_SET } = require('../json_rpc/constants');
1412

1513
class Middleware {
@@ -39,16 +37,19 @@ class Middleware {
3937
});
4038
}
4139

40+
// TODO(platfowner): Use dynamic origin (see https://www.npmjs.com/package/cors).
4241
corsLimiter() {
4342
return cors({ origin: NodeConfigs.CORS_WHITELIST === '*' ?
44-
NodeConfigs.CORS_WHITELIST : getRegexpList(NodeConfigs.CORS_WHITELIST) });
43+
NodeConfigs.CORS_WHITELIST : CommonUtil.getRegexpList(NodeConfigs.CORS_WHITELIST) });
4544
}
4645

4746
ipWhitelistLimiter() {
47+
const LOG_HEADER = 'ipWhitelistLimiter';
4848
return ipWhitelist((ip) => {
49-
return isWildcard(NodeConfigs.DEV_CLIENT_API_IP_WHITELIST) ||
50-
matchUrl(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST);
51-
})
49+
const isWhitelisted = CommonUtil.isWhitelistedIp(ip, NodeConfigs.DEV_CLIENT_API_IP_WHITELIST);
50+
logger.info(`[${LOG_HEADER}] IP whitelisting check for [${ip}] ${isWhitelisted ? 'succeeded' : 'failed'}!`);
51+
return isWhitelisted;
52+
});
5253
}
5354

5455
blockchainApiRateLimiter = (req, res, next) => {

client/protocol_versions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,8 @@
110110
},
111111
"1.0.11": {
112112
"min": "1.0.0"
113+
},
114+
"1.0.12": {
115+
"min": "1.0.0"
113116
}
114117
}

common/common-util.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const stringify = require('fast-json-stable-stringify');
44
const jsonDiff = require('json-diff');
55
const ainUtil = require('@ainblockchain/ain-util');
66
const _ = require('lodash');
7+
const matchUrl = require('match-url-wildcard');
8+
const ip = require('ip');
79
const {
810
FailedTxPrecheckCodeSet,
911
FunctionResultCode,
@@ -943,6 +945,30 @@ class CommonUtil {
943945
return CommonUtil.isWildcard(value) ? value : value.split(',');
944946
}
945947

948+
static isWhitelistedUrl(url, whitelist) {
949+
if (CommonUtil.isWildcard(whitelist)) return true;
950+
if (!CommonUtil.isArray(whitelist)) return false;
951+
return matchUrl(url, whitelist);
952+
}
953+
954+
static isWhitelistedIp(ipAddr, whitelist) {
955+
if (CommonUtil.isWildcard(whitelist)) return true;
956+
if (!CommonUtil.isArray(whitelist)) return false;
957+
if (!CommonUtil.isValidIpV4(ipAddr) && !CommonUtil.isValidIpV6(ipAddr)) {
958+
return false;
959+
}
960+
for (const listItem of whitelist) {
961+
try {
962+
if (ip.isEqual(ipAddr, listItem)) {
963+
return true;
964+
}
965+
} catch {
966+
continue;
967+
}
968+
}
969+
return false;
970+
}
971+
946972
static countMaxOccurrences(list) {
947973
if (!CommonUtil.isArray(list)) {
948974
return 0;

config_client_api_ip_whitelist.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
function usage() {
4+
printf "\n"
5+
printf "Usage: bash config_client_api_ip_whitelist.sh [dev|staging|sandbox|exp|spring|summer|mainnet] [get|add|remove] [<IP Address>]\n"
6+
printf "Example: bash config_client_api_ip_whitelist.sh dev get\n"
7+
printf "Example: bash config_client_api_ip_whitelist.sh dev add 32.190.239.181\n"
8+
printf "Example: bash config_client_api_ip_whitelist.sh dev add '*'\n"
9+
printf "Example: bash config_client_api_ip_whitelist.sh dev remove 32.190.239.181\n"
10+
printf "\n"
11+
exit
12+
}
13+
14+
if [[ $# -lt 2 ]] || [[ $# -gt 3 ]]; then
15+
usage
16+
fi
17+
printf "\n[[[[[ config_client_api_ip_whitelist.sh ]]]]]\n\n"
18+
19+
if [[ "$1" = 'dev' ]] || [[ "$1" = 'staging' ]] || [[ "$1" = 'sandbox' ]] || [[ "$1" = 'exp' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]] || [[ "$1" = 'mainnet' ]]; then
20+
SEASON="$1"
21+
else
22+
printf "Invalid <Project/Season> argument: $1\n"
23+
usage
24+
fi
25+
printf "SEASON=$SEASON\n"
26+
27+
if [[ "$2" = 'get' ]]; then
28+
COMMAND="$2"
29+
IP_ADDR="$3"
30+
if [[ ! "$IP_ADDR" = "" ]]; then
31+
printf "\nInvalid argument: $IP_ADDR\n"
32+
usage
33+
fi
34+
elif [[ "$2" = 'add' ]] || [[ "$2" = 'remove' ]]; then
35+
COMMAND="$2"
36+
IP_ADDR="$3"
37+
if [[ "$IP_ADDR" = "" ]]; then
38+
printf "\nInvalid <IP Address> argument: $IP_ADDR\n"
39+
usage
40+
fi
41+
else
42+
printf "Invalid <Command> argument: $2\n"
43+
usage
44+
fi
45+
printf "COMMAND=$COMMAND\n"
46+
printf "IP_ADDR=$IP_ADDR\n"
47+
48+
# Get confirmation.
49+
if [[ "$SEASON" = "mainnet" ]]; then
50+
printf "\n"
51+
printf "Do you want to proceed for $SEASON? Enter [mainnet]: "
52+
read CONFIRM
53+
printf "\n\n"
54+
if [[ ! $CONFIRM = "mainnet" ]]
55+
then
56+
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
57+
fi
58+
else
59+
printf "\n"
60+
read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r
61+
printf "\n\n"
62+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
63+
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
64+
fi
65+
fi
66+
67+
# Read node ip addresses
68+
IFS=$'\n' read -d '' -r -a IP_ADDR_LIST < ./ip_addresses/$SEASON.txt
69+
70+
# Get keystore password
71+
printf "Enter password: "
72+
read -s PASSWORD
73+
printf "\n\n"
74+
if [[ $SEASON = "mainnet" ]]; then
75+
KEYSTORE_DIR="mainnet_prod_keys"
76+
elif [[ $SEASON = "spring" ]] || [[ $SEASON = "summer" ]]; then
77+
KEYSTORE_DIR="testnet_prod_keys"
78+
else
79+
KEYSTORE_DIR="testnet_dev_staging_keys"
80+
fi
81+
82+
if [[ $COMMAND = "add" ]]; then
83+
COMMAND_NODE_JS_FILE="addToDevClientApiIpWhitelist.js"
84+
elif [[ $COMMAND = "remove" ]]; then
85+
COMMAND_NODE_JS_FILE="removeFromDevClientApiIpWhitelist.js"
86+
else
87+
COMMAND_NODE_JS_FILE="getDevClientApiIpWhitelist.js"
88+
fi
89+
90+
function config_node() {
91+
local node_index="$1"
92+
local node_ip_addr=${IP_ADDR_LIST[${node_index}]}
93+
94+
printf "\n\n<<< Configuring ip whitelist of node $node_index ($node_ip_addr) >>>\n\n"
95+
96+
KEYSTORE_FILE_PATH="$KEYSTORE_DIR/keystore_node_$node_index.json"
97+
CONFIG_NODE_CMD="node tools/api-access/$COMMAND_NODE_JS_FILE $node_ip_addr 0 keystore $KEYSTORE_FILE_PATH"
98+
if [[ ! $COMMAND = "get" ]]; then
99+
CONFIG_NODE_CMD="$CONFIG_NODE_CMD '$IP_ADDR'"
100+
fi
101+
102+
printf "\n"
103+
printf "CONFIG_NODE_CMD=$CONFIG_NODE_CMD\n\n"
104+
eval "echo $PASSWORD | $CONFIG_NODE_CMD"
105+
}
106+
107+
for j in `seq $(( 0 )) $(( 9 ))`; do
108+
config_node "$j"
109+
done

config_node_param.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
function usage() {
4+
printf "\n"
5+
printf "Usage: bash config_node_param.sh [dev|staging|sandbox|exp|spring|summer|mainnet] [get|add|remove] <Param> [<Value>]\n"
6+
printf "Example: bash config_node_param.sh dev get DEV_CLIENT_API_IP_WHITELIST\n"
7+
printf "Example: bash config_node_param.sh dev add DEV_CLIENT_API_IP_WHITELIST 32.190.239.181\n"
8+
printf "Example: bash config_node_param.sh dev add DEV_CLIENT_API_IP_WHITELIST '*'\n"
9+
printf "Example: bash config_node_param.sh dev remove DEV_CLIENT_API_IP_WHITELIST 32.190.239.181\n"
10+
printf "Example: bash config_node_param.sh dev set DEV_CLIENT_API_IP_WHITELIST '*'\n"
11+
printf "\n"
12+
exit
13+
}
14+
15+
if [[ $# -lt 3 ]] || [[ $# -gt 4 ]]; then
16+
usage
17+
fi
18+
printf "\n[[[[[ config_node_param.sh ]]]]]\n\n"
19+
20+
if [[ "$1" = 'dev' ]] || [[ "$1" = 'staging' ]] || [[ "$1" = 'sandbox' ]] || [[ "$1" = 'exp' ]] || [[ "$1" = 'spring' ]] || [[ "$1" = 'summer' ]] || [[ "$1" = 'mainnet' ]]; then
21+
SEASON="$1"
22+
else
23+
printf "Invalid <Project/Season> argument: $1\n"
24+
usage
25+
fi
26+
printf "SEASON=$SEASON\n"
27+
28+
if [[ "$2" = 'get' ]]; then
29+
COMMAND="$2"
30+
PARAM="$3"
31+
VALUE="$4"
32+
if [[ ! "$VALUE" = "" ]]; then
33+
printf "\nInvalid argument: $VALUE\n"
34+
usage
35+
fi
36+
elif [[ "$2" = 'add' ]] || [[ "$2" = 'remove' ]] || [[ "$2" = 'set' ]]; then
37+
COMMAND="$2"
38+
PARAM="$3"
39+
VALUE="$4"
40+
if [[ "$PARAM" = "" ]]; then
41+
printf "\nInvalid <Param> argument: $PARAM\n"
42+
usage
43+
fi
44+
if [[ "$VALUE" = "" ]]; then
45+
printf "\nInvalid <Value> argument: $VALUE\n"
46+
usage
47+
fi
48+
else
49+
printf "Invalid <Command> argument: $2\n"
50+
usage
51+
fi
52+
printf "COMMAND=$COMMAND\n"
53+
printf "PARAM=$PARAM\n"
54+
printf "VALUE=$VALUE\n"
55+
56+
# Get confirmation.
57+
if [[ "$SEASON" = "mainnet" ]]; then
58+
printf "\n"
59+
printf "Do you want to proceed for $SEASON? Enter [mainnet]: "
60+
read CONFIRM
61+
printf "\n\n"
62+
if [[ ! $CONFIRM = "mainnet" ]]
63+
then
64+
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
65+
fi
66+
else
67+
printf "\n"
68+
read -p "Do you want to proceed for $SEASON? [y/N]: " -n 1 -r
69+
printf "\n\n"
70+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
71+
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
72+
fi
73+
fi
74+
75+
# Read node ip addresses
76+
IFS=$'\n' read -d '' -r -a IP_ADDR_LIST < ./ip_addresses/$SEASON.txt
77+
78+
# Get keystore password
79+
printf "Enter password: "
80+
read -s PASSWORD
81+
printf "\n\n"
82+
if [[ $SEASON = "mainnet" ]]; then
83+
KEYSTORE_DIR="mainnet_prod_keys"
84+
elif [[ $SEASON = "spring" ]] || [[ $SEASON = "summer" ]]; then
85+
KEYSTORE_DIR="testnet_prod_keys"
86+
else
87+
KEYSTORE_DIR="testnet_dev_staging_keys"
88+
fi
89+
90+
if [[ $COMMAND = "add" ]]; then
91+
COMMAND_NODE_JS_FILE="addToWhitelistNodeParam.js"
92+
elif [[ $COMMAND = "remove" ]]; then
93+
COMMAND_NODE_JS_FILE="removeFromWhitelistNodeParam.js"
94+
elif [[ $COMMAND = "set" ]]; then
95+
COMMAND_NODE_JS_FILE="setNodeParam.js"
96+
else
97+
COMMAND_NODE_JS_FILE="getNodeParam.js"
98+
fi
99+
100+
function config_node() {
101+
local node_index="$1"
102+
local node_ip_addr=${IP_ADDR_LIST[${node_index}]}
103+
104+
printf "\n\n<<< Configuring ip whitelist of node $node_index ($node_ip_addr) >>>\n\n"
105+
106+
KEYSTORE_FILE_PATH="$KEYSTORE_DIR/keystore_node_$node_index.json"
107+
CONFIG_NODE_CMD="node tools/api-access/$COMMAND_NODE_JS_FILE $node_ip_addr 0 keystore $KEYSTORE_FILE_PATH $PARAM"
108+
if [[ ! $COMMAND = "get" ]]; then
109+
CONFIG_NODE_CMD="$CONFIG_NODE_CMD '$VALUE'"
110+
fi
111+
112+
printf "\n"
113+
printf "CONFIG_NODE_CMD=$CONFIG_NODE_CMD\n\n"
114+
eval "echo $PASSWORD | $CONFIG_NODE_CMD"
115+
}
116+
117+
for j in `seq $(( 0 )) $(( 9 ))`; do
118+
config_node "$j"
119+
done

db/functions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const logger = new (require('../logger'))('FUNCTIONS');
22

33
const axios = require('axios');
44
const _ = require('lodash');
5-
const matchUrl = require('match-url-wildcard');
65
const Accounts = require('web3-eth-accounts');
76
const stringify = require('fast-json-stable-stringify');
87
const {
@@ -238,7 +237,7 @@ class Functions {
238237
}
239238
} else if (functionEntry.function_type === FunctionTypes.REST) {
240239
if (NodeConfigs.ENABLE_REST_FUNCTION_CALL && functionEntry.function_url &&
241-
matchUrl(functionEntry.function_url, this.db.getRestFunctionsUrlWhitelist())) {
240+
CommonUtil.isWhitelistedUrl(functionEntry.function_url, this.db.getRestFunctionsUrlWhitelist())) {
242241
if (DevFlags.enableRichFunctionLogging) {
243242
logger.info(
244243
` ==> Triggering REST function [[ ${functionEntry.function_id} ]] of ` +

deploy_blockchain_incremental_gcp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function deploy_tracker() {
175175
if [[ $SETUP_OPTION = "--setup" ]]; then
176176
# 2. Set up tracker
177177
printf "\n\n[[[ Setting up tracker ]]]\n\n"
178-
SETUP_CMD="gcloud compute ssh $TRACKER_TARGET_ADDR --command '. setup_blockchain_ubuntu.sh' --project $PROJECT_ID --zone $TRACKER_ZONE"
178+
SETUP_CMD="gcloud compute ssh $TRACKER_TARGET_ADDR --command 'cd ./ain-blockchain; . setup_blockchain_ubuntu.sh' --project $PROJECT_ID --zone $TRACKER_ZONE"
179179
printf "SETUP_CMD=$SETUP_CMD\n\n"
180180
eval $SETUP_CMD
181181
fi

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ain-blockchain",
33
"description": "AI Network Blockchain",
4-
"version": "1.0.11",
4+
"version": "1.0.12",
55
"private": true,
66
"license": "MIT",
77
"author": "dev@ainetwork.ai",

0 commit comments

Comments
 (0)