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

โดยค่าเริ่มต้น 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 หรือไลบรารี หากต้องการสร้างเครื่องมือเชื่อมต่อข้อมูลประจำตัวโดยใช้ REST API โปรดดู ถึง 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 สองไฟล์และซิงค์กับข้อมูลประจำตัวของ 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 ออบเจ็กต์ งานนี้มักจะดำเนินการใน Repository ของชั้นเรียน init()

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

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

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