Node.js を使用して機密テキストを検査する

機密データの保護の Cloud Data Loss Prevention API と Google Cloud CLI、Node.js を使用して、サンプル文字列の機密情報をスキャンする方法について説明します。


このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。

ガイドを表示


始める前に

  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.

    • 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 DLP API:

    gcloud services enable dlp.googleapis.com
  8. 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.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/dlp.user

    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.
  10. Install the Google Cloud CLI.

  11. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  12. gcloud CLI を初期化するには、次のコマンドを実行します。

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

    • 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.

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

  15. Enable the DLP API:

    gcloud services enable dlp.googleapis.com
  16. 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.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/dlp.user

    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.
  18. Node.js と NPM をインストールします。
  19. 機密データの保護 CLI アプリを設定する

    Node.js を使用して機密データの保護 CLI アプリを設定するには、次のようにします。

    1. Node.js DLP クライアント ライブラリ のクローンを作成します。

       git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
      
    2. dlp ディレクトリに移動します。

       cd nodejs-docs-samples/dlp
      
    3. アプリの依存関係をインストールします。

       npm install
      

    文字列に含まれる機密情報を検査する

    DLP API と inspectString Node.js スクリプトを使用してサンプルテキストをスキャンするには、次のコマンドを実行します。

      node inspectString.js PROJECT_ID "My email address is joe@example.com."
    

    出力は次のようになります。

      Findings:
          Info type: EMAIL_ADDRESS
          Likelihood: LIKELY
    

    クリーンアップ

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、リソースを含む Google Cloud プロジェクトを削除します。

    プロジェクトの削除

    このクイックスタート向けに新しいプロジェクトを作成した場合、追加料金の発生を防ぐ最も簡単な方法としては、プロジェクトを削除します。

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    認証情報を取り消す

    1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

      gcloud auth application-default revoke
    2. Optional: Revoke credentials from the gcloud CLI.

      gcloud auth revoke

    次のステップ