Skip to content

Commit

Permalink
Merge pull request #9 from 0neTX/dev
Browse files Browse the repository at this point in the history
fix: Error in execution permissions in entrypoint.sh
  • Loading branch information
0neTX committed Jan 31, 2024
2 parents 903c120 + cff090c commit a0290af
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Expand Up @@ -35,6 +35,7 @@ COPY root/ /
RUN unzip /tmp/bw.zip && \
chmod +x /app/bw && \
install /app/bw /usr/local/bin/ && \
chmod +x /etc/cont-init.d/10-adduser && \
chmod +x /app/bw_export.sh && \
chmod +x /entrypoint.sh && \
echo "**** create abc user and make our folders ****" && \
Expand Down
83 changes: 42 additions & 41 deletions bw_export.sh
Expand Up @@ -26,16 +26,17 @@ Cyan='\033[0;36m' # Cyan
UCyan='\033[4;36m' # Cyan
UWhite='\033[4;37m' # White


echo Starting ...
#Set locations to save export files
if [[ -z "${OUTPUT_PATH}" ]]; then
echo -e "\n${Cyan}Info: OUTPUT_PATH enviroment not provided. Using default value: /var/data"
echo -e "\n$(date '+%F %T') ${Cyan}Info: OUTPUT_PATH enviroment not provided. Using default value: /var/data"
save_folder="/var/data/"
else
save_folder="${OUTPUT_PATH}"
if [[ ! -d "$save_folder" ]]
then
echo -e "\n${IYellow}ERROR: Could not find the folder in which to save the files: $save_folder "
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Could not find the folder in which to save the files: $save_folder "
echo
params_validated=-1
fi
Expand All @@ -44,12 +45,12 @@ fi
#Set locations to save attachment files
if [[ -z "${ATTACHMENTS_PATH}" ]]; then
save_folder_attachments="/var/attachments/"
echo -e "\n${Cyan}Info: ATTACHMENTS_PATH enviroment not provided. Using default value: /var/attachments"
echo -e "\n$(date '+%F %T') ${Cyan}Info: ATTACHMENTS_PATH enviroment not provided. Using default value: /var/attachments"
else
save_folder_attachments="${ATTACHMENTS_PATH}"
if [[ ! -d "$save_folder_attachments" ]]
then
echo -e "\n${IYellow}ERROR: Could not find the folder in which to save the attachments files: $save_folder_attachments "
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Could not find the folder in which to save the attachments files: $save_folder_attachments "
echo
params_validated=-1
fi
Expand All @@ -64,8 +65,8 @@ if [[ -z "${BW_URL_SERVER}" ]]; then
echo -e -n $Cyan # set text = yellow
echo -e "\nInfo: BW_SERVER enviroment not provided."

echo -n "If you have your own Bitwarden or Vaulwarden server, set in the environment variable BW_URL_SERVER its url address. "
echo -n "Example: https://skynet-vw.server.com"
echo -n "$(date '+%F %T') If you have your own Bitwarden or Vaulwarden server, set in the environment variable BW_URL_SERVER its url address. "
echo -n "$(date '+%F %T') Example: https://skynet-vw.server.com"
echo
else
bw_url_server="${BW_URL_SERVER}"
Expand All @@ -77,10 +78,10 @@ fi
# bw login #(follow the prompts);
if [[ -z "${BW_CLIENTID}" ]]; then

echo -e "\n${IYellow}ERROR: BW_CLIENTID enviroment variable not provided, exiting..."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: BW_CLIENTID enviroment variable not provided, exiting..."

echo -n "Your Bitwarden Personal API Key can be obtain in:"
echo -n "https://bitwarden.com/help/personal-api-key/"
echo -n "$(date '+%F %T') Your Bitwarden Personal API Key can be obtain in:"
echo -n "$(date '+%F %T') https://bitwarden.com/help/personal-api-key/"
params_validated=-1
else
if test -f "${BW_CLIENTID}"; then
Expand All @@ -94,10 +95,10 @@ fi

if [[ -z "${BW_CLIENTSECRET}" ]]; then

echo -e "\n${IYellow}ERROR: BW_CLIENTSECRET enviroment variable not provided, exiting..."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: BW_CLIENTSECRET enviroment variable not provided, exiting..."

echo -n "Your Bitwarden Personal API Key can be obtain in:"
echo -n "https://bitwarden.com/help/personal-api-key/"
echo -n "$(date '+%F %T') Your Bitwarden Personal API Key can be obtain in:"
echo -n "$(date '+%F %T') https://bitwarden.com/help/personal-api-key/"
params_validated=-1
else
if test -f "${BW_CLIENTSECRET}"; then
Expand All @@ -111,7 +112,7 @@ fi

if [[ -z "${BW_PASSWORD}" ]]; then

echo -e "\n${IYellow}ERROR: BW_PASSWORD enviroment variable not provided, exiting..."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: BW_PASSWORD enviroment variable not provided, exiting..."

params_validated=-1
else
Expand All @@ -126,7 +127,7 @@ fi

#Set Organization ID (if applicable)
if [[ -z "${BW_ORGANIZATIONS_LIST}" ]]; then
echo -e "\n${Cyan} BW_ORGANIZATIONS_LIST enviroment not provided. All detected organizations will be exported. "
echo -e "\n$(date '+%F %T') ${Cyan} BW_ORGANIZATIONS_LIST enviroment not provided. All detected organizations will be exported. "
echo -e "${Cyan} If you want to make a backup of specific organizations, set one or more organizations separated by comma"
echo -e "${Cyan} To obtain your organization_id value, open a terminal and type:"
echo -e "${Cyan} bw login #(follow the prompts); bw list organizations | jq -r '.[0] | .id'"
Expand All @@ -141,14 +142,14 @@ fi
if [[ -z "${EXPORT_PASSWORD}" ]]; then

echo
echo -e "\n${IYellow}-------------------------------------------------------------------------------------------------------------"
echo -e "\n${IYellow}Warning: EXPORT_PASSWORD enviroment not provided. Exports require a password to securize your exported vault."
echo -e "\n${IYellow}-------------------------------------------------------------------------------------------------------------"
echo -e "\n$(date '+%F %T') ${IYellow}-------------------------------------------------------------------------------------------------------------"
echo -e "\n$(date '+%F %T') ${IYellow}Warning: EXPORT_PASSWORD enviroment not provided. Exports require a password to securize your exported vault."
echo -e "\n$(date '+%F %T') ${IYellow}-------------------------------------------------------------------------------------------------------------"
echo
password1=""

else
echo -e "\n${Cyan}Info: Be sure to save your EXPORT_PASSWORD in a safe place!"
echo -e "\n$(date '+%F %T') ${Cyan}Info: Be sure to save your EXPORT_PASSWORD in a safe place!"
if test -f "${EXPORT_PASSWORD}"; then
password1=$(<${EXPORT_PASSWORD})
else
Expand All @@ -159,18 +160,18 @@ fi
# Check if required parameters has beed proviced.
if [[ $params_validated != 0 ]]
then
echo -e "\n${IYellow}One or more required environment variables have not been set."
echo -e "\n$(date '+%F %T') ${IYellow}One or more required environment variables have not been set."
echo -e "${IYellow}Please check the required environment variables:"
echo -e "${IYellow}BW_CLIENTID,BW_CLIENTSECRET,BW_PASSWORD"
exit -1
fi

echo "Starting exporting..."
echo "$(date '+%F %T') $(date '+%F %T') Starting exporting..."
echo

if [[ $bw_url_server != "" ]]
then
echo "Setting custom server..."
echo "$(date '+%F %T') Setting custom server..."
bw config server $bw_url_server --quiet --nointeraction
echo
fi
Expand All @@ -181,12 +182,12 @@ BW_CLIENTSECRET=$client_secret
#Login user if not already authenticated
if [[ $(bw status | jq -r .status) == "unauthenticated" ]]
then
echo "Performing login..."
echo "$(date '+%F %T') Performing login..."
bw login --apikey --method 0 --quiet --nointeraction
fi
if [[ $(bw status | jq -r .status) == "unauthenticated" ]]
then
echo -e "\n${IYellow}ERROR: Failed to authenticate."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to authenticate."
echo
exit 1
fi
Expand All @@ -197,10 +198,10 @@ session_key=$(bw unlock $bw_password --raw)
#Verify that unlock succeeded
if [[ $session_key == "" ]]
then
echo -e "\n${IYellow}ERROR: Failed to authenticate."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to authenticate."
exit 1
else
echo "Login successful."
echo "$(date '+%F %T') Login successful."
fi
#Export the session key as an env variable (needed by BW CLI)
export BW_SESSION="$session_key"
Expand All @@ -209,31 +210,31 @@ echo
#Check if the user has decided to enter a password or save unencrypted
if [[ $password1 == "" ]]
then
echo -e "\n${IYellow}WARNING! Your vault contents will be saved to an unencrypted file."
echo "WARNING! Your vault contents will be saved to an unencrypted file."
echo -e "\n$(date '+%F %T') ${IYellow}WARNING! Your vault contents will be saved to an unencrypted file."
echo "$(date '+%F %T') WARNING! Your vault contents will be saved to an unencrypted file."
else
echo -e "\n${Cyan}Info: Password for encrypted export has been provided."
echo -e "\n$(date '+%F %T') ${Cyan}Info: Password for encrypted export has been provided."
fi


echo "Performing vault exports..."
echo "$(date '+%F %T') Performing vault exports..."

# 1. Export the personal vault
if [[ ! -d "$save_folder" ]]
then
echo -e "\n${IYellow}ERROR: Could not find the folder in which to save the files. Path: $save_folder"
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Could not find the folder in which to save the files. Path: $save_folder"
echo
exit 1
fi

if [[ $password1 == "" ]]
then
echo
echo "Exporting personal vault to an unencrypted file..."
echo "$(date '+%F %T') Exporting personal vault to an unencrypted file..."
bw export --format json --output $save_folder
else
echo
echo "Exporting personal vault to a password-encrypted file..."
echo "$(date '+%F %T') Exporting personal vault to a password-encrypted file..."
bw export --format encrypted_json --password $password1 --output $save_folder
fi

Expand All @@ -245,7 +246,7 @@ then
organization_list=${list::-1}
if [[ ! -z "$organization_list" ]]
then
echo -e "\n${Cyan}Info: No BW_ORGANIZATIONS_LIST provided. Exporting all organizations detected in vault"
echo -e "\n$(date '+%F %T') ${Cyan}Info: No BW_ORGANIZATIONS_LIST provided. Exporting all organizations detected in vault"
fi
fi
fi
Expand All @@ -259,17 +260,17 @@ then
if [[ $password1 == "" ]]
then
echo
echo "Exporting organization vault to an unencrypted file..."
echo "$(date '+%F %T') Exporting organization vault to an unencrypted file..."
bw export --organizationid $org_id --format json --output $save_folder
else
echo
echo "Exporting organization vault to a password-encrypted file..."
echo "$(date '+%F %T') Exporting organization vault to a password-encrypted file..."
bw export --organizationid $org_id --format encrypted_json --password $password1 --output $save_folder
fi
done
else
echo
echo "No organizational vault exists, so nothing to export."
echo "$(date '+%F %T') No organizational vault exists, so nothing to export."
fi


Expand All @@ -278,23 +279,23 @@ fi
if [[ $(bw list items | jq -r '.[] | select(.attachments != null)') != "" ]]
then
echo
echo "Saving attachments..."
echo "$(date '+%F %T') Saving attachments..."
bash <(bw list items | jq -r '.[] | select(.attachments != null) | "bw get attachment \"\(.attachments[].fileName)\" --itemid \(.id) --output \"'$save_folder_attachments'\(.name)/\""' )
else
echo
echo "No attachments exist, so nothing to export."
echo "$(date '+%F %T') No attachments exist, so nothing to export."
fi

echo
echo "Vault export complete."
echo "$(date '+%F %T') Vault export complete."

# 4. Report items in the Trash (cannot be exported)
trash_count=$(bw list items --trash | jq -r '. | length')

if [[ $trash_count > 0 ]]
then

echo -e "\n${Cyan}Info: You have $trash_count items in the trash that cannot be exported."
echo -e "\n$(date '+%F %T') ${Cyan}Info: You have $trash_count items in the trash that cannot be exported."

fi

Expand All @@ -304,5 +305,5 @@ bw logout
BW_CLIENTID=
BW_CLIENTSECRET=
BW_SESSION=
echo -e "\n${IGreen}Info: Exporting finished. Have a good day"
echo -e "\n$(date '+%F %T') ${IGreen} Info: Exporting finished. Have a good day"
echo

0 comments on commit a0290af

Please sign in to comment.