rpc: remove file parameters from save/load_config in rpc.py

User can pipe or redirect data as needed.  As features like
bdev crypto are added, storing config data in files needs to
be carefully scrutinized - don't make it easy by providing
ways to write data to files that may not be sufficiently
protected.

Signed-off-by: Jim Harris <james.r.harris@intel.com>
Change-Id: I24dd32f81b965e275e1bac875f6d008d1b68007a

Reviewed-on: https://review.gerrithub.io/425165
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Jim Harris 2018-09-10 11:18:08 -07:00
parent 5a69eb5792
commit 55014ddf53
7 changed files with 51 additions and 86 deletions

View File

@ -59,49 +59,39 @@ if __name__ == "__main__":
@call_cmd
def save_config(args):
rpc.save_config(args.client,
filename=args.filename,
indent=args.indent)
p = subparsers.add_parser('save_config', help="""Write current (live) configuration of SPDK subsystems and targets.
If no filename is given write configuration to stdout.""")
p.add_argument('-f', '--filename', help="""File where to save JSON configuration to.""")
p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. If filename is not given default
indent level is 2. If writing to file of filename is '-' then default is compact mode.""", type=int, default=2)
p = subparsers.add_parser('save_config', help="""Write current (live) configuration of SPDK subsystems and targets to stdout.
""")
p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. Default indent level is 2.
""", type=int, default=2)
p.set_defaults(func=save_config)
@call_cmd
def load_config(args):
rpc.load_config(args.client,
filename=args.filename)
rpc.load_config(args.client)
p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and tagets using JSON RPC. If no file is
provided or file is '-' read configuration from stdin.""")
p.add_argument('-f', '--filename', help="""JSON Configuration file.""")
p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and targets using JSON RPC read from stdin.""")
p.set_defaults(func=load_config)
@call_cmd
def save_subsystem_config(args):
rpc.save_subsystem_config(args.client,
filename=args.filename,
indent=args.indent,
name=args.name)
p = subparsers.add_parser('save_subsystem_config', help="""Write current (live) configuration of SPDK subsystem.
If no filename is given write configuration to stdout.""")
p.add_argument('-f', '--filename', help='File where to save JSON configuration to.')
p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. If filename is not given default
indent level is 2. If writing to file of filename is '-' then default is compact mode.""", type=int, default=2)
p = subparsers.add_parser('save_subsystem_config', help="""Write current (live) configuration of SPDK subsystem to stdout.
""")
p.add_argument('-i', '--indent', help="""Indent level. Value less than 0 mean compact mode. Default indent level is 2.
""", type=int, default=2)
p.add_argument('-n', '--name', help='Name of subsystem', required=True)
p.set_defaults(func=save_subsystem_config)
@call_cmd
def load_subsystem_config(args):
rpc.load_subsystem_config(args.client,
filename=args.filename)
rpc.load_subsystem_config(args.client)
p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC. If no file is
provided or file is '-' read configuration from stdin.""")
p.add_argument('-f', '--filename', help="""JSON Configuration file.""")
p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC read from stdin.""")
p.set_defaults(func=load_subsystem_config)
# app

View File

@ -34,39 +34,20 @@ def get_rpc_methods(client, current=None):
return client.call('get_rpc_methods', params)
def _json_dump(config, filename, indent):
if filename is None:
if indent is None:
indent = 2
elif indent < 0:
indent = None
json.dump(config, sys.stdout, indent=indent)
sys.stdout.write('\n')
else:
if indent is None or indent < 0:
indent = None
with open(filename, 'w') as file:
json.dump(config, file, indent=indent)
file.write('\n')
def _json_dump(config, indent):
if indent is None:
indent = 2
elif indent < 0:
indent = None
json.dump(config, sys.stdout, indent=indent)
sys.stdout.write('\n')
def _json_load(filename):
if not filename or filename == '-':
return json.load(sys.stdin)
else:
with open(filename, 'r') as file:
return json.load(file)
def save_config(client, filename=None, indent=2):
"""Write current (live) configuration of SPDK subsystems and targets.
def save_config(client, indent=2):
"""Write current (live) configuration of SPDK subsystems and targets to stdout.
Args:
filename: File where to save JSON configuration to.
Print to stdout if not provided.
indent: Indent level. Value less than 0 mean compact mode.
If filename is not given default then indent level is 2.
If writing to file of filename is '-' then default is compact mode.
Default indent level is 2.
"""
config = {
'subsystems': []
@ -79,16 +60,15 @@ def save_config(client, filename=None, indent=2):
}
config['subsystems'].append(cfg)
_json_dump(config, filename, indent)
_json_dump(config, indent)
def load_config(client, filename=None):
"""Configure SPDK subsystems and tagets using JSON RPC.
def load_config(client):
"""Configure SPDK subsystems and targets using JSON RPC read from stdin.
Args:
filename: JSON Configuration file location.
If no file path is provided or file is '-' then read configuration from stdin.
none
"""
json_config = _json_load(filename)
json_config = json.load(sys.stdin)
# remove subsystems with no config
subsystems = json_config['subsystems']
@ -132,30 +112,26 @@ def load_config(client, filename=None):
print("Some configs were skipped because the RPC state that can call them passed over.")
def save_subsystem_config(client, filename=None, indent=2, name=None):
"""Write current (live) configuration of SPDK subsystem.
def save_subsystem_config(client, indent=2, name=None):
"""Write current (live) configuration of SPDK subsystem to stdout.
Args:
filename: File where to save JSON configuration to.
Print to stdout if not provided.
indent: Indent level. Value less than 0 mean compact mode.
If filename is not given default then indent level is 2.
If writing to file of filename is '-' then default is compact mode.
Default is indent level 2.
"""
cfg = {
'subsystem': name,
'config': client.call('get_subsystem_config', {"name": name})
}
_json_dump(cfg, filename, indent)
_json_dump(cfg, indent)
def load_subsystem_config(client, filename=None):
"""Configure SPDK subsystem using JSON RPC.
def load_subsystem_config(client):
"""Configure SPDK subsystem using JSON RPC read from stdin.
Args:
filename: JSON Configuration file location.
If no file path is provided or file is '-' then read configuration from stdin.
none
"""
subsystem = _json_load(filename)
subsystem = json.load(sys.stdin)
if not subsystem['config']:
return

View File

@ -39,7 +39,7 @@ echo "Process pid: $pid"
trap "killprocess $pid; delete_tmp_conf_files; exit 1 " SIGINT SIGTERM EXIT
waitforlisten $pid
$rpc_py load_subsystem_config -f $testdir/iscsi.json
$rpc_py load_subsystem_config < $testdir/iscsi.json
$rpc_py start_subsystem_init
echo "iscsi_tgt is listening. Running tests..."

View File

@ -18,7 +18,7 @@ $rpc_py add_portal_group $PORTAL_TAG 127.0.0.1:$ISCSI_PORT
$rpc_py add_initiator_group $INITIATOR_TAG $INITIATOR_NAME $NETMASK
$rpc_py construct_malloc_bdev 64 4096 --name Malloc0
$rpc_py construct_target_node Target3 Target3_alias 'Malloc0:0' $PORTAL_TAG:$INITIATOR_TAG 64 -d
$rpc_py save_config -f $base_iscsi_config
$rpc_py save_config > $base_iscsi_config
timing_exit iscsi_json_config_create_setup
timing_enter iscsi_json_config_test
@ -29,8 +29,8 @@ timing_enter iscsi_json_config_restart_spdk
$clear_config_py clear_config
kill_targets
run_spdk_tgt
$rpc_py load_config -f $base_iscsi_config
$rpc_py save_config -f $last_iscsi_config
$rpc_py load_config < $base_iscsi_config
$rpc_py save_config > $last_iscsi_config
timing_exit iscsi_json_config_restart_spdk
diff $base_iscsi_config $last_iscsi_config

View File

@ -31,7 +31,7 @@ function load_nvme() {
echo '{"subsystems": [' > nvme_config.json
$SPDK_BUILD_DIR/scripts/gen_nvme.sh --json >> nvme_config.json
echo ']}' >> nvme_config.json
$rpc_py load_config -f nvme_config.json
$rpc_py load_config < nvme_config.json
rm nvme_config.json
}
@ -78,18 +78,18 @@ function kill_targets() {
# 11. Remove all files.
function test_json_config() {
$rpc_py get_bdevs | jq '.|sort_by(.name)' > $base_bdevs
$rpc_py save_config -f $full_config
$rpc_py save_config > $full_config
$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $full_config > $base_json_config
$clear_config_py clear_config
$rpc_py save_config -f $tmp_config
$rpc_py save_config > $tmp_config
$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $tmp_config > $null_json_config
if [ "[]" != "$(jq '.subsystems | map(select(.config != null)) | map(select(.config != []))' $null_json_config)" ]; then
echo "Config has not been cleared"
return 1
fi
$rpc_py load_config -f $base_json_config
$rpc_py load_config < $base_json_config
$rpc_py get_bdevs | jq '.|sort_by(.name)' > $last_bdevs
$rpc_py save_config -f $tmp_config
$rpc_py save_config > $tmp_config
$JSON_DIR/config_filter.py -method "delete_global_parameters" -filename $tmp_config > $last_json_config
diff $base_json_config $last_json_config
diff $base_bdevs $last_bdevs
@ -184,7 +184,7 @@ function clear_bdev_subsystem_config() {
# 8. Delete all files.
function test_global_params() {
target=$1
$rpc_py save_config -f $full_config
$rpc_py save_config > $full_config
python $JSON_DIR/config_filter.py -method "delete_configs" -filename $full_config > $base_json_config
if [ $target == "spdk_tgt" ]; then
killprocess $spdk_tgt_pid
@ -196,8 +196,8 @@ function test_global_params() {
echo "Target is not specified for test_global_params"
return 1
fi
$rpc_py load_config -f $full_config
$rpc_py save_config -f $full_config
$rpc_py load_config < $full_config
$rpc_py save_config > $full_config
python $JSON_DIR/config_filter.py -method "delete_configs" -filename $full_config > $last_json_config
diff $base_json_config $last_json_config
rm $base_json_config $last_json_config

View File

@ -13,15 +13,15 @@ function test_subsystems() {
$rpc_py start_subsystem_init
create_nvmf_subsystem_config
$rpc_py save_config -f $base_nvmf_config
$rpc_py save_config > $base_nvmf_config
test_json_config
clear_nvmf_subsystem_config
kill_targets
run_spdk_tgt
$rpc_py load_config -f $base_nvmf_config
$rpc_py save_config -f $last_nvmf_config
$rpc_py load_config < $base_nvmf_config
$rpc_py save_config > $last_nvmf_config
diff $base_nvmf_config $last_nvmf_config

View File

@ -184,8 +184,7 @@ function spdk_vhost_run()
fi
if [[ -n "$vhost_json_path" ]]; then
$SPDK_BUILD_DIR/scripts/rpc.py -s $vhost_dir/rpc.sock load_config\
--filename "$vhost_json_path/conf.json"
$SPDK_BUILD_DIR/scripts/rpc.py -s $vhost_dir/rpc.sock load_config < "$vhost_json_path/conf.json"
fi
notice "vhost started - pid=$vhost_pid"