LDAP 政策

您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件
info

結果

LDAP 政策提供以下功能:

  • 驗證:系統會根據 LDAP 供應商中的憑證,驗證要求中提供的使用者憑證。LDAP 政策提供彈性極高的驗證方式,讓您使用任何 DN 值和密碼,即使要求中沒有您想要的 DN 值也沒問題。舉例來說,假設您需要使用電子郵件 / 密碼進行驗證,可能出現的選項如下:
    • 如果要求中包含電子郵件地址,您只要使用該地址和 LDAP 驗證密碼即可。
    • 如果要求中沒有電子郵件,但有其他 DN 屬性 (例如電話號碼), 您可以使用電話號碼從 LDAP 取得對應的電子郵件,然後使用電子郵件/密碼進行驗證。
  • 辨別名稱 (DN) 搜尋:除了驗證之外,您也可以使用 LDAP 政策識別要求中的使用者屬性 (例如電子郵件),並執行查詢,從 LDAP 擷取該使用者的其他 DN 屬性。擷取的 DN 會儲存在變數中。

如果受保護資源的存取權應僅限於 LDAP 供應商中的使用者 (例如管理員使用者、機構使用者和開發人員),請使用 LDAP 政策,尤其是在 OAuth 權杖存取權不必要或過於繁重時。這項政策也適用於擷取網域名稱中繼資料,以用於 API Proxy 流程。

舉例來說,您可以設定只有在使用者通過 LDAP 驗證時,才執行 API 呼叫;驗證成功後,您也可以選擇擷取使用者的 DN (網域名稱) 屬性。

詳情請參閱:

範例

使用者名稱/密碼驗證

<Ldap name="4GLdapPolicy">
   <LdapResource>ldap1</LdapResource>
   <Authentication>
       <UserName ref="request.header.username"/>
       <Password ref="request.header.password"/>
       <Scope>subtree</Scope>
       <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
    </Authentication>
 </Ldap>

這個範例會根據 LDAP 供應商進行驗證。這項政策會將要求中的使用者名稱和密碼傳遞至 LDAP 進行驗證。

DN 屬性驗證

<Ldap name="LdapPolicy">
   <LdapResource>ldap1</LdapResource>
   <Authentication>
       <Password ref="request.header.password"/>
       <SearchQuery>mail={request.header.mail}</SearchQuery>
       <Scope>subtree</Scope>
       <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
    </Authentication>
 </Ldap>

這項政策會透過要求標頭中的電子郵件地址取得使用者的 DN,然後使用要求標頭中提供的密碼,向 LDAP 驗證使用者身分。

搜尋 LDAP

<Ldap name="LdapPolicy">
    <!-- using a custom LDAP provider -->
    <LdapConnectorClass>com.custom.ldap.MyProvider</LdapConnectorClass>
    <LdapResource>MyLdap</LdapResource>
    <Search>
        <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
        <SearchQuery>mail={request.header.mail}</SearchQuery>
        <Attributes>
            <Attribute>address</Attribute>
            <Attribute>phone</Attribute>
            <Attribute>title</Attribute>
        </Attributes>
        <Scope></Scope> <!-- default is subtree -->
    </Search>
</Ldap>

這項政策會參照自訂 LDAP 提供者。這項服務會使用要求標頭中的電子郵件地址識別使用者,然後從 LDAP 擷取使用者的地址、電話號碼和職稱。擷取的 DN 屬性會儲存在變數中。請參閱「政策專屬變數」。

如要搜尋 LDAP 並擷取 DN 屬性,要求必須包含管理員憑證。

元素參考資料

以下說明 LDAP 政策元素和屬性。

元素

說明

Ldap

具有名稱屬性的父項元素,可供您輸入政策名稱。

LdapConnectorClass

使用 LDAP 政策搭配自訂 LDAP 提供者 (非 Apigee 提供) 時,請指定完整的 LDAP 連接器類別。這是您實作 Apigee ExternalLdapConProvider 介面的類別。

LdapResource

輸入 LDAP 資源的環境名稱。詳情請參閱「建立 LDAP 資源」。

BaseDN

所有資料所在的 LDAP 基本層級。舉例來說,在 Apigee 的 LDAP 提供者中,所有資料都位於 dc=apigee,dc=com 下方。

  • ref:用於指定包含 BaseDN 值的流程變數,例如 apigee.baseDN。ref 的優先順序高於明確的 BaseDN 值。如果同時指定 ref 和 value,系統會優先採用 ref。如果 ref 在執行階段無法解析,系統會使用 value。

Scope

  • object:驗證或搜尋只會在 LDAP 的基本層級進行。
  • onelevel:驗證或搜尋作業發生在基本層級的下一層級。
  • 子樹狀結構 (預設):驗證或搜尋作業會在基本層級執行,並完整遞迴至基本層級以下。

驗證

Authentication

您實作的驗證行為父項元素。

UserName

空白元素,可採用下列其中一個屬性:

  • ref:要求中的使用者名稱參照,例如 request.header.username
  • value:使用者名稱本身

如果您未使用使用者名稱進行驗證,或要求中未包含使用者名稱,則不需要加入這個元素。

如果要求中包含使用者名稱,但您想使用使用者名稱以外的 DN 屬性 (例如電子郵件) 驗證使用者,請加入 SearchQuery,取得與密碼相關聯的使用者電子郵件。LDAP 政策會使用使用者名稱查詢 LDAP 供應商的對應電子郵件地址,然後用於驗證。

Password

空白元素,可採用下列其中一個屬性:

  • ref:要求中密碼的參照,例如 request.header.password
  • value:加密後的密碼本身

SearchQuery

如要使用使用者名稱以外的 DN 屬性 (例如電子郵件) 進行驗證,請設定 LDAP 政策,從要求中取得 DN 屬性 (例如使用者名稱),用於在 LDAP 中識別使用者、擷取電子郵件,並驗證使用者。

舉例來說,假設 LDAP 定義了「mail」屬性來儲存電子郵件地址:

<SearchQuery>mail={request.header.mail}</SearchQuery>

搜尋

Search

您實作的搜尋行為的父項元素。

SearchQuery

透過要求或回應中的中繼資料識別使用者,您可以使用這個元素從 LDAP 擷取使用者的其他 DN 屬性。舉例來說,如果要求包含使用者電子郵件地址,且 LDAP 定義了用於儲存使用者電子郵件地址的 mail 屬性,您可以使用下列設定:

<SearchQuery>mail={request.header.mail}</SearchQuery>

這項查詢會在 LDAP 中搜尋與要求中的電子郵件地址相符的電子郵件,而政策現在可透過「Attributes」元素,擷取該使用者的其他 DN 屬性。

Attributes

使用一或多個 <Attribute> 元素,找出要為使用者擷取的 DN 中繼資料。至少需要一個屬性。

舉例來說,SearchQuery 識別使用者後,政策現在可以擷取使用者的 DN 屬性,例如地址、電話號碼和使用者職稱,如下例所示。

屬性值是 LDAP 中定義的 DN 屬性名稱。

<Attributes>
  <Attribute>address</Attribute>
  <Attribute>phone</Attribute>
  <Attribute>title</Attribute>
</Attributes>

使用須知

Apigee Edge for Private Cloud 可讓您在 API 呼叫中運用 LDAP 供應商。透過 LDAP 政策,應用程式可以根據儲存在 LDAP 中的使用者驗證憑證,並從 LDAP 擷取辨別名稱 (DN) (與每個使用者相關聯的屬性,例如電子郵件、地址和電話號碼)。傳回的 DN 會儲存在變數中,供 API 代理程式進一步使用。

建立 LDAP 資源

LDAP 政策會使用您在 Apigee Edge 中建立的 LDAP 資源。LDAP 資源會提供 LDAP 存放區的連線資訊。

如要建立及管理 LDAP 資源,請使用下列 API 和酬載:

API

建立 LDAP 資源 (POST) 或列出所有 LDAP 資源 (GET):

/v1/organizations/org_name/environments/environment/ldapresources

取得 LDAP 資源的詳細資料 (GET)、更新 (POST) 及刪除 (DELETE):

/v1/organizations/org_name/environments/environment/ldapresources/ldap_resource_name

酬載

以下是附有使用註解的 XML 酬載範例。

<LdapResource name="ldap1">
  <Connection>
    <Hosts>
      <!-- port is optional: defaults to 389 for ldap:// and 636 for ldaps:// -->
      <Host port="636">foo.com</Host>
    </Hosts>
    <SSLEnabled>false</SSLEnabled> <!-- optional, defaults to false -->
    <Version>3</Version> <!-- optional, defaults to 3-->
    <Authentication>simple</Authentication> <!-- optional, only simple supported -->
    <ConnectionProvider>jndi|unboundid</ConnectionProvider> <!-- required -->
    <ServerSetType>single|round robin|failover</ServerSetType> <!-- not applicable for jndi -->
    <!-- If using a custom LDAP provider, the fully qualified class: -->
    <LdapConnectorClass>com.custom.ldap.MyProvider</LdapConnectorClass>
  </Connection>
  <ConnectPool enabled="true"> <!-- enabled is optional, defaults to true -->
    <Timeout>30000</Timeout> <!-- optional, in milliseconds; if not set, no timeout -->
    <Maxsize>50</Maxsize> <!-- optional; if not set, no max connections -->
    <Prefsize>30</Prefsize> <!-- optional; if not set, no pref size -->
    <Initsize></Initsize> <!-- optional; if not set, defaults to 1 -->
    <Protocol></Protocol> <!-- optional; if not set, defaults to 'ssl plain' -->
  </ConnectPool>
  <Admin>
    <DN>cn=manager,dc=apigee,dc=com</DN>
    <Password>secret</Password>
  </Admin>
</LdapResource>

curl 範例:建立 LDAP 資源

以下範例會建立名為 ldap1 的 LDAP 資源。

curl -X POST -H "Content-Type: application/xml" \
  https://api.enterprise.apigee.com/v1/organizations/myorg/environments/test/ldapresources \
  -u apigee_email:password -d \
  '<LdapResource name="ldap1">
    <Connection>
      <Hosts>
      <Host>foo.com</Host>
      </Hosts>
      <SSLEnabled>false</SSLEnabled>
      <Version>3</Version>
      <Authentication>simple</Authentication>
      <ConnectionProvider>unboundid</ConnectionProvider>
      <ServerSetType>round robin</ServerSetType>
    </Connection>
    <ConnectPool enabled="true">
      <Timeout>30000</Timeout>
      <Maxsize>50</Maxsize>
      <Prefsize>30</Prefsize>
      <Initsize></Initsize>
      <Protocol></Protocol>
    </ConnectPool>
    <Admin>
      <DN>cn=manager,dc=apigee,dc=com</DN>
      <Password>secret</Password>
    </Admin>
  </LdapResource>'

回應代碼

以下是政策在成功或失敗時傳回的 HTML 回應碼:

  • 成功:200
  • 失敗:401

在 Edge for Private Cloud 中使用自訂 LDAP 供應商

使用自訂 LDAP 提供者

Apigee Edge for Private Cloud 隨附的 LDAP 提供者已設定完畢,可與 LDAP 政策互動。不過,如果您使用自訂 LDAP 提供者,則必須啟用該提供者,才能支援 LDAP 政策。步驟如下:

  1. 在 LDAP 供應器類別中,實作 ExternalLdapConProvider 介面。
    public interface ExternalLdapConProvider {
      void doAuthentication(LdapBean LlapBean, String userDN, String password, String baseDN);
    
      void doSearchAndAuthentication(LdapBean LlapBean, String password, String baseDN, String query, int scope);
    
      Collection<Map<String, String[]>> doSearch(LdapBean LlapBean, String query,
        String baseDN, Collection<String> requiredAttributes, int scope);
    
      void closeConnections();
    }
  2. 在政策設定的 <LdapConnectorClass> 中 (請參閱下節),新增自訂 LDAP 提供者的完整類別名稱。
  3. 下載這個檔案:custom-ldap.jar_.zip。 (你可能需要按一下滑鼠右鍵並選取「另存新檔」)。
  4. 解壓縮。
  5. 將 custom-ldap.jar 檔案新增至環境,並確認該檔案位於類別路徑中。
  6. 為 LDAP 供應商建立環境資源。您會在 LDAP 政策的 <LdapResource> 元素中使用環境資源名稱。

使用 UnboundID LDAP SDK for Java

您可以使用 UnboundID LDAP SDK 搭配 LDAP 政策,但必須先下載 2.3.1 版,並將其新增至每個 Message Processor 的類別路徑。

如要搭配使用 UnboundID LDAP SDK 與 LDAP 政策,請按照下列步驟操作:

  1. 開啟瀏覽器,然後前往 UnboundID LDAP SDK 的 Sourceforge 檔案存放區:
    https://sourceforge.net/projects/ldap-sdk/files/
  2. 找出 SDK 2.3.1 版 (SE 或標準版),然後下載該版本的 ZIP 檔案。例如,下載「unboundid-ldapsdk-2.3.1-se.zip」。
  3. 從 SDK ZIP 檔案中解壓縮 JAR 檔案,如下列範例所示:
    unzip -j -d ~/tmp ~/Downloads/unboundid-ldapsdk-2.3.1-se.zip unboundid-ldapsdk-2.3.1-se/unboundid-ldapsdk-se.jar

    這個指令只會將 JAR 檔案解壓縮到 ~/tmp 目錄。雖然這是選用功能,但它會捨棄目錄結構 (使用 -j)。

  4. 在每個 Message Processor 節點上:
    1. 將 JAR 檔案複製到 Message Processor 的 /opt/apigee/edge-gateway/lib/thirdparty 目錄。
    2. 如有必要,請授予 Apigee 使用者 JAR 檔案的權限,訊息處理器才能存取該檔案。
    3. Edge 會將 /opt/apigee/edge-gateway/lib/thirdparty 目錄中的所有第三方程式庫新增至類別路徑。

    4. 重新啟動 Message Processor:
      /opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart

流程變數

以下是 SearchQuery 填入的 LDAP 政策變數。

變數

說明

ldap.policyName.execution.success

政策執行完畢後,這個流程變數會包含「true」或「false」值,視結果而定。

ldap.policyName.search.result[index].
  attribute.attrName[index]=value

這項變數的彈性格式 (尤其是索引) 可處理多個屬性,以及具有多個值的屬性。索引是從 1 開始的數字。如未提供索引編號,則預設索引編號為 1。

如果政策傳回地址、電話和電子郵件,您可以使用下列變數擷取第一個屬性和值:

ldap.policyName.search.result.attribute.address
ldap.policyName.search.result.attribute.phone
ldap.policyName.search.result.attribute.email

如要擷取搜尋結果中的第三個地址屬性,請使用以下程式碼:

ldap.policyName.search.result[3].attribute.address

如果屬性有多個值 (例如使用者有多個電子郵件地址),您可以從結果中擷取第二個電子郵件地址,如下所示:

ldap.policyName.search.result.attribute.mail[2]

錯誤代碼

Edge 政策傳回的錯誤會採用一致的格式,詳情請參閱「錯誤代碼參考資料」。

這項政策會使用下列錯誤代碼:

錯誤代碼 訊息
InvalidAttributeName Invalid attribute name {0}.
InvalidSearchBase Search base can not be empty.
InvalidValueForPassword Invalid value for password field. It can not be empty.
InvalidSearchScope Invalid scope {0}. Allowed scopes are {1}.
InvalidUserCredentials Invalid user credentials.
InvalidExternalLdapReference Invalid external ldap reference {0}.
LdapResourceNotFound Ldap resource {0} not found.
BaseDNRequired Base DN required.
OnlyReferenceOrValueIsAllowed Only value or reference is allowed for {0}.
AttributesRequired At least one attribute required for search action.
UserNameIsNull User name is null.
SearchQueryAndUserNameCannotBePresent Both search query and username can not be present in the authentication action. Please specify either one of them.