摆脱访问密钥:在安全的边缘和本地工作负载中实现 IAM Roles Anywhere

发布: (2026年1月18日 GMT+8 16:10)
4 min read
原文: Dev.to

抱歉,我需要您提供要翻译的具体文本内容(除代码块和 URL 之外),才能为您进行简体中文翻译。请将文章的正文粘贴在这里,我会按照要求保留源链接、格式和技术术语进行翻译。

IAM Roles Anywhere 的工作原理

  • Trust Anchor – 您告诉 AWS 您信任哪个证书颁发机构(CA)。
  • Profile – 定义可以被假设的 IAM 角色以及持续时间。
  • Role – 一个 IAM 角色,带有信任策略,允许 rolesanywhere.amazonaws.com 服务主体来假设它。

部署 AWS 资源

以下是一个最小的 CloudFormation 模板,用于创建所需的信任锚、IAM 角色和配置文件。请根据您的环境调整名称、ARN 和策略。

AWSTemplateFormatVersion: '2010-09-09'
Description: Deploy IAM Roles Anywhere for On‑Premise Workloads

Parameters:
  CACertificateBody:
    Type: String
    Description: The PEM‑encoded public key of your Certificate Authority.

Resources:
  EdgeTrustAnchor:
    Type: AWS::RolesAnywhere::TrustAnchor
    Properties:
      Name: OnPremDataCenterAnchor
      Enabled: true
      Source:
        SourceData:
          X509CertificateData: !Ref CACertificateBody
        SourceType: CERTIFICATE_BUNDLE

  EdgeWorkloadRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: EdgeBackupRole
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: rolesanywhere.amazonaws.com
            Action:
              - sts:AssumeRole
              - sts:TagSession
              - sts:SetSourceIdentity
            Condition:
              StringEquals:
                "aws:PrincipalTag/x509Subject/OU": "DataCenter-Northeast"
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonS3FullAccess   # Scope as needed

  EdgeProfile:
    Type: AWS::RolesAnywhere::Profile
    Properties:
      Name: EdgeServerProfile
      DurationSeconds: 3600   # 1‑hour temporary credentials
      Enabled: true
      RoleArns:
        - !GetAtt EdgeWorkloadRole.Arn

配置本地服务器

  1. 证书和私钥 – 获取由您上传为信任锚点的 CA 签署的客户端证书和私钥。
  2. AWS Signing Helper – 安装 aws_signing_helper 二进制文件(由 AWS 提供)。
  3. 凭证进程 – 在 ~/.aws/config 中添加一个调用该助手的配置文件。
[profile edge-workload]
credential_process = /usr/local/bin/aws_signing_helper credential-process \
    --certificate /etc/pki/edge/server.crt \
    --private-key /etc/pki/edge/server.key \
    --trust-anchor-arn arn:aws:rolesanywhere:us-east-1:123456789012:trust-anchor/your-anchor-id \
    --profile-arn arn:aws:rolesanywhere:us-east-1:123456789012:profile/your-profile-id \
    --role-arn arn:aws:iam::123456789012:role/EdgeBackupRole

使用此配置,任何使用 edge-workload 配置文件的应用程序都会自动获取短期凭证;应用程序本身无需了解 IAM Roles Anywhere。

示例应用代码

import boto3
from botocore.exceptions import ClientError

def upload_to_s3(file_name: str, bucket: str) -> None:
    """Upload a file to S3 using the edge‑workload profile."""
    session = boto3.Session(profile_name='edge-workload')
    s3 = session.client('s3')

    try:
        s3.upload_file(file_name, bucket, file_name)
        print(f"Successfully uploaded {file_name} using temporary credentials!")
    except ClientError as e:
        print(f"Upload failed: {e}")

if __name__ == "__main__":
    upload_to_s3('daily_backup.tar.gz', 'my-secure-onprem-backups')

好处

  • 零静态密钥 – 如果边缘服务器被攻破,磁盘上不会存在 AWS 密钥。
  • 即时吊销 – 在您的 CA 撤销客户端证书后,立即阻止访问,无需任何 IAM 更改。
  • 可审计性 – 每个凭证请求都会记录在 CloudTrail 中,包括证书的序列号。
  • 零信任对齐 – 短期、自动轮换的凭证符合现代安全最佳实践。

迁移到 IAM Roles Anywhere 是成熟 AWS 架构的标志,弥合了本地稳定性与云原生安全之间的差距。

Back to Blog

相关文章

阅读更多 »

Rapg:基于 TUI 的密钥管理器

我们都有这种经历。你加入一个新项目,首先听到的就是:“在 Slack 的置顶消息里查找 .env 文件”。或者你有多个 .env …

技术是赋能者,而非救世主

为什么思考的清晰度比你使用的工具更重要。Technology 常被视为一种魔法开关——只要打开,它就能让一切改善。新的 software,...

踏入 agentic coding

使用 Copilot Agent 的经验 我主要使用 GitHub Copilot 进行 inline edits 和 PR reviews,让我的大脑完成大部分思考。最近我决定 t...