BasicAuthentication 政策執行階段錯誤疑難排解

您目前查看的是 ApigeeApigee Hybrid 說明文件。
查看 Apigee Edge 說明文件。

UnresolvedVariable

錯誤代碼

steps.basicauthentication.UnresolvedVariable

錯誤回應主體

{
    "fault": {
        "faultstring": "Unresolved variable : [variable_name]",
        "detail": {
            "errorcode": "steps.basicauthentication.UnresolvedVariable"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "Unresolved variable : request.header.Authorization",
        "detail": {
            "errorcode": "steps.basicauthentication.UnresolvedVariable"
        }
    }
}

原因

如果BasicAuthentication 政策<Source> 元素中指定的 Base64 編碼字串所含變數符合下列任一條件,就會發生這項錯誤:

  • 超出範圍 (無法在執行政策的特定流程中使用)
  • 無法解析 (未定義)

舉例來說,如果 BasicAuthentication 政策在 <Source> 元素中指定變數為 request.header.Authorization,但 Authorization 標頭未做為 API 要求的一部分傳遞,就會發生這個錯誤。

診斷

  1. 找出 BasicAuthentication 政策中用於 <Source> 元素的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列 faultstring, 中,用於 <Source> 元素的變數是 request.header.Authorization

    "faultstring": "Unresolved variable : request.header.Authorization"
    
  2. 檢查發生失敗的特定 API Proxy 中的所有 BasicAuthentication 政策。可能有一或多項 BasicAuthentication 政策。找出特定 BasicAuthentication 政策,其中 <Source> 元素中指定的變數與錯誤字串中識別的變數名稱相符 (如上述步驟 1 所示)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
      <DisplayName>Decode Basic Authentication Header</DisplayName>
      <Operation>Decode</Operation>
      <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
      <User ref="request.header.username"/>
      <Password ref="request.header.password"/>
      <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 判斷變數是否已定義,且可在執行 BasicAuthentication 政策的流程中使用。

  4. 如果變數為下列任一值:

    1. 超出範圍 (無法在執行政策的特定流程中使用)
    2. 無法解析 (未定義)

    這就是錯誤原因。

    在上述 BasicAuthentication 政策範例中,變數 request.header.Authorization 並未做為要求的一部分傳遞。也就是說,API 要求是在沒有授權標頭的情況下提出,如下所示:

    curl -v "http://$EXTERNAL_IP/basicauth"

    其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

    由於 BasicAuthentication 政策中使用的變數無法使用,您會收到下列錯誤代碼:

    steps.basicauthentication.UnresolvedVariable
    

解析度

確認 BasicAuthentication 政策中使用的變數已定義或做為輸入內容傳遞,且可在執行政策的流程中使用。

如要修正上述 BasicAuthentication 政策範例的問題,請發出如下所示的 API 要求:

curl -v "http://$EXTERNAL_IP/basicauth" -H "Authorization: Basic YWthc2g6MTIz"

其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

InvalidBasicAuthenticationSource

錯誤代碼

steps.basicauthentication.InvalidBasicAuthenticationSource

錯誤回應主體

{
    "fault": {
        "faultstring": "Source variable : [variable_name] for basic authentication decode policy is not valid",
        "detail": {
            "errorcode": "steps.basicauthentication.InvalidBasicAuthenticationSource"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid",
        "detail": {
            "errorcode": "steps.basicauthentication.InvalidBasicAuthenticationSource"
        }
    }
}

可能原因

這項錯誤的可能原因包括:

原因 說明
來源變數無效 BasicAuthentication 政策的輸入來源變數不是有效的 Base64 編碼字串。
標頭格式有誤 傳遞至 BasicAuthentication 原則的 Base64 編碼字串所含標頭格式有誤。

原因:來源變數無效

如果基本驗證政策<Source> 元素中指定的 Base64 編碼字串所含變數沒有有效值,就會發生這個錯誤。

舉例來說,如果 BasicAuthentication 政策的 <Source> 元素中指定的變數沒有有效的 Base64 編碼字串,就會發生這項錯誤。

診斷

  1. 找出 BasicAuthentication 政策中用於 <Source> 元素的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列 faultstring, 中,用於 <Source> 元素的變數是 request.header.Authorization

    "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid"
    
  2. 檢查發生失敗的特定 API Proxy 中的所有 BasicAuthentication 政策。可能有一或多項 BasicAuthentication 政策。找出變數在 <Source> 元素中指定的特定 BasicAuthentication 政策,與錯誤字串中識別的變數名稱相符 (如上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
        <DisplayName>Decode Basic Authentication Header</DisplayName>
        <Operation>Decode</Operation>
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <User ref="request.header.username"/>
        <Password ref="request.header.password"/>
        <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 如果 <Source> 元素中指定的變數所儲存的值不是有效的 Base64 編碼字串,則 就是造成錯誤的原因。

    在上述 BasicAuthentication 政策範例中,用戶端以標頭形式傳送的變數 request.header.Authorization無效,如下所示:

    curl -v "http://$EXTERNAL_IP /basicauth" -H "Authorization: Basic 23435"

    其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

    由於變數 request.header.Authorization 含有無效的 Base64 編碼字串「23435"」,因此您會收到下列錯誤代碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource

解析度

請確認 BasicAuthentication 政策中為 <Source> 元素指定的變數含有有效的 Base64 編碼字串。

如要修正上述 BasicAuthentication 政策的範例問題,請在 Authorization 標頭中傳遞有效的 Base64 編碼字串,提出 API 要求,如下所示:

curl -v "http://$EXTERNAL_IP/basicauth" -H "Authorization: Basic YWthc2g6MTIz"

其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

原因:標頭格式有誤/無效

如果傳遞至 BasicAuthentication 政策的標頭格式錯誤或無效,就會發生這項錯誤。

舉例來說,如果 BasicAuthentication 政策在 <Source> 元素中指定了 request.header.Authorization 變數,且做為 API 要求一部分傳遞的標頭格式有誤/無效,就會發生這項錯誤。

診斷

  1. 找出 BasicAuthentication 政策中用於 <Source> 元素的變數。您可以在錯誤回應的 faultstring 元素中找到這項資訊。舉例來說,在下列錯誤中,用於 <Source> 元素的變數是 request.header.Authorization

    "faultstring": "Source variable : request.header.Authorization for basic authentication decode policy is not valid"
    
  2. 檢查發生失敗的特定 API Proxy 中的所有 BasicAuthentication 政策。可能有一或多項 BasicAuthentication 政策。找出特定 BasicAuthentication 政策,其中 <Source> 元素中指定的變數與錯誤字串中識別的變數名稱相符 (如上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 request.header.Authorization 的變數,與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <BasicAuthentication name="DecodeBaseAuthHeaders">
        <DisplayName>Decode Basic Authentication Header</DisplayName>
        <Operation>Decode</Operation>
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <User ref="request.header.username"/>
        <Password ref="request.header.password"/>
        <Source>request.header.Authorization</Source>
    </BasicAuthentication>
    
  3. 如果變數是標頭,且格式有誤或無效 (即沒有驗證類型,或驗證類型不是「Basic」),就會導致錯誤。

    Authorization 標頭的格式如下:

    Authorization: <Authentication type> <credentials>
    

    格式錯誤的標頭可透過兩種方式傳遞至 BasicAuthentication 政策:

    範例 1:標頭中沒有驗證類型:

    curl -v "http://$EXTERNAL_IP/basicauth" -H "Authorization: YWthc2g6MTIz"

    其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

    在上述範例中,「Authorization」標頭沒有驗證類型。如果將這個標頭傳遞至上述 BasicAuthentication 政策,您會收到下列錯誤碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource
    

    範例 2:驗證類型不是「基本」類型:

    curl -v "http://$EXTERNAL_IP/basicauth" -H "Authorization: OAuth YWthc2g6MTIz"

    其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。

    在上述範例中,「Authorization」標頭沒有「Basic」驗證類型。如果將這個值傳遞至上述 BasicAuthentication 政策,您會收到下列錯誤代碼:

    steps.basicauthentication.InvalidBasicAuthenticationSource
    

    同樣地,如果授權標頭中使用的驗證類型為 Bearer、Digest Auth 等,也可能發生這項錯誤。

解析度

確認傳遞 BasicAuthentication 政策輸入內容的標頭具有驗證類型,且類型為「Basic」。

如要修正上述範例 BasicAuthentication 政策的問題,請傳遞有效的 Base64 編碼字串,並在 Authorization 標頭中加入「Basic」驗證類型,提出 API 要求,如下所示:

curl -v "http://$EXTERNAL_IP/basicauth" -H "Authorization: Basic YWthc2g6MTIz"

其中 $EXTERNAL_IP 是外部負載平衡器的 IP 位址。 這個 IP 位址會向網際網路公開。詳情請參閱「自訂存取路徑」。