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

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

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

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

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

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

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

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

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

คุณต้องรวมทรัพยากร Dependency บางอย่างไว้ในไฟล์บิลด์เพื่อใช้ 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'

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

ตัวเชื่อมต่อทุกตัวมีไฟล์การกำหนดค่าที่มีพารามิเตอร์ที่ตัวเชื่อมต่อใช้ เช่น รหัสสำหรับที่เก็บ พารามิเตอร์กำหนดเป็นคู่คีย์-ค่า เช่น 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

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

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() เมธอด ระบบจะเรียกใช้เมธอดนี้ 1 ครั้งในระหว่างการปิดเครื่องมือเชื่อมต่อ

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

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

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

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

ขั้นตอนถัดไปที่คุณอาจต้องดำเนินการมีดังนี้