プロキシサーバーでの AWS CodeBuild の使用 - AWS CodeBuild

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

プロキシサーバーでの AWS CodeBuild の使用

プロキシサーバーで AWS CodeBuild を使用して、インターネットとの間の HTTP および HTTPS トラフィックを規制できます。プロキシサーバーで CodeBuild を実行するには、パブリックサブネットにプロキシサーバーをインストールし、VPC のプライベートサブネットに CodeBuild をインストールします。

プロキシサーバーで CodeBuild を実行するためのプライマリユースケースが 2 つあります。

  • これにより、VPC で NAT ゲートウェイや NAT インスタンスを使用する必要がなくなります。

  • プロキシサーバーのインスタンスがアクセスを許可する URL と、プロキシサーバーがアクセスを拒否する URL を指定できます。

CodeBuild は、2 種類のプロキシサーバーで使用できます。どちらの場合も、プロキシサーバーはパブリックサブネットで動作し、CodeBuild はプライベートサブネットで動作します。

プロキシサーバーで CodeBuild を実行するために必要なコンポーネント

このコンポーネントは、透過的または明示的なプロキシサーバーで AWS CodeBuild を実行するために必要です。

  • VPC。

  • プロキシサーバー用に VPC 内の 1 つのパブリックサブネット。

  • CodeBuild 用に VPC 内の 1 つのプライベートサブネット。

  • VPC とインターネットの間の通信を可能にするインターネットゲートウェイ。

次の図は、これらのコンポーネントがどのように連携するかを示しています。

VPC、サブネット、ネットワークゲートウェイのセットアップ

以下のステップは、透過的または明示的なプロキシサーバーで AWS CodeBuild を実行するために必要です。

  1. VPC を作成します。VPC の作成の詳細については、Amazon VPC ユーザーガイド「VPC を作成する」をご参照ください。

  2. VPC 内に 2 つのサブネットを作成します。1 つは、プロキシサーバーを実行する Public Subnet という名前のパブリックサブネットです。もう 1 つは、CodeBuild を実行する Private Subnet という名前のプライベートサブネットです。

    詳細については、「VPC でのサブネットの作成」を参照してください。

  3. インターネットゲートウェイを作成して VPC にアタッチします。詳細については、「インターネットゲートウェイの作成とアタッチ」を参照してください。

  4. VPC (0.0.0.0/0) からインターネットゲートウェイに送信トラフィックをルーティングするルールをデフォルトルートテーブルに追加します。詳細については、「ルートテーブルでのルートの追加および削除」を参照してください。

  5. VPC (0.0.0.0/0) からの着信 SSH トラフィック (TCP 22) を許可するルールを VPC のデフォルトセキュリティグループに追加します。

  6. Amazon EC2 ユーザーガイドの「インスタンス起動ウィザードを使用したインスタンスの起動」の指示に従って Amazon Linux インスタンスを起動します。ウィザードを実行する場合は次のオプションを選択してください。

    • [Choose an Instance Type (インスタンスタイプの選択)] で、Amazon Linux の Amazon マシンイメージ (AMI) を選択します。

    • [サブネット] で、このトピックで先に作成したパブリックサブネットを選択します。推奨された名前を使用した場合は、[Public Subnet] です。

    • [Auto-assign Public IP] で、[Enable] を選択します。

    • [セキュリティグループの設定] ページの [セキュリティグループの割り当て] で、[Select an existing security group (既存のセキュリティグループの選択)] を選択します。次に、デフォルトのセキュリティグループを選択します。

    • [起動] を選択したら、既存のキーペアを選択するか、新しいキーペアを作成します。

    それ以外のオプションについては、デフォルト設定を選択します。

  7. EC2 インスタンスの実行後は、送信元/送信先チェックを無効にします。詳細については、 Amazon VPC ユーザーガイドの「送信元/送信先チェックを無効にする」を参照してください。

  8. VPC にルートテーブルを作成します。インターネット用のトラフィックをプロキシサーバーにルーティングするためのルールをルートテーブルに追加します。このルートテーブルをプライベートサブネットに関連付けます。これは、CodeBuild が実行されているプライベートサブネット内のインスタンスからのアウトバウンドリクエストを、常にプロキシサーバーを介してルーティングするために必要です。

プロキシサーバーのインストールと設定

選択できるプロキシサーバーは多数あります。ここでは、オープンソースのプロキシサーバー Squid を使用して、AWS CodeBuild がプロキシサーバーでどのように動作するかを説明します。同じ概念を他のプロキシサーバーにも適用できます。

Squid をインストールするには、次のコマンドを実行して yum repo を使用します。

sudo yum update -y sudo yum install -y squid

Squid をインストールしたら、このトピックで後述する手順に従って、その squid.conf ファイルを編集します。

HTTPS トラフィック用の Squid の設定

HTTPS では、HTTP トラフィックは Transport Layer Security (TLS) 接続でカプセル化されます。Squid では、SslPeekAndSplice と呼ばれる機能を使用して、リクエストされたインターネットホストを含む TLS 初期化から Server Name Indication (SNI) を取得します。これは必須のため、Squid で HTTPS トラフィックを復元する必要はありません。SslPeekAndSplice を有効にするには、Squid に証明書が必要です。OpenSSL を使用してこの証明書を作成する:

sudo mkdir /etc/squid/ssl cd /etc/squid/ssl sudo openssl genrsa -out squid.key 2048 sudo openssl req -new -key squid.key -out squid.csr -subj "/C=XX/ST=XX/L=squid/O=squid/CN=squid" sudo openssl x509 -req -days 3650 -in squid.csr -signkey squid.key -out squid.crt sudo cat squid.key squid.crt | sudo tee squid.pem
注記

HTTP では、Squid の設定は必要ありません。すべての HTTP/1.1 リクエストメッセージから、ホストヘッダーフィールドを取得することができます。これにより、リクエストされているインターネットホストが指定されます。

明示的なプロキシサーバーでの CodeBuild の実行

明示的なプロキシサーバーで AWS CodeBuild を実行するには、外部サイトとのトラフィックを許可または拒否するようにプロキシサーバーを設定し、さらに HTTP_PROXY 環境変数と HTTPS_PROXY 環境変数を設定する必要があります。

明示的なプロキシサーバーとしての Squid の設定

Squid プロキシサーバーが明示的になるように設定するには、/etc/squid/squid.conf ファイルに次の変更を加える必要があります。

  • 以下のデフォルトのアクセスコントロールリスト (ACL) ルールを削除します。

    acl localnet src 10.0.0.0/8 acl localnet src 172.16.0.0/12 acl localnet src 192.168.0.0/16 acl localnet src fc00::/7 acl localnet src fe80::/10

    削除したデフォルトの ACL ルールの代わりに次のコードを追加します。最初の行では VPC からのリクエストを許可します。次の 2 つの行では、AWS CodeBuild によって使用されている可能性のある送信先 URL へのアクセス権をプロキシサーバーに付与します。AWS リージョンで S3 バケットまたは CodeCommit リポジトリを指定するように、最終行の正規表現を変更します。次に例を示します。

    • 送信元が Amazon S3 の場合は、acl download_src dstdom_regex .*s3\.us-west-1\.amazonaws\.com コマンドを使用して、us-west-1 リージョンの S3 バケットへのアクセスを許可します。

    • 送信元が AWS CodeCommit の場合は、git-codecommit.<your-region>.amazonaws.com を使用して AWS リージョンを許可リストに追加します。

    acl localnet src 10.1.0.0/16 #Only allow requests from within the VPC acl allowed_sites dstdomain .github.com #Allows to download source from GitHub acl allowed_sites dstdomain .bitbucket.com #Allows to download source from Bitbucket acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from Amazon S3 or CodeCommit
  • http_access allow localnet を次のように置き換えます。

    http_access allow localnet allowed_sites http_access allow localnet download_src
  • ビルドでログとアーティファクトをアップロードする場合は、次のいずれかを実行します。

    1. http_access deny all ステートメントの前に、次のステートメントを挿入します。これにより、CodeBuild が CloudWatch と Amazon S3 にアクセスできるようになります。CodeBuild が CloudWatch Logs を作成できるようにするには、CloudWatch へのアクセスが必要です。Amazon S3 へのアクセスは、アーティファクトのアップロードと Amazon S3 のキャッシングを行う上で必要です。

      • https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept acl SSL_port port 443 http_access allow SSL_port acl allowed_https_sites ssl::server_name .amazonaws.com acl step1 at_step SslBump1 acl step2 at_step SslBump2 acl step3 at_step SslBump3 ssl_bump peek step1 all ssl_bump peek step2 allowed_https_sites ssl_bump splice step3 allowed_https_sites ssl_bump terminate step2 all
      • squid.conf 保存後、次のコマンドを実行します。

        sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130 sudo service squid restart
    2. proxy を buildspec ファイルに追加します。詳細については、「buildspec の構文」を参照してください。

      version: 0.2 proxy: upload-artifacts: yes logs: yes phases: build: commands: - command
注記

RequestError タイムアウトエラーが表示される場合は、「 RequestError プロキシサーバー CodeBuild での実行時のタイムアウトエラー」を参照してください。

詳細については、このトピックで後述する「明示的なプロキシサーバーのサンプル squid.conf ファイル」を参照してください。

CodeBuild プロジェクトを作成する

明示的なプロキシサーバーで AWS CodeBuild を実行するには、その HTTP_PROXY 環境変数と HTTPS_PROXY 環境変数に、プロキシサーバー用に作成した EC2 インスタンスのプライベート IP アドレスとポート 3128 をプロジェクトレベルで設定します。プライベート IP アドレスは、http://your-ec2-private-ip-address:3128 のようになります。詳細については、「 でのビルドプロジェクトの作成AWS CodeBuild」および「AWS CodeBuild でのビルドプロジェクトの設定の変更」を参照してください。

Squid プロキシのアクセスログを表示するには、次のコマンドを使用します。

sudo tail -f /var/log/squid/access.log

明示的なプロキシサーバーのサンプル squid.conf ファイル

明示的なプロキシサーバー用に設定した squid.conf ファイルの例を次に示します。

acl localnet src 10.0.0.0/16 #Only allow requests from within the VPC # add all URLS to be whitelisted for download source and commands to be run in build environment acl allowed_sites dstdomain .github.com #Allows to download source from github acl allowed_sites dstdomain .bitbucket.com #Allows to download source from bitbucket acl allowed_sites dstdomain ppa.launchpad.net #Allows to run apt-get in build environment acl download_src dstdom_regex .*\.amazonaws\.com #Allows to download source from S3 or CodeCommit acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT # # Recommended minimum Access Permission configuration: # # Deny requests to certain unsafe ports http_access deny !Safe_ports # Deny CONNECT to other than secure SSL ports http_access deny CONNECT !SSL_ports # Only allow cachemgr access from localhost http_access allow localhost manager http_access deny manager # We strongly recommend the following be uncommented to protect innocent # web applications running on the proxy server who think the only # one who can access services on "localhost" is a local user #http_access deny to_localhost # # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS # # Example rule allowing access from your local networks. # Adapt localnet in the ACL section to list your (internal) IP networks # from where browsing should be allowed http_access allow localnet allowed_sites http_access allow localnet download_src http_access allow localhost # Add this for CodeBuild to access CWL end point, caching and upload artifacts S3 bucket end point https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept acl SSL_port port 443 http_access allow SSL_port acl allowed_https_sites ssl::server_name .amazonaws.com acl step1 at_step SslBump1 acl step2 at_step SslBump2 acl step3 at_step SslBump3 ssl_bump peek step1 all ssl_bump peek step2 allowed_https_sites ssl_bump splice step3 allowed_https_sites ssl_bump terminate step2 all # And finally deny all other access to this proxy http_access deny all # Squid normally listens to port 3128 http_port 3128 # Uncomment and adjust the following to add a disk cache directory. #cache_dir ufs /var/spool/squid 100 16 256 # Leave coredumps in the first cache dir coredump_dir /var/spool/squid # # Add any of your own refresh_pattern entries above these. # refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 refresh_pattern . 0 20% 4320

透過的なプロキシサーバーでの CodeBuild の実行

透過的なプロキシサーバーで AWS CodeBuild を実行するには、やり取りするウェブサイトおよびドメインへのアクセス権を持つプロキシサーバーを設定する必要があります。

透過的なプロキシサーバーとしての Squid の設定

プロキシサーバーが透過的になるように設定するには、アクセスするドメインやウェブサイトへのアクセス権を付与する必要があります。透過的なプロキシサーバーで AWS CodeBuild を実行するには、amazonaws.com へのアクセス権を付与する必要があります。また、CodeBuild で使用する他のウェブサイトへのアクセス権も付与します。これらのアクセス権は、CodeBuild プロジェクトの作成方法によって異なります。ウェブサイトの例は、GitHub、Bitbucket、Yum、Maven などのリポジトリ用です。特定のドメインやウェブサイトへのアクセスを Squid に許可するには、次のようなコマンドを使用して squid.conf ファイルを更新します。このサンプルコマンドは amazonaws.comgithub.com、および bitbucket.com へのアクセスを許可します。このサンプルは、他のウェブサイトへのアクセス権を付与するように編集できます。

cat | sudo tee /etc/squid/squid.conf ≪EOF visible_hostname squid #Handling HTTP requests http_port 3129 intercept acl allowed_http_sites dstdomain .amazonaws.com #acl allowed_http_sites dstdomain domain_name [uncomment this line to add another domain] http_access allow allowed_http_sites #Handling HTTPS requests https_port 3130 cert=/etc/squid/ssl/squid.pem ssl-bump intercept acl SSL_port port 443 http_access allow SSL_port acl allowed_https_sites ssl::server_name .amazonaws.com acl allowed_https_sites ssl::server_name .github.com acl allowed_https_sites ssl::server_name .bitbucket.com #acl allowed_https_sites ssl::server_name [uncomment this line to add another website] acl step1 at_step SslBump1 acl step2 at_step SslBump2 acl step3 at_step SslBump3 ssl_bump peek step1 all ssl_bump peek step2 allowed_https_sites ssl_bump splice step3 allowed_https_sites ssl_bump terminate step2 all http_access deny all EOF

プライベートサブネット内のインスタンスからの着信リクエストで、Squid ポートにリダイレクトする必要があります。Squid は HTTP トラフィック (80 の代理) をポート 3129、HTTPS トラフィック (443 の代理) をポート 3130 でリッスンします。トラフィックをルーティングするには、iptables コマンドを使用します。

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3129 sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 3130 sudo service iptables save sudo service squid start

CodeBuild プロジェクトを作成する

プロキシサーバーを設定したら、プライベートサブネットの AWS CodeBuild で使用できます。追加設定は不要です。HTTP および HTTPS リクエストはすべて、パブリックプロキシサーバーを経由します。Squid プロキシのアクセスログを表示するには、次のコマンドを使用します。

sudo tail -f /var/log/squid/access.log

プロキシサーバーでのパッケージマネージャーなどのツールの実行

パッケージマネージャーなどのツールをプロキシサーバーで実行する方法
  1. squid.conf ファイルにステートメントを追加し、プロキシサーバーの許可リストにツールを追加します。

  2. プロキシサーバーのプライベートエンドポイントを指す行を buildspec ファイルに追加します。

次の例では、apt-getcurl、および maven でこの作業を行う方法を示しています。別のツールを使用する場合は、同じ原則が適用されます。これを squid.conf ファイルの許可リストに追加し、プロキシサーバーのエンドポイントを CodeBuild に認識させるためのコマンドを buildspec ファイルに追加します。

プロキシサーバーで apt-get を実行するには
  1. 次のステートメントを squid.conf ファイルに追加し、プロキシサーバーの許可リストに apt-get を追加します。最初の 3 行では、ビルド環境で apt-get を実行できるようになります。

    acl allowed_sites dstdomain ppa.launchpad.net # Required for apt-get to run in the build environment acl apt_get dstdom_regex .*\.launchpad.net # Required for CodeBuild to run apt-get in the build environment acl apt_get dstdom_regex .*\.ubuntu.com # Required for CodeBuild to run apt-get in the build environment http_access allow localnet allowed_sites http_access allow localnet apt_get
  2. apt-get コマンドで /etc/apt/apt.conf.d/00proxy のプロキシ設定を検索できるように、次のステートメントを buildspec ファイルを追加します。

    echo 'Acquire::http::Proxy "http://<private-ip-of-proxy-server>:3128"; Acquire::https::Proxy "http://<private-ip-of-proxy-server>:3128"; Acquire::ftp::Proxy "http://<private-ip-of-proxy-server>:3128";' > /etc/apt/apt.conf.d/00proxy
プロキシサーバーで curl を実行するには
  1. 次の内容を squid.conf ファイルに追加し、ビルド環境の許可リストに curl を追加します。

    acl allowed_sites dstdomain ppa.launchpad.net # Required to run apt-get in the build environment acl allowed_sites dstdomain google.com # Required for access to a webiste. This example uses www.google.com. http_access allow localnet allowed_sites http_access allow localnet apt_get
  2. curl でプライベートプロキシサーバーを使用して squid.conf に追加したウェブサイトにアクセスできるように、次のステートメントを buildspec ファイルに追加します。この例では、ウェブサイトは google.com です。

    curl -x <private-ip-of-proxy-server>:3128 https://www.google.com
プロキシサーバーで maven を実行するには
  1. 次の内容を squid.conf ファイルに追加し、ビルド環境の許可リストに maven を追加します。

    acl allowed_sites dstdomain ppa.launchpad.net # Required to run apt-get in the build environment acl maven dstdom_regex .*\.maven.org # Allows access to the maven repository in the build environment http_access allow localnet allowed_sites http_access allow localnet maven
  2. buildspec ファイルに次のステートメントを追加します。

    maven clean install -DproxySet=true -DproxyHost=<private-ip-of-proxy-server> -DproxyPort=3128