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

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

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

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

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

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

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

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

  • โปรแกรมเชื่อมต่อเนื้อหา: ประกาศ 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. แพ็กคู่ลงในตัววนซ้ำที่ 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() จะแสดงผล CheckpointCloseableIterable ของออบเจ็กต์ IdentityUser

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

รับกลุ่ม

แทนที่ 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() จะแสดงผล CheckpointCloseableIterable ของออบเจ็กต์ IdentityGroup

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

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