@@ -130,10 +130,12 @@ get_playbook_name() {
130130# Retrieve a secret from Azure Key Vault.
131131# :param key_vault_id: The ID of the Key Vault.
132132# :param secret_id: The ID of the secret in the Key Vault.
133+ # :param auth_type: The authentication type (e.g., "SSHKEY", "VMPASSWORD").
133134# :return: None. Exits with a non-zero status if retrieval fails.
134135retrieve_secret_from_key_vault () {
135136 local key_vault_id=$1
136137 local secret_id=$2
138+ local auth_type=$3 # Add auth_type as a parameter
137139
138140 subscription_id=$( echo " $key_vault_id " | awk -F' /' ' {for(i=1;i<=NF;i++){if($i=="subscriptions"){print $(i+1)}}}' )
139141
@@ -169,21 +171,29 @@ retrieve_secret_from_key_vault() {
169171
170172 log " INFO" " Successfully retrieved secret from Key Vault."
171173
172- # Define a unique temporary file path
173- temp_file=$( mktemp --dry-run --suffix=.ppk)
174+ # Define a unique temporary file path based on auth_type
175+ if [[ " $auth_type " == " SSHKEY" ]]; then
176+ temp_file=$( mktemp --dry-run --suffix=.ppk)
177+ elif [[ " $auth_type " == " VMPASSWORD" ]]; then
178+ temp_file=$( mktemp --dry-run)
179+ else
180+ log " ERROR" " Unknown authentication type: $auth_type "
181+ exit 1
182+ fi
183+
174184 if [[ -f " $temp_file " ]]; then
175185 log " ERROR" " Temporary file already exists: $temp_file "
176186 exit 1
177187 fi
178188
179189 # Create the temporary file and write the secret value to it
180- echo " $secret_value " > " $temp_file "
181- chmod 600 " $temp_file " # Set the correct permissions for the private key file
190+ echo " $secret_value " > " $temp_file " > /dev/null
191+ chmod 600 " $temp_file " # Set the correct permissions for the file
182192 if [[ ! -s " $temp_file " ]]; then
183193 log " ERROR" " Failed to store the retrieved secret in the temporary file."
184194 exit 1
185195 fi
186- log " INFO" " Temporary SSH key file created with secure permissions: $temp_file "
196+ log " INFO" " Temporary file created with secure permissions: $temp_file "
187197}
188198
189199# Run the ansible playbook.
@@ -217,7 +227,7 @@ run_ansible_playbook() {
217227
218228 if [[ -n " $key_vault_id " && -n " $secret_id " ]]; then
219229 log " INFO" " Key Vault ID and Secret ID are set. Retrieving SSH key from Key Vault."
220- retrieve_secret_from_key_vault " $key_vault_id " " $secret_id "
230+ retrieve_secret_from_key_vault " $key_vault_id " " $secret_id " " SSHKEY "
221231
222232 check_file_exists " $temp_file " \
223233 " Temporary SSH key file not found. Please check the Key Vault secret ID."
@@ -236,7 +246,7 @@ run_ansible_playbook() {
236246
237247 if [[ -n " $key_vault_id " && -n " $secret_id " ]]; then
238248 log " INFO" " Key Vault ID and Secret ID are set. Retrieving VM password from Key Vault."
239- retrieve_secret_from_key_vault " $key_vault_id " " $secret_id "
249+ retrieve_secret_from_key_vault " $key_vault_id " " $secret_id " " VMPASSWORD "
240250
241251 check_file_exists " $temp_file " \
242252 " Temporary SSH key file not found. Please check the Key Vault secret ID."
0 commit comments