AWS Vault with Yubikey
Preface
It's been a while I'm using aws-vault to manage my AWS credentials for accessing the different client accounts. It has been quite comfortable, regardless of the setup the clients choose for their users. However, there are 3 places which I'm trying to improve my own development experience:
- Requirement of 2FA authentication
- SSO Login through a browser (to get access over shell)
- Session duration
Today I managed to resolve the first one (to some extent), and here is how:
2FA
Well, I understand how the second-factor authentication contributes to keeping me secure. However, as a developer, it can be quite distracting to find your phone and go through the authenticator app to find the code you need to type to be able to continue your work (yeah, call me lazy!).
So, to solve this issue, I was using the 2fa CLI application, so I had this code with the comfort of my CLI. But:
- 2fa stores the secret key in a plain text file in
~/.2fa
. Meaning anyonecat
ing it can regenerate my tokens (more of a silly practice than a security risk, as 2fa is not the only layer of protection). - I need to have a journey between terminal/tmux windows to get this code in my clipboard (told you already, I'm lazy!).
Yubikey
Thanks to DataChef, we've switched to using yubikey devices, for our second-factor authentication. And I was wondering if it can help me to cover these concerns!
Well, it is. All I need to do is to provide it with the secret key (either resync, or extract it from one of the registered devices), and after that, whenever needed just touch the key and it's done:
In Action
Gotchas
Using this so far, I've faced 2 small glitches, which confused me at first:
- When a token has been used, it's not possible to reuse it for a different session on AWS. This I couldn't face before (tbh, the use-case is quite rare), because the context switch was probably taking me around 30 seconds (the time window for token refresh)!
av
aliases for zsh-aws-vault plugin (link to my fork), need a bit of refactoring to be able to understand--prompt
parameter. Otherwise, I need to provideAWS_VAULT_PL_MFA=yubikey
as an environment variable, you can see why:
avsh () {
case ${AWS_VAULT_PL_MFA} in
(inline) aws-vault exec -t $2 $1 -- zsh ;;
(yubikey) totp=${2:-$1}
aws-vault exec -t $(ykman oath code --single $totp) $1 -- zsh ;;
(*) aws-vault exec $1 -- zsh ;;
esac
}