

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

# 明示的なプロキシサーバーでの CodeBuild の実行
<a name="run-codebuild-in-explicit-proxy-server"></a>

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

**Topics**
+ [明示的なプロキシサーバーとしての Squid の設定](#use-proxy-server-explicit-squid-configure)
+ [CodeBuild プロジェクトを作成する](#use-proxy-server-explicit-create-acb-project)
+ [明示的なプロキシサーバーのサンプル `squid.conf` ファイル](#use-proxy-server-explicit-sample-squid-conf)

## 明示的なプロキシサーバーとしての Squid の設定
<a name="use-proxy-server-explicit-squid-configure"></a>

 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\$1src dstdom\$1regex .\$1s3\$1.us-west-1\$1.amazonaws\$1.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
       ```

  1.  `proxy` を buildspec ファイルに追加します。詳細については、「[buildspec の構文](build-spec-ref.md#build-spec-ref-syntax)」を参照してください。

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

**注記**  
RequestError タイムアウトエラーが表示される場合は、「[プロキシサーバーで CodeBuild を実行しているときの RequestError タイムアウトエラー](troubleshooting.md#code-request-timeout-error)」を参照してください。

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

## CodeBuild プロジェクトを作成する
<a name="use-proxy-server-explicit-create-acb-project"></a>

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

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

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

## 明示的なプロキシサーバーのサンプル `squid.conf` ファイル
<a name="use-proxy-server-explicit-sample-squid-conf"></a>

 明示的なプロキシサーバー用に設定した `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
```