서비스 계정 사용 중지 및 사용 설정

이 페이지에서는 Identity and Access Management(IAM) API, Trusted Cloud 콘솔, gcloud CLI를 사용하여 서비스 계정을 사용 중지하고 사용 설정하는 방법을 설명합니다.

시작하기 전에

  • Enable the IAM API.

    Enable the API

  • 인증을 설정합니다.

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.

    gcloud

    Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다. 로그인한 후 다음 명령어를 실행하여 Google Cloud CLI를 초기화합니다.

    gcloud init

    C#

    로컬 개발 환경에서 이 페이지의 .NET 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Trusted Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    C++

    로컬 개발 환경에서 이 페이지의 C++ 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Trusted Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    Go

    로컬 개발 환경에서 이 페이지의 Go 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Trusted Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    Java

    로컬 개발 환경에서 이 페이지의 Java 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Trusted Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    Python

    로컬 개발 환경에서 이 페이지의 Python 샘플을 사용하려면 gcloud CLI를 설치하고 초기화한 후 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

      Create local authentication credentials for your user account:

      gcloud auth application-default login

      If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    자세한 내용은 Trusted Cloud 인증 문서의 로컬 개발 환경의 ADC 설정을 참조하세요.

    REST

    로컬 개발 환경에서 이 페이지의 REST API 샘플을 사용하려면 gcloud CLI에 제공한 사용자 인증 정보를 사용합니다.

      Google Cloud CLI를 설치한 후 제휴 ID로 gcloud CLI에 로그인합니다.

    자세한 내용은 Trusted Cloud 인증 문서의 REST 사용을 위한 인증을 참조하세요.

  • IAM 서비스 계정 이해

필요한 역할

서비스 계정을 관리하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 서비스 계정 관리자(roles/iam.serviceAccountAdmin) IAM 역할을 부여해 달라고 요청합니다. 역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.

이 역할에 대한 자세한 내용은 서비스 계정 역할을 참조하세요.

IAM 기본 역할도 서비스 계정을 관리하는 권한을 포함하지만, 프로덕션 환경에서는 기본 역할을 부여하지 말아야 하지만 개발 환경 또는 테스트 환경에서는 부여해도 됩니다.

서비스 계정 사용 중지

서비스 계정 삭제와 마찬가지로 서비스 계정을 사용 중지하면 애플리케이션에서 더 이상 해당 서비스 계정을 통해 Trusted Cloud 리소스에 액세스할 수 없습니다. 기본 Compute Engine 서비스 계정을 사용 중지하면 인스턴스에서 더 이상 프로젝트의 리소스에 액세스할 수 없습니다. 이미 사용 중지된 서비스 계정을 중지하려 해도 아무런 영향이 없습니다.

서비스 계정을 삭제하는 것과 달리 사용 중지된 서비스 계정은 필요에 따라 쉽게 다시 사용 설정할 수 있습니다. 중요 애플리케이션에서 서비스 계정을 사용하지 않도록 서비스 계정을 삭제하기 전에 먼저 사용 중지하는 것이 좋습니다.

콘솔

  1. Trusted Cloud 콘솔에서 서비스 계정 페이지로 이동합니다.

    서비스 계정으로 이동

  2. 프로젝트를 선택합니다.

  3. 사용 중지할 서비스 계정의 이름을 클릭합니다.

  4. 서비스 계정 상태에서 서비스 계정 사용 중지를 클릭한 다음 사용 중지를 클릭하여 변경사항을 확인합니다.

gcloud

  1. In the Trusted Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. gcloud iam service-accounts disable 명령어를 실행하여 사용하여 서비스 계정을 사용 중지합니다.

    명령어:

    gcloud iam service-accounts disable SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com

    출력:

    Disabled service account SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
  3. C++

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM C++ API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    namespace iam = ::google::cloud::iam_admin_v1;
    [](std::string const& name) {
      iam::IAMClient client(iam::MakeIAMConnection());
      google::iam::admin::v1::DisableServiceAccountRequest request;
      request.set_name(name);
      auto response = client.DisableServiceAccount(request);
      if (!response.ok()) throw std::runtime_error(response.message());
      std::cout << "ServiceAccount successfully disabled.\n";
    }

    C#

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM C# API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    
    using System;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Iam.v1;
    using Google.Apis.Iam.v1.Data;
    
    public partial class ServiceAccounts
    {
        public static void DisableServiceAccount(string email)
        {
            var credential = GoogleCredential.GetApplicationDefault()
                .CreateScoped(IamService.Scope.CloudPlatform);
            var service = new IamService(new IamService.Initializer
            {
                HttpClientInitializer = credential
            });
    
            var request = new DisableServiceAccountRequest();
    
            string resource = "projects/-/serviceAccounts/" + email;
            service.Projects.ServiceAccounts.Disable(request, resource).Execute();
            Console.WriteLine("Disabled service account: " + email);
        }
    }

    Go

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Go API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import (
    	"context"
    	"fmt"
    	"io"
    
    	iam "google.golang.org/api/iam/v1"
    )
    
    // disableServiceAccount disables a service account.
    func disableServiceAccount(w io.Writer, email string) error {
    	// email:= service-account@your-project.iam.gserviceaccount.com
    	ctx := context.Background()
    	service, err := iam.NewService(ctx)
    	if err != nil {
    		return fmt.Errorf("iam.NewService: %w", err)
    	}
    
    	request := &iam.DisableServiceAccountRequest{}
    	_, err = service.Projects.ServiceAccounts.Disable("projects/-/serviceAccounts/"+email, request).Do()
    	if err != nil {
    		return fmt.Errorf("Projects.ServiceAccounts.Disable: %w", err)
    	}
    	fmt.Fprintf(w, "Disabled service account: %v", email)
    	return nil
    }
    

    Java

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Java API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import com.google.cloud.iam.admin.v1.IAMClient;
    import com.google.iam.admin.v1.DisableServiceAccountRequest;
    import java.io.IOException;
    
    public class DisableServiceAccount {
    
      public static void main(String[] args) throws IOException {
        // TODO(Developer): Replace the below variables before running.
        String projectId = "your-project-id";
        String serviceAccountName = "your-service-account-name";
    
        disableServiceAccount(projectId, serviceAccountName);
      }
    
      // Disables a service account.
      public static void disableServiceAccount(String projectId, String accountName)
              throws IOException {
        String email = String.format("%s@%s.iam.gserviceaccount.com", accountName, projectId);
    
        // Initialize client that will be used to send requests.
        // This client only needs to be created once, and can be reused for multiple requests.
        try (IAMClient iamClient = IAMClient.create()) {
          iamClient.disableServiceAccount(DisableServiceAccountRequest.newBuilder()
                  .setName(String.format("projects/%s/serviceAccounts/%s", projectId, email))
                  .build());
    
          System.out.println("Disabled service account: " + accountName);
        }
      }
    }

    Python

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Python API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import time
    
    from google.cloud import iam_admin_v1
    from google.cloud.iam_admin_v1 import types
    
    
    def disable_service_account(project_id: str, account: str) -> types.ServiceAccount:
        """Disables a service account.
    
        project_id: ID or number of the Google Cloud project you want to use.
        account: ID or email which is unique identifier of the service account.
        """
    
        iam_admin_client = iam_admin_v1.IAMClient()
        request = types.DisableServiceAccountRequest()
        name = f"projects/{project_id}/serviceAccounts/{account}"
        request.name = name
    
        iam_admin_client.disable_service_account(request=request)
        time.sleep(5)  # waiting to make sure changes applied
    
        get_request = types.GetServiceAccountRequest()
        get_request.name = name
    
        service_account = iam_admin_client.get_service_account(request=get_request)
        if service_account.disabled:
            print(f"Disabled service account: {account}")
        return service_account

    REST

    serviceAccounts.disable 메서드는 서비스 계정을 즉시 사용 중지합니다.

    요청 데이터를 사용하기 전에 다음을 바꿉니다.

    • PROJECT_ID: Trusted Cloud 프로젝트 ID. 프로젝트 ID는 my-project와 같은 영숫자 문자열입니다.
    • SA_ID: 서비스 계정의 ID입니다. SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com 형식의 서비스 계정 이메일 주소이거나 서비스 계정의 고유 숫자 ID일 수 있습니다.

    HTTP 메서드 및 URL:

    POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_ID:disable

    요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

    성공한 경우 응답 본문은 비어 있습니다.

서비스 계정 사용 설정

사용 중지된 서비스 계정을 사용 설정하면 애플리케이션에서 서비스 계정을 통해Trusted Cloud 리소스에 다시 액세스할 수 있습니다.

사용 중지된 서비스 계정을 필요할 때마다 사용 설정할 수 있습니다. 이미 사용 설정된 서비스 계정을 사용 설정하려 해도 아무런 영향이 없습니다.

콘솔

  1. Trusted Cloud 콘솔에서 서비스 계정 페이지로 이동합니다.

    서비스 계정으로 이동

  2. 프로젝트를 선택합니다.

  3. 사용 설정할 서비스 계정의 이름을 클릭합니다.

  4. 서비스 계정 상태에서 서비스 계정 사용 설정을 클릭한 다음 사용 설정을 클릭하여 변경사항을 확인합니다.

gcloud

  1. In the Trusted Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. gcloud iam service-accounts enable 명령어를 실행하여 서비스 계정을 사용 설정합니다.

    명령어:

    gcloud iam service-accounts enable SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com

    출력:

    Enabled service account SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
  3. C++

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM C++ API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    namespace iam = ::google::cloud::iam_admin_v1;
    [](std::string const& name) {
      iam::IAMClient client(iam::MakeIAMConnection());
      google::iam::admin::v1::EnableServiceAccountRequest request;
      request.set_name(name);
      auto response = client.EnableServiceAccount(request);
      if (!response.ok()) throw std::runtime_error(response.message());
      std::cout << "ServiceAccount successfully enabled.\n";
    }

    C#

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM C# API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    
    using System;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Iam.v1;
    using Google.Apis.Iam.v1.Data;
    
    public partial class ServiceAccounts
    {
        public static void EnableServiceAccount(string email)
        {
            var credential = GoogleCredential.GetApplicationDefault()
                .CreateScoped(IamService.Scope.CloudPlatform);
            var service = new IamService(new IamService.Initializer
            {
                HttpClientInitializer = credential
            });
    
            var request = new EnableServiceAccountRequest();
    
            string resource = "projects/-/serviceAccounts/" + email;
            service.Projects.ServiceAccounts.Enable(request, resource).Execute();
            Console.WriteLine("Enabled service account: " + email);
        }
    }

    Go

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Go API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import (
    	"context"
    	"fmt"
    	"io"
    
    	iam "google.golang.org/api/iam/v1"
    )
    
    // enableServiceAccount enables a service account.
    func enableServiceAccount(w io.Writer, email string) error {
    	// email:= service-account@your-project.iam.gserviceaccount.com
    	ctx := context.Background()
    	service, err := iam.NewService(ctx)
    	if err != nil {
    		return fmt.Errorf("iam.NewService: %w", err)
    	}
    
    	request := &iam.EnableServiceAccountRequest{}
    	_, err = service.Projects.ServiceAccounts.Enable("projects/-/serviceAccounts/"+email, request).Do()
    	if err != nil {
    		return fmt.Errorf("Projects.ServiceAccounts.Enable: %w", err)
    	}
    	fmt.Fprintf(w, "Enabled service account: %v", email)
    	return nil
    }
    

    Java

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Java API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import com.google.cloud.iam.admin.v1.IAMClient;
    import com.google.iam.admin.v1.EnableServiceAccountRequest;
    import java.io.IOException;
    
    
    public class EnableServiceAccount {
    
      public static void main(String[] args) throws IOException {
        // TODO(Developer): Replace the below variables before running.
        String projectId = "your-project-id";
        String serviceAccountName = "your-service-account-name";
    
        enableServiceAccount(projectId, serviceAccountName);
      }
    
      // Enables a service account.
      public static void enableServiceAccount(String projectId, String accountName)
              throws IOException {
        String email = String.format("%s@%s.iam.gserviceaccount.com", accountName, projectId);
    
        // Initialize client that will be used to send requests.
        // This client only needs to be created once, and can be reused for multiple requests.
        try (IAMClient iamClient = IAMClient.create()) {
          iamClient.enableServiceAccount(EnableServiceAccountRequest.newBuilder()
                  .setName(String.format("projects/%s/serviceAccounts/%s", projectId, email))
                  .build());
    
          System.out.println("Enabled service account: " + email);
        }
      }
    }

    Python

    IAM용 클라이언트 라이브러리를 설치하고 사용하는 방법은 IAM 클라이언트 라이브러리를 참조하세요. 자세한 내용은 IAM Python API 참고 문서를 참조하세요.

    IAM에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 시작하기 전에를 참조하세요.

    코드 샘플을 실행하기 전에 GOOGLE_CLOUD_UNIVERSE_DOMAIN 환경 변수를 s3nsapis.fr으로 설정합니다.

    import time
    
    from google.cloud import iam_admin_v1
    from google.cloud.iam_admin_v1 import types
    
    
    def enable_service_account(project_id: str, account: str) -> types.ServiceAccount:
        """Enables a service account.
    
        project_id: ID or number of the Google Cloud project you want to use.
        account: ID or email which is unique identifier of the service account.
        """
    
        iam_admin_client = iam_admin_v1.IAMClient()
        request = types.EnableServiceAccountRequest()
        name = f"projects/{project_id}/serviceAccounts/{account}"
        request.name = name
    
        iam_admin_client.enable_service_account(request=request)
        time.sleep(5)  # waiting to make sure changes applied
    
        get_request = types.GetServiceAccountRequest()
        get_request.name = name
    
        service_account = iam_admin_client.get_service_account(request=get_request)
        if not service_account.disabled:
            print(f"Enabled service account: {account}")
        return service_account

    REST

    serviceAccounts.enable 메서드는 이전에 사용 중지된 서비스 계정을 사용 설정합니다.

    요청 데이터를 사용하기 전에 다음을 바꿉니다.

    • PROJECT_ID: Trusted Cloud 프로젝트 ID. 프로젝트 ID는 my-project와 같은 영숫자 문자열입니다.
    • SA_ID: 서비스 계정의 ID입니다. SA_NAME@PROJECT_ID.s3ns-system.iam.gserviceaccount.com 형식의 서비스 계정 이메일 주소이거나 서비스 계정의 고유 숫자 ID일 수 있습니다.

    HTTP 메서드 및 URL:

    POST https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts/SA_ID:enable

    요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

    성공한 경우 응답 본문은 비어 있습니다.

다음 단계