Membuat konektor identitas

Secara default, Google Cloud Search hanya mengenali identitas Google di Google Cloud Directory. Gunakan konektor identitas untuk menyinkronkan identitas perusahaan dengan identitas Google yang digunakan Cloud Search.

Google menyediakan opsi berikut untuk mengembangkan konektor identitas:

Membuat konektor identitas menggunakan Identity Connector SDK

Konektor identitas standar melakukan tugas-tugas berikut:

  1. Mengonfigurasi konektor.
  2. Mengambil pengguna dari sistem identitas Anda dan mengirimkannya ke Google.
  3. Mengambil grup dari sistem identitas Anda dan mengirimkannya ke Google.

Menyiapkan dependensi

Sertakan dependensi ini dalam file build Anda.

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'

Buat konfigurasi konektor Anda

Setiap konektor menggunakan file konfigurasi untuk parameter seperti ID repositori Anda. Tentukan parameter sebagai pasangan key-value, seperti api.sourceId=1234567890abcdef.

Google Cloud Search SDK menyertakan parameter yang disediakan Google untuk semua konektor. Anda harus mendeklarasikan hal berikut dalam file konfigurasi:

  • Konektor konten: Nyatakan api.sourceId dan api.serviceAccountPrivateKeyFile. Parameter ini mengidentifikasi repositori Anda dan kunci pribadi yang diperlukan untuk akses.
  • Konektor identitas: Nyatakan api.identitySourceId untuk mengidentifikasi sumber identitas eksternal Anda. Untuk sinkronisasi pengguna, deklarasikan juga api.customerId (ID unik untuk akun Google Workspace Anda).

Deklarasikan parameter lain yang disediakan Google hanya untuk mengganti nilai defaultnya. Untuk mengetahui detail tentang cara membuat ID dan kunci, lihat Parameter yang disediakan Google.

Anda juga dapat menentukan parameter spesifik repositori dalam file konfigurasi.

Teruskan file konfigurasi ke konektor

Tetapkan properti sistem config untuk meneruskan file konfigurasi. Gunakan argumen -D saat memulai konektor. Contoh:

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

Jika Anda menghilangkan argumen ini, SDK akan mencoba menggunakan file bernama connector-config.properties di direktori lokal.

Membuat konektor identitas sinkronisasi lengkap menggunakan class template

SDK menyertakan template FullSyncIdentityConnector untuk menyinkronkan semua pengguna dan grup dari repositori Anda. Bagian ini menjelaskan cara menggunakannya.

Bagian ini mengacu pada kode dari sampel IdentityConnectorSample.java, yang membaca identitas dari file CSV.

Menerapkan titik entri konektor

Titik entri adalah metode main(). Tindakan ini akan membuat instance Application dan memanggil start() untuk menjalankan konektor.

Sebelum memanggil application.start(), gunakan IdentityApplication.Builder untuk membuat instance template 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 memanggil initConfig() setelah metode main() Anda memanggil Application.build(). Metode initConfig():

  1. Memastikan Configuration belum diinisialisasi.
  2. Menginisialisasi objek Configuration dengan key-value pair yang disediakan Google.

Menerapkan antarmuka Repository

Objek Repository menyinkronkan identitas repositori ke identitas Google. Saat menggunakan template, Anda hanya perlu mengganti metode tertentu. Untuk FullSyncIdentityConnector, ganti metode berikut:

  • init(): Untuk penyiapan dan inisialisasi.
  • listUsers(): Untuk menyinkronkan semua pengguna.
  • listGroups(): Untuk menyinkronkan semua grup.
  • (Opsional) close(): Untuk pembersihan selama penonaktifan.

Mendapatkan parameter konfigurasi khusus

Ambil parameter kustom dari objek Configuration, biasanya dalam metode init(). Cuplikan berikut menunjukkan cara mengambil jalur 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();
}

Untuk mendapatkan dan mengurai parameter yang berisi beberapa nilai, gunakan salah satu jenis parser class Configuration untuk mengurai data menjadi potongan terpisah. Cuplikan berikut, dari konektor tutorial, menggunakan metode getMultiValue untuk mendapatkan daftar nama repositori GitHub:

GithubRepository.java
ConfigValue<List<String>> repos = Configuration.getMultiValue(
    "github.repos",
    Collections.emptyList(),
    Configuration.STRING_PARSER);

Mendapatkan pemetaan untuk semua pengguna

Ganti listUsers() untuk mengambil pemetaan pengguna. Metode ini menerima checkpoint untuk melanjutkan sinkronisasi jika terganggu. Untuk setiap pengguna:

  1. Dapatkan pemetaan antara identitas Google dan identitas eksternal.
  2. Gabungkan pasangan ke dalam iterator yang ditampilkan oleh listUsers().

Mendapatkan pemetaan pengguna

Cuplikan ini menunjukkan cara mengambil pemetaan identitas dari file 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);
    }
  }
  // ...
}

Menggabungkan pemetaan pengguna menjadi iterator

Metode listUsers() menampilkan CheckpointCloseableIterable dari objek IdentityUser.

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

Mendapatkan grup

Ganti listGroups() untuk mengambil grup dan anggotanya. Metode ini menerima checkpoint. Untuk setiap grup:

  1. Dapatkan grup dan anggotanya.
  2. Gabungkan ke dalam iterator yang ditampilkan oleh listGroups().

Mendapatkan identitas grup

Cuplikan ini menunjukkan cara mengambil grup dan anggota dari file 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);
    }
  }
  // ...

}

Menggabungkan grup dan anggota ke dalam iterator

Metode listGroups() menampilkan CheckpointCloseableIterable dari objek IdentityGroup.

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

Langkah Berikutnya