สร้างโปรแกรมเชื่อมต่อข้อมูลประจําตัว

โดยค่าเริ่มต้น Google Cloud Search จะรู้จักเฉพาะข้อมูลประจำตัว Google ใน Google Cloud Directory ให้ใช้ เครื่องมือเชื่อมต่อข้อมูลประจำตัวเพื่อซิงค์ข้อมูลประจำตัวขององค์กรกับข้อมูลประจำตัว Google ที่ Cloud Search ใช้

Google มีตัวเลือกต่อไปนี้สำหรับการพัฒนาเครื่องมือเชื่อมต่อข้อมูลประจำตัว

สร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวโดยใช้ Identity Connector SDK

เครื่องมือเชื่อมต่อข้อมูลประจำตัวทั่วไปจะทำงานต่อไปนี้

  1. กำหนดค่าเครื่องมือเชื่อมต่อ
  2. ดึงข้อมูลผู้ใช้จากระบบข้อมูลประจำตัวและส่งไปยัง Google
  3. ดึงข้อมูลกลุ่มจากระบบข้อมูลประจำตัวและส่งไปยัง Google

ตั้งค่าการขึ้นต่อกัน

รวมการขึ้นต่อกันต่อไปนี้ในไฟล์บิลด์

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'

สร้างการกำหนดค่าเครื่องมือเชื่อมต่อ

เครื่องมือเชื่อมต่อทุกรายการใช้ไฟล์การกำหนดค่าสำหรับพารามิเตอร์ต่างๆ เช่น รหัสที่เก็บ กำหนดพารามิเตอร์เป็นคู่ คีย์-ค่า เช่น api.sourceId=1234567890abcdef

Google Cloud Search SDK มีพารามิเตอร์ที่ Google จัดหาให้สำหรับเครื่องมือเชื่อมต่อทั้งหมด คุณต้องประกาศพารามิเตอร์ต่อไปนี้ในไฟล์การกำหนดค่า

  • เครื่องมือเชื่อมต่อเนื้อหา: ประกาศ api.sourceId และ api.serviceAccountPrivateKeyFile พารามิเตอร์เหล่านี้จะระบุที่เก็บและคีย์ส่วนตัวที่จำเป็นสำหรับการเข้าถึง
  • เครื่องมือเชื่อมต่อข้อมูลประจำตัว: ประกาศ api.identitySourceId เพื่อระบุ แหล่งที่มาของข้อมูลประจำตัวภายนอก สำหรับการซิงค์ผู้ใช้ ให้ประกาศ api.customerId (รหัสที่ไม่ซ้ำกันสำหรับบัญชี Google Workspace) ด้วย

ประกาศพารามิเตอร์อื่นๆ ที่ Google จัดหาให้ก็ต่อเมื่อต้องการลบล้างค่าเริ่มต้นของพารามิเตอร์เหล่านั้น ดูรายละเอียดเกี่ยวกับการสร้างรหัสและคีย์ได้ที่ พารามิเตอร์ที่ Google จัดหาให้

นอกจากนี้ คุณยังกำหนดพารามิเตอร์เฉพาะที่เก็บในไฟล์การกำหนดค่าได้ด้วย

ส่งไฟล์การกำหนดค่าไปยังเครื่องมือเชื่อมต่อ

ตั้งค่าพร็อพเพอร์ตี้ระบบ config เพื่อส่งไฟล์การกำหนดค่า ใช้อาร์กิวเมนต์ -D เมื่อเริ่มต้นเครื่องมือเชื่อมต่อ เช่น

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

หากไม่ระบุอาร์กิวเมนต์นี้ SDK จะพยายามใช้ไฟล์ชื่อ connector-config.properties ในไดเรกทอรีภายใน

สร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวแบบซิงค์เต็มโดยใช้คลาสเทมเพลต

SDK มีเทมเพลต FullSyncIdentityConnector สำหรับซิงค์ผู้ใช้และกลุ่มทั้งหมดจากที่เก็บ ส่วนนี้จะอธิบายวิธีใช้เทมเพลตดังกล่าว

ส่วนนี้อ้างอิงถึงโค้ดจากตัวอย่าง IdentityConnectorSample.java ซึ่งอ่านข้อมูลประจำตัวจากไฟล์ CSV

ใช้จุดเข้าใช้งานเครื่องมือเชื่อมต่อ

จุดเข้าใช้งานคือเมธอด main() โดยจะสร้างอินสแตนซ์ Application และเรียก start() เพื่อเรียกใช้เครื่องมือเชื่อมต่อ

ก่อนที่จะเรียก application.start() ให้ใช้ IdentityApplication.Builder เพื่อสร้างอินสแตนซ์เทมเพลต FullSyncIdentityConnector

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.build() เมธอด initConfig() จะทำดังนี้

  1. ตรวจสอบว่า Configuration ได้เริ่มต้นแล้ว
  2. เริ่มต้นออบเจ็กต์ Configuration ด้วยคู่คีย์-ค่าที่ Google จัดหาให้

ใช้อินเทอร์เฟซที่เก็บ

ออบเจ็กต์ Repository จะซิงค์ข้อมูลประจำตัวที่เก็บกับข้อมูลประจำตัว Google เมื่อใช้เทมเพลต คุณจะต้องลบล้างเมธอดบางรายการเท่านั้น สำหรับ FullSyncIdentityConnector ให้ลบล้างเมธอดต่อไปนี้

  • init(): สำหรับการตั้งค่าและการเริ่มต้น
  • listUsers(): สำหรับซิงค์ผู้ใช้ทั้งหมด
  • listGroups(): สำหรับซิงค์กลุ่มทั้งหมด
  • (ไม่บังคับ) close(): สำหรับการล้างข้อมูลระหว่างการปิดระบบ

รับพารามิเตอร์การกำหนดค่าที่กำหนดเอง

ดึงข้อมูลพารามิเตอร์ที่กำหนดเองจากออบเจ็กต์ Configuration ซึ่งโดยปกติจะอยู่ในเมธอด init() ข้อมูลโค้ดต่อไปนี้แสดงวิธีดึงข้อมูลเส้นทาง CSV

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() เพื่อดึงข้อมูลการแมปผู้ใช้ เมธอดนี้ยอมรับจุดตรวจสอบเพื่อดำเนินการซิงค์ต่อหากมีการขัดจังหวะ สำหรับผู้ใช้แต่ละราย ให้ทำดังนี้

  1. รับการแมประหว่างข้อมูลประจำตัว Google กับข้อมูลประจำตัวภายนอก
  2. จัดแพ็กเกจคู่ลงใน Iterator ที่แสดงผลโดย 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);
    }
  }
  // ...
}

จัดแพ็กเกจการแมปผู้ใช้ลงใน Iterator

เมธอด listUsers() จะแสดงผล CheckpointCloseableIterable ของ IdentityUser ออบเจ็กต์

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

รับกลุ่ม

ลบล้าง listGroups() เพื่อดึงข้อมูลกลุ่มและสมาชิกของกลุ่ม เมธอดนี้ยอมรับจุดตรวจสอบ สำหรับแต่ละกลุ่ม ให้ทำดังนี้

  1. รับกลุ่มและสมาชิกของกลุ่ม
  2. จัดแพ็กเกจกลุ่มและสมาชิกของกลุ่มลงใน Iterator ที่แสดงผลโดย 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);
    }
  }
  // ...

}

จัดแพ็กเกจกลุ่มและสมาชิกของกลุ่มลงใน Iterator

เมธอด listGroups() จะแสดงผล CheckpointCloseableIterable ของ IdentityGroup ออบเจ็กต์

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

ขั้นตอนถัดไป