使用专用池访问专用 JFrog Artifactory 中的资源

本页面演示了如何使用 Cloud Build 私有池从私有 Virtual Private Cloud 网络访问资源。

在本教程中,您将在专用 VPC 网络内托管的 Compute Engine 中创建 JFrog Artifactory,然后配置在专用池中运行的构建以访问该 Artifactory 中的数据。Jfrog Artifactory 是一个开源二进制代码库管理器。

创建私有工件

  1. 从容器创建 Compute Engine 实例:

    gcloud compute instances create-with-container jfrog \
    --container-image docker.bintray.io/jfrog/artifactory-jcr:latest \
    --zone us-central1-a
    
  2. 通过 SSH 登录实例。容器可能需要几分钟才能完成初始化。

    gcloud compute ssh --zone us-central1-a jfrog
    
  3. 通过运行以下命令来测试连接。容器准备就绪后,它将以 200 HTTP 代码响应,后跟 HTML 页面。

    curl -i http://localhost:8081
    
  4. 如需在 Artifactory 中创建代码库,您必须签署 JFrog EULA(最终用户许可协议):

    curl -XPOST -vu admin:password http://localhost:8081/artifactory/ui/jcr/eula/accept
    

    您将看到如下所示的输出:

        *   Trying 127.0.0.1:8081...
        * Connected to localhost (127.0.0.1) port 8081 (#0)
        * Server auth using Basic with user 'admin'
        > POST /artifactory/ui/jcr/eula/accept HTTP/1.1
        > Host: localhost:8081
        > Authorization: Basic ….
        > User-Agent: curl/7.74.0
        > Accept: */*
        >
        * Mark bundle as not supporting multiuse
        < HTTP/1.1 200 OK
        < X-JFrog-Version: Artifactory/7.19.9 71909900
        < X-Artifactory-Id: ….
        < X-Artifactory-Node-Id: jfrog2
        < SessionValid: false
        < Content-Length: 0
        < Date: Fri, 25 Jun 2021 19:08:10 GMT
    
        * Connection #0 to host localhost left intact
    

将文件上传到 Artifactory

  1. 创建 txt 文件以上传到 Artifactory:

    echo "Hello world" >> helloworld.txt
    
  2. JFrog 附带默认示例代码库。使用默认凭据上传到代码库:

    curl -u admin:password -X PUT \
    "http://localhost:8081/artifactory/example-repo-local/helloworld.txt" \
    -T helloworld.txt
    

    此时应返回:

        {
        "repo" : "example-repo-local",
        "path" : "/helloworld.txt",
        "created" : "2021-06-25T19:08:24.176Z",
        "createdBy" : "admin",
        "downloadUri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt",
        "mimeType" : "text/plain",
        "size" : "12",
        "checksums" : {
          "sha1" : "...",
          "md5" : "...",
          "sha256" : "..."
        },
        "originalChecksums" : {
          "sha256" : "..."
        },
        "uri" : "http://localhost:8081/artifactory/example-repo-local/helloworld.txt"
        }
    
  3. 输入 exit 以结束 SSH 会话。

  4. 移除外部 IP 地址,因此 Artifactory 只能从专用内部来源访问。

    gcloud compute instances delete-access-config --zone us-central1-a jfrog
    

尝试从 Artifactory 访问数据

  1. 设置环境变量以存储您的项目 ID 和项目编号:

    PROJECT_ID=$(gcloud config list --format='value(core.project)')
    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
    
  2. Compute Engine Viewer 角色授予您用于 build 的服务账号,以便查看 JFrog 实例的内部 IP 地址:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:SERVICE_ACCOUNT \
        --role=roles/compute.viewer
    

    其中,SERVICE_ACCOUNT 是服务账号电子邮件地址。

  3. 创建一个名为 cloudbuild.yaml 的文件,其中包含从 Artifactory 读取的以下代码。这是构建配置文件。

    第一步是从您创建的 Artifactory 提取内部 IP 地址。第二步:向该地址发送请求,读取您创建的 helloworld.txt 文件。这些步骤是分开的,以便更轻松地隔离权限和网络错误。如果第一步失败,这是由于权限错误,您需要确保 build 服务账号有权访问 Compute Engine 资源,如上一步所示。如果第二步失败,则原因可能是网络错误。本教程的其余部分介绍网络配置。

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
  4. 使用构建配置文件启动构建。

    默认情况下,当您在 Cloud Build 上运行构建时,该构建在可访问公共互联网的安全托管环境中运行。每个构建都在其自己的工作器上运行,并与其他工作负载隔离。默认池对环境的可自定义程度有限制,特别是对于专用网络访问权限。在此示例中,您正在尝试从公共工作器访问专用网络。

    使用以下命令运行 cloudbuild.yaml。应该会失败。

    gcloud builds submit --no-source
    

    输出结果如下所示:

    BUILD
    Starting Step #0 - "Get Private Artifactory Address"
    Step #0 - "Get Private Artifactory Address": Already have image (with digest): gcr.io/cloud-builders/gcloud
    Finished Step #0 - "Get Private Artifactory Address"
    Starting Step #1 - "Pull from Private Artifactory"
    Step #1 - "Pull from Private Artifactory": Already have image (with digest): gcr.io/cloud-builders/curl
    Step #1 - "Pull from Private Artifactory":   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    Step #1 - "Pull from Private Artifactory":                                  Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:--  0:02:09 --:--:--     0curl: (7) Failed to connect to 10.128.0.2 port 8081: Connection timed out
    Finished Step #1 - "Pull from Private Artifactory"
    ERROR
    ERROR: build step 1 "gcr.io/cloud-builders/curl" failed: step exited with non-zero status: 7
    

    您可以按连接超时看到 Cloud Build 无法访问内部 IP 地址。要访问此专用资源,您必须使用 Cloud Build 专用池。

在 Artifactory 的 VPC 网络与服务提供方网络之间创建专用连接

  1. 首先,确保您的 VPC 网络允许入站流量。创建防火墙规则,以允许入站内部流量进入具有 jfrog 实例的网络。范围 10.0.0.0/16 位于专用地址空间中,以下步骤将用于 Cloud Build 专用池。

    gcloud compute firewall-rules create allow-private-pools --direction=INGRESS \
    --priority=1000 --network=default --action=ALLOW --rules=all --source-ranges=10.0.0.0/16
    
  2. 为 Cloud Build 专用池创建预留范围,以便用于工作器。预留范围必须位于 Artifactory 所在的网络中。在本例中,计算为 default 计算网络。

    设置预留范围时,您有以下两种选项。 您可以通过提供 --addresses--prefix-length 明确指定范围,也可以允许 Google Cloud 根据提供的 prefix-length 预配可用范围。

    在以下示例中,您将地址明确设置为与您创建的防火墙规则匹配。专用池将使用此地址空间,并且入站流量不会被阻止。

    gcloud compute addresses create jfrog-ranges --global --purpose=VPC_PEERING \
    --addresses=10.0.0.0 --prefix-length=16 --network=default
    
  3. 将 VPC 网络与 Service Networking API 对等互连。

    Cloud Build 专用池使用 Service Networking API 运行工作器。这样您就可以在内部 IP 地址上提供托管式服务。这是通过将运行 Cloud Build 专用池工作器的 Google 管理的 VPC 与您自己的 VPC 对等互连来实现的。此操作可能需要几分钟时间才能完成。

    gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \
    --ranges=jfrog-ranges --network=default
    

创建专用池

  1. default VPC 网络现已可以与 Cloud Build 专用池搭配使用。创建专用池,并将其与 VPC 网络进行对等互连。

     gcloud builds worker-pools create jfrog-pool --region us-central1 \
     --peered-network=projects/${PROJECT_ID}/global/networks/default
    
  2. 如需使用新的专用池运行构建,您可以使用 gcloud 命令传入 --worker-pool 标志,或者更新 cloudbuild.yaml 配置以确保它始终使用专用池。在本教程中,请添加以下选项来更新 cloudbuild.yaml

    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  3. 完整的文件如下所示:

    steps:
      - id: Get Private Artifactory Address
        name: gcr.io/cloud-builders/gcloud
        entrypoint: /bin/bash
        args: 
          - -c
          - |
            gcloud compute instances describe jfrog \
            --zone us-central1-a \
            --format="value(networkInterfaces.networkIP)" >> _INTERNAL_IP_ADDRESS
    
      - id: Pull from Private Artifactory
        name: gcr.io/cloud-builders/curl
        entrypoint: /bin/bash
        args:
          - -c
          - |
            curl -u admin:password --connect-timeout 10.00 \
            http://$(cat _INTERNAL_IP_ADDRESS):8081/artifactory/example-repo-local/helloworld.txt
    
    options:
      pool:
        name: 'projects/${PROJECT_ID}/locations/us-central1/workerPools/jfrog-pool'
  4. 启动构建:

     gcloud builds submit --no-source
    
  5. 构建将使用与 VPC 网络对等互连的新的专用池,从而能够访问 Artifactory 的内部 IP 地址。输出将成功,Step #1 应输出“Hello world”。