【Amazon WorkSpaces】Radiusサーバへのユーザー追加およびGoogle Authenticator設定の自動化

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

はじめに

WorkSpacesでMFAを利用する際にRadiusサーバを構築する場合があり、その際にRadiusサーバへのユーザー追加およびGoogle Authenticatorの設定は地味に大変かと思います。

ということで今回はその作業を自動化するスクリプトを作ってみたため、ブログでご紹介いたします。

使い方

useradd-and-google-authenticator.sh [リストファイル]

詳細

リストファイル

リストファイルの名前は任意で問題なく、中身はユーザー名とUIDをカンマ区切りで指定します。 以下は例として、10ユーザー分の作成リストを記載しておりますが、行を追加すれば10ユーザー以上の作成が可能です。

# cat useradd-list.txt
test-user01,2001
test-user02,2002
test-user03,2003
test-user04,2004
test-user05,2005
test-user06,2006
test-user07,2007
test-user08,2008
test-user09,2009
test-user10,2010
#

useradd-and-google-authenticator.sh

#!/bin/bash

## 引数チェック
if [[ -z $1 ]] ; then
   echo "第1引数にリストファイルのパスを指定してください。"
   exit 9
fi

## 変数を定義
LIST_FILE=${1}
HOME_DIR=/home
FILE_OUTPUT_DIR=/tmp/mfa

## FILE_OUTPUT_DIRに定義したディレクトリがあるかチェックし存在しない場合自動作成
if [[ ! -d ${FILE_OUTPUT_DIR} ]] ; then
   mkdir -p ${FILE_OUTPUT_DIR}
fi

cat ${LIST_FILE} | while read line
do
   user_name=$(echo $line | cut -d, -f 1)
   user_id=$(echo $line | cut -d, -f 2)

   ## ユーザー作成
   useradd -u ${user_id} -g radius-enabled -c MFA -m -d $HOME_DIR/${user_name} -s /sbin/nologin ${user_name}
   ## Google Authenticatorを設定し、QRのURLをファイルへ出力
   sudo -u ${user_name} /usr/bin/google-authenticator -tdf -r 3 -R 30 -S 30 -w 17 | grep http > ${FILE_OUTPUT_DIR}/${user_name}
done

実行例

実行結果はエラーが出なければ特にメッセージは出力されません。

# ./useradd-and-google-authenticator.sh  useradd-list.txt
#

実際には、Google Authenticatorの設定で払い出されたURL(QR)を各ユーザーに展開し、MFAの設定をする必要がありますが、ユーザーごとのURL(QR)は以下のコマンドで確認可能です。

# grep http /tmp/mfa/*
/tmp/mfa/test-user01:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user01@fk-test-radius-01%3Fsecret%3DSBJDVQNGY3I5YQIY4VF66FKEMY%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user02:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user02@fk-test-radius-01%3Fsecret%3DVYVI7ILYDQWKGEUHOIZW7HJLUQ%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user03:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user03@fk-test-radius-01%3Fsecret%3DEQXGHRC7Q5QFNYS4XNPW3BUP54%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user04:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user04@fk-test-radius-01%3Fsecret%3D52EWWM3D277PDPL6K42WN3CYOQ%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user05:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user05@fk-test-radius-01%3Fsecret%3DI6QCOL5U62LFGOSQDNBEZFRIAA%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user06:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user06@fk-test-radius-01%3Fsecret%3DV4DDFH2XVISEMZYXVS7B2JXXWE%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user07:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user07@fk-test-radius-01%3Fsecret%3DQQ6KDF4NLUFZPN3PNHGVBIFV5M%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user08:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user08@fk-test-radius-01%3Fsecret%3DAMDT7W42ZBPSNDOTVFUUNG5FBI%26issuer%3Dfk-test-radius-01
/tmp/mfa/test-user09:  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/test-user09@fk-test-radius-01%3Fsecret%3DGAFGK73LN6OD4TQGPLL6H22FVY%26issuer%3Dfk-test-radius-01
#

※上記の情報は重要なデータとなるため、取り扱いには注意してください。

終わりに

今回は、Radiusサーバへのユーザー追加およびGoogle Authenticatorの設定の自動化スクリプトをご紹介いたしました。

どなたかのお役に立てれば幸いです。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。