Quantcast
Viewing all articles
Browse latest Browse all 524

JSON String processing injects quotes

I get json encoded secrets from a Vaulwarden-Instance by their UUID.I want so loop over any uuid and set environment variables accordingly.

For every field there should be variable (see below)

uuid_USERNAME=usernameuuid_PASSWORD=passworduuid_FIELD_fieldname=fieldvalue...

echo does inject single quotes so that the script breaks in the for loop ehich sets these field-based environment variables.

Can anyone give me a hint on how to handle this situation?

Best regards!

Here is the script:

#!/bin/bashfor item in 55f8864a-ec88-410c-ab0e-8bee2119042ado  entry='{"passwordHistory":[{"lastUsedDate":"2024-02-23T17:10:59.343Z","password":"versteckt_name-leerzeichen: versteckt_wert leerzeichen"},{"lastUsedDate":"2024-02-23T17:10:59.343Z","password":"TestPasswort"},{"lastUsedDate":"2024-02-23T16:45:56.662Z","password":"versteckt_name leerzeichen: versteckt_wert leerzeichen"},{"lastUsedDate":"2024-02-23T16:28:04.951Z","password":"versteckt_name: versteckt_wert"}],"revisionDate":"2024-02-23T17:14:48.810Z","creationDate":"2024-02-18T21:04:02.811Z","deletedDate":null,"object":"item","id":"55f8864a-ec88-410c-ab0e-8bee2119042a","organizationId":null,"folderId":null,"type":1,"reprompt":0,"name":"TestName","notes":"notizen notizen1 notizen2","favorite":false,"fields":[{"name":"benutzerdefiniert_name leerzeichen","value":"benutzerdefiniert_wert leerzeichen","type":0,"linkedId":null},{"name":"versteckt_name leerzeichen","value":"versteckt_wert leerzeichen","type":1,"linkedId":null},{"name":"bool_name leerzeichen","value":"false","type":2,"linkedId":null}],"login":{"fido2Credentials":[],"uris":[],"username":"TestBenutzername leerzeichen","password":"TestPasswort leerzeichen","totp":null,"passwordRevisionDate":"2024-02-23T17:10:59.343Z"},"collectionIds":[]}'  echo $entry  echo ${item}_USERNAME=$(echo ${entry} | jq -r ".login.username")  > GITHUB_ENV  echo ${item}_PASSWORD=$(echo $entry | jq -r ".login.password")  >> GITHUB_ENV  echo ${item}_NOTES=$(echo $entry | jq -r ".notes | select( . != null )") >> GITHUB_ENV  for field in $(echo $entry | jq -cr .fields[])  do    echo $field    field_name=$(echo $field | jq -r ".name" | sed -e "s/ /_/g")    field_value=$(echo $field | jq -r ".value")    echo ${item}_FIELD_${field_name}=${field_value} >> GITHUB_ENV  donedone

Here is the JSON in pretty:

{"passwordHistory": [    {"lastUsedDate": "2024-02-23T17:10:59.343Z","password": "versteckt_name-leerzeichen: versteckt_wert leerzeichen"    },    {"lastUsedDate": "2024-02-23T17:10:59.343Z","password": "TestPasswort"    },    {"lastUsedDate": "2024-02-23T16:45:56.662Z","password": "versteckt_name leerzeichen: versteckt_wert leerzeichen"    },    {"lastUsedDate": "2024-02-23T16:28:04.951Z","password": "versteckt_name: versteckt_wert"    }  ],"revisionDate": "2024-02-23T17:14:48.810Z","creationDate": "2024-02-18T21:04:02.811Z","deletedDate": null,"object": "item","id": "55f8864a-ec88-410c-ab0e-8bee2119042a","organizationId": null,"folderId": null,"type": 1,"reprompt": 0,"name": "TestName","notes": "notizen notizen1 notizen2","favorite": false,"fields": [    {"name": "benutzerdefiniert_name leerzeichen","value": "benutzerdefiniert_wert leerzeichen","type": 0,"linkedId": null    },    {"name": "versteckt_name leerzeichen","value": "versteckt_wert leerzeichen","type": 1,"linkedId": null    },    {"name": "bool_name leerzeichen","value": "false","type": 2,"linkedId": null    }  ],"login": {"fido2Credentials": [],"uris": [],"username": "TestBenutzername leerzeichen","password": "TestPasswort leerzeichen","totp": null,"passwordRevisionDate": "2024-02-23T17:10:59.343Z"  },"collectionIds": []}

Here is the output:

55f8864a-ec88-410c-ab0e-8bee2119042a_USERNAME=TestBenutzername leerzeichen55f8864a-ec88-410c-ab0e-8bee2119042a_PASSWORD=TestPasswort leerzeichen55f8864a-ec88-410c-ab0e-8bee2119042a_NOTES=notizen notizen1 notizen2{"name":"benutzerdefiniert_namejq: parse error: Unfinished string at EOF at line 2, column 0jq: parse error: Unfinished string at EOF at line 2, column 055f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=leerzeichen","value":"benutzerdefiniert_wertjq: parse error: Invalid numeric literal at line 1, column 12jq: parse error: Invalid numeric literal at line 1, column 1255f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=leerzeichen","type":0,"linkedId":null}jq: parse error: Invalid numeric literal at line 1, column 12jq: parse error: Invalid numeric literal at line 1, column 1255f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_={"name":"versteckt_namejq: parse error: Unfinished string at EOF at line 2, column 0jq: parse error: Unfinished string at EOF at line 2, column 055f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=leerzeichen","value":"versteckt_wertjq: parse error: Invalid numeric literal at line 1, column 12jq: parse error: Invalid numeric literal at line 1, column 1255f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=leerzeichen","type":1,"linkedId":null}jq: parse error: Invalid numeric literal at line 1, column 12jq: parse error: Invalid numeric literal at line 1, column 1255f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_={"name":"bool_namejq: parse error: Unfinished string at EOF at line 2, column 0jq: parse error: Unfinished string at EOF at line 2, column 055f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=leerzeichen","value":"false","type":2,"linkedId":null}jq: parse error: Invalid numeric literal at line 1, column 12jq: parse error: Invalid numeric literal at line 1, column 1255f8864a-ec88-410c-ab0e-8bee2119042a_FIELD_=

I tried executing this script and tried different methods e.g. echo <<< $entry or piping the output into files and then cat-ing them which didn't do the trick.

The variables should be set like mentioned in the text above.


Viewing all articles
Browse latest Browse all 524

Trending Articles