Project

General

Profile

Actions

Bug #35254

open

The " POST /api/hosts/:id/parameters " API endpoint does not work as expected in case of value in Array format.

Added by Leos Stejskal over 1 year ago. Updated over 1 year ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Parameters
Target version:
-
Difficulty:
Triaged:
No
Fixed in Releases:
Found in Releases:

Description

Reproducer Steps and Actual Results:

Let's assume the following parameters,

Host ID: 3

Actual Param name: myparam

Actual Value : ["item1","item2"] ## This can be used in both UI and hammer.

Scenario 0: When using GUI or hammer i.e. hammer -d host set-parameter --host-id 3 --name myparam --parameter-type array --value ["item1","item2"]

The API POST data looks like as below,
~
[ INFO 2021-03-20T13:05:45 API] POST /api/hosts/3/parameters
[DEBUG 2021-03-20T13:05:45 API] Params: {
"parameter" => {
"name" => "myparam",
"value" => "[item1,item2]",
"parameter_type" => "array",
"hidden_value" => nil
}
}
..
..
[DEBUG 2021-03-20T13:05:45 API] Response: {
"priority" => 70,
"created_at" => "2021-03-20 07:35:45 UTC",
"updated_at" => "2021-03-20 07:35:45 UTC",
"id" => 19,
"name" => "myparam",
"parameter_type" => "array",
"value" => [
[0] "item1",
[1] "item2"
]
}
~

--> As we can see that,
input value is --> ["item1","item2"] which is in array format
Processed value is --> "[item1,item2]" which is basically a string conversion of the array format
output value is same as input value i.e. the expected array value.

Let's Consider 4 more scenarios,

Scenario 1: Null Value being passed when mentioning the value in the same way as we mention with hammer or in UI --> ["item1","item2"]

  1. cat data1.json {
    "parameter": {
    "parameter_type": "array",
    "name": "myparam",
    "value": ["item1","item2"]
    }
    }
  1. json_verify < data1.json
    JSON is valid
  1. curl -ksu admin:RedHat1! -X POST -H "Accept:application/json,version=2" -H "Content-Type:application/json" https://`hostname -f`/api/hosts/3/parameters -d @data1.json | json_reformat | grep myparam -A3
    "name": "myparam",
    "parameter_type": "array",
    "value": null
    }

Logging,

~
2021-03-20T12:51:58 [I|app|b4828eff] Started POST "/api/hosts/3/parameters" for 127.0.0.1 at 2021-03-20 12:51:58 +0530
2021-03-20T12:51:58 [I|app|b4828eff] Processing by Api::V2::ParametersController#create as JSON
2021-03-20T12:51:58 [I|app|b4828eff] Parameters: {"parameter"=>{"parameter_type"=>"array", "name"=>"myparam", "value"=>"[FILTERED]"}, "apiv"=>"v2", "host_id"=>"3"}
2021-03-20T12:51:58 [I|app|b4828eff] Authorized user admin(Admin User)
2021-03-20T12:51:58 [I|aud|b4828eff] Parameter (14) create event on name myparam
2021-03-20T12:51:58 [I|aud|b4828eff] Parameter (14) create event on value
2021-03-20T12:51:58 [I|aud|b4828eff] Parameter (14) create event on reference_id 3
2021-03-20T12:51:58 [I|aud|b4828eff] Parameter (14) create event on hidden_value false
2021-03-20T12:51:58 [I|aud|b4828eff] Parameter (14) create event on key_type array
2021-03-20T12:51:58 [I|app|b4828eff] Rendering api/v2/parameters/create.json.rabl
2021-03-20T12:51:58 [I|app|b4828eff] Rendered api/v2/parameters/create.json.rabl (Duration: 9.0ms | Allocations: 7575)
~

Scenario 2: Correct value gets created, While escaping the value in this way --> "[\"item1\",\"item2\"]"

  1. cat data2.json {
    "parameter": {
    "parameter_type": "array",
    "name": "myparam",
    "value": "[\"item1\",\"item2\"]"
    }
    }
  1. json_verify < data2.json
    JSON is valid
  1. curl -ksu admin:RedHat1! -X POST -H "Accept:application/json,version=2" -H "Content-Type:application/json" https://`hostname -f`/api/hosts/3/parameters -d @data2.json | json_reformat | grep myparam -A5
    "name": "myparam",
    "parameter_type": "array",
    "value": [
    "item1",
    "item2"
    ]

Logging,

2021-03-20T12:55:39 [I|app|8786ca0c] Started POST "/api/hosts/3/parameters" for 127.0.0.1 at 2021-03-20 12:55:39 +0530
2021-03-20T12:55:39 [I|app|8786ca0c] Processing by Api::V2::ParametersController#create as JSON
2021-03-20T12:55:39 [I|app|8786ca0c] Parameters: {"parameter"=>{"parameter_type"=>"array", "name"=>"myparam", "value"=>"[FILTERED]"}, "apiv"=>"v2", "host_id"=>"3"}
2021-03-20T12:55:39 [I|app|8786ca0c] Authorized user admin(Admin User)
2021-03-20T12:55:39 [I|aud|8786ca0c] Parameter (16) create event on name myparam
2021-03-20T12:55:39 [I|aud|8786ca0c] Parameter (16) create event on value ["item1", "item2"]
2021-03-20T12:55:39 [I|aud|8786ca0c] Parameter (16) create event on reference_id 3
2021-03-20T12:55:39 [I|aud|8786ca0c] Parameter (16) create event on hidden_value false
2021-03-20T12:55:39 [I|aud|8786ca0c] Parameter (16) create event on key_type array
2021-03-20T12:55:39 [I|app|8786ca0c] Rendering api/v2/parameters/create.json.rabl
2021-03-20T12:55:39 [I|app|8786ca0c] Rendered api/v2/parameters/create.json.rabl (Duration: 9.1ms | Allocations: 7576)
2021-03-20T12:55:39 [I|app|8786ca0c] Completed 201 Created in 148ms (Views: 10.0ms | ActiveRecord: 12.8ms | Allocations: 27207)

Scenario 3: Correct value gets created , While posting the value in this way --> "['item1','item2']"

  1. cat data3.json {
    "parameter": {
    "parameter_type": "array",
    "name": "myparam",
    "value": "['item1','item2']"
    }
    }
  1. json_verify < data3.json
    JSON is valid
  1. curl -ksu admin:RedHat1! -X POST -H "Accept:application/json,version=2" -H "Content-Type:application/json" https://`hostname -f`/api/hosts/3/parameters -d @data3.json | json_reformat | grep myparam -A5
    "name": "myparam",
    "parameter_type": "array",
    "value": [
    "item1",
    "item2"
    ]

Logging,

2021-03-20T13:00:19 [I|app|003a94d6] Started POST "/api/hosts/3/parameters" for 127.0.0.1 at 2021-03-20 13:00:19 +0530
2021-03-20T13:00:19 [I|app|003a94d6] Processing by Api::V2::ParametersController#create as JSON
2021-03-20T13:00:19 [I|app|003a94d6] Parameters: {"parameter"=>{"parameter_type"=>"array", "name"=>"myparam", "value"=>"[FILTERED]"}, "apiv"=>"v2", "host_id"=>"3"}
2021-03-20T13:00:19 [I|app|003a94d6] Authorized user admin(Admin User)
2021-03-20T13:00:19 [I|aud|003a94d6] Parameter (17) create event on name myparam
2021-03-20T13:00:19 [I|aud|003a94d6] Parameter (17) create event on value ["item1", "item2"]
2021-03-20T13:00:19 [I|aud|003a94d6] Parameter (17) create event on reference_id 3
2021-03-20T13:00:19 [I|aud|003a94d6] Parameter (17) create event on hidden_value false
2021-03-20T13:00:19 [I|aud|003a94d6] Parameter (17) create event on key_type array
2021-03-20T13:00:19 [I|app|003a94d6] Rendering api/v2/parameters/create.json.rabl
2021-03-20T13:00:19 [I|app|003a94d6] Rendered api/v2/parameters/create.json.rabl (Duration: 10.6ms | Allocations: 7578)
2021-03-20T13:00:19 [I|app|003a94d6] Completed 201 Created in 156ms (Views: 11.4ms | ActiveRecord: 14.7ms | Allocations: 27191)

Scenario 4: Correct value gets created if I post the value in the same way hammer converts it before doing a POST i.e. "[item1,item2]"

  1. cat data4.json {
    "parameter": {
    "parameter_type": "array",
    "name": "myparam",
    "value": "[item1,item2]"
    }
    }
  1. json_verify < data4.json
    JSON is valid
  1. curl -ksu admin:RedHat1! -X POST -H "Accept:application/json,version=2" -H "Content-Type:application/json" https://`hostname -f`/api/hosts/3/parameters -d @data4.json | json_reformat | grep myparam -A5
    "name": "myparam",
    "parameter_type": "array",
    "value": [
    "item1",
    "item2"
    ]

Logging,

2021-03-20T13:04:01 [I|app|c4c99dfb] Started POST "/api/hosts/3/parameters" for 127.0.0.1 at 2021-03-20 13:04:01 +0530
2021-03-20T13:04:01 [I|app|c4c99dfb] Processing by Api::V2::ParametersController#create as JSON
2021-03-20T13:04:01 [I|app|c4c99dfb] Parameters: {"parameter"=>{"parameter_type"=>"array", "name"=>"myparam", "value"=>"[FILTERED]"}, "apiv"=>"v2", "host_id"=>"3"}
2021-03-20T13:04:01 [I|app|c4c99dfb] Authorized user admin(Admin User)
2021-03-20T13:04:01 [I|aud|c4c99dfb] Parameter (18) create event on name myparam
2021-03-20T13:04:01 [I|aud|c4c99dfb] Parameter (18) create event on value ["item1", "item2"]
2021-03-20T13:04:01 [I|aud|c4c99dfb] Parameter (18) create event on reference_id 3
2021-03-20T13:04:01 [I|aud|c4c99dfb] Parameter (18) create event on hidden_value false
2021-03-20T13:04:01 [I|aud|c4c99dfb] Parameter (18) create event on key_type array
2021-03-20T13:04:01 [I|app|c4c99dfb] Rendering api/v2/parameters/create.json.rabl
2021-03-20T13:04:01 [I|app|c4c99dfb] Rendered api/v2/parameters/create.json.rabl (Duration: 8.4ms | Allocations: 7591)

Scenario 1 and scenario 4 is being considered as a bug as,

In scenario 1 -> blank value is being posted even if that is a valid JSON
In scenario 4 -> correct array value is being created even if the data format is not an array but actually a string representation of that array

Expected Results:

Scenario 1, should be able to create the param with the expected value and the hammer should also not process the array data into a different format before POST'ing it.
This is being expected as API should allow data input in the same way we can do from GUI while creating a host parameter.

Actions #1

Updated by Leos Stejskal over 1 year ago

  • Subject changed from The " POST /api/hosts/:id/parameters " API endpoint does not work as expected in case of value in Array format. to The " POST /api/hosts/:id/parameters " API endpoint does not work as expected in case of value in Array format.
  • Category set to Parameters
Actions

Also available in: Atom PDF