Để test IAM Roles Anywhere trên Windows, bạn tạo file PFX tương thích bằng openssl pkcs12 -export -legacy, import vào Current User bằng Certificate Import Wizard, chạy aws_signing_helper credential-process để lấy credentials tạm thời, rồi khai báo credential_process trong ~/.aws/config và kiểm tra bằng aws sts get-caller-identity --profile roleanywhere.
IAM Roles Anywhere trên Windows hoạt động như thế nào?
IAM Roles Anywhere cho phép workload ngoài AWS (server on-premises, máy Windows) lấy temporary credentials bằng X.509 certificate thay vì access key dài hạn. Luồng hoạt động của bài lab:
client.pfx → Windows Certificate Store → aws_signing_helper
→ Trust Anchor → Profile → IAM Role → Temporary credentials → AWS CLI
Trước khi bắt đầu, bạn cần chuẩn bị sẵn 3 thứ trên AWS và 1 thứ ở máy Linux:
- Trust Anchor trỏ tới
rootCA.pem. - Profile của Roles Anywhere chứa IAM Role cần test.
- IAM Role có trust policy cho principal
rolesanywhere.amazonaws.com. - Bộ file
client.key,client.pem,rootCA.pem(client certificate cần có Extended Key Usage là Client Authentication).
Hướng dẫn tạo Root CA cho IAM Roles Anywhere
Bước 1: Tạo file PFX import vào Windows
openssl pkcs12 -export \
-out client-modern.pfx \
-inkey client.key \
-in client.pem \
-certfile rootCA.pem
OpenSSL 3.x mặc định mã hóa PFX bằng PBES2/PBKDF2/AES-256-CBC. Một số bản Windows không import được kiểu này dù password đúng. Giải pháp là tạo lại PFX với tùy chọn -legacy trên Ubuntu.
openssl pkcs12 -export -legacy \
-out client-win.pfx \
-inkey client.key \
-in client.pem \
-certfile rootCA.pem
Nhập password khi được hỏi (bài lab dùng password đơn giản, môi trường thật hãy dùng password mạnh). Kiểm tra lại file vừa tạo:
openssl pkcs12 -info -in client-win.pfx -noout
Kết quả mong đợi: lệnh chạy không báo lỗi và hiển thị thông tin MAC cùng Shrouded Keybag. Sau đó copy client-win.pfx sang Windows, ví dụ C:\iam-test\client-win.pfx.
Bước 2: Import certificate vào Windows Certificate Store
Dùng Certificate Import Wizard của Windows là cách nhanh nhất để test. Bạn import vào Current User.
- Double-click file
client-win.pfxđể mở Certificate Import Wizard. - Chọn Current User rồi bấm Next hai lần.
- Nhập password của PFX (để trống nếu lúc tạo bạn không đặt password).
- Ở phần Certificate Store, chọn Automatically select the certificate store based on the type of certificate.
- Bấm Next, sau đó Finish và chờ thông báo import thành công.
Để kiểm tra, nhấn Win + R, gõ certmgr.msc, vào Personal → Certificates và mở certificate vừa import. Tab General cần có dòng Client Authentication và câu You have a private key that corresponds to this certificate. Nếu thiếu private key thì Roles Anywhere sẽ không ký request được.
Bước 3: Cài aws_signing_helper
aws_signing_helper là credential helper chính thức của AWS dành cho IAM Roles Anywhere.
- Tải bản Windows từ tài liệu IAM Roles Anywhere của AWS.
- Đặt file
aws_signing_helper.exeở đường dẫn cố định, ví dụC:\Tools\aws_signing_helper.exe. - Chạy bằng chính user Windows đang import certificate, không cần service account.
Bạn cũng cần cài sẵn AWS CLI v2 để chạy các lệnh kiểm tra ở bước sau.
Link tải: https://docs.aws.amazon.com/rolesanywhere/latest/userguide/credential-helper.html
Bước 4: Lấy temporary credentials từ Roles Anywhere
Chạy credential-process trực tiếp trong PowerShell để xác nhận chuỗi Trust Anchor, Profile và Role hoạt động. Thay các ARN bằng giá trị của bạn:
.\aws_signing_helper.exe credential-process `
--trust-anchor-arn "arn:aws:rolesanywhere:ap-southeast-1:123456789012:trust-anchor/TRUST_ANCHOR_ID" `
--profile-arn "arn:aws:rolesanywhere:ap-southeast-1:123456789012:profile/PROFILE_ID" `
--role-arn "arn:aws:iam::123456789012:role/ROLE_NAME"
Nếu máy có nhiều certificate, thêm --cert-selector để helper biết dùng cái nào, ví dụ --cert-selector "Key=x509Subject,Value=CN=ten-certificate". Kết quả thành công là một JSON như sau:
{
"Version": 1,
"AccessKeyId": "ASIA...",
"SecretAccessKey": "...",
"SessionToken": "...",
"Expiration": "2026-10-06T17:59:25Z"
}
Đến đây Roles Anywhere đã hoạt động. Lưu ý bảo mật: đừng paste output này vào chat, ticket hay bài viết. Đó là credentials thật dù chỉ có hiệu lực ngắn.
Bước 5: Cấu hình AWS CLI profile với credential_process
Để AWS CLI tự gọi helper mỗi khi cần credentials, bạn khai báo credential_process trong file %USERPROFILE%\.aws\config. Nếu thư mục .aws chưa tồn tại, tạo nó trước:
New-Item -ItemType Directory -Force "$env:USERPROFILE\.aws"
Sau đó ghi file config bằng một lệnh:
@"
[profile roleanywhere]
region = ap-southeast-1
credential_process = C:\Tools\aws_signing_helper.exe credential-process --trust-anchor-arn arn:aws:rolesanywhere:ap-southeast-1:123456789012:trust-anchor/TRUST_ANCHOR_ID --profile-arn arn:aws:rolesanywhere:ap-southeast-1:123456789012:profile/PROFILE_ID --role-arn arn:aws:iam::123456789012:role/ROLE_NAME
"@ | Set-Content "$env:USERPROFILE\.aws\config"
Kiểm tra lại nội dung file bằng Get-Content "$env:USERPROFILE\.aws\config". Lưu ý credential_process phải nằm trên một dòng duy nhất.
Bước 6: Test end-to-end bằng aws sts get-caller-identity
Đây là bài test quan trọng nhất vì nó chứng minh cả chuỗi từ certificate đến AWS CLI đã thông:
aws sts get-caller-identity --profile roleanywhere
Kết quả mong đợi, trong đó phần Arn phải là assumed-role đúng role bạn cấu hình:
{
"UserId": "...",
"Account": "123456789012",
"Arn": "arn:aws:sts::123456789012:assumed-role/ROLE_NAME/..."
}
Sau đó thử một lệnh thực tế, tùy quyền của role: aws s3 ls --profile roleanywhere.