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

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

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

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

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

Identity Connector SDK का इस्तेमाल करके, आइडेंटिटी कनेक्टर बनाना

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

  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>

ग्रेडल

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() को कॉल करने से पहले, FullSyncIdentityConnector टेंप्लेट को इंस्टैंटिएट करने के लिए, IdentityApplication.Builder का इस्तेमाल करें.

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. यह फ़ंक्शन, Google की ओर से दिए गए की-वैल्यू पेयर के साथ Configuration ऑब्जेक्ट को शुरू करता है.

Repository इंटरफ़ेस लागू करना

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() वाला तरीका, IdentityUser ऑब्जेक्ट का CheckpointCloseableIterable दिखाता है.

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() वाला तरीका, IdentityGroup ऑब्जेक्ट का CheckpointCloseableIterable दिखाता है.

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

अगले चरण