OpenID Connect を使用して、Pipelines の AWS ECR イメージを使用する
OpenID Connect で AWS ECR Docker イメージを使用するには、Pipelines をウェブ ID プロバイダーとして設定して IAM ロールを作成し、IAM ロールをビルド イメージに関連付ける必要があります。
AWS でウェブ ID プロバイダーとして Bitbucket Pipelines を設定する
Web Identity Providers allow the system to receive an authentication token, and then use or exchange that token for temporary security credentials in AWS. These temporary security credentials map to an IAM role with permissions to use the resources in your AWS account. Learn more about Web Identity Providers from AWS
左側のサイドバーの [アクセス管理] 見出しの下にある [ID プロバイダー] を選択します。
[Add provider (プロバイダーの追加)] ボタンを選択します。
[Configure provider (プロバイダーの設定)] セクションで、[OpenID Connect] を選択します。
Bitbucket の OpenID Connect に ID プロバイダーとして表示されるプロバイダー URL を、対応するテキスト フィールドに追加します。プロバイダー URL は、認証要求に使用されるセキュアな OpenID Connect URL です。
Bitbucket の OpenID Connect に ID プロバイダーとして表示される対象者を、対応するテキスト フィールドに追加します。対象者は、アプリの ID プロバイダーによって発行されたクライアント ID です。
[Add provider (プロバイダーの追加)] ボタンを選択します。
To verify the thumbprint, you can follow the steps for obtaining the root CA thumbprint for an OpenID Connect Identity Provider.
ID とアクセス管理 (IAM) ロールを作成する
以下の手順に従って、この OIDC プロバイダーで Bitbucket Pipelines を使用するすべてのユーザーが引き受けられる新しい IAM ロールを作成します。
このロールの信頼されたエンティティはウェブ ID である必要があります。これによって、前の手順で作成したプロバイダーを選択できます。
AWS ID とアクセス管理 (IAM) にアクセスします。
左側のサイドバーの [アクセス管理] 見出しの下にある [ロール] を選択します。
[ロールの作成] を選択します。
信頼されたエンティティの種類として [ウェブ ID] を選択します。
[ID プロバイダー] ドロップダウン、上記の設定から作成した ID プロバイダーの順に選択します。
[対象者] ドロップダウン、上記の設定から作成した対象者の順に選択します。 これによって、ワークスペース内のすべてのリポジトリでビルドがロールを引き受けられるようになります。
[次へ]: [権限] を選択します。
Create a policy that allows to retrieve the authentication token and access the ECR registry. For more details on creating a policy, check out the following AWS help doc: Amazon Elastic Container Registry Identity-Based Policy Examples.
{ "Version":"2012-10-17", "Statement":[ { "Sid":"GetAuthorizationToken", "Effect":"Allow", "Action":[ "ecr:GetAuthorizationToken" ], "Resource":"*" }, { "Sid":"ReadRepositoryContents", "Effect":"Allow", "Action":[ "ecr:BatchCheckLayerAvailability", "ecr:GetDownloadUrlForLayer", "ecr:GetRepositoryPolicy", "ecr:DescribeRepositories", "ecr:ListImages", "ecr:DescribeImages", "ecr:BatchGetImage" ], "Resource":"arn:aws:ecr:us-east-1:123456789012:repository/my-repo" } ] }[次へ: タグ] を選択します。
必要に応じて、ラベル (タグ) を追加します。
[次へ: 確認] を選択します。
[ロールを作成] ページで、[ロール名] (必須、例:
pipelines-ecr-access) と [ロールの説明] (該当する場合) を入力します。[ロールの作成] を選択します。
選択した Docker イメージを使用するようにビルドを設定する
この手順では、前の手順で作成したロールを引き受けるようにビルドを設定します。ロールを引き受けて、一時的な認証情報を要求するために使用できる一意の OIDC トークンを作成するには、BitbucketCI ステップを有効にする必要があります。このトークンは、環境変数 BITBUCKET_STEP_OIDC_TOKEN として公開されます。
AWS ECR 認証に有効な IP アドレス
Refer to the following help document for a list of IP addresses that need to be allowlisted for AWS ECR authentication: IP addresses and domains to allowlist in you corporate firewall.
パイプラインを介した ECR 認証のためにホワイトリストに登録する必要がある IP アドレスは、次のスクリプトを使用してフィルタリングできます。
curl https://ip-ranges.atlassian.com/ | jq -r '.items[] | select(.direction[]=="ingress") | .cidr' bitbucket-pipelines.yml ファイルの例
pipelines:
default:
- step:
image:
name: 123456789012.dkr.ecr.us-east-1.amazonaws.com/repository/my-repo:latest
aws:
oidc-role: arn:aws:iam::123456789012:role/pipelines-ecr-access
oidc: true
script:
- echo "hello world"上記のコードは、明示的な AWS_ACCESS_KEY_ID と AWS_SECRET_KEY シークレットを入力せずに、パイプライン内のプライベート ECR イメージを使用する Bitbucket-Pipelines.yml ファイルの例です。
ビルドを実行する
AWS で Pipelines をウェブ ID プロバイダーとして設定してウェブ ID プロバイダー内に IAM ロールを作成し、作成したロールを引き受けるようにビルドを設定したらビルドを実行します。
For more options on limiting access to specific repositories, deployments, and more, see the Using claims in ID tokens to limit access to the IAM role in AWS section of the following help doc: Deploy on AWS using Bitbucket Pipelines OpenID Connect.
この内容はお役に立ちましたか?