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

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

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

  • Identity Connector SDK ตัวเลือกนี้มีไว้สำหรับนักพัฒนาซอฟต์แวร์ที่กำลังเขียนโปรแกรม ในภาษาโปรแกรม Java Identity Connector SDK คือ Wrapper ของ REST API ช่วยให้คุณสร้างเครื่องมือเชื่อมต่อได้อย่างรวดเร็ว หากต้องการสร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวโดยใช้ SDK โปรดดูสร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวโดยใช้ Identity Connector SDK

  • REST API และไลบรารี API ระดับต่ำ ตัวเลือกเหล่านี้มีไว้สำหรับนักพัฒนาซอฟต์แวร์ที่อาจไม่ได้เขียนโปรแกรมใน Java หรือซึ่งมีโค้ดเบสที่รองรับ REST API หรือไลบรารีมากกว่า ในการสร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวโดยใช้ API ของ REST โปรดดู Directory API: บัญชีผู้ใช้สำหรับข้อมูลเกี่ยวกับการแมปผู้ใช้และเอกสารประกอบของ Cloud Identity สำหรับข้อมูลเกี่ยวกับกลุ่มการจับคู่

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

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

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

ตั้งค่าทรัพยากร Dependency

คุณต้องรวมทรัพยากร Dependency บางอย่างในไฟล์บิลด์เพื่อใช้ SDK คลิกแท็บด้านล่างเพื่อดูทรัพยากร Dependency สำหรับสภาพแวดล้อมของบิลด์

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 มีให้หลายรายการที่เครื่องมือเชื่อมต่อทั้งหมดใช้ คุณต้องประกาศพารามิเตอร์ที่ Google จัดหาต่อไปนี้ในไฟล์การกำหนดค่า

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

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

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

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

ตั้งค่าพร็อพเพอร์ตี้ของระบบ config เพื่อส่งไฟล์การกำหนดค่าไปยังเครื่องมือเชื่อมต่อ คุณสามารถตั้งค่าพร็อพเพอร์ตี้ได้โดยใช้อาร์กิวเมนต์ -D เมื่อเริ่มต้นเครื่องมือเชื่อมต่อ เช่น คำสั่งต่อไปนี้จะเริ่มเครื่องมือเชื่อมต่อด้วยไฟล์การกำหนดค่า MyConfig.properties

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

หากไม่มีอาร์กิวเมนต์นี้ SDK จะพยายามเข้าถึงไฟล์การกำหนดค่าเริ่มต้นชื่อ connector-config.properties

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

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

เอกสารส่วนนี้หมายถึงข้อมูลโค้ดจากตัวอย่าง IdentityConnecorSample.java ตัวอย่างนี้อ่านข้อมูลประจำตัวของผู้ใช้และกลุ่มจากไฟล์ CSV 2 ไฟล์ และซิงค์กับข้อมูลประจำตัวของ 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.build เมธอด initConfig() จะทํางานต่อไปนี้

  1. เรียกใช้เมธอด Configuation.isInitialized() เพื่อให้มั่นใจว่า Configuration ยังไม่ได้เริ่มต้น
  2. เริ่มต้นออบเจ็กต์ Configuration ด้วยคู่คีย์-ค่าที่ Google จัดหา คู่คีย์-ค่าแต่ละคู่จะเก็บไว้ในออบเจ็กต์ ConfigValue ภายในออบเจ็กต์ Configuration

ใช้อินเทอร์เฟซ Repository

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

  • เมธอด init() หากต้องการดำเนินการตั้งค่าและเริ่มต้นที่เก็บข้อมูลประจำตัว ให้ลบล้างเมธอด "init()

  • เมธอด listUsers() หากต้องการซิงค์ผู้ใช้ทั้งหมดในที่เก็บข้อมูลประจำตัวกับผู้ใช้ Google ให้ลบล้างเมธอด listUsers()

  • เมธอด listGroups() หากต้องการซิงค์กลุ่มทั้งหมดในที่เก็บข้อมูลประจำตัวกับ Google Groups ให้ลบล้างเมธอด listGroups()

  • (ไม่บังคับ) เมธอด close() หากต้องการล้างข้อมูลที่เก็บ ให้ลบล้างเมธอด close() ระบบจะเรียกวิธีนี้เพียงครั้งเดียวระหว่างการปิดเครื่องเชื่อมต่อ

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

คุณจะต้องรับพารามิเตอร์ที่กำหนดเองจากออบเจ็กต์ Configuration เพื่อให้สอดคล้องกับการกำหนดค่าของเครื่องมือเชื่อมต่อ โดยปกติงานนี้จะทำในเมธอด init() ของคลาส Repository

คลาส Configuration มีหลายวิธีในการรับข้อมูลประเภทต่างๆ จากการกำหนดค่า แต่ละเมธอดจะแสดงผลออบเจ็กต์ ConfigValue จากนั้นจะใช้เมธอด get() ของออบเจ็กต์ ConfigValue เพื่อดึงค่าจริง ข้อมูลโค้ดต่อไปนี้แสดงวิธีดึงค่า userMappingCsvPath และ groupMappingCsvPath จากออบเจ็กต์ Configuration

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 คุณใช้คลาส CheckpointClosableIterableImpl.Builder เพื่อสร้างและแสดงผลตัววนซ้ำได้ ข้อมูลโค้ดต่อไปนี้แสดงวิธีจัดแพ็กเกจการแมปแต่ละรายการลงในรายการสร้างตัววนซ้ำจากรายการนั้น

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

รับกลุ่ม

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

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

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

ขั้นตอนถัดไปที่ทำได้มีดังนี้