역할 binding에 주 구성원 추가
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
IAM 정책의 기존 역할 binding에 주 구성원을 추가하는 방법을 설명합니다.
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code examples demonstrating how to add a principal (member) to an existing role binding within an Identity and Access Management (IAM) policy.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are available in C#, Go, Java, and Python, showcasing different approaches to modifying IAM policies.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves identifying the correct role binding and then adding the specified member to the list of members associated with that role.\u003c/p\u003e\n"],["\u003cp\u003eThe page also provides links to learn how to setup the IAM client libraries and authenticate to IAM using Application Default Credentials.\u003c/p\u003e\n"]]],[],null,["# Add a principal to a role binding\n\nDemonstrates adding a principal to an existing role binding in an IAM policy.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access)\n\nCode sample\n-----------\n\n### C#\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM C# API\nreference documentation](https://developers.google.com/api-client-library/dotnet/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n using System.Linq;\n using Google.Apis.CloudResourceManager.v1.Data;\n\n public partial class AccessManager\n {\n public static Policy AddMember(Policy policy, string role, string member)\n {\n var binding = policy.Bindings.First(x =\u003e x.Role == role);\n binding.Members.Add(member);\n return policy;\n }\n }\n\n### Go\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Go API\nreference documentation](https://godoc.org/google.golang.org/genproto/googleapis/iam/admin/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"fmt\"\n \t\"io\"\n\n \t\"google.golang.org/api/iam/v1\"\n )\n\n // addMember adds a member to a role binding.\n func addMember(w io.Writer, policy *iam.Policy, role, member string) {\n \tfor _, binding := range policy.Bindings {\n \t\tif binding.Role != role {\n \t\t\tcontinue\n \t\t}\n \t\tfor _, m := range binding.Members {\n \t\t\tif m != member {\n \t\t\t\tcontinue\n \t\t\t}\n \t\t\tfmt.Fprintf(w, \"Role %q found. Member already exists.\\n\", role)\n \t\t\treturn\n \t\t}\n \t\tbinding.Members = append(binding.Members, member)\n \t\tfmt.Fprintf(w, \"Role %q found. Member added.\\n\", role)\n \t\treturn\n \t}\n \tfmt.Fprintf(w, \"Role %q not found. Member not added.\\n\", role)\n }\n\n### Java\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Java API\nreference documentation](https://developers.google.com/api-client-library/java/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.iam.v1.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html;\n import com.google.iam.v1.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html;\n import java.util.ArrayList;\n import java.util.List;\n\n public class AddMember {\n public static void main(String[] args) {\n // TODO(developer): Replace the variables before running the sample.\n // TODO: Replace with your policy, GetPolicy.getPolicy(projectId, serviceAccount).\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html policy = https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html.newBuilder().build();\n // TODO: Replace with your role.\n String role = \"roles/existing-role\";\n // TODO: Replace with your principal.\n // For examples, see https://cloud.google.com/iam/docs/principal-identifiers\n String member = \"principal-id\";\n\n addMember(policy, role, member);\n }\n\n // Adds a principal to a pre-existing role.\n public static https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html addMember(https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html policy, String role, String member) {\n List\u003cBinding\u003e newBindingsList = new ArrayList\u003c\u003e();\n\n for (https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.html b : policy.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html#com_google_iam_v1_Policy_getBindingsList__()) {\n if (b.getRole().equals(role)) {\n newBindingsList.add(b.toBuilder().https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Binding.Builder.html#com_google_iam_v1_Binding_Builder_addMembers_java_lang_String_(member).build());\n } else {\n newBindingsList.add(b);\n }\n }\n\n // Update the policy to add the principal.\n https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html updatedPolicy = policy.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html#com_google_iam_v1_Policy_toBuilder__()\n .https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.Builder.html#com_google_iam_v1_Policy_Builder_clearBindings__()\n .https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.Builder.html#com_google_iam_v1_Policy_Builder_addAllBindings_java_lang_Iterable___extends_com_google_iam_v1_Binding__(newBindingsList)\n .build();\n\n System.out.println(\"Added principal: \" + updatedPolicy.https://cloud.google.com/java/docs/reference/proto-google-iam-v1/latest/com.google.iam.v1.Policy.html#com_google_iam_v1_Policy_getBindingsList__());\n\n return updatedPolicy;\n }\n }\n\n### Python\n\n\nTo learn how to install and use the client library for IAM, see\n[IAM client libraries](/iam/docs/reference/libraries).\n\n\nFor more information, see the\n[IAM Python API\nreference documentation](https://developers.google.com/api-client-library/python/apis/iam/v1).\n\n\nTo authenticate to IAM, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.iam.v1 import policy_pb2\n from snippets.get_policy import get_project_policy\n from snippets.set_policy import set_project_policy\n\n\n def modify_policy_add_principal(\n project_id: str, role: str, principal: str\n ) -\u003e policy_pb2.Policy:\n \"\"\"Add a principal to certain role in project policy.\n\n project_id: ID or number of the Google Cloud project you want to use.\n role: role to which principal need to be added.\n principal: The principal requesting access.\n\n For principal ID formats, see https://cloud.google.com/iam/docs/principal-identifiers\n \"\"\"\n policy = get_project_policy(project_id)\n\n for bind in policy.bindings:\n if bind.role == role:\n bind.members.append(principal)\n break\n\n return set_project_policy(project_id, policy)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=iam)."]]