Skip to content

Commit 3ad13c6

Browse files
committed
fix: resolve Magento 2.4.8+ 2FA configuration bug in magento2-init
Implements workaround for Adobe Commerce core issue #39836 where using --lock-env with twofactorauth/general/force_providers causes DuoSecurity provider to fail due to array vs string storage mismatch in env.php.
1 parent e5c445b commit 3ad13c6

1 file changed

Lines changed: 140 additions & 40 deletions

File tree

commands/magento2-init.cmd

Lines changed: 140 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -594,46 +594,87 @@ echo -e "\033[36m[11/12] Running initial indexing...\033[0m"
594594

595595
echo -e "\033[36m[11/12] Creating admin user and configuring 2FA...\033[0m"
596596

597-
# Generate admin user and 2FA setup for Magento 2.4.6+ (all supported versions require 2FA)
598-
"${ROLL_DIR}/bin/roll" cli bash -c "
599-
set -e
600-
601-
# Generate admin credentials
602-
ADMIN_PASS=\"\$(pwgen -n1 16)\"
603-
ADMIN_USER=admin
604-
605-
echo 'Creating admin user...'
606-
bin/magento admin:user:create \\
607-
--admin-password=\"\${ADMIN_PASS}\" \\
608-
--admin-user=\"\${ADMIN_USER}\" \\
609-
--admin-firstname=\"Local\" \\
610-
--admin-lastname=\"Admin\" \\
611-
--admin-email=\"\${ADMIN_USER}@example.com\"
612-
613-
echo \"Admin Username: \${ADMIN_USER}\"
614-
echo \"Admin Password: \${ADMIN_PASS}\"
615-
616-
# Configure 2FA
617-
echo 'Configuring 2FA...'
618-
TFA_SECRET=\$(python3 -c \"import base64; print(base64.b32encode('\$(pwgen -A1 128)'.encode()).decode().strip('='))\")
619-
OTPAUTH_URL=\$(printf \"otpauth://totp/%s%%3Alocaladmin%%40example.com?issuer=%s&secret=%s\" \\
620-
\"app.${PROJECT_NAME}.test\" \"app.${PROJECT_NAME}.test\" \"\${TFA_SECRET}\"
621-
)
622-
623-
bin/magento config:set --lock-env twofactorauth/general/force_providers google
624-
bin/magento security:tfa:google:set-secret \"\${ADMIN_USER}\" \"\${TFA_SECRET}\"
597+
# Function to check if version is 2.4.8 or higher
598+
is_magento_248_or_higher() {
599+
local version="$1"
600+
local base_version
625601

626-
echo \"2FA Setup URL: \${OTPAUTH_URL}\"
627-
echo \"2FA Backup Codes:\"
628-
oathtool -s 30 -w 10 --totp --base32 \"\${TFA_SECRET}\"
602+
# Extract base version (remove patch info)
603+
if [[ "${version}" =~ ^([0-9]+\.[0-9]+\.[0-9x]+) ]]; then
604+
base_version="${BASH_REMATCH[1]}"
605+
else
606+
base_version="${version}"
607+
fi
629608

630-
# Generate QR code
631-
segno \"\${OTPAUTH_URL}\" -s 4 -o \"pub/media/\${ADMIN_USER}-totp-qr.png\"
632-
QR_URL=\"https://app.${PROJECT_NAME}.test/media/\${ADMIN_USER}-totp-qr.png?t=\$(date +%s)\"
633-
echo \"QR Code URL: \${QR_URL}\"
609+
# Check if version is 2.4.8+ or 2.4.x (which defaults to latest)
610+
case "${base_version}" in
611+
"2.4.x"|"2.4.9"*|"2.4.8"*)
612+
return 0 # true
613+
;;
614+
*)
615+
return 1 # false
616+
;;
617+
esac
618+
}
619+
620+
# Generate admin user and 2FA setup for Magento 2.4.6+ (all supported versions require 2FA)
621+
if is_magento_248_or_higher "${MAGENTO_VERSION}"; then
622+
echo -e "\033[33m🔧 Detected Magento 2.4.8+ - Using workaround for 2FA configuration issue\033[0m"
623+
echo -e "\033[33m (Adobe Commerce core issue #39836 - DuoSecurity provider array handling)\033[0m"
634624

635-
# Save credentials to file for user reference
636-
cat > /var/www/html/admin-credentials.txt << EOL
625+
# Magento 2.4.8+ workaround for 2FA configuration bug
626+
"${ROLL_DIR}/bin/roll" cli bash -c "
627+
set -e
628+
629+
# Generate admin credentials
630+
ADMIN_PASS=\"\$(pwgen -n1 16)\"
631+
ADMIN_USER=admin
632+
633+
echo 'Creating admin user...'
634+
bin/magento admin:user:create \\
635+
--admin-password=\"\${ADMIN_PASS}\" \\
636+
--admin-user=\"\${ADMIN_USER}\" \\
637+
--admin-firstname=\"Local\" \\
638+
--admin-lastname=\"Admin\" \\
639+
--admin-email=\"\${ADMIN_USER}@example.com\"
640+
641+
echo \"Admin Username: \${ADMIN_USER}\"
642+
echo \"Admin Password: \${ADMIN_PASS}\"
643+
644+
# Configure 2FA - using workaround for 2.4.8+ core bug
645+
echo 'Configuring 2FA (using 2.4.8+ workaround)...'
646+
TFA_SECRET=\$(python3 -c \"import base64; print(base64.b32encode('\$(pwgen -A1 128)'.encode()).decode().strip('='))\")
647+
OTPAUTH_URL=\$(printf \"otpauth://totp/%s%%3Alocaladmin%%40example.com?issuer=%s&secret=%s\" \\
648+
\"app.${PROJECT_NAME}.test\" \"app.${PROJECT_NAME}.test\" \"\${TFA_SECRET}\"
649+
)
650+
651+
# Step 1: Set 2FA provider without --lock-env to avoid array storage bug
652+
echo 'Setting 2FA provider (step 1/4)...'
653+
bin/magento config:set twofactorauth/general/force_providers google
654+
655+
# Step 2: Run DI compile to ensure TFA commands are available
656+
echo 'Compiling DI container (step 2/4)...'
657+
bin/magento setup:di:compile --quiet
658+
659+
# Step 3: Set the TFA secret
660+
echo 'Setting 2FA secret (step 3/4)...'
661+
bin/magento security:tfa:google:set-secret \"\${ADMIN_USER}\" \"\${TFA_SECRET}\"
662+
663+
# Step 4: Run setup:upgrade to ensure all configs are applied
664+
echo 'Running setup upgrade (step 4/4)...'
665+
bin/magento setup:upgrade --keep-generated
666+
667+
echo \"2FA Setup URL: \${OTPAUTH_URL}\"
668+
echo \"2FA Backup Codes:\"
669+
oathtool -s 30 -w 10 --totp --base32 \"\${TFA_SECRET}\"
670+
671+
# Generate QR code
672+
segno \"\${OTPAUTH_URL}\" -s 4 -o \"pub/media/\${ADMIN_USER}-totp-qr.png\"
673+
QR_URL=\"https://app.${PROJECT_NAME}.test/media/\${ADMIN_USER}-totp-qr.png?t=\$(date +%s)\"
674+
echo \"QR Code URL: \${QR_URL}\"
675+
676+
# Save credentials to file for user reference
677+
cat > /var/www/html/admin-credentials.txt << EOL
637678
Magento Admin Credentials
638679
========================
639680
Username: \${ADMIN_USER}
@@ -645,10 +686,69 @@ Admin Panel: https://app.${PROJECT_NAME}.test/shopmanager/
645686
Frontend: https://app.${PROJECT_NAME}.test/
646687
647688
Generated on: \$(date)
689+
690+
Note: This installation used the 2.4.8+ workaround for Adobe Commerce core issue #39836
648691
EOL
649-
650-
echo 'Admin credentials saved to admin-credentials.txt'
651-
"
692+
693+
echo 'Admin credentials saved to admin-credentials.txt'
694+
"
695+
else
696+
# Standard 2FA setup for Magento 2.4.6-2.4.7
697+
"${ROLL_DIR}/bin/roll" cli bash -c "
698+
set -e
699+
700+
# Generate admin credentials
701+
ADMIN_PASS=\"\$(pwgen -n1 16)\"
702+
ADMIN_USER=admin
703+
704+
echo 'Creating admin user...'
705+
bin/magento admin:user:create \\
706+
--admin-password=\"\${ADMIN_PASS}\" \\
707+
--admin-user=\"\${ADMIN_USER}\" \\
708+
--admin-firstname=\"Local\" \\
709+
--admin-lastname=\"Admin\" \\
710+
--admin-email=\"\${ADMIN_USER}@example.com\"
711+
712+
echo \"Admin Username: \${ADMIN_USER}\"
713+
echo \"Admin Password: \${ADMIN_PASS}\"
714+
715+
# Configure 2FA - standard method for 2.4.6-2.4.7
716+
echo 'Configuring 2FA...'
717+
TFA_SECRET=\$(python3 -c \"import base64; print(base64.b32encode('\$(pwgen -A1 128)'.encode()).decode().strip('='))\")
718+
OTPAUTH_URL=\$(printf \"otpauth://totp/%s%%3Alocaladmin%%40example.com?issuer=%s&secret=%s\" \\
719+
\"app.${PROJECT_NAME}.test\" \"app.${PROJECT_NAME}.test\" \"\${TFA_SECRET}\"
720+
)
721+
722+
bin/magento config:set --lock-env twofactorauth/general/force_providers google
723+
bin/magento security:tfa:google:set-secret \"\${ADMIN_USER}\" \"\${TFA_SECRET}\"
724+
725+
echo \"2FA Setup URL: \${OTPAUTH_URL}\"
726+
echo \"2FA Backup Codes:\"
727+
oathtool -s 30 -w 10 --totp --base32 \"\${TFA_SECRET}\"
728+
729+
# Generate QR code
730+
segno \"\${OTPAUTH_URL}\" -s 4 -o \"pub/media/\${ADMIN_USER}-totp-qr.png\"
731+
QR_URL=\"https://app.${PROJECT_NAME}.test/media/\${ADMIN_USER}-totp-qr.png?t=\$(date +%s)\"
732+
echo \"QR Code URL: \${QR_URL}\"
733+
734+
# Save credentials to file for user reference
735+
cat > /var/www/html/admin-credentials.txt << EOL
736+
Magento Admin Credentials
737+
========================
738+
Username: \${ADMIN_USER}
739+
Password: \${ADMIN_PASS}
740+
2FA Setup URL: \${OTPAUTH_URL}
741+
QR Code URL: \${QR_URL}
742+
743+
Admin Panel: https://app.${PROJECT_NAME}.test/shopmanager/
744+
Frontend: https://app.${PROJECT_NAME}.test/
745+
746+
Generated on: \$(date)
747+
EOL
748+
749+
echo 'Admin credentials saved to admin-credentials.txt'
750+
"
751+
fi
652752

653753
echo -e "\033[36m[12/12] Finalizing setup...\033[0m"
654754

0 commit comments

Comments
 (0)