IAM 策略文档学习笔记

策略文档

  • IAM 许可策略 附加到确定角色可执行哪些任务的角色。将许可限定为仅角色需要进行的操作,以及仅为进行这些操作角色需要的资源。您可以使用 AWS 管理的或客户创建的 IAM 许可策略
    • 操作:您将允许哪些操作。每个 AWS 服务都有自己的一组操作。例如,您可能允许用户使用 Amazon S3 ListBucket 操作,它将返回有关存储段中项目的信息。任何您没有显式允许的操作都将被拒绝。
    • 资源:您允许对哪些资源执行操作。例如,您将允许用户对哪些特定 Amazon S3 存储段执行 ListBucket 操作?用户不能访问任何您没有显式授予许可的任何资源。
    • 效果:当用户请求访问权限时将产生什么效果 (允许或拒绝)。因为默认设置为拒绝用户访问资源,因此您通常需要指定您将允许用户访问资源。
{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": [ "A list of the permissions the role is allowed to use" ],
        "Resource": [ "A list of the resources the role is allowed to access" ]
    }
}   
  • 允许服务代入角色的信任策略。例如,您可以将以下信任策略与具有 UpdateAssumeRolePolicy 操作的角色进行附加。该信任策略允许 Amazon EC2 使用角色和附加在角色上的许可。
{
    "Version": "2012-10-17",
    "Statement": {
        "Sid": "TrustPolicyStatementThatAllowsEC2ServiceToAssumeTheAttachedRole"
        "Effect": "Allow",
        "Principal": { "Service": "ec2.amazonaws.com" },
       "Action": "sts:AssumeRole"
    }
}    
  • IAM 许可策略 附加至允许用户仅授予这些被批准策略的 IAM 用户。iam:PassRole 通常与 iam:GetRole 共同使用,使用户能够获取准备进行传递的角色的详细信息。在此示例中,用户仅可以传递名称以EC2-roles-for-XYZ-开头的角色:
{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": [
            "iam:GetRole",
            "iam:PassRole"
        ],
        "Resource": "arn:aws:iam::*:role/EC2-roles-for-XYZ-*"
    }]
}
  • 授权用户使用 Amazon EC2 控制台启动具有任何角色的实例的策略
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": [
      "iam:PassRole",
      "iam:ListInstanceProfiles",
      "ec2:*"
    ],
    "Resource": "*"
  }]
}
  • 通过以下示例策略,用户可以使用 Amazon EC2 API,通过角色来启动实例。Resource 元素指定角色的 Amazon 资源名称 (ARN)。通过指定 ARN,该策略授予用户仅传递 Get-pics 角色的权限。如果在启动实例时用户尝试指定其他角色,则该操作会失败。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ec2:RunInstances",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:role/Get-pics"
    }
  ]
}
  • 要创建用户,用户必须拥有 iam:CreateUser 权限 (API 命令:CreateUser)。要允许用户创建其他 IAM 用户,您可以为该用户附加一个类似以下的策略:
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "iam:CreateUser",
    "Resource": "*"
  }
}
  • 允许用户管理自己的密码 (从“My Password”页面)
  • 如果账户的密码策略未设置为允许所有用户更改自己的密码,您可以将以下策略附加到选定的用户或群组,从而允许这些用户只能更改自己的密码。此策略仅允许用户在控制台中使用特殊的 “My Password”页面,并未授予用户通过 IAM 控制台中的控制面板工作的权限。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iam:GetAccountPasswordPolicy",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:ChangePassword",
      "Resource": "arn:aws:iam::account-id-without-hyphens:user/${aws:username}"
    }
  ]
}
  • 允许用户管理自己的密码、访问密钥和 SSH 密钥
    • 创建、更改或删除自己的密码。这包括 CreateLoginProfile、DeleteLoginProfile、GetLoginProfile 和 UpdateLoginProfile 操作。
    • 创建或删除自己的访问密钥 (访问密钥 ID 和秘密访问密钥)。这包括 CreateAccessKey、DeleteAccessKey、GetAccessKeyLastUsed、ListAccessKeys 和 UpdateAccessKey 操作。
    • 创建或删除自己的 SSH 密钥。这包括 UploadSSHPublicKey、DeleteSSHPublicKey、GetSSHPublicKey、ListSSHPublicKeys 和 UpdateSSHPublicKey 操作。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iam:*LoginProfile",
        "iam:*AccessKey*",
        "iam:*SSHPublicKey*"
      ],
      "Resource": "arn:aws:iam::account-id-without-hyphens:user/${aws:username}"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iam:ListAccount*",
        "iam:GetAccountSummary",
        "iam:GetAccountPasswordPolicy",
        "iam:ListUsers"
      ],
      "Resource": "*"
    }
  ]
}
  • 上述策略中的操作包含通配符 (例如 iam:LoginProfile、iam:*AccessKey 和 iam:SSHPublicKey)。这种方法可以轻松包含一组相关操作。如果要删除任一相关操作的权限,必须改为列出每个单独的操作。例如,如果不想让用户能够删除密码,则必须分别列出 iam:CreateLoginProfile、iam:GetLoginProfile 和 iam:UpdateLoginProfile,并忽略 iam:DeleteLoginProfile。
  • Statement 数组中的第二个元素 (包括 iam:GetAccountSummary、iam:GetAccountPasswordPolicy、iam:ListAccount* 和 iam:ListUsers 权限) 允许用户在 IAM 控制台控制面板上查看特定信息,例如密码策略是否已启用、该账户拥有的组的数量、账户 URL 和别名等等。例如,GetAccountSummary 操作返回一个对象,该对象包含有关稍后在 IAM 控制台控制面板上显示的账户的信息集合。

  • 以下策略与前一个策略类似,但不包含仅在控制台访问时需要的权限。此策略允许用户使用 AWS CLI、Windows PowerShell 工具、AWS 软件开发工具包或 IAM HTTP 查询 API 管理自己的凭证。
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:*LoginProfile",
      "iam:*AccessKey*",
      "iam:*SSHPublicKey*"
    ],
    "Resource": "arn:aws:iam::account-id-without-hyphens:user/${aws:username}"
  }
}
  • 允许用户列出账户的组、用户、策略等,以供报告之用
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:Get*",
      "iam:List*"
    ],
    "Resource": "*"
  }
}
  • 允许用户管理群组的成员资格
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:AddUserToGroup",
      "iam:RemoveUserFromGroup",
      "iam:GetGroup"
    ],
    "Resource": "arn:aws:iam::account-id-without-hyphens:group/MarketingGroup"
  }
}

  • 允许用户管理 IAM 用户
    • 创建用户 (CreateUser 操作)。
    • 删除用户。此任务需要授予执行以下所有操作的权限:DeleteSigningCertificate、DeleteLoginProfile、RemoveUserFromGroup 和 DeleteUser。
    • 列出账户和组中的用户 (GetUser、ListUsers 和 ListGroupsForUser 操作)。
    • 列出和删除用户的策略 (ListUserPolicies、ListAttachedUserPolicies、DetachUserPolicy 和 DeleteUserPolicy 操作)
    • 重命名或更改用户的路径 (UpdateUser 操作)。Resource 元素必须包括涉及源路径和目标路径的 ARN。有关路径的更多信息,请参阅易记名称和路径。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowUsersToPerformUserActions",
      "Effect": "Allow",
      "Action": [
        "iam:CreateUser",
        "iam:ListUsers",
        "iam:GetUser",
        "iam:UpdateUser",
        "iam:DeleteUser",
        "iam:ListGroupsForUser",
        "iam:ListUserPolicies",
        "iam:ListAttachedUserPolicies",
        "iam:DeleteSigningCertificate",
        "iam:DeleteLoginProfile",
        "iam:RemoveUserFromGroup",
        "iam:DetachUserPolicy",
        "iam:DeleteUserPolicy"
      ],
      "Resource": "*"
    },
    {
      "Sid": "AllowUsersToSeeStatsOnIAMConsoleDashboard",
      "Effect": "Allow",
      "Action": [
        "iam:GetAccount*",
        "iam:ListAccount*"
      ],
      "Resource": "*"
    }
  ]
}
  • 允许用户设置账户密码策略
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:GetAccountPasswordPolicy",
      "iam:UpdateAccountPasswordPolicy"
    ],
    "Resource": "*"
  }
}
  • 允许用户生成和检索 IAM 证书报告
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:GenerateCredentialReport",
      "iam:GetCredentialReport"
    ],
    "Resource": "*"
  }
}
  • 仅允许用户管理自己的虚拟 MFA 设备
    • 以下策略允许用户从 AWS 管理控制台或使用任意命令行工具来配置和管理其虚拟 MFA 设备。该策略仅允许进行 MFA 身份验证的用户停用并删除其虚拟 MFA 设备。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowUsersToCreateEnableResyncDeleteTheirOwnVirtualMFADevice",
      "Effect": "Allow",
      "Action": [
        "iam:CreateVirtualMFADevice",
        "iam:EnableMFADevice",
        "iam:ResyncMFADevice",
        "iam:DeleteVirtualMFADevice"
      ],
      "Resource": [
        "arn:aws:iam::account-id-without-hyphens:mfa/${aws:username}",
        "arn:aws:iam::account-id-without-hyphens:user/${aws:username}"
      ]
    },
    {
      "Sid": "AllowUsersToDeactivateTheirOwnVirtualMFADevice",
      "Effect": "Allow",
      "Action": [
        "iam:DeactivateMFADevice"
      ],
      "Resource": [
        "arn:aws:iam::account-id-without-hyphens:mfa/${aws:username}",
        "arn:aws:iam::account-id-without-hyphens:user/${aws:username}"
      ],
      "Condition": {
        "Bool": {
          "aws:MultiFactorAuthPresent": true
        }
      }
    },
    {
      "Sid": "AllowUsersToListMFADevicesandUsersForConsole",
      "Effect": "Allow",
      "Action": [
        "iam:ListMFADevices",
        "iam:ListVirtualMFADevices",
        "iam:ListUsers"
      ],
      "Resource": "*"
    }
  ]
}
  • 注意:
    • 操作 iam:DeleteVirtualMFADevice 包含在第一条而不是第二条语句中,因此不 受 MFA 条件检查的影响。这不会造成安全问题,因为您只能在停用 MFA 设备后才能删除它,而要执行停用操作,用户必须先进行 MFA 身份验证。这可防止以下情况的发生:您在 Create MFA Device 向导创建设备后但尚未验证两个代码并将其关联到用户之前取消向导。由于此时用户尚未进行 MFA 身份验证,如果策略要求必须经过 MFA 身份验证才能删除设备的话,向导 (以该用户权限执行) 将无法清除该设备

  • 策略是使用 JSON 创建的文档。策略包含一个或多个语句,每个语句描述一组权限。以下为一个简单的策略示例。
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "s3:ListBucket",
    "Resource": "arn:aws:s3:::example_bucket"
  }
}
  • 下面示例演示的策略可以附加到 Amazon S3 存储桶,并向特定 AWS 账户授予在 mybucket 中执行任何 Amazon S3 操作的权限。这包括使用存储桶以及其中的对象。(因为该策略仅向该账户授予信任,所以仍必须向该账户中的各个用户授予执行指定 Amazon S3 操作的权限。)
{
  "Version": "2012-10-17",
  "Id": "S3-Account-Permissions",
  "Statement": [{
    "Sid": "1",
    "Effect": "Allow",
    "Principal": {"AWS": ["arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:root"]},
    "Action": "s3:*",
    "Resource": [
      "arn:aws:s3:::mybucket",
      "arn:aws:s3:::mybucket/*"
    ]
  }]
}
  • 允许创建、更新、删除、列出、获取所有策略以及设置这些策略的默认版本的策略
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:CreatePolicy",
      "iam:CreatePolicyVersion",
      "iam:DeletePolicy",
      "iam:DeletePolicyVersion",
      "iam:GetPolicy",
      "iam:GetPolicyVersion",
      "iam:ListPolicies",
      "iam:ListPolicyVersions",
      "iam:SetDefaultPolicyVersion"
    ],
    "Resource": "*"
  }
}
  • 仅允许针对特定策略删除策略版本和设置默认版本的策略
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:DeletePolicyVersion",
      "iam:SetDefaultPolicyVersion"
    ],
    "Resource": "arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:policy/TEAM-A/*"
  }
}
  • 仅允许将托管策略附加到特定组或角色的策略
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:AttachGroupPolicy",
      "iam:AttachRolePolicy"
    ],
    "Resource": [
      "arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:group/TEAM-A/*",
      "arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:role/TEAM-A/*"
    ]
  }
}
  • 此策略展示对同一项服务的允许和拒绝。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "FullAccess",
            "Effect": "Allow",
            "Action": ["s3:*"],
            "Resource": ["*"]
        },
        {
            "Sid": "DenyCustomerBucket",
            "Action": ["s3:*"],
            "Effect": "Deny",
            "Resource": ["arn:aws:s3:::customer", "arn:aws:s3:::customer/*" ]
        }
    ]
}
  • 此策略基于用户 Amazon Cognito ID 提供对 Amazon DynamoDB 的行级别访问权限。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:DeleteItem",
                "dynamodb:GetItem",
                "dynamodb:PutItem",
                "dynamodb:Query",
                "dynamodb:UpdateItem"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-west-1:123456789012:table/myDynamoTable"
            ],
            "Condition": {
                "ForAllValues:StringEquals": {
                    "dynamodb:LeadingKeys": [
                        "${cognito-identity.amazonaws.com:sub}"
                    ]
                }
            }
        }
    ]
}
  • MultipleResourceCondition
{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": ["s3:PutObject", "s3:PutObjectAcl"],
        "Resource": ["arn:aws:s3:::Apple_bucket/*"],
        "Condition": {
            "StringEquals": {
                "s3:x-amz-acl": ["public-read"]
            }
        }
    }, {
        "Effect": "Allow",
        "Action": ["s3:PutObject", "s3:PutObjectAcl"],
        "Resource": ["arn:aws:s3:::Orange_bucket/*"],
        "Condition": {
            "StringEquals": {
                "s3:prefix": ["custom", "other"]
            }
        }
    }]
}

  • 允许用户访问 Amazon S3 中的特定存储桶
    • 在以下策略中,您需要用您的存储桶名称替换 EXAMPLE-BUCKET-NAME。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:ListAllMyBuckets",
      "Resource": "arn:aws:s3:::*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListBucket",
        "s3:GetBucketLocation"
      ],
      "Resource": "arn:aws:s3:::EXAMPLE-BUCKET-NAME"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:DeleteObject"
      ],
      "Resource": "arn:aws:s3:::EXAMPLE-BUCKET-NAME/*"
    }
  ]
}
  • 允许用户访问 Amazon S3 中的个人“主目录”
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListAllMyBuckets",
        "s3:GetBucketLocation"
      ],
      "Resource": "arn:aws:s3:::*"
    },
    {
      "Effect": "Allow",
      "Action": "s3:ListBucket",
      "Resource": "arn:aws:s3:::BUCKET-NAME",
      "Condition": {"StringLike": {"s3:prefix": [
        "",
        "home/",
        "home/${aws:username}/*"
      ]}}
    },
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::BUCKET-NAME/home/${aws:username}",
        "arn:aws:s3:::BUCKET-NAME/home/${aws:username}/*"
      ]
    }
  ]
}
  • 允许使用 Amazon Cognito 登录的用户访问自己的 Amazon S3 文件夹
    • 下例所示的策略可用于使用 Amazon Cognito 的移动应用程序。该条件可确保仅当由 EXAMPLE-BUCKET-NAME 表示的 Amazon S3 存储桶中的对象名称包含提供商名称 (此处为 cognito)、应用程序的友好名称 (此处为 mynumbersgame) 以及联合身份用户的 ID 时,用户才能访问这些对象。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::EXAMPLE-BUCKET-NAME"],
      "Condition": {"StringLike": {"s3:prefix": ["cognito/mynumbersgame/"]}}
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject"
      ],
      "Resource": [
        "arn:aws:s3:::EXAMPLE-BUCKET-NAME/cognito/mynumbersgame/${cognito-identity.amazonaws.com:sub}",
        "arn:aws:s3:::EXAMPLE-BUCKET-NAME/cognito/mynumbersgame/${cognito-identity.amazonaws.com:sub}/*"
      ]
    }
  ]
}
  • 允许用户访问对名称与用户名称匹配的 DynamoDB 表执行的所有操作
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "dynamodb:*",
      "Resource": "arn:aws:dynamodb:AWS-REGION-IDENTIFIER:ACCOUNT-ID-WITHOUT-HYPHENS:table/${aws:username}"
    }
  ]
}
  • 该策略使用在运行时评估的策略变量 (${aws:username}),并包含发出请求的 IAM 用户的友好名称。

  • 阻止并非来自批准的 IP 地址或范围的请求
    • 如果要以用户身份直接调用待测试 API,aws:SourceIp 条件键只在 IAM 策略中有效。如果改为使用服务代表您调用目标服务,则目标服务看到的是进行调用的服务的 IP 地址而不是源用户的 IP 地址。举例来说,如果使用 AWS CloudFormation 调用 Amazon EC2 来构建实例,则会发生这种情况。目前,通过服务调用目标服务无法传递源 IP 地址,因此无法在 IAM 策略中进行评估。对于这些服务 API 调用类型,请勿使用 aws:SourceIp 条件键。
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Deny",
    "Action": "*",
    "Resource": "*",
    "Condition": {"NotIpAddress": {"aws:SourceIp": [
      "192.0.2.0/24",
      "203.0.113.0/24"
    ]}}
  }
}
  • 编程式读和写权限
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::test"]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:DeleteObject"
      ],
      "Resource": ["arn:aws:s3:::test/*"]
    }
  ]
}
  • 让AWS管理控制台访问Amazon S3存储桶
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetBucketLocation",
        "s3:ListAllMyBuckets"
      ],
      "Resource": "arn:aws:s3:::*"
    },
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::test"]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:DeleteObject"
      ],
      "Resource": ["arn:aws:s3:::test/*"]
    }
  ]
}

猜你喜欢

转载自blog.csdn.net/m0_37204491/article/details/74684668
IAM