Skip to content
Snippets Groups Projects
Verified Commit 00b377ce authored by Marcin Sobczyk's avatar Marcin Sobczyk Committed by Mateusz Żebrak
Browse files

Use clive unlock in docker entrypoint

parent a4a4313d
No related branches found
No related tags found
3 merge requests!524v1.27.5.19 Release,!501Implement clive unlock/lock and use in docker entrypoint script,!496Draft: Msobczyk/encrypt profile
......@@ -4,6 +4,7 @@
set -euo pipefail
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P)"
SELECTED_PROFILE=""
TESTNET_NODE_LOG_FILE="testnet_node.log"
INTERACTIVE_CLI_MODE=0
......
......@@ -3,32 +3,6 @@
BEEKEEPER_ALREADY_CLOSED=0
trap "clean_up" SIGTERM SIGQUIT SIGHUP EXIT
# Retry passed function
retry() {
local func=$1
local retries=0
local max_retries=3
if ! declare -f "$func" > /dev/null; then
echo "Error: '$func' is not a function."
exit 1
fi
while (( retries < max_retries )); do
"$func"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]; then
return 0
fi
((retries++))
echo "Attempt $retries of $max_retries failed. Retrying..."
done
echo "Maximum attempts reached. Exiting."
exit 1
}
# Start Beekeeper with prepared session token
start_beekeeper_with_prepared_session_token() {
output=$(clive beekeeper spawn)
......@@ -37,17 +11,17 @@ start_beekeeper_with_prepared_session_token() {
echo "Error: Fail to spawn Beekeeper. Aborting..."
exit 1
fi
BEEKEEPER_HTTP_ENDPOINT=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
CLIVE_BEEKEEPER__REMOTE_ADDRESS=$(echo "$output" | grep -oE 'http://[0-9.]+:[0-9]+')
CLIVE_BEEKEEPER__SESSION_TOKEN=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.create_session",
"params": {
"salt": "clive-cli-session",
"notifications_endpoint": "'"${BEEKEEPER_HTTP_ENDPOINT}"'"
"notifications_endpoint": "'"${CLIVE_BEEKEEPER__REMOTE_ADDRESS}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}" | jq .result.token | tr -d '"')
}' "${CLIVE_BEEKEEPER__REMOTE_ADDRESS}" | jq .result.token | tr -d '"')
if [[ "${CLIVE_BEEKEEPER__SESSION_TOKEN}" == "null" ]]; then
......@@ -55,32 +29,19 @@ start_beekeeper_with_prepared_session_token() {
exit 1
fi
export CLIVE_BEEKEEPER__SESSION_TOKEN=${CLIVE_BEEKEEPER__SESSION_TOKEN}
export BEEKEEPER_HTTP_ENDPOINT=${BEEKEEPER_HTTP_ENDPOINT}
export CLIVE_BEEKEEPER__REMOTE_ADDRESS
export CLIVE_BEEKEEPER__SESSION_TOKEN
}
# Unlock wallet for selected profile
unlock_wallet() {
read -rsp "Enter password for profile ${SELECTED_PROFILE}: " password
echo
password="${password//$'\n'/}"
response=$(curl -s --data '{
"jsonrpc": "2.0",
"method": "beekeeper_api.unlock",
"params": {
"token": "'"${CLIVE_BEEKEEPER__SESSION_TOKEN}"'",
"wallet_name": "'"${SELECTED_PROFILE}"'",
"password": "'"${password}"'"
},
"id": 1
}' "${BEEKEEPER_HTTP_ENDPOINT}")
error=$(echo "${response}" | jq .error)
if [[ "${error}" != "null" ]]; then
return 1
local profile_name_arg=""
if [[ -n "$SELECTED_PROFILE" ]]; then
profile_name_arg="--profile-name=${SELECTED_PROFILE}"
fi
return 0
# shellcheck disable=SC2086
clive unlock $profile_name_arg --include-create-new-profile
return $?
}
# Print info about how to create profile
......@@ -91,55 +52,6 @@ how_to_create_profile() {
echo ""
}
# Select one of the existing profiles
select_profile(){
clive_profiles=$(clive show profiles)
clive_profiles_formated=$(echo "$clive_profiles" | grep -oP "\[\K[^\]]+")
IFS=',' read -ra profile_array <<< "$clive_profiles_formated"
if [[ -n "${SELECTED_PROFILE:-}" ]]; then
for profile in "${profile_array[@]}"; do
profile=$(echo "${profile}" | tr -d "' ")
if [[ "${profile}" == "${SELECTED_PROFILE}" ]]; then
retry unlock_wallet
return
fi
done
echo "Error: Given profile ${SELECTED_PROFILE} does not exists."
echo "${clive_profiles}"
exit 1
fi
if [ ${#profile_array[@]} -eq 0 ]; then
echo "There are no profiles."
how_to_create_profile
else
echo "Select profile:"
for i in "${!profile_array[@]}"; do
profile=$(echo "${profile_array[i]}" | tr -d "' ")
echo "$((i + 1)). $profile"
done
echo "$(( ${#profile_array[@]} + 1 )). create new profile"
read -rp "Enter the number: " choice
if [[ $choice -ge 1 && $choice -le ${#profile_array[@]} ]]; then
selected_profile=$(echo "${profile_array[$((choice - 1))]}" | tr -d "' ")
echo "You selected: $selected_profile"
SELECTED_PROFILE=${selected_profile}
echo "Selected profile is ${SELECTED_PROFILE}"
retry unlock_wallet
elif [[ $choice -eq $(( ${#profile_array[@]} + 1 )) ]]; then
echo "You selected: create new profile"
how_to_create_profile
else
echo "Error: Invalid selection!"
return 1
fi
fi
}
# Execute a script passed as an argument
execute_passed_script() {
if [[ -n "${FILE_TO_EXECUTE:-}" ]]; then
......@@ -167,7 +79,10 @@ close_beekeeper() {
# Execute before entering interactive mode
setup() {
start_beekeeper_with_prepared_session_token
retry select_profile
if ! unlock_wallet; then
echo "Error: Failed to unlock wallet. Aborting..."
exit 1
fi
execute_passed_script
# shellcheck disable=SC1090
source ~/.bashrc
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment