cli icon indicating copy to clipboard operation
cli copied to clipboard

item id not immediately usable in a script ?

Open ssttkk opened this issue 6 years ago • 4 comments

I'm trying to write a small bash script to automatise the creation of multiple items, and assigning them to at least one collection.

The part I'm having trouble with is the following : (minding that $newItemB64 and $collListB64 are valid)

newItemId=$(bw create item $newItemB64 --session $sessionKey | jq ".id")
bw sync -f --session $sessionKey
bw edit item-collections $newItemId $collListB64 --session $sessionKey

the creation and syncing work fine, but when the time comes for the edit, I am prompted with a Not Found. The same happens if I simply try to bw get item $newItemId . However, if I try to bw get item <ItemName>, I get a result back, with the newly created item, and its ID. Also, if I try to run the edit item-collections by hand, out of the script, it works normally.

Am I doing something wrong in my script for it not to work ?

ssttkk avatar Sep 24 '20 13:09 ssttkk

Here is an example script that may be helpful for you. This example will create a Login Item and share it with a Collection. Let me know if you have any other questions.

#!/bin/bash

name="A_NEW_LOGIN_ITEM"
username="TestUser"
password="Password123"
collectionID="bc0ca4e3-4749-4824-a8b5-ab53010a4bc6"
organizationID="75aeb479-e2d7-4bd3-adb0-aaac005df65a"

session_key="$(bw unlock --raw)"

bw --session $session_key sync

bw --session $session_key get template item | jq '.name="'$name'" | .login.username="'$username'" | .login.password="'$password'"' | bw encode | bw --session $session_key create item

objectID="$(bw --session $session_key list items --search "A_NEW_LOGIN_ITEM" | jq -r '.[].id')"

echo '["'"$collectionID"'"]' | bw encode | bw --session $session_key share $objectID $organizationID

sevensixseven avatar Sep 24 '20 13:09 sevensixseven

Thank you for your quick answer ! When I try to run your script, i get the following error in the last step : Cannot encrypt cipher for organization. No key. However, when shared directly from the command line, it works just fine.

ssttkk avatar Sep 24 '20 14:09 ssttkk

I found some time, so I went ahead and created a new script for you. This one is interactive and the sharing is based on the item id instead of looking up the name just in case you create more than one item with the same name.

#!/bin/bash

read -p 'Login Item Name: ' name
read -p ''"$name"' Username: ' username
read -p ''"$name"' Password: ' password
read -p 'Organization ID: ' organizationID
read -p 'Collection ID: ' collectionID

session_key="$(bw unlock --raw)"

bw --session $session_key sync

objectID="$(bw --session $session_key get template item | jq '.name="'"$name"'" | .login.username="'$username'" | .login.password="'$password'"' | bw encode | bw --session $session_key create item | cut -d '"' -f8)"

echo '["'"$collectionID"'"]' | bw encode | bw --session $session_key share $objectID $organizationID

sevensixseven avatar Oct 03 '20 04:10 sevensixseven

Closing this issue @ssttkk , please let us know if there are any other issues/problems getting this to work.

cscharf avatar Nov 04 '20 20:11 cscharf