【问题标题】:Access denied when using aws cli but allowed in web console使用 aws cli 时访问被拒绝,但在 Web 控制台中允许访问
【发布时间】:2021-04-02 03:51:00
【问题描述】:

我的 IAM 帐户至少有“管理员”权限。据我所知,我可以在 Web 控制台中执行所有操作。例如,

最近我通过提供访问密钥、默认区域和输出格式下载了 aws-cli 和 quickly configured。然后我尝试发出一些命令,发现其中大部分(但不是全部)都有权限问题。例如

$ aws --version
aws-cli/1.16.243 Python/3.7.4 Windows/10 botocore/1.12.233
$ aws s3 ls s3://test-bucket

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
$ aws ec2 describe-instances

An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
$ aws iam get-user
{
    "User": {
        "Path": "/",
        "UserName": "xxx@xxx.xxx",
        "UserId": "xxxxx",
        "Arn": "arn:aws:iam::nnnnnnnnnn:user/xxx@xxx.xxx",
        "CreateDate": "2019-08-21T17:09:25Z",
        "PasswordLastUsed": "2019-09-21T16:11:34Z"
    }
}

在我看来,使用访问密钥进行身份验证的 cli 与使用 MFA 进行身份验证的 Web 控制台具有不同的权限集。

为什么 CLI 和 GUI 之间的权限不一致?如何使其保持一致?

【问题讨论】:

  • 你生成了私钥和公钥吗?您是否在尝试使用 aws cli 的机器上配置了它们?
  • @muasif80,cli 使用访问密钥而不是 RSA pri/pub 密钥。我有 RSA 密钥,但它们用于 ssh 到 ec2,而不是用于 cli authn。我还为 cli 生成了访问密钥。
  • 使用 --debug 运行 cli 命令,您将看到它是如何进行身份验证的,并且基于此您可以查看您是否使用了正确的角色/用户等
  • @JamesDean,--debug 仅转储本地调用堆栈。拒绝发生在服务器端,因此无济于事。我从服务器得到的只是 403 响应代码和一些无意义的哈希代码在响应正文中使用 --debug。

标签: aws-cli


【解决方案1】:

事实证明,由于缺少 MFA,我的一项策略中的以下语句阻止了 CLI 访问。

{
      "Condition": {
        "BoolIfExists": {
          "aws:MultiFactorAuthPresent": "false"
        }
      },
      "Resource": "*",
      "Effect": "Deny",
      "NotAction": [
        "iam:CreateVirtualMFADevice",
        "iam:EnableMFADevice",
        "iam:GetUser",
        "iam:ListMFADevices",
        "iam:ListVirtualMFADevices",
        "iam:ResyncMFADevice",
        "sts:GetSessionToken"
      ],
      "Sid": "DenyAllExceptListedIfNoMFA"
},

【讨论】:

  • 我也面临同样的问题。从策略中我无法看到为什么控制台的访问被阻止。你能再解释一下吗?
  • 我也遇到了这个问题,我通过使用 MFA 对 CLI 进行身份验证解决了这个问题。 aws.amazon.com/premiumsupport/knowledge-center/…
【解决方案2】:

如果您将BoolIfExists 替换为Bool,它应该可以工作。您的 CLI 请求不会因为未使用 MFA 而被拒绝。

https://aws.amazon.com/premiumsupport/knowledge-center/mfa-iam-user-aws-cli/相反

【讨论】:

  • 谢谢。你勾起了我的记忆。我忘记了 CLI 也设置了 MFA
【解决方案3】:

我遇到了同样的问题,我通过将我的用户添加到在 IAM 中具有管理员访问权限的新组来修复它。

要做到这一点去IAM,用户,点击你的用户,然后[添加权限] 在下一个屏幕中单击[创建组],然后选择管理员访问权限

【讨论】:

  • 我的帐户之前没有组/策略。添加后,我仍然收到拒绝访问错误。
  • 您确定您的用户具有正确的访问类型吗?创建用户时,您可以在“编程访问:为 AWS API、CLI、开发工具包和其他开发工具启用访问密钥 ID 和秘密访问密钥。”和“AWS 管理控制台访问启用密码以允许用户访问登录 AWS 管理控制台。"。也许尝试创建一个具有控制台访问权限的新用户。我认为这可能是您的问题,因为当我执行 aws iam get-user 时,我得到的输出与您的不同,我的没有字段'“PasswordLastUsed”:“XXXX”'
  • 在我创建帐户时未启用访问密钥,而是在事后添加。授予的顺序无关紧要 - 就像您稍后可以将自己添加到管理员组一样。
【解决方案4】:

为了保持真正的安全,请查看这个很好的解释:MFA token for AWS CLI

只需几个步骤

  1. 获取临时 36 小时会话令牌。
aws sts get-session-token --serial-number arn:aws:iam::123456789012:mfa/user --token-code code-from-token

{
    "Credentials": {
        "SecretAccessKey": "secret-access-key",
        "SessionToken": "temporary-session-token",
        "Expiration": "expiration-date-time",
        "AccessKeyId": "access-key-id"
    }
}
  1. 将这些值保存在 mfa 配置文件配置中。
    [mfa]
        aws_access_key_id = example-access-key-as-in-returned-output
        aws_secret_access_key = example-secret-access-key-as-in-returned-output
        aws_session_token = example-session-Token-as-in-returned-output
  1. 使用个人资料调用
aws --profile mfa

Ps:不要按照建议执行 cron 工作,它又是安全问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多