Skip to content

Instantly share code, notes, and snippets.

@marcinantkiewicz
Last active October 9, 2024 03:38
Show Gist options
  • Save marcinantkiewicz/2cbefbeea4a67b32f85786840006d43d to your computer and use it in GitHub Desktop.
Save marcinantkiewicz/2cbefbeea4a67b32f85786840006d43d to your computer and use it in GitHub Desktop.
write secret with arbitrary attributes to 1password vault
#!/bin/bash
# default for business accounts, override with `--vault name`
vault="Employee"
while [[ "$#" -gt 0 ]]; do
case $1 in
--vault)
vault="$1"
;;
esac
shift
done
echo ""
echo "+--------------------------------------------------------------+"
echo "This script will create a generic credential entry in 1Password."
echo "+--------------------------------------------------------------+"
echo " - It will ask for the credential name and how many attibutes it has."
echo " Like \"username\" and \"password\""
echo " - For each of the elements, it will ask it it is a an actual secret"
echo " secrets get obscured when entered and more, do mark your secrets!"
echo " ----> ^^^^^^^^^^^^^^^^^^^^"
echo ""
echo "Working in valut '$vault'"
if op vault get "$vault" > /dev/null 2>&1; then
echo " -- Checking if $vault exists, you may need to authenticate to 1password"
echo " -- $vault exists, good."
echo ""
else
echo "[ ERROR! ] Vault does not exist. Exiting."
exit 1
fi
echo ""
read -p "Enter name for the secret: " title
# Initialize the JSON structure
json_output="{
\"title\": \"$title\",
\"category\": \"API_CREDENTIAL\",
\"fields\": [
"
# Ask the user how many fields to create
read -p "How many fields would you like to add? " num_fields
# Loop through to get field information from the user
for ((i=1; i<=num_fields; i++))
do
read -p "Enter name for field $i: " field_name
read -p "Credentials get special protection, if $field_name is a credential press 'y': " is_credential
if [ "$is_credential" = "y" -o "$is_credential" = "Y" ]; then
field_type="CONCEALED"
read -s -p "Enter alue for $field_name (input will be hidden): " field_value
echo ""
else
field_type="STRING"
read -p "Enter value for $field_name: " field_value
fi
json_output+=" {
\"id\": \"$field_name\",
\"type\": \"$field_type\",
\"label\": \"$field_name\",
\"value\": \"$field_value\"
},
"
done
json_output+=' {
"id": "notesPlain",
"type": "STRING",
"purpose": "NOTES",
"label": "notesPlain",
"value": ""
}
]
}
'
echo ""
echo "Final check"
echo "$json_output" | jq '.fields[] | {id, type, label, value: (if .type == "CONCEALED" then "concealed" else .value end)}'
read -p " Press [enter] to create the entry" input
if [ -z "$input" ]; then
echo "$json_output" | op item create --vault "$vault" -
else
echo "Canceled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment