6868# Detect encrypted state files (both standard and dynamic naming)
6969echo " Detecting encrypted state files to decrypt..."
7070
71- # Find all .gpg files in current directory
71+ # Find all .gpg files in current directory using find command for more reliable detection
7272ALL_ENCRYPTED_FILES=()
73- for file in * .gpg; do
73+ echo " Scanning for encrypted files..."
74+
75+ # Use find command to get all .gpg files (more reliable than shell globbing)
76+ while IFS= read -r -d ' ' file; do
7477 if [ -f " $file " ]; then
75- ALL_ENCRYPTED_FILES+=(" $file " )
76- echo " Found encrypted file: $file "
78+ # Get just the filename without path
79+ filename=$( basename " $file " )
80+ ALL_ENCRYPTED_FILES+=(" $filename " )
81+ echo " Found encrypted file: $filename "
7782 fi
78- done
83+ done < <( find . -maxdepth 1 -name " *.gpg " -type f -print0 )
7984
8085if [ ${# ALL_ENCRYPTED_FILES[@]} -eq 0 ]; then
8186 echo " No encrypted state files found"
@@ -98,16 +103,17 @@ for encrypted_file in "${ALL_ENCRYPTED_FILES[@]}"; do
98103 echo " Successfully decrypted $encrypted_file "
99104
100105 # Verify the decrypted file is valid
101- if [ " $decrypted_file " = " terraform .tfstate" ] && [ -s " $decrypted_file " ]; then
102- echo " 📋 Terraform state file decrypted and ready for operations"
106+ if ([[ " $decrypted_file " == * .tfstate ]] || [[ " $decrypted_file " == * .tfstate.backup ]]) && [ -s " $decrypted_file " ]; then
107+ echo " 📋 Terraform state file decrypted and ready for operations: $decrypted_file "
103108
104- # Verify JSON structure for state files
105- if [[ " $decrypted_file " == * .tfstate ]]; then
106- if python3 -c " import json; json.load(open('$decrypted_file '))" 2> /dev/null; then
107- echo " JSON structure is valid"
108- else
109- echo " ⚠️ Warning: JSON structure may be invalid"
110- fi
109+ # Verify JSON structure for state files (skip for backup files as they might be older format)
110+ if [[ " $decrypted_file " == * .tfstate ]] && python3 -c " import json; json.load(open('$decrypted_file '))" 2> /dev/null; then
111+ echo " JSON structure is valid"
112+ elif [[ " $decrypted_file " == * .tfstate.backup ]]; then
113+ echo " Backup state file restored"
114+ # Skip JSON validation for backup files as they might be from different versions
115+ else
116+ echo " ⚠️ Warning: JSON structure may be invalid"
111117 fi
112118 fi
113119
0 commit comments