Skip to content

Instantly share code, notes, and snippets.

@mrantivirus
Last active October 31, 2023 08:38
Show Gist options
  • Save mrantivirus/24d5966ba5c6b161e5ecd8cd45c9f9c3 to your computer and use it in GitHub Desktop.
Save mrantivirus/24d5966ba5c6b161e5ecd8cd45c9f9c3 to your computer and use it in GitHub Desktop.
Bash script to import dotenv files into 1Password.
#!/bin/bash
# Function to display help information
function display_help() {
echo "Usage: $0 [dotenv_path] [item_name] [-v vault_name] [-s section_name]"
echo ""
echo "dotenv_path: Path to the dotenv file."
echo "item_name: Name of the 1Password item."
echo "Optional Parameters:"
echo " -v vault_name: Name of the vault in 1Password."
echo " -s section_name: Section name for the dotenv key-values."
exit 1
}
# Check if help flag is set
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
display_help
fi
# Check arguments
if [ "$#" -lt 2 ]; then
echo "Error: Missing required arguments."
display_help
fi
DOTENV_PATH=$1
ITEM_NAME=$2
shift 2
VAULT=""
SECTION=""
while getopts ":v:s:" opt; do
case $opt in
v)
VAULT="$OPTARG"
;;
s)
SECTION="$OPTARG."
;;
\?)
echo "Invalid option: -$OPTARG" >&2
display_help
;;
:)
echo "Option -$OPTARG requires an argument." >&2
display_help
;;
esac
done
# Check if item exists in the vault
if [[ ! -z "$VAULT" ]]; then
op item list --categories API\ Credential --vault "$VAULT" | grep -q "\"name\":\"$ITEM_NAME\""
ITEM_EXISTS=$?
else
op item list --categories API\ Credential | grep -q "\"name\":\"$ITEM_NAME\""
ITEM_EXISTS=$?
fi
# If the item does not exist, create it
if [ "$ITEM_EXISTS" -eq 1 ]; then
op item create --category="API Credential" --title="$ITEM_NAME" ${VAULT:+--vault=$VAULT}
fi
# Read dotenv file and update 1Password item
while IFS='=' read -r key value || [[ -n "$key" ]]; do
# Ignore comments
if [[ ! $key =~ ^# ]]; then
# Edit the existing item
op item edit "$ITEM_NAME" "${SECTION}${key}[text]=$value"
fi
done < "$DOTENV_PATH"
@mrantivirus
Copy link
Author

mrantivirus commented Oct 24, 2023

1Password dotenvImporter

This script, 1password_import_dotenv.sh, imports key-value pairs from a dotenv file and populates a 1Password item with those values. It can optionally specify a vault and a section within the item.

Prerequisites

  • Ensure you have op (1Password CLI) installed and configured.

Usage

bash ./1password_import_dotenv.sh [dotenv_path] [item_name] [-v vault_name] [-s section_name]

Parameters:

  • dotenv_path: Path to the dotenv file you wish to import from.
  • item_name: Name of the 1Password item you wish to update or create.
  • -v vault_name (optional): Name of the 1Password vault where the item resides or should be created.
  • -s section_name (optional): Name of the section within the item where the key-values from the dotenv file should be placed.

Examples:

  1. To import key-value pairs from ./local.env to an item named "DatabaseCreds" in the default vault:

bash ./1password_import_dotenv.sh ./local.env DatabaseCreds
2. To import key-value pairs from ./local.env to an item named "DatabaseCreds" in a vault named "WorkVault", within a section named "Database":

bash ./1password_import_dotenv.sh ./local.env DatabaseCreds -v WorkVault -s Database

Notes

  1. Before running the script, ensure you are signed in to the 1Password CLI (op). If not, you'll need to sign in to be able to interact with your vaults and items.
  2. If the specified 1Password item does not exist in the vault, the script will create a new Secure Note item with the given name and populate it with the fields from the dotenv file.
  3. Any lines in the dotenv file starting with a '#' character (comments) will be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment