Cloud KMS로 암호화 키 만들기

이 빠른 시작에서는 소유한 프로젝트에서 Cloud Key Management Service로 암호화 키를 만들고 사용하는 방법을 보여줍니다. 이 안내에서는Google Cloud 콘솔을 사용하여 Cloud KMS에 키링, 키, 키 버전을 만듭니다. 다른 방법을 사용하는 안내는 Autokey 개요, 키링 만들기, 키 만들기를 참고하세요.

이 빠른 시작 가이드에서는 명령줄을 사용하여 Cloud KMS API에 요청을 보냅니다. 클라이언트 라이브러리를 사용하여 요청을 Cloud KMS API에 보내는 프로그래밍 예는 암호화 및 복호화를 참조하세요.

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  4. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud KMS API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudkms.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudkms.admin, roles/cloudkms.cryptoKeyEncrypterDecrypter, roles/servicemanagement.serviceConsumer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

  10. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  11. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. Verify that billing is enabled for your Google Cloud project.

  14. Enable the Cloud KMS API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudkms.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudkms.admin, roles/cloudkms.cryptoKeyEncrypterDecrypter, roles/servicemanagement.serviceConsumer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  16. 키링 및 키

    콘텐츠를 암호화하고 복호화하려면 키링의 일부인 Cloud KMS 키가 필요합니다.

    test라는 키링과 quickstart라는 키를 만듭니다. 이러한 객체에 대한 자세한 내용과 이러한 객체들이 어떻게 관련되어 있는지는 객체 계층 구조 개요를 참조하세요.

    gcloud kms keyrings create "test" \
        --location "global"
    gcloud kms keys create "quickstart" \
        --location "global" \
        --keyring "test" \
        --purpose "encryption"

    list 옵션을 사용하면 방금 만든 키의 이름과 메타데이터를 볼 수 있습니다.

    gcloud kms keys list \
        --location "global" \
        --keyring "test"

    다음과 같이 표시됩니다.

    NAME                                                                      PURPOSE          PRIMARY_STATE
    projects/PROJECT_ID/locations/global/keyRings/test/cryptoKeys/quickstart  ENCRYPT_DECRYPT  ENABLED
    

    데이터 암호화

    이제 키가 있으므로 해당 키를 사용하여 텍스트 또는 바이너리 콘텐츠를 암호화할 수 있습니다.

    암호화할 일부 텍스트를 'mysecret.txt'라는 파일에 저장합니다.

    echo -n "Some text to be encrypted" > mysecret.txt

    gcloud kms encrypt로 데이터를 암호화하려면 키 정보를 제공하고, 암호화할 일반 텍스트 파일의 이름을 지정하고, 암호화된 콘텐츠가 포함될 파일의 이름을 지정합니다.

    gcloud kms encrypt \
        --location "global" \
        --keyring "test" \
        --key "quickstart" \
        --plaintext-file ./mysecret.txt \
        --ciphertext-file ./mysecret.txt.encrypted

    encrypt 메서드는 암호화된 콘텐츠를 --ciphertext-file 플래그에서 지정된 파일에 저장합니다.

    암호문 복호화

    gcloud kms decrypt로 데이터를 복호화하려면 키 정보를 제공하고, 암호화된 파일(암호문 파일)의 이름을 지정하고, 복호화된 콘텐츠가 포함될 파일의 이름을 지정합니다.

    gcloud kms decrypt \
        --location "global" \
        --keyring "test" \
        --key "quickstart" \
        --ciphertext-file ./mysecret.txt.encrypted \
        --plaintext-file ./mysecret.txt.decrypted

    decrypt 메서드는 복호화된 콘텐츠를 --plaintext-file 플래그에서 지정된 파일에 저장합니다.

    암호화된 콘텐츠를 복호화하려면 콘텐츠 암호화에 사용한 키와 동일한 키를 사용해야 합니다.

    삭제

    이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 Google Cloud 프로젝트를 삭제하세요.

    키에 사용할 수 있는 버전을 나열합니다.

    gcloud kms keys versions list \
        --location "global" \
        --keyring "test" \
        --key "quickstart"

    버전을 폐기하려면 다음 명령어를 실행합니다. 여기서 1는 폐기할 키 버전의 번호입니다.

    gcloud kms keys versions destroy 1 \
        --location "global" \
        --keyring "test" \
        --key "quickstart"
    

    다음 단계