搭 AWS CodeBuild 配代理伺服器使用 - AWS CodeBuild

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

搭 AWS CodeBuild 配代理伺服器使用

您可以 AWS CodeBuild 與代理服務器一起使用,以規範HTTP和從互聯網進出的HTTPS流量。若要 CodeBuild 使用 Proxy 伺服器執行,請將 Proxy 伺服器安裝 CodeBuild 在公用子網路和VPC.

在 Proxy 伺服器 CodeBuild 中執行有兩種主要使用案例:

  • 它消除了在NAT您的VPC. NAT

  • 它可讓您指定 Proxy 伺服器中可以存取的執行個體,以及 Proxy 伺服器拒絕存取的執行個體。URLs URLs

您可以搭 CodeBuild 配兩種類型的 Proxy 伺服器使用。對於這兩者,Proxy 伺服器會在公用子網路中 CodeBuild 執行,並在私有子網路中執行。

在 Proxy 伺服器 CodeBuild 中執行所需的元件

您需要這些元件才能 AWS CodeBuild 在透明或明確的 Proxy 伺服器中執行:

  • VPC。

  • 代理服務器中VPC的一個公共子網。

  • 一個私有子網路在您VPC的 CodeBuild.

  • 一個互聯網網關,允許與互聯網之間VPC的通信。

下圖顯示元件如何互動。

該圖顯示了組件的交互方式。

設定VPC、子網路和網路閘道

若要在透明或明確的 Proxy 伺服器 AWS CodeBuild 中執行,需要執行下列步驟。

  1. 創建一個VPC. 如需詳細資訊,請Amazon VPC 使用者指南VPC中的建立。

  2. 在您的VPC. 一個是名為 Public Subnet 的公有子網路,您的代理伺服器在其中執行。另一個是名為Private Subnet在其中 CodeBuild 運行的私有子網絡。

    如需詳細資訊,請參閱在 VPC.

  3. 建立網際網路閘道並將其附加至您的VPC. 如需詳細資訊,請參閱建立和連接網際網路閘道

  4. 將規則新增至預設路由表,以將來自 VPC (0.0.0.0/0) 的傳出流量路由至網際網路閘道。如需相關資訊,請參閱從路由表新增和移除路由

  5. 將規則新增至允許來自 (0.0.0.0/0) VPC 的輸入SSH流量 (TCP22) 的VPC預設安全性群組。

  6. 遵循 Amazon 使用者指南中的使用啟動執行個體精靈啟動執行個體中的EC2指示來啟動 Amazon Linux 執行個體。當您執行精靈時,請選擇以下選項:

    • 選擇執行個體類型中,選擇一個 Amazon Linux Amazon 機器映像 (AMI)。

    • Subnet (子網路) 中,選擇您先前在此主題中建立的公有子網路。如果您使用建議的名稱,則為 公有子網路 (Public Subnet)

    • Auto-assign Public IP (自動指派公有 IP) 中,選擇 Enable (啟用)

    • Configure Security Group (設定安全群組) 頁面,針對 Assign a security group (指派安全群組),選擇 Select an existing security group (選取現有的安全群組)。接下來,選擇預設安全群組。

    • 當您選擇 Launch (啟動) 之後,選擇現有的金鑰對或建立金鑰對。

    對於其他所有選項,選擇預設設定。

  7. 執行EC2個體執行後,請停用來源/目的地檢查。如需詳細資訊,請參閱 Amazon VPC 使用者指南中的停用來源/目的地檢查

  8. 在中建立路由表格VPC。在路由表中新增規則,將流向網際網路的流量路由傳送到您的代理伺服器。將此路由表與您的私有子網路建立關聯。這是必要的,以便一律透過 Proxy 伺服器路由來自私有子網路 (執 CodeBuild 行位置) 中執行個體的輸出要求。

安裝和設定代理伺服器

有許多代理伺服器可供選擇。此處使用開放原始碼代理伺服器 Squid 來示範如何在代理伺服器中 AWS CodeBuild 執行。您可以將相同的概念套用到其他代理伺服器。

若要安裝 Squid,請執行下列命令來使用 yum 儲存庫:

sudo yum update -y sudo yum install -y squid

安裝 Squid 之後,請按照本主題稍後的指示編輯其 squid.conf 檔案。

為HTTPS流量設定 Squid

對於HTTPS,HTTP流量封裝在傳輸層安全性 (TLS) 連接中。Squid 會使用名SslPeekAndSplice為的功能,從包含所要求網際網路主機的TLS起始時擷取伺服器名稱指示 (SNI)。這是必要的,因此 Squid 不需要解密HTTPS流量。若要啟用此功能 SslPeekAndSplice,Squid 需要憑證。使用「開啟」建立此憑證SSL:

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 在明確 Proxy 伺服器中執行,您必須將 Proxy 伺服器設定為允許或拒絕進出外部網站的流量,然後設定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. 接下來的兩行授予您的代理服務器訪問可URLs能被使用的目的地 AWS CodeBuild。編輯最後一行的規則運算式,以指定 S3 儲存貯體或 AWS 區域中的 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。需要 CloudWatch 存取權,才 CodeBuild 能建立 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 使用明確的代理服務器運行,請使用為代理服務器創建的EC2實例的私有 IP 地址和項目級 3128 端口 3128 設置其HTTP_PROXYHTTPS_PROXY環境變量。私有 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 在透明 Proxy 伺服器中執行,您必須設定 Proxy 伺服器,以存取其互動的網站和網域。

將 Squid 設定為透明代理伺服器

若要將代理伺服器設定為透明,您必須授與它您希望其存取之網域和網站的存取權。若要使 AWS CodeBuild 用透明 Proxy 伺服器執行,您必須授與它的存取權amazonaws.com。您還必須授予對其他網站 CodeBuild 使用的訪問權限。這些會有所不同,具體取決於您建立 CodeBuild 專案的方式。示例網站是那些存儲庫 GitHub,如比特桶,百勝和 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 連接埠。魷魚在端口 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 專案

設定 Proxy 伺服器之後,您可以在私有子網路 AWS CodeBuild 中使用它,而無需進行更多設定。每個HTTP和HTTPS請求都通過公共代理服務器。使用以下命令來檢視 Squid 代理存取日誌:

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

在代理伺服器中執行套用管理員和其他工具

在 Proxy 伺服器中執行工具,例如套件管理員
  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. 在 buildspec 檔案中新增以下陳述式,讓 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