【问题标题】:Upload to S3 failed with the following error: Access Denied - CodeStarConnections上传到 S3 失败并出现以下错误:访问被拒绝 - CodeStarConnections
【发布时间】:2021-05-01 03:20:06
【问题描述】:

我正在使用 AWS Codepipeline 构建 CI/CD 管道,存储库源位于 bitbucket 上,我使用 AWS-Codestarconnections 在 bitbucket 存储库和管道之间创建连接。

管道详情如下:

{
    "pipeline": {
        "name": "test_pipeline",
        "roleArn": "arn:aws:iam::<AccountId>:role/PipelineServiceRole",
        "artifactStore": {
            "type": "S3",
            "location": "tadadadada-artifact"
        },
        "stages": [
            {
                "name": "Source",
                "actions": [
                    {
                        "name": "Source",
                        "actionTypeId": {
                            "category": "Source",
                            "owner": "AWS",
                            "provider": "CodeStarSourceConnection",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "BranchName": "dev",
                            "ConnectionArn": "arn:aws:codestar-connections:us-east-2:<AccountId>:connection/4ca7b1cf-2917-4fda-b681-c5239944eb33",
                            "FullRepositoryId": "<username>/repository_name",
                            "OutputArtifactFormat": "CODE_ZIP"
                        },
                        "outputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "inputArtifacts": [],
                        "region": "us-east-2",
                        "namespace": "SourceVariables"
                    }
                ]
            },
            {
                "name": "Build",
                "actions": [
                    {
                      ....
                    }
                ]
            }
        ],
        "version": 1
    },
    "metadata": {
        "pipelineArn": "arn:aws:codepipeline:us-east-2:<AccountId>:test_pipeline",
        "created": 1611669087.267,
        "updated": 1611669087.267
    }
}

PipelineServiceRole + 附加到它的策略是:

服务角色

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codepipeline.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

政策

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "IamPassRolePolicy",
      "Effect": "Allow",
      "Action": [
          "iam:PassRole"
      ],
      "Resource": "*",
      "Condition": {
        "StringEqualsIfExists": {
          "iam:PassedToService": [
            "cloudformation.amazonaws.com",
            "ec2.amazonaws.com",
            "ecs-tasks.amazonaws.com"
          ]
        }
      }
    },
    {
      "Sid": "CodeBuildPolicy",
      "Effect": "Allow",
      "Action": [
        "codebuild:BatchGetBuilds",
        "codebuild:StartBuild"
      ],
      "Resource": "*"
    },
    {
      "Sid": "S3AccessPolicy",
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:GetObjectVersion",
        "s3:GetBucketAcl",
        "s3:GetBucketLocation"
      ],
      "Resource": "*"
    },
    {
      "Sid": "ECRAccessPolicy",
      "Effect": "Allow",
      "Action": [
        "ecr:DescribeImages"
      ],
      "Resource": "*"
    },
    {
      "Sid": "CodeStarConnectionsAccessPolicy",
      "Effect": "Allow",
      "Action": [
        "codestar-connections:UseConnection"
      ],
      "Resource": "*"
    }
  ]
}

源阶段失败并出现错误:

[Bitbucket] Upload to S3 failed with the following error: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 085999D90C19E650; S3 Extended Request ID: gJ6l08+cX3U6i2Vj0+fW7PiqA/UzM6ZGCfyECmWb+Jit4Knu+gi/L4y3F24uqkFWUfGy9tZo0VE=; Proxy: null) (Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null; Proxy: null) (Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null; Proxy: null)

错误消息缺少详细信息,我不确定哪个服务正在尝试访问 s3,不应该是代码管道(在这种情况下具有 PutObject 权限)吗?

【问题讨论】:

    标签: amazon-web-services bitbucket aws-codepipeline


    【解决方案1】:

    通过将 OutputArtifactFormat 从 "OutputArtifactFormat": "CODE_ZIP" 更改为 "OutputArtifactFormat": "CODEBUILD_CLONE_REF" 解决了这个问题。

    CODEBUILD_CLONE_REF - 来自控制台描述的是完整克隆,在这种情况下 AWS CodePipeline 传递有关允许后续操作执行完整 git 克隆的存储库的元数据。仅支持 AWS CodeBuild 操作。 “CODE_ZIP”选项不包括关于存储库的 git 元数据

    【讨论】:

    • 一千谢谢!我正在使用 AWS CDK,这导致我使用以下方法解决了同样的问题:new codepipeline_actions.BitBucketSourceAction({ ..., codeBuildCloneOutput: true })
    • 如果你将旧的现有Code Pipeline升级到GitHub V2访问,选择上述选项后的第二步是为Code Build角色添加UseConnection权限,这与角色不同在源步骤中使用
    【解决方案2】:

    此问题似乎与 CDK 的 BitBucketSourceAction 的默认 IAM 角色最近发生变化有关。

    我发现通过将“s3:PutObjectAcl”操作添加到列表中,我能够成功集成 BitBucketSourecAction(用于 GitHub 版本 2 连接)。注意:这不需要:

    • 将 OutputArtifactFormat 从 "OutputArtifactFormat": "CODE_ZIP" 更改为 "OutputArtifactFormat": "CODEBUILD_CLONE_REF",或者,
    • S3 完全访问“s3:*”

    this CDK issue 中所述,我正在使用 BitBucketSourceAction 与 GitHub 存储库集成。 CodePipeline 首次尝试 GitHub (Version2) 操作时出现以下错误:

    [GitHub] Upload to S3 failed with the following error: Access Denied
    

    在我使用 BitBucketSourceAction 发布的先前管道中,“s3:PutObject*”通配符操作包含在合成模板中。在查看我最新的 cdk 部署(使用版本 1.91.0)期间生成的 IAM 角色时,BitBucketSourceAction 只有“s3:PutObject”操作(即未使用通配符)。这不包括“s3:PutObjectAcl”操作,该操作似乎需要将源存储库从 GitHub 上传到 S3 并释放它以在管道中进一步使用。

    【讨论】:

    • 这对我有用,即除了“s3:GetObjet”和“s3:PutObject”之外,只向与管道存储桶存储关联的角色策略添加“s3:PutObjectAcl”操作权限
    【解决方案3】:

    s3:PutObjectAcl 操作权限添加到与管道存储桶关联的角色策略对我有用。

    我必须添加以下权限:

    • s3:GetObject
    • s3:GetObjectVersion
    • s3:PutObject
    • s3:GetBucketVersioning
    • s3:PutObjectAcl

    【讨论】:

    【解决方案4】:

    我在使用 GitHub 时遇到了同样的问题。

    [GitHub] Upload to S3 failed with the following error: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: foo; S3 Extended Request ID: bar; Proxy: null)

    但在工件存储 S3 存储桶中,对象已更新。

    所以我将 s3 服务策略更改为完全访问权限。

          "s3:PutObject",
          "s3:GetObject",
          "s3:GetObjectVersion",
          "s3:GetBucketVersioning",
    

          "s3:*",
    

    【讨论】:

      【解决方案5】:

      今天遇到了这个确切的问题,我知道为什么要修复它,但是PipelineGithubRole 附加的策略有 2 个 s3 语句,一个仅包含 List* 操作,另一个包含所有 Read & Put 操作,所以我只是移动了它们到一个语句中,它就开始工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-05
        • 2019-10-11
        • 2012-05-02
        • 2022-08-02
        • 1970-01-01
        • 2018-06-18
        相关资源
        最近更新 更多