बाहरी भूमिका की मैपिंग

Edge for Private Cloud v4.18.01

एक्सटर्नल रोल मैपिंग की मदद से, अपने ग्रुप या रोल के लिए भूमिका के हिसाब से ऐक्सेस कंट्रोल (RBAC) रोल और Apigee Edge पर बनाए गए ग्रुप को मैप किया जा सकता है. यह सुविधा सिर्फ़ Edge Private Cloud के साथ उपलब्ध है.

नया क्या है

निजी क्लाउड के लिए Edge की रिलीज़ के लिए, बाहरी रोल मैपिंग की सेवा अब काम नहीं करती. यह सेवा 4.18.01 से पहले की है. बाहरी भूमिका मैपिंग का रिलीज़ 4.18.01 एक अपडेट किया गया वर्शन है, जिसमें गड़बड़ियां ठीक कर दी गई हैं और नई सुविधाएं जोड़ी गई हैं:

  • उस समस्या को ठीक कर दिया गया है जिसमें आपको ऐसे उपयोगकर्ताओं से पुष्टि करते समय 403 पाबंदी वाले रिस्पॉन्स मिले थे जिनके पास ऐक्सेस होना चाहिए.
  • X-Apigee-Current-User हेडर, अब बाहरी रोल मैपिंग में काम करता है. सही ऐक्सेस वाले उपयोगकर्ता (sysadmin) अब किसी दूसरे उपयोगकर्ता को असाइन की गई भूमिकाएं देख सकते हैं.

ज़रूरी शर्तें

  • यह कॉन्फ़िगरेशन करने के लिए, आपको Apigee Private Cloud सिस्टम एडमिन होना चाहिए. साथ ही, आपके पास ग्लोबल सिस्टम एडमिन क्रेडेंशियल भी होने चाहिए.
  • आपको अपने Apigee Edge Private Cloud को इंस्टॉल करने की रूट डायरेक्ट्री के बारे में पता होना चाहिए. डिफ़ॉल्ट रूट डायरेक्ट्री /opt है.

सिलसिलेवार तरीके से सेटअप करने का उदाहरण

बाहरी भूमिका की मैपिंग सेट अप करने के सिलसिलेवार उदाहरण के लिए, Apigee कम्यूनिटी फ़ोरम पर यह लेख देखें.

डिफ़ॉल्ट कॉन्फ़िगरेशन

बाहरी भूमिका की मैपिंग डिफ़ॉल्ट रूप से बंद होती है.

बाहरी भूमिका की मैपिंग चालू करना

  1. नीचे दिए गए कॉन्फ़िगरेशन को पूरा करने से पहले, आपको एक ऐसी Java क्लास बनानी होगी जो externalRoleMapperServiceV2 इंटरफ़ेस को लागू करती हो और जिसे मैनेजमेंट सर्वर क्लासपाथ में लागू किया गया हो:

    /opt/apigee/edge-management-server/lib/thirdparty/

    इसे लागू करने के बारे में ज़्यादा जानकारी के लिए, बाद में इस दस्तावेज़ पर externalRoleMapperImpl सैंपल लागू करने के बारे में सेक्शन देखें.
  2. अपने Apigee Edge मैनेजमेंट सर्वर में लॉग इन करें और इसके बाद, मैनेजमेंट सर्वर की प्रोसेस को बंद कर दें:
    > /opt/apigee/apigee-service/bin/apigee-service Edge-management-server बंद
  3. टेक्स्ट एडिटर में /opt/apigee/customer/application/management-server.properties खोलें. अगर यह फ़ाइल मौजूद नहीं है, तो इसे बनाएं.
  4. नीचे दी गई सेटिंग करने के लिए, प्रॉपर्टी फ़ाइल में बदलाव करें:
    # पुष्टि करने के लिए इस्तेमाल किया जाने वाला यूज़र स्टोर.
    # LDAP उपयोगकर्ता स्टोर के लिए "externalized.authentication" का इस्तेमाल करें.
    # ध्यान दें कि अनुमति देने के लिए, हम एलडीएपी का इस्तेमाल करते हैं.
    # बाहरी तौर पर पुष्टि करने की सुविधा को चालू करने के लिए, बाहरी सोर्स से पुष्टि करने की सुविधा ज़्यादा देखें.
    conf_security_authentication.user.store=externalized.authentication

    #बाहरी अनुमति देने वाले रोल को मैप करने वाले टूल को चालू करें.
    conf_security_externalized.authentication.role.mapper.enabled=true conf_security_externalized.authentication.role.mapper. इलाज.क्लास=
    com.customer.authorization.impl.ExternalRoleMapperImpl

    ज़रूरी जानकारी:
    ऊपर दिए गए कॉन्फ़िगरेशन में बताए गए किसी भी क्लास और पैकेज के नाम को लागू करने के लिए जिस भी क्लास और पैकेज नाम का इस्तेमाल किया जा सकता है वह है (बाहरी) इस क्लास को लागू करने के बारे में ज़्यादा जानने के लिए, नीचे दी गई externalRoleMapperImpl का नमूना लागू करने के बारे में जानकारी क्लास देखें. यह ऐसी क्लास है जिसे आपको अपने ग्रुप दिखाने के लिए लागू करना होगा.
  5. management-server.properties फ़ाइल को सेव करें.
  6. पक्का करें कि management-server.properties का मालिकाना हक, apigee उपयोगकर्ता के पास हो:?
    > chown apigee:apigee /opt/apigee/customer/application/management-server.properties
  7. मैनेजमेंट सर्वर शुरू करें:
    > /opt/apigee/apigee-service/bin/apigee-service lead-management-server start

बाहरी अनुमति देने की सुविधा बंद करना

बाहरी अनुमति की सुविधा बंद करने के लिए:

  1. टेक्स्ट एडिटर में /opt/apigee/customer/application/management-server.properties खोलें. अगर फ़ाइल मौजूद नहीं है, तो उसे बनाएं.
  2. पुष्टि करने वाले उपयोगकर्ता स्टोर को ldap में बदलें:
    conf_security_authentication.user.store=ldap
  3. इस प्रॉपर्टी को 'गलत है' पर सेट करें:
    conf_security_externalized.authentication.role.mapper.enabled=false
  4. मैनेजमेंट सर्वर को रीस्टार्ट करें:
    > /opt/apigee/apigee-service/bin/apigee-service lead-management-server start

externalRoleMapperImpl का नमूना लागू करने के बारे में जानकारी

बाहरी रोल की मैपिंग को चालू करने में, पहले बताई गई security.properties कॉन्फ़िगरेशन फ़ाइल में, इस लाइन पर ध्यान दें:

externalized.authentication.role.mapper.implementation.class=com.customer.authorization.impl.ExternalRoleMapperImpl

यह क्लास, externalRoleMapperServiceV2 इंटरफ़ेस को लागू करती है और यह ज़रूरी है. आपको इस क्लास के लिए खुद लागू करना होगा, जो आपके संबंधित ग्रुप के बारे में बताता हो. काम पूरा हो जाने पर, कंपाइल किए गए क्लास को JAR में रखें और उस JAR को मैनेजमेंट सर्वर के क्लासपाथ में रखें:

/opt/apigee/edge-management-server/lib/thirdparty/

आप क्लास और पैकेज का नाम अपनी पसंद के मुताबिक रख सकते हैं. हालांकि, इसके लिए ज़रूरी है कि वह externalRoleMapperServiceV2 को लागू करता हो और इसे आपके क्लासपाथ में ऐक्सेस किया जा सकता हो. साथ ही, इसकी जानकारी management-server.properties कॉन्फ़िगरेशन फ़ाइल में सही तरीके से दी गई हो.

नीचे हमने externalRoleMapperImpl क्लास का अच्छी तरह से समीक्षा किया गया नमूना लागू किया गया है.

package com.customer.authorization.impl;

import com.apigee.authentication.*;
import com.apigee.authorization.namespace.OrganizationNamespace;
import com.apigee.authorization.namespace.SystemNamespace;
import java.util.Collection;
import java.util.HashSet;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/** *
* Sample Implementation constructed with dummy roles with expected namespaces.
*/

public class ExternalRoleMapperImpl
       implements ExternalRoleMapperServiceV2 {

   InitialDirContext dirContext = null;

   @Override
   public void start(ConfigBean arg0) throws ConnectionException {

       try {
           // Customer Specific Implementation will override the
           // ImplementDirContextCreationLogicForSysAdmin method implementation.
           // Create InitialDirContext based on the system admin user credentials.
           dirContext = ImplementDirContextCreationLogicForSysAdmin();
       } catch (NamingException e) {
           // TODO Auto-generated catch block
           throw new ConnectionException(e);
       }
   }

   @Override
   public void stop() throws Exception {
   }

   /**
    * This method should be replaced with customer's implementation
    * For given roleName under expectedNamespace, return all users that belongs to this role
    * @param roleName
    * @param expectedNamespace
    * @return All users that belongs to this role. For each user, please return the username/email that is stored in Apigee LDAP
    * @throws ExternalRoleMappingException
    */
   @Override
   public Collection<String> getUsersForRole(String roleName, NameSpace expectedNamespace) throws ExternalRoleMappingException {
       Collection<String> users = new HashSet<>();
       if (expectedNamespace instanceof SystemNamespace) {
           // If requesting all users with sysadmin role
           if (roleName.equalsIgnoreCase("sysadmin")) {
               // Add sysadmin's email to results
               users.add("sysadmin@wacapps.net");
           }
       } else {
           String orgName = ((OrganizationNamespace) expectedNamespace).getOrganization();
           // If requesting all users of engRole in Apigee LDAP
           if (roleName.equalsIgnoreCase("engRole")) {
               // Get all users in corresponding groups in customer's LDAP. In this case looking for 'engGroup';
               SearchControls controls = new SearchControls();
               controls.setSearchScope(1);
               try {
                   NamingEnumeration<SearchResult> res = dirContext.search("ou=groups,dc=corp,dc=wacapps,dc=net",
                           "cn=engGroup", new Object[]{"",""}, controls);
                   while (res.hasMoreElements()) {
                       SearchResult sr = res.nextElement();
                       // Add all users into return
                       users.addAll(sr.getAttributes().get("users").getAll());
                   }
               } catch (NamingException e) {
                   // Customer needs to handle the exception here
               }
           }
       }
       return users;
   }

   /**
    *
    * This method would be implemented by the customer and would be invoked
    * while including using X-Apigee-Current-User header in request.
    *
    * X-Apigee-Current-User allows the customer to login as another user
    *
    * Below is the basic example.
    *
    * If User has sysadmin role then it's expected to set SystemNameSpace
    * along with the expected NameSpace. Otherwise role's expectedNameSpace
    * to be set for the NameSpacedRole.
    *
    * Collection<NameSpacedRole> results = new HashSet<NameSpacedRole>();
    *
    * NameSpacedRole sysNameSpace = new NameSpacedRole("sysadmin",
    * SystemNamespace.get());
    *
    * String orgName =
    * ((OrganizationNamespace) expectedNameSpace).getOrganization();
    *
    * NameSpacedRole orgNameSpace = new NameSpacedRole ("orgadmin",
    * expectedNameSpace);
    *
    * results.add(sysNameSpace);
    *
    * results.add(orgNameSpace);
    *
    *
    * @param username UserA's username
    * @param password UserA's password
    * @param requestedUsername UserB's username. Allow UserA to request UserB's userroles with
    *                          UserA's credentials when requesting UserB as X-Apigee-Current-User
    * @param expectedNamespace
    * @return
    * @throws ExternalRoleMappingException
    */
   @Override
   public Collection<NameSpacedRole> getUserRoles(String username, String password, String requestedUsername, NameSpace expectedNamespace) throws ExternalRoleMappingException {
       /************************************************************/
       /******************** Authenticate UserA ********************/
       /************************************************************/

       // Customer Specific Implementation will override the
       // ImplementDnameLookupLogic method implementation.

       // obtain dnName for given username.
       String dnName = ImplementDnNameLookupLogic(username);
       // Obtain dnName for given requestedUsername.
       String requestedDnName = ImplementDnNameLookupLogic(requestedUsername);

       if (dnName == null || requestedDnName == null) {
           System.out.println("Error ");
       }

       DirContext dirContext = null;
       try {

           // Customer Specific Implementation will override the
           // ImplementDirectoryContextCreationLogic method implementation

           // Create a directory context with dnName or requestedDnName and password
           dirContext = ImplementDirectoryContextCreationLogic();

           /************************************************/
           /*** Map internal groups to apigee-edge roles ***/
           /************************************************/
           return apigeeEdgeRoleMapper(dirContext, requestedDnName, expectedNamespace);

       } catch (Exception ex) {
           ex.printStackTrace();
           System.out.println("Error in authenticating User: {}" + new Object[] { username });

       } finally {
           // Customer implementation to close
           // ActiveDirectory/LDAP context.
       }

       return null;

   }

   /**
    *
    * This method would be implemented by the customer and would be invoked
    * wihle using username and password for authentication and without the
    * X-Apigee-Current-User header
    *
    * The customer can reuse implementations in
    *      getUserRoles(String username, String password, String requestedUsername, NameSpace expectedNamespace)
    * by
    *      return getUserRoles(username, password, username, expectedNamespace)
    * in implementations.
    *
    * or the customer can provide new implementations as shown below.
    */

   @Override
   public Collection<NameSpacedRole> getUserRoles(String username, String password, NameSpace expectedNamespace) throws ExternalRoleMappingException {
       /*************************************************************/
       /****************** Authenticate Given User ******************/
       /*************************************************************/

       // Customer Specific Implementation will override the
       // ImplementDnameLookupLogic implementation.

       // Obtain dnName for given username or email address.
       String dnName = ImplementDnNameLookupLogic(username);

       if (dnName == null) {
           System.out.println("Error ");
       }

       DirContext dirContext = null;
       try {
           // Create a directory context with username or dnName and password
           dirContext = ImplementDirectoryContextCreationLogic();

           /************************************************/
           /*** Map internal groups to apigee-edge roles ***/
           /************************************************/
           return apigeeEdgeRoleMapper(dirContext, dnName, expectedNamespace);

       } catch (Exception ex) {
           ex.printStackTrace();
           System.out.println("Error in authenticating User: {}" + new Object[] { username });

       } finally {
           // Customer implementation to close
           // ActiveDirectory/LDAP context.
       }

       return null;
   }

   /**
    *
    * This method would be implemented by the customer and would be invoked
    * while using security token or access token as authentication credentials.
    *
    */
   @Override
   public Collection<NameSpacedRole> getUserRoles(String username, NameSpace expectedNamespace) throws ExternalRoleMappingException {

       /*************************************************************/
       /****************** Authenticate Given User ******************/
       /*************************************************************/

       // Customer Specific Implementation will override the
       // ImplementDnameLookupLogic implementation.

       // Obtain dnName for given username or email address.
       String dnName = ImplementDnNameLookupLogic(username);

       if (dnName == null) {
           System.out.println("Error ");
       }

       DirContext dirContext = null;
       try {
           // Create a directory context with username or dnName and password
           dirContext = ImplementDirectoryContextCreationLogic();

           /************************************************/
           /*** Map internal groups to apigee-edge roles ***/
           /************************************************/
           return apigeeEdgeRoleMapper(dirContext, dnName, expectedNamespace);

       } catch (Exception ex) {
           ex.printStackTrace();
           System.out.println("Error in authenticating User: {}" + new Object[] { username });

       } finally {
           // Customer implementation to close
           // ActiveDirectory/LDAP context.
       }

       return null;
   }

   /**
    *  This method should be replaced with Customer Specific Implementations
    *
    *  Provided as a sample Implementation of mapping user groups to apigee-edge roles
    */
   private Collection<NameSpacedRole> apigeeEdgeRoleMapper(DirContext dirContext, String dnName, NameSpace expectedNamespace) throws Exception {

       Collection<NameSpacedRole> results = new HashSet<NameSpacedRole>();

       /****************************************************/
       /************ Fetch internal groups *****************/
       /****************************************************/

       String groupDN = "OU=Groups,DC=corp,DC=wacapps,DC=net";
       String userFilter = "(user=userDnName)";
       SearchControls controls = new SearchControls();
       controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);

       // Looking for all groups the user belongs to in customer's LDAP
       NamingEnumeration<SearchResult> groups = dirContext.search(groupDN,userFilter.replace("userDnName", dnName), new Object[] { "", "" }, controls);

       if (groups.hasMoreElements()) {
           while (groups.hasMoreElements()) {
               SearchResult searchResult = groups.nextElement();
               Attributes attributes = searchResult.getAttributes();
               String groupName = attributes.get("name").get().toString();

               /************************************************/
               /*** Map internal groups to apigee-edge roles ***/
               /************************************************/

               if (groupName.equals("BusDev")) {
                   results.add(new NameSpacedRole("businessAdmin",SystemNamespace.get()));

               } else if (groupName.equals("Engineering")) {
                   if (expectedNamespace instanceof OrganizationNamespace) {
                       String orgName = ((OrganizationNamespace) expectedNamespace).getOrganization();
                       results.add(new NameSpacedRole("orgadmin", new OrganizationNamespace(orgName)));
                   }

               } else if (groupName.equals("Marketing")) {
                   results.add(new NameSpacedRole("marketAdmin",SystemNamespace.get()));

               } else {
                   results.add(new NameSpacedRole("readOnly",SystemNamespace.get()));
               }
           }

       } else {
           // In case of no group found or exception found we throw empty roles.
           System.out.println(" !!!!! NO  GROUPS FOUND !!!!!");
       }
       return results;
   }

   /**
    * The customer need to replace with own implementations for getting dnName for given user
    */
   private String ImplementDnNameLookupLogic(String username) {
       // Connect to the customer's own LDAP to fetch user dnName
       return customerLDAP.getDnName(username);
   }

   /**
    * The customer need to replace with own implementations for creating DirContext
    */
   private DirContext ImplementDirectoryContextCreationLogic() {
       // Connect to the customer's own LDAP to create DirContext for given user
       return customerLDAP.createLdapContextUsingCredentials();
   }

}