Monday, March 4, 2019

AWS MFA with Yubikey and macOS

Purpose: Take advantage of the Yubikey to generate the 6 digit TOTP required by AWS MFA without using Google Authenticator.
Downside: You can't just press the Yubikey button and have the code generated as you would expect, but this can be alleviated with a keyboard shortcut.
Sources:
  1. Install the Yubikey CLI
  2. brew install ykman
  3. Insert the Yubikey
  4. Show a list of configured TOTP accounts
  5. [jerome@jeroboam] > ykman oath list
    [jerome@jeroboam] > 
  6. Log in to AWS Management Console as usual, pop up the menu by clicking on your user name and select My Security Credentials.
  7. Push the "Manage MFA Device" button.
  8. Select Remove to disable MFA, and then re-start the procedure to activate MFA again.

  9. In the next screen, select “Virtual MFA device”.
  10. Show the secret key: it will be passed to ykman.
  11. Configure MFA for your service.
  12. [jerome@jeroboam] > ykman oath add 'Amazon Web Services:toto@org-prod' 
    [jerome@jeroboam] > ykman oath list
    Amazon Web Services:toto@org-prod
    
  13. Then, this will get you a 6 digit code.
  14. [jerome@jeroboam] > ykman oath code --single 'Amazon Web Services:toto@org-prod'
  15. Start Automator, and create a new Quick Action.
  16. Search for applescript.
  17. Drag and drop "Run applescript" to the right hand side, select "Workflow receives no input", and type the following code.
  18. Then File | Save, and go to System Preferences | Keyboard | Shortcuts | Services to assign a shortcut to your new service.
  19. Now, simply log in the AWS Management Console with your password, and when the site asks for the MFA, use the programmed shortcut, which will automatically generate the 6 digit code and grant you access.
NB: First time I tried the shortcut in Firefox, I got this error: 

but macOS Mojave, Automator “Not authorized to send Apple events to System Events.” gave the solution:
I quote:
System Preferences > Security & Privacy > Accessibility > Click Automator and TADA it works.
End quote