PassBatchInsert/PassBatchInsert.sh

136 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================================
# Script for Bulk Creating Pass Entries Based on a JSON File
# ============================================================================================
#
: '
Title :
PassBatchInsert.sh
Usage :
Automate inserts of pass (password-store) entries based on a JSON file or input.
Requires:
jq
> brew install jq
Model :
{
filename:'',
password:'',
url:'',
user:''
}
';
###################### VARS ######################
DATE_CUR=$(date '+%Y-%m-%d__%H:%M:%S');
SCRIPT_NAME="PassBatchInsert";
SCRIPT_DIR=$(dirname "$0");
JSON_FILE=$SCRIPT_DIR/input/$SCRIPT_NAME.json;
SCRIPT_TMP="$SCRIPT_DIR/log/$SCRIPT_NAME.tmp";
SCRIPT_LOG="$SCRIPT_DIR/log/$SCRIPT_NAME.log";
LOG_BEGIN="
=================================================================
|| ||
|| $SCRIPT_NAME ||
|| $DATE_CUR ||
|| ||
=================================================================";
LOG_END="
=================================================================
||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||
=================================================================";
################## LOG STUFF ####################
function start_log() {
echo -e "$LOG_BEGIN" | tee -a $SCRIPT_LOG;
}
function end_log() {
echo -e "$LOG_END" | tee -a $SCRIPT_LOG;
}
function echo_log() {
echo -e "\n -:- $1\n" | tee -a $SCRIPT_LOG;
}
function echo_error() {
echo -e "\n$SCRIPT_NAME ~ GENERAL SCRIPT ERROR: \n$1\n" | tee -a $SCRIPT_LOG;
}
function arg_error() {
echo -e "\n$SCRIPT_NAME ~ REQUIRED ARGUMENT NOT FOUND: \n$1\n" | tee -a $SCRIPT_LOG;
}
################## PROCEDURES ####################
function process_json_entries() {
# Load JSON Input
JSON_LENGTH=$(jq -r '. | length' $JSON_FILE);
# Loop Through JSON_INPUT And Insert Pass Entries
if [[ JSON_LENGTH > 0 ]]; then
for (( i=0; i<$JSON_LENGTH; i++ )); do
path=$(jq -r ".[${i}].path" $JSON_FILE);
filename=$(jq -r ".[${i}].filename" $JSON_FILE);
password=$(jq -r ".[${i}].password" $JSON_FILE);
url=$(jq -r ".[${i}].url" $JSON_FILE);
user=$(jq -r ".[${i}].user" $JSON_FILE);
# Start Log
echo -e "\nProcessing Entry for: $user at $url" | tee -a $SCRIPT_LOG;
# Check If Entry Exists
check_for_entry=$(pass ls "$path/$filename");
# If New, Enter
if [ -z "$check_for_entry" ]; then
echo -e "-- Adding New Entry -- $path/$filename." | tee -a $SCRIPT_LOG;
echo -e "$password\nurl: $url\nuser: $user" | pass insert -m "$path/$filename";
else
echo -e "-- Skipping Entry -- entry already exists." | tee -a $SCRIPT_LOG;
fi
done
else
echo -e "\n\n !!! Init Fail -- Unable to load the JSON file !!!\n" | tee -a $SCRIPT_LOG;
fi
}
##################### MAIN ######################
function main_process() {
process_json_entries;
}
###################### INIT ######################
start_log;
main_process;
end_log;
exit 1;