Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Yubikey scripts to support device name #1245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions contrib/scripts/aws-iam-create-yubikey-mfa.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
# Adds a Yubikey TOTP device to IAM using your IAM User as the $MFA_DEVICE_NAME
# Currently, aws iam enable-mfa-device doesn't support specifying your MFA Device Name.
# Adds a Yubikey TOTP device to IAM user.
# By default the device name is set to "YubiKey-<serial number>" but can be
# overridden with the $MFA_DEVICE_NAME environment variable.

set -eu

Expand All @@ -9,6 +10,11 @@ if [ -n "${AWS_SESSION_TOKEN:-}" ]; then
exit 1
fi

if [ -z "${MFA_DEVICE_NAME:-}" ]; then
MFA_DEVICE_NAME=YubiKey-$(ykman list --serials | tr -d '\n')
fi


ACCOUNT_ARN=$(aws sts get-caller-identity --query Arn --output text)

# Assume that the final portion of the ARN is the username
Expand All @@ -19,7 +25,7 @@ OUTFILE=$(mktemp)
trap 'rm -f "$OUTFILE"' EXIT

SERIAL_NUMBER=$(aws iam create-virtual-mfa-device \
--virtual-mfa-device-name "$USERNAME" \
--virtual-mfa-device-name "$MFA_DEVICE_NAME" \
--bootstrap-method Base32StringSeed \
--outfile "$OUTFILE" \
--query VirtualMFADevice.SerialNumber \
Expand All @@ -40,3 +46,6 @@ aws iam enable-mfa-device \
--serial-number "$SERIAL_NUMBER" \
--authentication-code1 "$CODE1" \
--authentication-code2 "$CODE2"

echo "mfa_serial = $SERIAL_NUMBER"
echo "mfa_process = ykman oath accounts code --single $SERIAL_NUMBER"
11 changes: 8 additions & 3 deletions contrib/scripts/aws-iam-resync-yubikey-mfa.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#!/bin/sh
# Resync a Yubikey TOTP device to IAM using your IAM User as the $MFA_DEVICE_NAME
# Currently, aws iam resync-mfa-device doesn't support specifying your MFA Device Name.
# Resync a Yubikey TOTP device to IAM user
# By default the device name is set to "YubiKey-<serial number>" but can be
# overridden with the $MFA_DEVICE_NAME environment variable.

set -eu

if [ -z "${MFA_DEVICE_NAME:-}" ]; then
MFA_DEVICE_NAME="YubiKey-$(ykman list --serials | tr -d '\n')"
fi

ACCOUNT_ARN=$(aws sts get-caller-identity --query Arn --output text)

# Assume that the final portion of the ARN is the username
# Works for ARNs like `users/<user>` and `users/engineers/<user>`
USERNAME=$(echo "$ACCOUNT_ARN" | rev | cut -d/ -f1 | rev)

ACCOUNT_ID=$(echo "$ACCOUNT_ARN" | cut -d: -f5)
SERIAL_NUMBER="arn:aws:iam::${ACCOUNT_ID}:mfa/${USERNAME}"
SERIAL_NUMBER="arn:aws:iam::${ACCOUNT_ID}:mfa/${MFA_DEVICE_NAME}"

CODE1=$(ykman oath accounts code -s "$SERIAL_NUMBER")

Expand Down