使用 AWS IAM Roles Anywhere 和 Private CA 将 Salesforce 与 AWS 集成
Source: Dev.to
概览
| 组件 | 目的 |
|---|---|
| Salesforce Certificate & Key Management | 创建一个自签名证书,该证书将上传到 AWS。 |
| AWS Certificate Manager Private CA | 颁发由私有根 CA 签名的证书。 |
| IAM Roles Anywhere | 信任私有 CA,创建信任锚、配置文件和 Salesforce 可承担的角色。 |
| Salesforce External Credential & Named Credential | 使用上述创建的角色配置 AWS Signature V4 身份验证。 |
步骤说明
1️⃣ 在 Salesforce 中创建自签名证书
- 在 Setup 中,搜索 Certificate & Key Management。
- 点击 Create Self‑Signed Certificate。
- 输入 Label 和唯一的 Name,然后 Save。
- Download 生成的证书为
cert.pem并安全保存。
2️⃣ 在 AWS ACM‑PCA 中设置私有根 CA
- 打开 AWS Certificate Manager (ACM) Private CA 控制台。
- 选择 Create CA → Mode: General‑Purpose → CA Type: Root。
- 填写必填信息,并为密钥算法选择 RSA 2048。
- 检查设置后点击 Create CA。
创建后 – 在 Actions 下,选择 Install CA certificate 以激活 CA。
验证 CA 状态从 Pending Certificate 变为 Active。
3️⃣ 颁发由私有根 CA 签名的证书
# Issue a certificate (replace placeholders)
aws acm-pca issue-certificate \
--certificate-authority-arn "" \
--csr fileb://crt.pem \
--signing-algorithm "SHA256WITHRSA" \
--validity Value=365,Type="DAYS" \
--region "us-east-1"
- 从命令输出中复制 CertificateArn。
- 获取签名证书:
aws acm-pca get-certificate \
--certificate-authority-arn "" \
--certificate-arn ""
- 将返回的 PEM 内容保存为名为
cert.crt的文件。- 确保每行(除头部/尾部外)恰好 64 个字符。
- 包含
-----BEGIN CERTIFICATE-----和-----END CERTIFICATE-----行。
4️⃣ 将签名证书上传到 Salesforce
- 在 Setup → Certificate & Key Management 中,点击 Upload Certificate。
- 选择前一步创建的
cert.crt文件并 Save。
5️⃣ 在 IAM Roles Anywhere 中创建信任锚
- 在 AWS 控制台打开 IAM Roles Anywhere。
- 点击 Create Trust Anchor。
- 输入名称,选择步骤 2 中创建的 Private CA,然后点击 Create Trust Anchor。
6️⃣ 创建带有信任策略的 IAM 角色
Trust Policy (JSON)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "rolesanywhere.amazonaws.com"
},
"Action": [
"sts:AssumeRole",
"sts:TagSession",
"sts:SetSourceIdentity"
]
}
]
}
- 在 IAM 控制台创建新角色,并粘贴上述信任策略。
- 附加 AmazonConnect_FullAccess 策略(或具有所需权限的自定义策略)。
7️⃣ 在 IAM Roles Anywhere 中创建配置文件
- 在 IAM Roles Anywhere 中,点击 Create Profile。
- 输入配置文件名称,关联步骤 6 中创建的角色,然后点击 Create Profile。
8️⃣ 配置 Salesforce 外部凭证
- Setup → Named Credentials → External Credentials → New。
| 字段 | 值 |
|---|---|
| Label | AWS IAM Anywhere Credential |
| Name | AWS_IAM_Anywhere_Credential |
| Authentication Protocol | AWS Signature V4 |
| Service | sts (later will change to connect) |
| Region | us-east-1 |
| AWS Account ID | (optional) |
| Obtain Temporary IAM Credentials via STS | Roles Anywhere |
| Trust Anchor ARN | ARN from Step 5 |
| Profile ARN | ARN from Step 7 |
| **Signing Certificate |
Source: …
| 字段 | 值 |
|---|---|
| 命名凭证 | Connect_API_Connection |
| URL | https://sts.us-east-1.amazonaws.com (稍后会更新) |
| 外部凭证 | 选择上面创建的外部凭证 |
| 生成授权标头 | 已勾选 |
🔟 为主体授予权限
- 设置 → 权限集 → 新建。
- 输入标签(例如
AWS_RolesAnywhere_Access)。 - 在权限集中,进入 外部凭证主体访问,将
connect_principal移动到 已启用 列。 - 将该权限集分配给相应的 Salesforce 用户。
1️⃣1️⃣ 测试集成(STS 调用)
打开 开发者控制台 → 执行匿名,运行:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Connect_API_Connection/?Action=GetCallerIdentity&Version=2011-06-15');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
检查调试日志: 返回 200 状态码即表示 STS GetCallerIdentity 调用成功。
1️⃣2️⃣ 将命名凭证切换为 Amazon Connect
- 编辑 命名凭证(
Connect_API_Connection)。 - 将 服务 从
sts改为connect。 - 将 URL 更新为
https://connect.us-east-1.amazonaws.com。
测试 Connect 调用
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Connect_API_Connection/?Action=ListInstances&Version=2017-08-08');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
如果返回成功响应,则说明 Salesforce 现在可以使用 IAM Roles Anywhere 调用 Amazon Connect。
🎉 All Done!
您现在拥有一个 certificate‑based, key‑less 的 Salesforce 与 AWS 之间的集成,利用 IAM Roles Anywhere 和 private CA 来安全地颁发临时凭证。
参考文献
// APN_API_Connection is the name of the named credential
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Connect_API_Connection/?Action=GetCallerIdentity&Version=2011-06-15');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
注意: 打开日志文件以查看状态。
如果成功,日志将显示 200 状态码以及返回的属性,表明已成功从 AWS 调用 Amazon Connect API。
类似于 Amazon Connect,您可以将此方法配置到任何支持 HTTP 调用的服务中。