Identity and Access Management로 보호되는 Cloud Run 서비스를 호출하는 클라이언트는 수신 서비스의 *.run.app URL과 일치하는 대상 클레임이 포함된 유효한 ID 토큰을 제공해야 합니다. 이 URL을 모르는 클라이언트의 경우 커스텀 대상 값을 사용하면 됩니다.
커스텀 대상 이해
Cloud Run은 IAM으로 액세스 제어를 지원하는 호출자(roles/run.invoker) 역할을 제공합니다.
IAM 액세스 제어는 JSON 웹 토큰(JWT)으로 패키징된 Google 서명 ID 토큰을 사용합니다.
이러한 토큰의 콘텐츠는 OIDC 표준을 준수합니다.
대상 필드는 토큰을 사용할 수 있는 의도된 타겟을 지정하기 위해 토큰에 인코딩됩니다.
이렇게 하면 한 서비스에 사용하도록 의도된 가로채인 토큰이 다른 서비스에서 재실행되는 재생 공격의 위험이 제한됩니다.
규칙에 따라 이 대상은 타겟 서비스의 전체 URL입니다. 기본적으로 Cloud Run에서 run.app으로 끝나는 서비스의 Google 생성 URL입니다.
그러나 다음 시나리오와 같이 Cloud Run 서비스는 기본 생성된 URL이 아닌 URL 뒤에 있을 수 있습니다.
클라이언트가 Google에서 생성한 URL을 인식하지 못하는 서비스에 연결하기 위해 커스텀 도메인을 사용하는 경우
클라이언트에서 요청이 도달할 리전 서비스를 예측할 수 없는 부하 분산기 뒤에서 여러 서비스를 배포하는 경우.
Google에서 생성한 서비스 URL은 서비스 이름이 동일하더라도 리전에 따라 달라집니다.
이러한 시나리오에서는 클라이언트가 알고 있는 추가 타겟을 허용하는 커스텀 대상 값을 허용하도록 서비스를 구성해야 합니다. Google에서 생성한 기본 URL은 항상 허용되는 대상 값으로 유지됩니다.
커스텀 대상 설정 및 업데이트
Cloud Run의 커스텀 대상 설정은 서비스 수준에서 수행되며 IAM 승인 멤버십과 마찬가지로 모든 제공 버전에 적용됩니다.
대상의 JSON 인코딩이 문자열 목록으로 32,768자를 초과하지 않는 한 여러 커스텀 대상을 설정할 수 있습니다.
구성을 변경하면 새 버전이 생성됩니다. 이를 변경하는 명시적 업데이트가 없으면 이후 버전에도 이 구성 설정이 자동으로 적용됩니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-08-23(UTC)"],[],[],null,["# Set custom audiences for services\n\nThis page describes how to use custom audiences for authorization.\n\nClients that call an Identity and Access Management-protected Cloud Run service\nmust provide a valid ID token that includes an\n[audience claim](/docs/authentication/token-types#id-aud) matching the receiving\nservice's `*.run.app` URL. For clients that don't know this URL, you can use a\ncustom audience value.\n\nUnderstanding custom audiences\n------------------------------\n\nCloud Run provides an Invoker (`roles/run.invoker`) role to support\n[access control with IAM](/run/docs/securing/managing-access).\nIAM access control makes use of Google-signed ID tokens,\nwhich are packaged as [JSON Web Tokens (JWTs)](https://en.wikipedia.org/wiki/JSON_Web_Token).\nThe contents of these tokens conform to an OIDC standard.\n\nAn audience field is encoded in the token to specify the intended target that\ncan use the token.\nThis limits the risk of a replay attack, where an intercepted token intended\nfor use with one service is replayed against a different service.\n\nBy convention, the audience is the full URL of the target service. By default in\nCloud Run, this is the Google-generated URL for a service ending in\n`run.app`.\n\nHowever, a Cloud Run service might sit behind a URL other than the\ndefault-generated URL, such as in the following scenarios:\n\n- When using a [custom domain](/run/docs/mapping-custom-domains) to reach a service where the client is unaware of the Google-generated URL.\n- When deploying multiple services [behind a load balancer](/load-balancing/docs/https/setting-up-https-serverless) where a client can't anticipate which regional service a request reaches. Google-generated URLs for services are region-specific even if the service name is the same.\n\nIn these scenarios, you must configure a service to accept custom\naudience values that allow additional targets known by a client. The default\nGoogle-generated URL always remains as an accepted audience value.\n\nSet and update custom audiences\n-------------------------------\n\nSetting custom audiences for Cloud Run is done at the service level\nand applies to all serving revisions, similar to IAM\nauthorization membership.\n\nYou can set multiple custom audiences, as long as JSON-encoding of\nthe audiences as a string list does not exceed 32,768 characters.\n\nAny configuration change leads to the\ncreation of a new revision. Subsequent revisions will also automatically get\nthis configuration setting unless you make explicit updates to change it. \n\n### gcloud\n\nYou can set custom audiences on a service by using the following command: \n\n```bash\ngcloud run services update SERVICE --add-custom-audiences=AUDIENCE\n```\n\nReplace\n\n- \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service\n- \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice` or `https://myservice.example.com`\n\nYou can remove all custom audiences from a service by using the following command: \n\n```bash\ngcloud run services update SERVICE --clear-custom-audiences\n```\n\n### YAML\n\n1. If you are creating a new service, skip this step.\n If you are updating an existing service, download its [YAML configuration](/run/docs/reference/yaml/v1):\n\n ```bash\n gcloud run services describe SERVICE --format export \u003e service.yaml\n ```\n2. Set the `run.googleapis.com/custom-audiences` annotation on the Service\n metadata (**not** on the `template` metadata):\n\n ```yaml\n apiVersion: serving.knative.dev/v1\n kind: Service\n metadata:\n name: SERVICE\n annotations:\n run.googleapis.com/custom-audiences: '[\"\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e\"]'\n spec:\n template:\n ...\n ```\n\n Replace\n - \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service\n - \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice` or `https://myservice.example.com`\n\n Note that the value of the attribute is a quoted JSON array of strings,\n requiring the use of both double and single quotes.\n3. Replace the service with its new configuration by using the following\n command:\n\n ```bash\n gcloud run services replace service.yaml\n ```\n\n### Terraform\n\n\u003cbr /\u003e\n\nTo learn how to apply or remove a Terraform configuration, see\n[Basic Terraform commands](/docs/terraform/basic-commands).\nAdd the following to a [`google_cloud_run_v2_service`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service) resource in your Terraform configuration: \n\n\u003cbr /\u003e\n\n resource \"google_cloud_run_v2_service\" \"default\" {\n name = \"\u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e\"\n location = \"\u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e\"\n\n custom_audiences = [\"\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e\"]\n template {\n containers {\n image = \"us-docker.pkg.dev/cloudrun/container/hello\"\n }\n }\n }\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e with the name of your Cloud Run service.\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e with the Google Cloud region. For example, `europe-west1`.\n- \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with a string for the custom audience you want to support, for example, `myservice`.\n\nVerifying custom audiences\n--------------------------\n\n1. Get an ID token for a service account which has IAM permission\n to invoke the service. Note the use of the custom audience \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e.\n\n ```bash\n export TOKEN=$(gcloud auth print-identity-token --impersonate-service-account SERVICE_ACCOUNT_EMAIL --audiences='\u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e')\n ```\n\n Replace:\n - \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT_EMAIL\u003c/var\u003e with the email of the service account. It ends with `.iam.gserviceaccount.com`.\n - \u003cvar translate=\"no\"\u003eAUDIENCE\u003c/var\u003e with the custom audience value that you set on the service.\n2. Call the endpoint of the service with that ID token\n\n ```bash\n curl -H \"Authorization: Bearer ${TOKEN}\" ENDPOINT\n ```\n\n Replace \u003cvar translate=\"no\"\u003eENDPOINT\u003c/var\u003e with the endpoint to reach your service, for\n example its custom domain or `.run.app` URL.\n3. Confirm that the request is authorized and you see the expected response of\n your service."]]