पहचान कनेक्टर बनाएं

डिफ़ॉल्ट रूप से, Google Cloud Search सिर्फ़ Google Cloud Directory में मौजूद Google की पहचानों को पहचानता है. कंपनी की पहचानों को Google की उन पहचानों के साथ सिंक करने के लिए आइडेंटिटी कनेक्टर का इस्तेमाल करें जिनका इस्तेमाल Cloud Search करता है.

Google, आइडेंटिटी कनेक्टर डेवलप करने के लिए ये विकल्प उपलब्ध कराता है:

  • आइडेंटिटी कनेक्टर एसडीके: यह Java प्रोग्रामर के लिए सबसे सही है. यह एसडीके, REST API के लिए रैपर है. इसकी मदद से, कनेक्टर तेज़ी से बनाए जा सकते हैं. एसडीके का इस्तेमाल करने के लिए, देखें आइडेंटिटी कनेक्टर एसडीके का इस्तेमाल करके आइडेंटिटी कनेक्टर बनाना.

  • लो-लेवल REST API और एपीआई लाइब्रेरी: यह नॉन-Java प्रोग्रामर के लिए सबसे सही है. REST API का इस्तेमाल करके आइडेंटिटी कनेक्टर बनाने के लिए, उपयोगकर्ताओं की मैपिंग के बारे में जानने के लिए Directory API: User Accounts लेख पढ़ें. ग्रुप की मैपिंग के लिए Google Cloud Identity documentation का दस्तावेज़ पढ़ें.

आइडेंटिटी कनेक्टर एसडीके का इस्तेमाल करके आइडेंटिटी कनेक्टर बनाना

आम तौर पर, आइडेंटिटी कनेक्टर ये टास्क पूरे करता है:

  1. कनेक्टर को कॉन्फ़िगर करता है.
  2. आपके आइडेंटिटी सिस्टम से उपयोगकर्ताओं की जानकारी वापस पाता है और उसे Google को भेजता है.
  3. आपके आइडेंटिटी सिस्टम से ग्रुप की जानकारी वापस पाता है और उसे Google को भेजता है.

डिपेंडेंसी सेट अप करना

अपनी बिल्ड फ़ाइल में ये डिपेंडेंसी शामिल करें.

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

अगर आपने यह आर्ग्युमेंट नहीं दिया, तो एसडीके, लोकल डायरेक्ट्री में connector-config.properties नाम की फ़ाइल का इस्तेमाल करने की कोशिश करता है.

टेंप्लेट क्लास का इस्तेमाल करके, पूरी तरह सिंक होने वाला आइडेंटिटी कनेक्टर बनाना

एसडीके में, 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();
}

एसडीके, main() तरीके से Application.build() को कॉल करने के बाद, initConfig() को कॉल करता है. 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 क्लास के किसी एक टाइप पार्सर का इस्तेमाल करें. ट्यूटोरियल कनेक्टर के इस स्निपेट में, GitHub रिपॉज़िटरी के नामों की सूची पाने के लिए, getMultiValue तरीके का इस्तेमाल किया गया है:

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();

अगले चरण