将 Amazon CodeBuild 与代理服务器结合使用 - Amazon CodeBuild
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Amazon CodeBuild 与代理服务器结合使用

您可以将 Amazon CodeBuild 与代理服务器结合使用,以控制往来于 Internet 的 HTTP 和 HTTPS 流量。要使用代理服务器运行 CodeBuild,您需要在 VPC 的公有子网中安装代理服务器,并在私有子网中安装 CodeBuild。

在代理服务器中运行 CodeBuild 有两种主要使用案例:

  • 它不再需要您的 VPC 中的 NAT 网关或 NAT 实例。

  • 它允许您指定代理服务器中的实例可以访问的 URL 以及代理服务器拒绝访问的 URL。

您可以将 CodeBuild 与两种类型的代理服务器结合使用。对于这两种类型,代理服务器都在公有子网中运行,CodeBuild 在私有子网中运行。

在代理服务器中运行 CodeBuild 所需的组件

您需要这些组件在透明或显式代理服务器中运行 Amazon CodeBuild:

  • VPC。

  • 代理服务器的 VPC 中的一个公有子网。

  • CodeBuild 的 VPC 中的一个私有子网。

  • 一个 Internet 网关,允许 VPC 和 Internet 之间进行通信。

下图显示了组件的交互方式。

设置 VPC、子网和网络网关

在透明或显式代理服务器中运行 Amazon CodeBuild 需要以下步骤。

  1. 创建 VPC。有关信息,请参阅《Amazon VPC 用户指南》中的创建 VPC

  2. 在您的 VPC 中创建两个子网。一个是名为 Public Subnet 的公有子网,代理服务器将在其中运行。另一个是一个名为 Private Subnet 的私有子网,CodeBuild 将在其中运行。

    有关信息,请参阅在 VPC 中创建子网

  3. 创建 Internet 网关,并将其连接到您的 VPC。有关更多信息,请参阅创建并附加 Internet 网关

  4. 向默认路由表添加一条规则,该规则将来自 VPC 的传出流量路由到 Internet 网关。有关信息,请参阅在路由表中添加和删除路由

  5. 向 VPC 的默认安全组添加一条规则,该规则允许来自 VPC (0.0.0.0/0) 的入站 SSH 流量 (0.0.0.0/0)。

  6. 请按照《Amazon EC2 用户指南》中的使用启动实例向导启动实例来启动 Amazon Linux 实例。当您运行该向导时,请选择以下选项:

    • 选择实例类型中,选择一个 Amazon Linux 亚马逊机器映像 (AMI)。

    • 子网中,选择您在本主题的前面步骤中创建的公有子网。如果您使用了建议的名称,则该名称是公有子网

    • 自动分配公有 IP 中,选择启用

    • 配置安全组页面上,对于分配安全组,选择选择现有安全组。接下来,选择默认安全组。

    • 选择启动后,选择现有密钥对或创建密钥对。

    选择所有其他选项的默认设置。

  7. 您的 EC2 实例开始运行后,禁用源/目标检查。有关信息,请参阅《Amazon VPC 用户指南》中的禁用源/目标检查

  8. 在 VPC 中创建路由表。向路由表中添加一条规则,该规则将发往 Internet 的流量路由到您的代理服务器。将此路由表与私有子网关联。这是必需的,以便来自私有子网(CodeBuild 在其中运行)中的实例的出站请求始终通过代理服务器进行路由。

安装和配置代理服务器

有许多可供选择的代理服务器。Squid 是一个开源代理服务器,此处用于演示 Amazon CodeBuild 如何在代理服务器中运行。您可以将相同的概念应用于其他代理服务器。

要安装 Squid,请通过运行以下命令使用 yum 存储库:

sudo yum update -y sudo yum install -y squid

安装 Squid 后,请按照本主题后面的说明操作来编辑其 squid.conf 文件。

为 HTTPS 流量配置 Squid

对于 HTTPS,HTTP 流量封装在一个传输层安全性 (TLS) 连接中。Squid 使用一个名为 SslPeekAndSplice 的功能从包含请求的 Internet 主机的 TLS 启动中检索服务器名称指示 (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 请求消息中检索主机标头字段,该字段指定所请求的 Internet 主机。

在显式代理服务器中运行 CodeBuild

要在显式代理服务器中运行 Amazon CodeBuild,您必须配置代理服务器以允许或拒绝进出外部网站的流量,然后配置 HTTP_PROXYHTTPS_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 的请求。接下来的两行授予您的代理服务器访问 Amazon CodeBuild 可能使用的目标 URL 的权限。修改最后一行中的正则表达式,以指定 Amazon 区域中的 S3 存储桶或 CodeCommit 存储库。例如:

    • 如果您的源是 Amazon S3,请使用命令 acl download_src dstdom_regex .*s3\.us-west-1\.amazonaws\.com 来授权访问 us-west-1 区域中的 S3 存储桶。

    • 如果您的源是 Amazon CodeCommit,请使用 git-codecommit.<your-region>.amazonaws.com 将 Amazon 区域添加到允许列表中。

    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。需要访问 CloudWatch,这样 CodeBuild 才能创建 CloudWatch Logs。上传构件和 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 项目

要使用显式代理服务器运行 Amazon CodeBuild,请使用您为代理服务器创建的 EC2 实例的私有 IP 地址和项目级别的端口 3128 设置 HTTP_PROXYHTTPS_PROXY 环境变量。私有 IP 地址看起来类似于 http://your-ec2-private-ip-address:3128。有关更多信息,请参阅 在 Amazon CodeBuild 中创建构建项目更改 Amazon 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

要在透明代理服务器中运行 Amazon CodeBuild,您必须配置代理服务器,使其能够访问与其交互的网站和域。

将 Squid 配置为透明代理服务器

要将代理服务器配置为透明,您必须授予其访问您希望其访问的域和网站的权限。要使用透明代理服务器运行 Amazon CodeBuild,您必须向它授予对 amazonaws.com 的访问权限。您还必须授予对 CodeBuild 使用的其他网站的访问权限。这些网站因您创建 CodeBuild 项目的方式而异。示例网站是用于存储库的网站,例如 GitHub、Bitbucket、Yum 和 Maven。要授予 Squid 访问特定域和网站的权限,请使用类似于以下内容的命令来更新 squid.conf 文件。此示例命令授予对 amazonaws.comgithub.combitbucket.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 在端口 3129 上侦听 HTTP 流量(而不是 80),并在端口 3130 上侦听 HTTPS 流量(而不是 443)。使用 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 项目

配置您的代理服务器之后,便可以将其与私有子网中的 Amazon CodeBuild 结合使用,而无需进行更多配置。每个 HTTP 和 HTTPS 请求都经过公共代理服务器。使用以下命令查看 Squid 代理服务器访问日志:

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

在代理服务器中运行程序包管理器和其他工具

要在代理服务器中运行一个工具,如程序包管理器,请执行以下操作:
  1. 通过将语句添加到您的 squid.conf 文件中,将该工具添加到代理服务器的允许列表中。

  2. 在 buildspec 文件中添加指向代理服务器的私有终端节点的命令行。

以下示例演示了如何为 apt-getcurlmaven 执行此操作。如果您使用其他工具,则相同的原则将适用。将它添加到 squid.conf 文件中的允许列表中,并向 buildspec 文件添加一个命令以使 CodeBuild 了解您的代理服务器的端点。

在代理服务器中运行 apt-get
  1. 将以下语句添加到您的 squid.conf 文件中,以便将 apt-get 添加到代理服务器中的允许列表。前三行允许 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 中查找代理配置。

    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. 在 buildspec 文件中添加以下语句,以便 curl 使用专用代理服务器访问您添加到 squid.conf 的网站。在此示例中,网站为 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