ลดความซับซ้อนของการตรวจสอบสิทธิ์ระหว่างบริการใน Cloud Run ด้วย Express Link

1. ภาพรวม

เมื่อสร้าง Microservice หรือแอปพลิเคชันแบบ Multi-Agent ใน Cloud Run บริการแบ็กเอนด์ภายในไม่ควรเปิดให้เข้าถึงได้จากอินเทอร์เน็ตสาธารณะ แต่คุณจะกำหนดค่าบริการแบ็กเอนด์แต่ละรายการให้ต้องมีการตรวจสอบสิทธิ์ (--no-allow-unauthenticated) และให้สิทธิ์บทบาท IAM ของ Cloud Run Invoker (roles/run.invoker) แก่ผู้โทรที่ได้รับอนุญาตเท่านั้น

โดยปกติแล้ว การเรียกใช้บริการ Cloud Run ที่ผ่านการตรวจสอบสิทธิ์กำหนดให้แอปพลิเคชันที่เรียกใช้ต้องดำเนินการแทรกโทเค็นด้วยตนเองในโค้ด ก่อนส่งคำขอ HTTP โค้ดจะค้นหาเซิร์ฟเวอร์ข้อมูลเมตาของ Compute เพื่อรับโทเค็นรหัส OpenID Connect (OIDC) ที่ลงชื่อโดย Google โทเค็นดังกล่าวจะกำหนดขอบเขตตาม URL กลุ่มเป้าหมายของบริการปลายทาง และโค้ดของคุณจะแนบโทเค็นนั้นไว้กับส่วนหัว Authorization: Bearer การเขียนและดูแลรักษาโครงสร้างพื้นฐานของการตรวจสอบสิทธิ์ภายในโค้ดของแอปพลิเคชันทำให้เกิดความยุ่งยากโดยไม่จำเป็น ดังนี้

  • ซึ่งจะเชื่อมโยงตรรกะทางธุรกิจของคุณกับไลบรารีการตรวจสอบสิทธิ์ของ Google Cloud ซึ่งจะลดความสามารถในการอ่านและความสามารถในการพกพาในสภาพแวดล้อมต่างๆ
  • ตรรกะการรับโทเค็นจะแตกต่างกันไปตามประเภทปลายทางและโหมดความปลอดภัย เช่น Cloud Run ต้องการโทเค็นรหัส OIDC, Google APIs ต้องการโทเค็นเพื่อการเข้าถึง OAuth 2.0 และ mTLS ต้องใช้โทเค็นที่เชื่อมโยง

Cloud Run Express Link (เวอร์ชันตัวอย่าง) จะนำการแทรกโทเค็นด้วยตนเองออกจากโค้ดของแอปพลิเคชัน คุณเปิดใช้เครือข่ายแอมเบียนท์ในบริการ Cloud Run ที่เรียก (ต้นทาง) และสร้างทรัพยากร ExpressLink แบบประกาศที่เชื่อมโยงบริการต้นทางกับบริการปลายทาง พร็อกซีไฟล์ช่วยเหลือที่ Google จัดการจะสกัดกั้นคำขอ HTTP ข้อความธรรมดาขาออกไปยังปลายทาง แทรกโทเค็นข้อมูลประจำตัวของบริการผู้โทรโดยอัตโนมัติ และเข้ารหัสการเชื่อมต่อไปยังปลายทาง

ใน Codelab นี้ คุณจะเปรียบเทียบทั้ง 2 แนวทางกับบริการแบ็กเอนด์ส่วนตัวเดียวกัน

  1. ติดตั้งใช้งานบริการปลายทางส่วนตัวและบริการผู้โทร (caller-oidc) ที่ดึงข้อมูลโทเค็นรหัส OIDC จากเซิร์ฟเวอร์ข้อมูลเมตาของ Compute ใน Python ขั้นตอนนี้จะยืนยันการเชื่อมต่อจากต้นทางถึงปลายทางกับแบ็กเอนด์ และแสดงรหัสการตรวจสอบสิทธิ์ที่จำเป็นโดยไม่มี Express Link
  2. ติดตั้งใช้งานบริการผู้โทรที่ 2 (caller-express-link) โดยนำโค้ดการดึงข้อมูลโทเค็นข้อมูลเมตาออก และเชื่อมต่อกับบริการปลายทางโดยใช้ Ambient Networking และ Express Link
PART 1: WITHOUT EXPRESS LINK (caller-oidc)
+----------------------------------------------+          +-----------------+
| Caller Service (caller-oidc)                 |  HTTPS   | Destination     |
|                                              | + token  | (destination)   |
|  App Container                               | -------> |                 |
|   1. Parse audience URL with urllib.parse    |          | Verifies caller |
|   2. Fetch OIDC ID token via google-auth     |          | has IAM role    |
|   3. Set Authorization: Bearer header        |          | run.invoker     |
+----------------------------------------------+          +-----------------+

PART 2: WITH EXPRESS LINK (caller-express-link)
+----------------------------------------------+          +-----------------+
| Caller Service (caller-express-link)         |  HTTPS   | Destination     |
|  +----------------+      +----------------+  | + token  | (destination)   |
|  | App Container  | HTTP | Ambient        |  | -------> |                 |
|  | requests.get() | ---> | Sidecar Proxy  |  |          | Same service    |
|  | (no auth code) |      | (Express Link) |  |          | as Part 1       |
|  +----------------+      +----------------+  |          | (unchanged)     |
+----------------------------------------------+          +-----------------+

ข้อกำหนดเบื้องต้น

  • มีความคุ้นเคยขั้นพื้นฐานกับ Python และเว็บเซอร์วิส HTTP
  • ความคุ้นเคยพื้นฐานกับบัญชีบริการ Google Cloud IAM และการติดตั้งใช้งาน Cloud Run

สิ่งที่คุณจะได้เรียนรู้

  • วิธีทำให้บริการปลายทาง Cloud Run ส่วนตัวที่บังคับใช้การตรวจสอบสิทธิ์ IAM ใช้งานได้
  • วิธีตรวจสอบสิทธิ์คำขอแบบบริการต่อบริการด้วยตนเองใน Python โดยใช้โทเค็นระบุตัวตน OIDC ของ google-auth จากเซิร์ฟเวอร์ข้อมูลเมตาเพื่อยืนยันการเชื่อมต่อพื้นฐาน
  • วิธีนำโค้ดการดึงข้อมูลโทเค็นด้วยตนเองออกและทำให้บริการผู้โทรของ Cloud Run ใช้งานได้โดยเปิดใช้ Ambient Networking
  • วิธีสร้าง Cloud Run Express Link เพื่อให้บริการผู้โทรเรียกใช้บริการปลายทางที่ตรวจสอบสิทธิ์แล้วได้โดยไม่ต้องใช้รหัสการตรวจสอบสิทธิ์

สิ่งที่คุณจะสร้าง

  • destination: บริการ Python Flask ส่วนตัวใน Cloud Run ที่แสดงปลายทาง /ping
  • caller-oidc: บริการ Python Flask ที่เรียกใช้ destination โดยการดึงข้อมูลและแทรกโทเค็นรหัส OIDC ในโค้ดของแอปพลิเคชันด้วยตนเอง
  • caller-express-link: บริการ Python Flask ที่ทำงานควบคู่กันโดยไม่มีโค้ดการแทรกโทเค็น ซึ่งเรียกใช้บริการปลายทางเดียวกันผ่าน Express Link

สิ่งที่คุณต้องมี

  • เว็บเบราว์เซอร์ เช่น Chrome
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงินและสิทธิ์ในการสร้างบัญชีบริการ IAM, บริการ Cloud Run และทรัพยากร Network Services

2. ตั้งค่าสภาพแวดล้อม

ตั้งค่าโปรเจ็กต์ Google Cloud

  1. หากยังไม่มีบัญชี Google คุณต้องสร้างบัญชี Google
    • ใช้บัญชีส่วนตัวแทนบัญชีงานหรือบัญชีโรงเรียน บัญชีงานและบัญชีโรงเรียนอาจมีข้อจำกัดที่ทำให้คุณไม่สามารถเปิดใช้ API ที่จำเป็นสำหรับห้องทดลองนี้ได้
  2. ลงชื่อเข้าใช้คอนโซล Google Cloud
  3. เปิดใช้การเรียกเก็บเงินใน Cloud Console
    • การทำห้องทดลองนี้ให้เสร็จสมบูรณ์ควรใช้ทรัพยากร Cloud ไม่เกิน $1 USD
    • คุณสามารถทำตามขั้นตอนที่ตอนท้ายของห้องทดลองนี้เพื่อลบทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินเพิ่มเติม
    • ผู้ใช้ใหม่มีสิทธิ์รับช่วงทดลองใช้ฟรีมูลค่า $300 USD
  4. สร้างโปรเจ็กต์ใหม่หรือเลือกใช้โปรเจ็กต์ที่มีอยู่ซ้ำ
    • หากเห็นข้อผิดพลาดเกี่ยวกับโควต้าโปรเจ็กต์ ให้ใช้โปรเจ็กต์ที่มีอยู่ซ้ำหรือลบโปรเจ็กต์ที่มีอยู่เพื่อสร้างโปรเจ็กต์ใหม่

เริ่มต้น Cloud Shell

ใน Codelab นี้ คุณจะใช้ Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งบนเบราว์เซอร์ที่ติดตั้ง Google Cloud CLI (gcloud), Python และ curl ไว้ล่วงหน้า

  1. ในคอนโซล Google Cloud ให้คลิกเปิดใช้งาน Cloud Shell ในแถบนำทางด้านบน หากได้รับข้อความแจ้ง ให้คลิกให้สิทธิ์
  2. ส่งออกรหัสโปรเจ็กต์และภูมิภาคการติดตั้งใช้งาน และกำหนดค่า gcloud ให้ใช้ $PROJECT_ID แทนที่ YOUR_PROJECT_ID ด้วยรหัสโปรเจ็กต์ และแทนที่ us-west1 ด้วยภูมิภาค Cloud Run อื่น (ไม่บังคับ)
export PROJECT_ID="YOUR_PROJECT_ID"
export REGION="us-west1"
gcloud config set project "$PROJECT_ID"
  1. ส่งออกหมายเลขโปรเจ็กต์และบัญชี Google Cloud ที่ใช้งานอยู่ แล้วยืนยันว่ามีการป้อนตัวแปรทั้ง 4 รายการแล้ว
export PROJECT_NUMBER=$(gcloud projects describe "$PROJECT_ID" --format="value(projectNumber)")
export ACTIVE_ACCOUNT=$(gcloud config get-value account 2>/dev/null)
echo "PROJECT_ID: $PROJECT_ID | PROJECT_NUMBER: $PROJECT_NUMBER | ACCOUNT: $ACTIVE_ACCOUNT | REGION: $REGION"

อัปเดต gcloud และเปิดใช้ API

  1. อัปเดตคอมโพเนนต์ gcloud เพื่อให้คุณมีคำสั่ง beta ล่าสุดสำหรับ Ambient Networking และ Express Link ดังนี้
gcloud components update --quiet
  1. เปิดใช้ Google Cloud API ที่จำเป็นสำหรับการติดตั้งใช้งานแหล่งที่มาของ Cloud Run, บัญชีบริการ IAM, Ambient Networking และ Network Services Express Link ดังนี้
gcloud services enable \
    iam.googleapis.com \
    run.googleapis.com \
    cloudbuild.googleapis.com \
    artifactregistry.googleapis.com \
    networkservices.googleapis.com \
    networksecurity.googleapis.com \
    trafficdirector.googleapis.com \
    cloudtrace.googleapis.com

สร้างไดเรกทอรีพื้นที่ทำงาน

สร้างไดเรกทอรีที่ใช้งานได้เพื่อเก็บโฟลเดอร์บริการ 3 โฟลเดอร์ที่คุณจะสร้างใน Codelab นี้

mkdir -p ~/express-link-codelab
cd ~/express-link-codelab

3. ติดตั้งใช้งานบริการปลายทางส่วนตัว

ก่อนอื่น คุณจะต้องทำให้บริการ Cloud Run ส่วนตัวที่ชื่อ destination ใช้งานได้ คุณจะใช้บริการปลายทางเดียวนี้ซ้ำสำหรับทั้ง 2 ส่วนของ Codelab ดังนี้

  • บริการปลายทางจะไม่ทำการแทรกโทเค็นขาออก
  • บริการปลายทางไม่จำเป็นต้องเปิดใช้ Ambient Networking เพื่อรับการเข้าชมผ่าน Express Link
  1. สร้างบัญชีบริการ IAM โดยเฉพาะสำหรับบริการปลายทาง โดยทำดังนี้
gcloud iam service-accounts create destination-identity \
    --display-name="Cloud Run destination service account"
  1. สร้างโฟลเดอร์ destination และย้ายไฟล์เข้าไปโดยทำดังนี้
mkdir -p ~/express-link-codelab/destination
cd ~/express-link-codelab/destination
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ requirements.txt ด้วย Flask และ Gunicorn
cat << 'EOF' > requirements.txt
Flask==3.0.0
gunicorn==21.2.0
EOF
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ main.py ที่แสดงปลายทาง /ping ซึ่งแสดงผลชื่อบริการ Cloud Run จากตัวแปรสภาพแวดล้อม K_SERVICE
cat << 'EOF' > main.py
import os
from flask import Flask

app = Flask(__name__)

@app.route("/ping")
def ping():
    service_name = os.environ.get("K_SERVICE", "unknown")
    return f"Hello from {service_name}"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
EOF
  1. ติดตั้งใช้งาน destination จากแหล่งที่มาด้วย --no-allow-unauthenticated เพื่อให้ Cloud Run ปฏิเสธคำขอที่ไม่ได้รับการตรวจสอบสิทธิ์ โดยทำดังนี้
gcloud run deploy destination \
    --source . \
    --region="$REGION" \
    --service-account="destination-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --no-allow-unauthenticated
  1. จัดเก็บ URL เชิงกำหนดและ URI ของทรัพยากรแบบเต็มของบริการปลายทางในตัวแปรสภาพแวดล้อม ดังนี้
export DESTINATION_URL="https://destination-${PROJECT_NUMBER}.${REGION}.run.app"
export DESTINATION_URI="//run.googleapis.com/projects/${PROJECT_NUMBER}/locations/${REGION}/services/destination"
echo "DESTINATION_URL: $DESTINATION_URL"
echo "DESTINATION_URI: $DESTINATION_URI"
  1. ให้สิทธิ์บัญชีผู้ใช้ของคุณมีบทบาทผู้เรียกใช้ Cloud Run (roles/run.invoker) ใน destination เพื่อให้คุณทดสอบได้โดยตรงจาก Cloud Shell โดยทำดังนี้
gcloud run services add-iam-policy-binding destination \
    --member="user:$ACTIVE_ACCOUNT" \
    --role="roles/run.invoker" \
    --region="$REGION"
  1. ส่งคำขอที่ตรวจสอบสิทธิ์แล้วไปยัง /ping โดยใช้โทเค็นข้อมูลประจำตัวของคุณเองเพื่อยืนยันว่า destination กำลังทำงานอยู่
TOKEN=$(gcloud auth print-identity-token)
curl -H "Authorization: Bearer $TOKEN" "$DESTINATION_URL/ping"

คุณควรเห็นการตอบกลับต่อไปนี้

Hello from destination

4. โทรหาปลายทางโดยไม่มี Express Link

ก่อนที่จะเปิดตัว Express Link ให้สร้าง caller-oidc เพื่อดูว่าการตรวจสอบสิทธิ์ระหว่างบริการต้องใช้อะไรบ้างในปัจจุบัน และยืนยันว่าบริการที่เรียกใช้สามารถเข้าถึง destination ได้ หากต้องการโทรหา destination อย่างปลอดภัยโดยไม่ใช้ Express Link คุณต้องกำหนดค่า 2 ชั้น ดังนี้

  • การให้สิทธิ์ IAM: มอบบทบาท roles/run.invoker ใน destination ให้กับบัญชีบริการ caller-oidc
  • การแทรกโทเค็นระดับแอปพลิเคชัน: เพิ่มไลบรารี google-auth ลงใน caller-oidc และแยกวิเคราะห์ URL เป้าหมายเพื่อดึงข้อมูลกลุ่มเป้าหมาย OIDC ระดับรูท จากนั้นดึงข้อมูลโทเค็นระบุตัวตนที่ลงชื่อแล้วจากเซิร์ฟเวอร์ข้อมูลเมตา และแนบ Authorization: Bearer ID_TOKEN ไปกับคำขอ HTTPS ขาออก

ทำตามขั้นตอนต่อไปนี้เพื่อกำหนดค่าและทำให้ caller-oidc ใช้งานได้

  1. สร้างบัญชีบริการเฉพาะสำหรับ caller-oidc โดยทำดังนี้
gcloud iam service-accounts create caller-oidc-identity \
    --display-name="Cloud Run OIDC caller service account"
  1. ให้สิทธิ์ caller-oidc-identity เรียกใช้ destination:
gcloud run services add-iam-policy-binding destination \
    --member="serviceAccount:caller-oidc-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/run.invoker" \
    --region="$REGION"
  1. สร้างไดเรกทอรี caller-oidc ควบคู่ไปกับ destination:
mkdir -p ~/express-link-codelab/caller-oidc
cd ~/express-link-codelab/caller-oidc
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ requirements.txt โปรดทราบว่าคุณต้องเพิ่ม google-auth โดยเฉพาะเพื่อจัดการการดึงข้อมูลโทเค็นในคอนเทนเนอร์แอปพลิเคชัน
cat << 'EOF' > requirements.txt
Flask==3.0.0
google-auth==2.23.0
requests==2.31.0
gunicorn==21.2.0
EOF
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ main.py ที่มีการแทรกโทเค็น OIDC ด้วยตนเอง
cat << 'EOF' > main.py
import os
from urllib.parse import urlparse
from flask import Flask, request
from google.auth.transport.requests import Request as GoogleAuthRequest
from google.oauth2 import id_token
import requests

app = Flask(__name__)

@app.route("/ping")
def ping():
    service_name = os.environ.get("K_SERVICE", "unknown")
    return f"Hello from {service_name}"

@app.route("/")
def call_destination():
    target_url = request.args.get("target_url")
    if not target_url:
        return "Error: target_url query parameter not set.", 400

    try:
        # 1. Extract the root service URL (scheme + host) for the OIDC audience claim
        parsed_url = urlparse(target_url)
        target_audience = f"{parsed_url.scheme}://{parsed_url.netloc}"

        # 2. Fetch a Google-signed OIDC ID token from the metadata server
        auth_req = GoogleAuthRequest()
        token = id_token.fetch_id_token(auth_req, target_audience)

        # 3. Inject the token as an HTTP Bearer header
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(target_url, headers=headers, timeout=10)
        return f"I called the destination and it replied: '{response.text}'"
    except Exception as e:
        return f"Error calling destination: {str(e)}", 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
EOF
  1. ติดตั้งใช้งาน caller-oidc ใน Cloud Run:
gcloud run deploy caller-oidc \
    --source . \
    --region="$REGION" \
    --service-account="caller-oidc-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --no-allow-unauthenticated
  1. ให้สิทธิ์บัญชีผู้ใช้ในการเรียกใช้ caller-oidc โดยทำดังนี้
gcloud run services add-iam-policy-binding caller-oidc \
    --member="user:$ACTIVE_ACCOUNT" \
    --role="roles/run.invoker" \
    --region="$REGION"
  1. จัดเก็บ URL เชิงกำหนดของ caller-oidc และเรียกใช้โดยส่ง HTTPS URL $DESTINATION_URL/ping เป็น target_url:
export CALLER_OIDC_URL="https://caller-oidc-${PROJECT_NUMBER}.${REGION}.run.app"
TOKEN=$(gcloud auth print-identity-token)
curl -H "Authorization: Bearer $TOKEN" "$CALLER_OIDC_URL/?target_url=$DESTINATION_URL/ping"

คุณควรได้รับการตอบกลับที่สำเร็จจาก destination ดังนี้

I called the destination and it replied: 'Hello from destination'

5. ติดตั้งใช้งานบริการผู้โทรสำหรับ Express Link

ตอนนี้คุณจะสร้างบริการผู้โทรที่ 2 ชื่อ caller-express-link ในไดเรกทอรีข้างเคียง เมื่อใช้ Express Link caller-express-link จะไม่ต้องใช้โค้ดแทรกโทเค็น เนื่องจากจะเรียกใช้ requests.get(target_url, timeout=10) โดยตรง

  1. สร้างบัญชีบริการเฉพาะสำหรับ caller-express-link โดยทำดังนี้
gcloud iam service-accounts create caller-express-link-identity \
    --display-name="Cloud Run Express Link caller service account"
  1. ให้สิทธิ์ caller-express-link-identity ในบทบาท roles/trafficdirector.client เพื่อให้ไฟล์ช่วยเหลือ Ambient Networking ที่มีการจัดการสามารถดาวน์โหลดการกำหนดค่าการกำหนดเส้นทางและการรักษาความปลอดภัยของ Express Link ได้ นอกจากนี้ ให้สิทธิ์ roles/cloudtrace.agent เพื่อให้ Sidecar ส่งออกช่วงการติดตามได้
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
    --member="serviceAccount:caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/trafficdirector.client"

gcloud projects add-iam-policy-binding "$PROJECT_ID" \
    --member="serviceAccount:caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/cloudtrace.agent"
  1. สร้างไดเรกทอรี caller-express-link ควบคู่ไปกับ caller-oidc:
mkdir -p ~/express-link-codelab/caller-express-link
cd ~/express-link-codelab/caller-express-link
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ requirements.txt โดยไม่มี google-auth
cat << 'EOF' > requirements.txt
Flask==3.0.0
requests==2.31.0
gunicorn==21.2.0
EOF
  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างไฟล์ main.py ที่มีการเรียก requests.get(target_url, timeout=10) แบบธรรมดาและไม่มีตรรกะการแทรกโทเค็น
cat << 'EOF' > main.py
import os
from flask import Flask, request
import requests

app = Flask(__name__)

@app.route("/ping")
def ping():
    service_name = os.environ.get("K_SERVICE", "unknown")
    return f"Hello from {service_name}"

@app.route("/")
def call_destination():
    target_url = request.args.get("target_url")
    if not target_url:
        return "Error: target_url query parameter not set.", 400

    try:
        response = requests.get(target_url, timeout=10)
        return f"I called the destination and it replied: '{response.text}'"
    except Exception as e:
        return f"Error calling destination: {str(e)}", 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
EOF
  1. เรียกใช้ diff เทียบกับ ../caller-oidc/main.py เพื่อดูต้นแบบการตรวจสอบสิทธิ์ที่คุณกำจัดไป
diff -u ../caller-oidc/main.py main.py || true
  1. ติดตั้งใช้งาน caller-express-link โดยใช้ gcloud beta run deploy ที่เปิดใช้ --ambient-networking และ --ambient-scope=global:
gcloud beta run deploy caller-express-link \
    --source . \
    --region="$REGION" \
    --service-account="caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --no-allow-unauthenticated \
    --ambient-networking \
    --ambient-scope=global
  1. ตรวจสอบว่า Ambient Networking ทำงานอยู่ใน caller-express-link โดยทำดังนี้
gcloud run services describe caller-express-link --region="$REGION"

ที่ส่วนท้ายของสรุปการแก้ไข ให้ตรวจสอบว่า Ambient Networking และ Ambient Scope ปรากฏขึ้น

  Ambient Networking: Enabled
  Ambient Scope:     global
  1. ให้สิทธิ์บัญชีผู้ใช้ในการเรียกใช้ caller-express-link และให้สิทธิ์ caller-express-link-identity ในการเรียกใช้ destination
gcloud run services add-iam-policy-binding caller-express-link \
    --member="user:$ACTIVE_ACCOUNT" \
    --role="roles/run.invoker" \
    --region="$REGION"

gcloud run services add-iam-policy-binding destination \
    --member="serviceAccount:caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/run.invoker" \
    --region="$REGION"
  1. จัดเก็บ URL และ URI ของทรัพยากรของ caller-express-link พร้อมกับ URL เชิงกำหนด HTTP (http://) ของ destination ดังนี้
export CALLER_EL_URL="https://caller-express-link-${PROJECT_NUMBER}.${REGION}.run.app"
export CALLER_EL_URI="//run.googleapis.com/projects/${PROJECT_NUMBER}/locations/${REGION}/services/caller-express-link"
export DESTINATION_HTTP_URL="http://destination-${PROJECT_NUMBER}.${REGION}.run.app"
echo "CALLER_EL_URL: $CALLER_EL_URL"
echo "CALLER_EL_URI: $CALLER_EL_URI"
echo "DESTINATION_HTTP_URL: $DESTINATION_HTTP_URL"
  1. ทดสอบการโทรหา destination ผ่าน caller-express-link ก่อนสร้าง Express Link โดยทำดังนี้
TOKEN=$(gcloud auth print-identity-token)
curl -H "Authorization: Bearer $TOKEN" "$CALLER_EL_URL/?target_url=$DESTINATION_HTTP_URL/ping"

ตามที่คาดไว้ การเรียกใช้จะล้มเหลวพร้อมข้อความ 403 Forbidden เนื่องจากยังไม่มี Express Link ที่จะสกัดกั้นคำขอและแทรกข้อมูลเข้าสู่ระบบ

I called the destination and it replied: '
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>403 Forbidden</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Forbidden</h1>
<h2>Your client does not have permission to get URL <code>/ping</code> from this server.</h2>
<h2></h2>
</body></html>
'

6. เชื่อมต่อบริการด้วย Express Link

ตอนนี้คุณจะประกาศทรัพยากร ExpressLink ที่เชื่อมต่อ caller-express-link (บริการต้นทาง) กับ destination โดยไม่ต้องเปลี่ยนโค้ดแม้แต่บรรทัดเดียวหรือทำให้บริการ Cloud Run ใช้งานได้อีกครั้ง

  1. สร้างทรัพยากร Express Link ในตำแหน่ง global ดังนี้
gcloud beta network-services express-links create caller-to-destination \
    --location=global \
    --description="Express Link from caller-express-link to destination" \
    --source-services="$CALLER_EL_URI" \
    --destination-services="$DESTINATION_URI"
  1. แสดงรายการ Express Link เพื่อยืนยันว่ามีทรัพยากรอยู่
gcloud beta network-services express-links list --location=global

เอาต์พุตจากคำสั่ง:

NAME
caller-to-destination
  1. ตรวจสอบรายละเอียดการกำหนดค่า Express Link ดังนี้
gcloud beta network-services express-links describe caller-to-destination --location=global

เอาต์พุตจากคำสั่ง:

createTime: '2026-10-27T19:30:04.492105068Z'
description: Express Link from caller-express-link to destination
destination:
  services:
  - //run.googleapis.com/projects/PROJECT_NUMBER/locations/us-west1/services/destination
etag: ...
name: projects/PROJECT_ID/locations/global/expressLinks/caller-to-destination
source:
  services:
  - //run.googleapis.com/projects/PROJECT_NUMBER/locations/us-west1/services/caller-express-link
updateTime: '2026-10-27T19:30:11.309054865Z'
  1. เรียกใช้คำขอ curl เดียวกันกับ caller-express-link อีกครั้ง
TOKEN=$(gcloud auth print-identity-token)
curl -H "Authorization: Bearer $TOKEN" "$CALLER_EL_URL/?target_url=$DESTINATION_HTTP_URL/ping"

ครั้งนี้ ไซด์คาร์ Ambient จะสกัดกั้นคำขอ HTTP ไปยัง destination, แทรกโทเค็นข้อมูลประจำตัวสำหรับ caller-express-link-identity, อัปเกรดการเชื่อมต่อเป็น TLS และแสดงผลการตอบกลับของปลายทาง ดังนี้

I called the destination and it replied: 'Hello from destination'

ตารางต่อไปนี้สรุปสิ่งที่เปลี่ยนแปลงระหว่าง caller-oidc และ caller-express-link

มิติข้อมูล

ไม่มี Express Link (caller-oidc)

ด้วย Express Link (caller-express-link)

ไลบรารีการตรวจสอบสิทธิ์ใน requirements.txt

ต้องมี google-auth

ไม่มี (Flask, requests, gunicorn เท่านั้น)

สายโทรออกใน main.py

แยกวิเคราะห์ URL ของกลุ่มเป้าหมาย, เรียกใช้ id_token.fetch_id_token(), แนบส่วนหัว Authorization: Bearer

ธรรมดา requests.get(target_url, timeout=10)

URL Scheme เป้าหมายในแอป

https://destination-PROJECT_NUMBER.REGION.run.app

http://destination-PROJECT_NUMBER.REGION.run.app (อัปเกรดเป็น TLS โดยไฟล์ช่วยเหลือ Ambient)

การกำหนดค่า Cloud Run ของผู้โทร

การติดตั้งใช้งานแบบมาตรฐาน

--ambient-networking --ambient-scope=global + roles/trafficdirector.client ในบัญชีบริการของผู้โทร

การกำหนดค่า Cloud Run ปลายทาง

--no-allow-unauthenticated + roles/run.invoker

ไม่เปลี่ยนแปลง (--no-allow-unauthenticated + roles/run.invoker)

7. ล้างข้อมูล

โปรดลบทรัพยากรที่สร้างขึ้นระหว่างทำ Codelab นี้เพื่อหลีกเลี่ยงไม่ให้มีการเรียกเก็บเงินอย่างต่อเนื่องในบัญชี Google Cloud

  1. ลบแหล่งข้อมูล Express Link โดยทำดังนี้
gcloud beta network-services express-links delete caller-to-destination --location=global --quiet
  1. ลบบริการ Cloud Run 3 รายการต่อไปนี้
gcloud run services delete caller-express-link --region="$REGION" --quiet
gcloud run services delete caller-oidc --region="$REGION" --quiet
gcloud run services delete destination --region="$REGION" --quiet
  1. นำการเชื่อมโยงนโยบาย IAM ระดับโปรเจ็กต์ออกและลบบัญชีบริการ 3 บัญชีต่อไปนี้
gcloud projects remove-iam-policy-binding "$PROJECT_ID" \
    --member="serviceAccount:caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/trafficdirector.client" \
    --quiet

gcloud projects remove-iam-policy-binding "$PROJECT_ID" \
    --member="serviceAccount:caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" \
    --role="roles/cloudtrace.agent" \
    --quiet

gcloud iam service-accounts delete "caller-express-link-identity@${PROJECT_ID}.iam.gserviceaccount.com" --quiet
gcloud iam service-accounts delete "caller-oidc-identity@${PROJECT_ID}.iam.gserviceaccount.com" --quiet
gcloud iam service-accounts delete "destination-identity@${PROJECT_ID}.iam.gserviceaccount.com" --quiet
  1. (ไม่บังคับ) หากคุณสร้างโปรเจ็กต์ Google Cloud โดยเฉพาะสำหรับ Codelab นี้เท่านั้น คุณสามารถลบทั้งโปรเจ็กต์ได้โดยทำดังนี้
# Run only if you want to delete the entire project and all its resources
gcloud projects delete "$PROJECT_ID"

8. ขอแสดงความยินดี

คุณได้ติดตั้งใช้งานบริการแบ็กเอนด์ Cloud Run ส่วนตัวและเรียกใช้บริการดังกล่าวโดยใช้การแทรกโทเค็น OIDC ด้วยตนเองใน Python จากนั้นคุณแทนที่โค้ดการตรวจสอบสิทธิ์ระดับแอปพลิเคชันทั้งหมดด้วย Cloud Run Express Link แบบประกาศ

สิ่งที่คุณได้เรียนรู้

  • เหตุใดการแทรกโทเค็น OIDC ด้วยตนเองจึงเชื่อมโยงตรรกะทางธุรกิจของแอปพลิเคชันกับไลบรารีการตรวจสอบสิทธิ์และโหมดความปลอดภัยของแพลตฟอร์ม
  • วิธีติดตั้งใช้งานบริการปลายทาง Cloud Run ที่ตรวจสอบสิทธิ์แล้วซึ่งทำงานได้โดยไม่มีการเปลี่ยนแปลงไม่ว่าจะใช้หรือไม่ใช้ Express Link
  • วิธีเปิดใช้ Ambient Networking (--ambient-networking ด้วย --ambient-scope=global) และให้สิทธิ์ไคลเอ็นต์ Traffic Director (roles/trafficdirector.client) ในบริการ Cloud Run ที่เรียกใช้
  • วิธีสร้างและตรวจสอบทรัพยากร ExpressLink ของ Network Services ที่แทรกโทเค็นข้อมูลระบุตัวตนและอัปเกรดการเรียก http:// แบบข้อความธรรมดาจากคอนเทนเนอร์เป็นการเชื่อมต่อ TLS ที่เข้ารหัสโดยอัตโนมัติ

ขั้นตอนถัดไป