steampipe-postgres-fdw icon indicating copy to clipboard operation
steampipe-postgres-fdw copied to clipboard

`net_http_request` table from `net` plugin fails to handle `jsonb_build_object` in `WHERE` clauses

Open hellupline opened this issue 3 years ago • 2 comments

when I use a jsonb_object with both keys and values as literal arrays, the request runs as expected, but when I use an ARRAY [...] or jsonb_build_object the result is a empty set, when the request hit the target server, the request is also missing headers, and if using jsonb_build_object on body, body also goes empty

this snipplet contains 3 example cases

-- success
SELECT *
FROM net_http_request
WHERE
    url = 'https://webhook.site/6cf89317-df09-40bb-b14a-30b4e83412a9?case=1'
    AND method = 'POST'
    AND request_headers = jsonb_object(
        '{accept, content-type, authorization}',
        '{application/json, application/json, Bearer testing}'
    )
    AND request_body = jsonb_object(
        '{key}',
        ARRAY ['value']
    )::TEXT;
​
​
--- no headers sent
SELECT *
FROM net_http_request
WHERE
    url = 'https://webhook.site/6cf89317-df09-40bb-b14a-30b4e83412a9?case=2'
    AND method = 'POST'
    AND request_headers = jsonb_object(
        '{accept, content-type, authorization}',
        ARRAY ['application/json', 'application/json', FORMAT('Bearer %s', 'testing')]
    )
    AND request_body = jsonb_object(
        '{key}',
        ARRAY ['value']
    )::TEXT;
​
​
-- no header, no body sent
SELECT *
FROM net_http_request
WHERE
    url = 'https://webhook.site/6cf89317-df09-40bb-b14a-30b4e83412a9?case=3'
    AND method = 'POST'
    AND request_headers = jsonb_build_object(
        'accept', 'application/json',
        'content-type', 'application/json',
        'authorization', 'Bearer testing'
    )
    AND request_body = jsonb_build_object(
        'key', ARRAY ['value']
    )::TEXT;
​
​
​-- all 3 json objects on `TEXT` serialization are equal
SELECT
    jsonb_object(
        '{accept, content-type, authorization}',
        ARRAY ['application/json', 'application/json', FORMAT('Bearer %s', 'testing')]
    )::TEXT =
    jsonb_object(
        '{accept, content-type, authorization}',
        '{application/json, application/json, Bearer testing}'
    )::TEXT,
    jsonb_object(
        '{accept, content-type, authorization}',
        ARRAY ['application/json', 'application/json', FORMAT('Bearer %s', 'testing')]
    )::TEXT =
    jsonb_build_object(
        'accept', 'application/json',
        'content-type', 'application/json',
        'authorization', 'Bearer testing'
    )::TEXT;

hellupline avatar Nov 12 '22 18:11 hellupline

update: I did manage to avoid the issue using a "proxy" function:

CREATE OR REPLACE FUNCTION fix_jsonb(token TEXT) RETURNS TEXT
    LANGUAGE SQL
    IMMUTABLE
    RETURN jsonb_object(
        '{accept, content-type, authorization}',
        ARRAY ['application/json', 'application/json', FORMAT('Bearer %s', token)]
    )::TEXT;
SELECT *
FROM net_http_request
WHERE
    url = 'https://webhook.site/6cf89317-df09-40bb-b14a-30b4e83412a9?case=2'
    AND method = 'POST'
    AND request_headers = fix_jsonb('hello')::JSONB
    AND request_body = jsonb_object(
        '{key}',
        ARRAY ['value']
    )::TEXT;

hellupline avatar Nov 14 '22 14:11 hellupline