ID コネクタを作成する

デフォルトでは、Google Cloud Search は Google Cloud Directory(ユーザーとグループ)に保存されている Google ID のみを認識します。ID コネクタは、 企業の ID を Google Cloud Search で使用される Google ID と同期できます。

Google は、ID コネクタを開発するための以下のオプションを提供しています。

  • Identity Connector SDK。このオプションは、Java や SDK などの 記述できます。Identity Connector SDK は、コネクタをすばやく作成するための REST API を簡単に使えるようにしたラッパーです。この SDK で ID コネクタを作成する場合は、Identity Connector SDK を使用して ID コネクタを作成するをご覧ください。

  • 低レベル REST API と API ライブラリ。これらのオプションは、Java でプログラミングしていないデベロッパーや、コードベースが REST API やライブラリに問題なく対応できるデベロッパー向けです。REST API を使用して ID コネクタを作成するには、このモジュールの から Directory API: ユーザー アカウント: ユーザーとリソースのマッピングと Cloud Identity のドキュメント: 説明します。

Identity Connector SDK を使用して ID コネクタを作成する

ID コネクタによって行われる標準的なタスクは、次のとおりです。

  1. コネクタを構成します。
  2. 企業 ID システムからすべてのユーザーを取得し、Google ID との同期のために Google に送信します。
  3. 企業 ID システムからすべてのグループを取得し、Google ID との同期のために Google に送信します。

依存関係を設定する

SDK を使用するには、ビルドファイルに特定の依存関係を含める必要があります。使用するビルド環境の依存関係を表示するには、以下のタブをクリックします。

Maven

<dependency>
<groupId>com.google.enterprise.cloudsearch</groupId>
<artifactId>google-cloudsearch-identity-connector-sdk</artifactId>
<version>v1-0.0.3</version>
</dependency>

Gradle

 compile group: 'com.google.enterprise.cloudsearch',
         name: 'google-cloudsearch-identity-connector-sdk',
         version: 'v1-0.0.3'

コネクタ構成を作成する

コネクタごとに対応する構成ファイルが存在し、コネクタはそこに含まれているパラメータ(お客様のリポジトリの ID など)を使用します。パラメータは次のように定義されます。 Key-Value ペア: api.sourceId=1234567890abcdef

Google Cloud Search SDK には、すべてのコネクタで使用される Google 提供構成パラメータが複数含まれています。以下の Google 提供パラメータをお客様の構成ファイル内で宣言してください。

  • コンテンツ コネクタの場合は、api.sourceIdapi.serviceAccountPrivateKeyFile(これらのパラメータはロケーションを識別するため) 秘密鍵の情報が含まれています。
で確認できます。
  • ID コネクタの場合は、次のように api.identitySourceId を宣言する必要があります。 パラメータは、外部の ID ソースの場所を指定します。もし ユーザーを同期するには、api.customerId を 企業用の Google Workspace アカウントを作成します。

他の Google 提供のデフォルト値をオーバーライドする場合以外は、 それらのパラメータを構成ファイルで宣言する必要はありません。 Google 提供の構成パラメータについて詳しくは、 詳細については、このモジュールの Google 提供の構成パラメータ

独自のリポジトリ パラメータを定義して、お客様の構成ファイル内で使用することもできます。

構成ファイルをコネクタに渡す

システム プロパティ config を設定して、構成ファイルを あります起動時に -D 引数を使用してプロパティを設定できます。 表示されます。たとえば、次のコマンドはコネクタを起動します。 MyConfig.properties 構成ファイルに置き換えます。

java -classpath myconnector.jar;... -Dconfig=MyConfig.properties MyConnector

この引数がない場合、SDK はデフォルト構成にアクセスしようとします。 connector-config.properties という名前のファイル。

テンプレート クラスを使用して完全同期 ID コネクタを作成する

Identity Connector SDK には FullSyncIdentityConnector テンプレート クラスが含まれています。 ID アカウントからすべてのユーザーとグループを同期できます。 Google ID でリポジトリに保存します。このセクションでは、 Google 以外の ID からユーザーとグループを完全に同期するための FullSyncIdentityConnector テンプレート できます。

このドキュメントのこのセクションは、 IdentityConnecorSample.java のサンプル。このサンプルでは、2 つの CSV ファイルからユーザー ID とグループ ID を読み取り、Google ID と同期します。

コネクタのエントリ ポイントを実装する

コネクタのエントリ ポイントは、 main() メソッドを使用します。このメソッドの主なタスクは、 Application そのクラスの情報を start() メソッドを使用してコネクタを実行します。

電話をかける前に application.start() 使用 IdentityApplication.Builder クラスをインスタンス化して FullSyncIdentityConnector テンプレート。FullSyncIdentityConnector は、 Repository そのオブジェクトを指定します。 次のコード スニペットは、main() メソッドの実装方法を示しています。

IdentityConnectorSample.java
/**
 * This sample connector uses the Cloud Search SDK template class for a full
 * sync connector. In the full sync case, the repository is responsible
 * for providing a snapshot of the complete identity mappings and
 * group rosters. This is then reconciled against the current set
 * of mappings and groups in Cloud Directory.
 *
 * @param args program command line arguments
 * @throws InterruptedException thrown if an abort is issued during initialization
 */
public static void main(String[] args) throws InterruptedException {
  Repository repository = new CsvRepository();
  IdentityConnector connector = new FullSyncIdentityConnector(repository);
  IdentityApplication application = new IdentityApplication.Builder(connector, args).build();
  application.start();
}

SDK はバックグラウンドで initConfig() メソッド(コネクタの main() メソッド呼び出し後の) Application.buildinitConfig() メソッドは、次のタスクを実行します。

  1. このメソッドを呼び出して、 Configuation.isInitialized() メソッドを Configuration 初期化されていないことを確認します。
  2. Google 提供の Key-Value で Configuration オブジェクトを初期化します。 あります。各 Key-Value ペアは ConfigValue Configuration オブジェクト内のオブジェクト。

Repository インターフェースを実装する

Repository オブジェクトの唯一の目的は、 リポジトリ ID と Google ID の同期。使用する場合 テンプレート内でオーバーライドする必要があるのは、 ID コネクタを作成するための Repository インターフェース。対象: FullTraversalConnector 次のメソッドをオーバーライドすることになります。

  • init() メソッドを呼び出します。ID リポジトリを設定して初期化するには、init() メソッドをオーバーライドします。

  • listUsers() メソッドを呼び出します。ID リポジトリ内のすべてのユーザーを Google ユーザーと同期するには、オーバーライドします listUsers() メソッドを使用します。

  • listGroups() メソッドを呼び出します。ID リポジトリ内のすべてのグループを Google グループと同期するには、次の操作を行います。 listGroups() メソッドをオーバーライドする。

  • (省略可) close() メソッドを呼び出します。リポジトリのクリーンアップを行う必要がある場合は、close() をオーバーライドします。 メソッドを呼び出します。このメソッドは、コネクタのシャットダウン時に 1 回呼び出されます。

カスタム構成パラメータを取得する

コネクタの構成を扱う際に、コネクタの カスタムパラメータの Configuration 渡されます。このタスクは通常、 Repository クラスの init() メソッドを呼び出します。

Configuration クラスには、さまざまなデータ型を取得するためのメソッドがいくつかあります。 構成から分離できます。各メソッドは ConfigValue 渡されます。次に、ConfigValue オブジェクトの get() メソッドを使用して実際の値を取得できます。 次のスニペットは、userMappingCsvPath を取得する方法と、 Configuration オブジェクトからの groupMappingCsvPath 値:

IdentityConnectorSample.java
/**
 * Initializes the repository once the SDK is initialized.
 *
 * @param context Injected context, contains convenienve methods
 *                for building users & groups
 * @throws IOException if unable to initialize.
 */
@Override
public void init(RepositoryContext context) throws IOException {
  log.info("Initializing repository");
  this.context = context;
  userMappingCsvPath = Configuration.getString(
      "sample.usersFile", "users.csv").get().trim();
  groupMappingCsvPath = Configuration.getString(
      "sample.groupsFile", "groups.csv").get().trim();
}

複数の値を含むパラメータを取得して解析するには、 Configuration クラスの型パーサー: データを個別のチャンクに解析します。 チュートリアル コネクタからの次のスニペットでは、 getMultiValue メソッドを使用して、GitHub リポジトリ名のリストを取得します。

GithubRepository.java
ConfigValue<List<String>> repos = Configuration.getMultiValue(
    "github.repos",
    Collections.emptyList(),
    Configuration.STRING_PARSER);

すべてのユーザーのマッピングを取得する

オーバーライド listUsers() ID リポジトリからすべてのユーザーのマッピングを取得します。「 listUsers() メソッドは、最後の ID を表すチェックポイントを受け取ります。 同期します。チェックポイントは、プロセスが中断された場合に同期を再開する目的で使用できます。リポジトリ内のユーザーごとに、次の手順を実行します。 listUsers() メソッドを使用します。

  1. Google ID および関連する外部 ID からなるマッピングを取得します。
  2. ペアを listUsers() メソッドから返されるイテレータにパッケージ化します。

ユーザー マッピングを取得する

以下のコード スニペットは、CSV ファイルに保存されている ID マッピングを取得する方法を示しています。

IdentityConnectorSample.java
/**
 * Retrieves all user identity mappings for the identity source. For the
 * full sync connector, the repository must provide a complete snapshot
 * of the mappings. This is reconciled against the current mappings
 * in Cloud Directory. All identity mappings returned here are
 * set in Cloud Directory. Any previously mapped users that are omitted
 * are unmapped.
 *
 * The connector does not create new users. All users are assumed to
 * exist in Cloud Directory.
 *
 * @param checkpoint Saved state if paging over large result sets. Not used
 *                   for this sample.
 * @return Iterator of user identity mappings
 * @throws IOException if unable to read user identity mappings
 */
@Override
public CheckpointCloseableIterable<IdentityUser> listUsers(byte[] checkpoint)
    throws IOException {
  List<IdentityUser> users = new ArrayList<>();
  try (Reader in = new FileReader(userMappingCsvPath)) {
    // Read user mappings from CSV file
    CSVParser parser = CSVFormat.RFC4180
        .withIgnoreSurroundingSpaces()
        .withIgnoreEmptyLines()
        .withCommentMarker('#')
        .parse(in);
    for (CSVRecord record : parser.getRecords()) {
      // Each record is in form: "primary_email", "external_id"
      String primaryEmailAddress = record.get(0);
      String externalId = record.get(1);
      if (primaryEmailAddress.isEmpty() || externalId.isEmpty()) {
        // Skip any malformed mappings
        continue;
      }
      log.info(() -> String.format("Adding user %s/%s",
          primaryEmailAddress, externalId));

      // Add the identity mapping
      IdentityUser user = context.buildIdentityUser(
          primaryEmailAddress, externalId);
      users.add(user);
    }
  }
  // ...
}

イテレータにユーザー マッピングをパッケージ化する

listUsers() メソッドは Iterator を返します。具体的には、 CheckpointCloseableIterable, / IdentityUser 説明します。こちらの CheckpointClosableIterableImpl.Builder イテレータを作成して返すことができます。以下のコード スニペットは、各マッピングをリストにパッケージ化して、そのリストからイテレータを構築する方法を示しています。

IdentityConnectorSample.java
CheckpointCloseableIterable<IdentityUser> iterator =
  new CheckpointCloseableIterableImpl.Builder<IdentityUser>(users)
      .setHasMore(false)
      .setCheckpoint((byte[])null)
      .build();

グループを取得する

オーバーライド listGroups() ID からすべてのグループとそのメンバーを取得できます。 できます。listGroups() メソッドは、最後の状態を表すチェックポイントを 指定します。チェックポイントは、プロセスが中断された場合に同期を再開する目的で使用できます。リポジトリ内のユーザーごとに、以下の操作を行います。 listGroups() メソッドのステップ:

  1. グループとそのメンバーを取得します。
  2. 各グループとメンバーを listGroups() メソッドを使用します。

グループ ID を取得する

以下のコード スニペットは、CSV ファイルに保存されているグループとメンバーを取得する方法を示しています。

IdentityConnectorSample.java
/**
 * Retrieves all group rosters for the identity source. For the
 * full sync connector, the repository must provide a complete snapshot
 * of the rosters. This is reconciled against the current rosters
 * in Cloud Directory. All groups and members  returned here are
 * set in Cloud Directory. Any previously created groups or members
 * that are omitted are removed.
 *
 * @param checkpoint Saved state if paging over large result sets. Not used
 *                   for this sample.
 * @return Iterator of group rosters
 * @throws IOException if unable to read groups
 */    @Override
public CheckpointCloseableIterable<IdentityGroup> listGroups(byte[] checkpoint)
    throws IOException {
  List<IdentityGroup> groups = new ArrayList<>();
  try (Reader in = new FileReader(groupMappingCsvPath)) {
    // Read group rosters from CSV
    CSVParser parser = CSVFormat.RFC4180
        .withIgnoreSurroundingSpaces()
        .withIgnoreEmptyLines()
        .withCommentMarker('#')
        .parse(in);
    for (CSVRecord record : parser.getRecords()) {
      // Each record is in form: "group_id", "member"[, ..., "memberN"]
      String groupName = record.get(0);
      log.info(() -> String.format("Adding group %s", groupName));
      // Parse the remaining columns as group memberships
      Supplier<Set<Membership>> members = new MembershipsSupplier(record);
      IdentityGroup group = context.buildIdentityGroup(groupName, members);
      groups.add(group);
    }
  }
  // ...

}

グループとメンバーをイテレータにパッケージ化する

listGroups() メソッドは Iterator を返します。具体的には、 CheckpointCloseableIterable, / IdentityGroup 説明します。 こちらの CheckpointClosableIterableImpl.Builder イテレータを作成して返すことができます。次のコード スニペットは、 各グループとメンバーをリストにパッケージ化し、そこからイテレータを作成する list:

IdentityConnectorSample.java
CheckpointCloseableIterable<IdentityGroup> iterator =
   new CheckpointCloseableIterableImpl.Builder<IdentityGroup>(groups)
      .setHasMore(false)
      .setCheckpoint((byte[])null)
      .build();

次のステップ

必要に応じて次の手順を行います。