Skip to content

Commit b1cec9c

Browse files
authored
Merge pull request #1 from dekachiri/feature/support_switch_role
Switch Role環境で利用可能にする
2 parents 2f73791 + 9d83192 commit b1cec9c

2 files changed

Lines changed: 77 additions & 14 deletions

File tree

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,44 @@ aws cli がない状況でも RDS IAM Token を取得したく生み出された
44

55
## 使い方
66

7-
```
7+
### 通常のIAMユーザ + MFA認証の場合
8+
9+
```bash
810
export AWS_ACCESS_KEY_ID="FOO"
9-
export AWS_SECRET_ACCESS_KE="BAR"
11+
export AWS_SECRET_ACCESS_KEY="BAR"
1012
export AWS_DEFAULT_REGION="ap-northeast-1"
1113

1214
./RDSIamToken -user ${RDSのユーザ名} -endpoint ${RDSのエンドポイント}:${RDSのポート} -mfaARN ${arn:aws:iam::1234567890:mfa/xxx}
1315
```
16+
17+
### Switch Role(AssumeRole)を使用する場合
18+
19+
```bash
20+
export AWS_ACCESS_KEY_ID="FOO"
21+
export AWS_SECRET_ACCESS_KEY="BAR"
22+
export AWS_DEFAULT_REGION="ap-northeast-1"
23+
24+
./RDSIamToken -user ${RDSのユーザ名} -endpoint ${RDSのエンドポイント}:${RDSのポート} -mfaARN ${arn:aws:iam::1234567890:mfa/xxx} -roleArn ${arn:aws:iam::1234567890:role/MyRole}
25+
```
26+
27+
### カスタムセッション名を指定する場合
28+
29+
```bash
30+
./RDSIamToken -user ${RDSのユーザ名} -endpoint ${RDSのエンドポイント}:${RDSのポート} -mfaARN ${arn:aws:iam::1234567890:mfa/xxx} -roleArn ${arn:aws:iam::1234567890:role/MyRole} -roleSessionName "CustomSessionName"
31+
```
32+
33+
### プロファイルを使用する場合
34+
35+
```bash
36+
./RDSIamToken -profile ${プロファイル名} -user ${RDSのユーザ名} -endpoint ${RDSのエンドポイント}:${RDSのポート} -mfaARN ${arn:aws:iam::1234567890:mfa/xxx} -roleArn ${arn:aws:iam::1234567890:role/MyRole}
37+
```
38+
39+
### パラメータ説明
40+
41+
- `-user`: DBのユーザー名(例: iam_operator)
42+
- `-endpoint`: DBのエンドポイント(例: mydb.rds.amazonaws.com:3306)
43+
- `-mfaARN`: MFAデバイスのARN(例: arn:aws:iam::1234567890:mfa/username)
44+
- `-roleArn`: Switch Roleで使用するロールのARN(オプション、例: arn:aws:iam::1234567890:role/MyRole)
45+
- `-roleSessionName`: AssumeRoleで使用するセッション名(オプション、デフォルト: RDSIAMTokenSession)
46+
- `-profile`: 使用するAWSプロファイル名(オプション)
47+
- `-region`: 使用するAWSリージョン(デフォルト: ap-northeast-1)

main.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"flag"
1515
)
1616

17-
func buildRDSTokenWithMFA(profile, defaultRegion, mfaARN, endpoint, dbUser string) (string, error) {
17+
func buildRDSTokenWithMFA(profile, defaultRegion, mfaARN, roleArn, roleSessionName, endpoint, dbUser string) (string, error) {
1818
// Profile の読み込み
1919
cfg, err := config.LoadDefaultConfig(context.Background(),
2020
config.WithSharedConfigProfile(profile),
@@ -29,20 +29,47 @@ func buildRDSTokenWithMFA(profile, defaultRegion, mfaARN, endpoint, dbUser strin
2929
fmt.Fprintln(os.Stderr, "Please input MFA code:")
3030
fmt.Scan(&mfaToken)
3131
stsClient := sts.NewFromConfig(cfg)
32-
creds, err := stsClient.GetSessionToken(context.Background(), &sts.GetSessionTokenInput{
33-
TokenCode: aws.String(mfaToken),
34-
SerialNumber: aws.String(mfaARN),
35-
DurationSeconds: aws.Int32(3600),
36-
})
3732

38-
if err != nil {
39-
return "", fmt.Errorf("Failed to get session token: %w", err)
33+
var finalCredentials aws.Credentials
34+
35+
if roleArn != "" {
36+
// Switch Role を使用する場合
37+
assumeRoleOutput, err := stsClient.AssumeRole(context.Background(), &sts.AssumeRoleInput{
38+
RoleArn: aws.String(roleArn),
39+
RoleSessionName: aws.String(roleSessionName),
40+
TokenCode: aws.String(mfaToken),
41+
SerialNumber: aws.String(mfaARN),
42+
DurationSeconds: aws.Int32(3600),
43+
})
44+
if err != nil {
45+
return "", fmt.Errorf("Failed to assume role: %w", err)
46+
}
47+
finalCredentials = aws.Credentials{
48+
AccessKeyID: *assumeRoleOutput.Credentials.AccessKeyId,
49+
SecretAccessKey: *assumeRoleOutput.Credentials.SecretAccessKey,
50+
SessionToken: *assumeRoleOutput.Credentials.SessionToken,
51+
}
52+
} else {
53+
// 通常のMFA認証の場合
54+
creds, err := stsClient.GetSessionToken(context.Background(), &sts.GetSessionTokenInput{
55+
TokenCode: aws.String(mfaToken),
56+
SerialNumber: aws.String(mfaARN),
57+
DurationSeconds: aws.Int32(3600),
58+
})
59+
if err != nil {
60+
return "", fmt.Errorf("Failed to get session token: %w", err)
61+
}
62+
finalCredentials = aws.Credentials{
63+
AccessKeyID: *creds.Credentials.AccessKeyId,
64+
SecretAccessKey: *creds.Credentials.SecretAccessKey,
65+
SessionToken: *creds.Credentials.SessionToken,
66+
}
4067
}
4168

4269
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(
43-
*creds.Credentials.AccessKeyId,
44-
*creds.Credentials.SecretAccessKey,
45-
*creds.Credentials.SessionToken,
70+
finalCredentials.AccessKeyID,
71+
finalCredentials.SecretAccessKey,
72+
finalCredentials.SessionToken,
4673
))
4774

4875
// RDS の認証情報の取得
@@ -60,6 +87,8 @@ func main() {
6087
// 使用するAWSプロファイル名を設定
6188
profile := flag.String("profile", "", "AWS の Profile 名")
6289
mfaARN := flag.String("mfaARN", "", "mfaARN 例) arn:aws:iam::xxxxxxxx:mfa/isao-aruga")
90+
roleArn := flag.String("roleArn", "", "Switch Roleで使用するロールのARN 例) arn:aws:iam::xxxxxxxx:role/MyRole")
91+
roleSessionName := flag.String("roleSessionName", "RDSIAMTokenSession", "AssumeRoleで使用するセッション名")
6392
dbUser := flag.String("user", "", "DBのユーザー名 例) iam_operator")
6493
endpoint := flag.String("endpoint", "", "DBのエンドポイント 例) sms-main-xxxxxx.rds.amazonaws.com:3306")
6594
defaultRegion := flag.String("region", "ap-northeast-1", "利用するAWSのリージョン")
@@ -80,7 +109,7 @@ func main() {
80109
os.Exit(1)
81110
}
82111

83-
token, err := buildRDSTokenWithMFA(*profile, *defaultRegion, *mfaARN, *endpoint, *dbUser)
112+
token, err := buildRDSTokenWithMFA(*profile, *defaultRegion, *mfaARN, *roleArn, *roleSessionName, *endpoint, *dbUser)
84113
if err != nil {
85114
fmt.Fprintln(os.Stderr, err)
86115
os.Exit(1)

0 commit comments

Comments
 (0)