建立識別資訊連接器

根據預設,Google Cloud Search 只能辨識儲存在 Google 身分的 Google 身分 Google Cloud Directory (使用者和群組)。識別資訊連接器 將貴公司的身分與 Google Cloud Search 所用的 Google 身分同步處理。

Google 提供下列識別資訊連接器的開發選項:

  • Identity Connector SDK。這個選項適合程式設計的開發人員 還是 Java 程式設計語言Identity Connector SDK 是包裝函式 ,可讓您快速建立連接器。如要建立 識別身分連接器 使用 Identity Connector SDK 建立識別資訊連接器

  • 低階 REST API 和 API 程式庫。這些選項適用於 可能沒有以 Java 編寫程式 或程式碼集更適合 REST API 或程式庫如要使用 REST API 建立識別資訊連接器,請參閱 到 Directory API:使用者帳戶 即可瞭解地圖使用者 以下項目的 Cloud Identity 說明文件: 有關對應群組的資訊。

使用 Identity Connector SDK 建立識別資訊連接器

一般識別資訊連接器會執行下列工作:

  1. 設定連接器。
  2. 從企業身分識別系統擷取所有使用者,並傳送至 Google 用於同步處理 Google 身分。
  3. 從企業身分識別系統擷取所有群組,然後傳送至 Google 用於同步處理 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參數的定義為 鍵/值的組合,例如 api.sourceId=1234567890abcdef

Google Cloud Search SDK 包含多項 Google 提供的設定 參數。您必須宣告下列項目 Google 在設定檔中提供的參數:

  • 如果是內容連接器,您必須宣告 api.sourceIdapi.serviceAccountPrivateKeyFile,因為這些參數能指出位置 以及存取存放區所需的私密金鑰
  • 對於識別資訊連接器,您必須將 api.identitySourceId 宣告為 參數識別外部識別資訊來源的位置。如果您是 同步處理使用者,您也必須宣告 api.customerId 為 貴公司的 Google Workspace 帳戶。

除非您想覆寫其他 Google 提供的預設值 參數,因此您不需要在設定檔中宣告這些參數。 如要進一步瞭解 Google 提供的設定參數,例如 想要瞭解如何產生特定 ID 和鍵 Google 提供的設定參數

您也可以定義自己的存放區專屬參數,以便用於 設定檔

將設定檔傳送至連接器

設定系統屬性 config,將設定檔傳送至 。您可以在啟動時使用 -D 引數設定屬性 與連接器建立連結。舉例來說,下列指令會啟動連接器 使用 MyConfig.properties 設定檔:

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

如果缺少這個引數,SDK 會嘗試存取預設設定 名為 connector-config.properties 的檔案。

使用範本類別建立完整的同步處理識別資訊連接器

Identity Connector SDK 包含 FullSyncIdentityConnector 範本類別 可用來同步處理識別資訊中的所有使用者和群組 Google 身分的存放區本節將說明如何使用 FullSyncIdentityConnector 範本:從非 Google 身分執行完整的使用者和群組同步處理 Cloud Storage 也提供目錄同步處理功能

在這一節,說明文件中的 IdentityConnecorSample.java 範例。此樣本讀取了使用者和群組 並將身分資訊與 Google 身分同步處理。

實作連接器的進入點

連接器的進入點是 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 提供的鍵/值初始化 Configuration 物件 配對。每個鍵/值組合都儲存在 ConfigValue敬上 物件中 Configuration 物件內。

導入 Repository 介面

Repository 物件的唯一用途是執行 將存放區身分同步至 Google 身分使用 範本,您只需覆寫 建立識別資訊連接器的 Repository 介面。對於 FullTraversalConnector敬上 ,您可能會覆寫以下方法:

  • init()敬上 方法。如要設定和初始化任何身分存放區,請覆寫 `init() 方法

  • listUsers()敬上 方法。如要將識別資訊存放區中的所有使用者與 Google 使用者同步處理,請覆寫 listUsers() 方法。

  • listGroups()敬上 方法。如要將識別資訊存放區中的所有群組與 Google 群組同步處理, 覆寫 listGroups() 方法。

  • (選用) close()敬上 方法。如果您需要執行存放區清理工作,請覆寫 close() 方法。在連接器關閉期間,此方法會呼叫一次。

取得自訂設定參數

在處理連接器設定的過程中,您必須 自訂參數 Configuration敬上 物件。這項工作通常會在 Repository敬上 類別的 init() 方法。

Configuration 類別提供多種取得不同資料類型的方法 從設定中移除每個方法都會傳回 ConfigValue敬上 物件。然後,您將會使用 ConfigValue 物件的 get() 方法來擷取實際值。 下列程式碼片段說明如何擷取 userMappingCsvPathConfiguration 物件的 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()敬上 從身分存放區擷取所有使用者的對應關係。 listUsers() 方法接受查核點,以代表要做為最後一個身分 已同步。如果程序順利執行,您可以使用查核點繼續執行同步程序。 已中斷。您將為存放區中的每位使用者執行這些步驟 listUsers() 方法:

  1. 取得包含 Google 身分和相關外部的對應關係 識別個人身分
  2. 將配對封裝至 listUsers() 方法傳回的疊代器中。

取得使用者對應

下列程式碼片段說明如何擷取識別資訊對應檔 儲存在 CSV 檔案中:

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 如需儲存大量結構化物件 建議使用 Cloud Bigtable您可以使用 CheckpointClosableIterableImpl.Builder敬上 類別,建構並傳回疊代器。下列程式碼片段顯示 將每個對應關係封裝到清單 清單:

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

取得群組

覆寫 listGroups()敬上 從您的身分識別擷取所有群組及其成員 Cloud Storage 也提供目錄同步處理功能listGroups() 方法接受代表最後一個 才能同步處理身分識別資訊檢查點可用來繼續同步處理, 程序中斷。您將對存放區中的每位使用者執行以下作業 listGroups() 方法中的步驟:

  1. 取得群組及其成員。
  2. 將每個群組和成員封裝成由 listGroups() 方法。

取得群組身分

下列程式碼片段說明如何擷取群組和成員 儲存在 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 如需儲存大量結構化物件 建議使用 Cloud Bigtable 您可以使用 CheckpointClosableIterableImpl.Builder敬上 類別,建構並傳回疊代器。下列程式碼片段顯示 將每個群組和成員封裝成清單,並據此建構疊代器 清單:

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

後續步驟

以下是您可能採取的後續步驟: