2017-08-09 16 views
5

के माध्यम से DocumentDB API के साथ CosmosDb मैं एपीआई प्रबंधन के माध्यम से अपने नए कॉसमॉस डीबी संग्रह से पूछने का प्रयास कर रहा हूं। एक बार साबित हो जाने पर, यह लॉग डेटा तक उपयोगकर्ता पहुंच के लिए फ्रंट-एंड होगा। इसी कारण से, मेरे पास सदस्यता आईडी द्वारा विभाजित डेटा है। मेरे WebApi डीबी के लॉग संग्रह के लिए Azure पोर्टल में मैं विभाजन कुंजी/api_subscription_key के रूप में देखता हूं। मेरे पास एपीआई एमजीटी से डेटा जा रहा है। -> इवेंट हब -> स्ट्रीम Analytics -> ब्रह्मांड।Azure API प्रबंधन

SELECT * FROM c WHERE c.api_subscription_key = '573a1c65bceb52192c140131' 

इस पीठ की उम्मीद लाता दस्तावेजों मैं सफलतापूर्वक कई दिनों

[ 
     { 
     "eventenqueuedutctimesecond": "2017-07-27T15:09:02Z", 
     "business_unit_key": null, 
     "user_key": null, 
     "api_message_id": "1718ea66-d225-45ec-b3fc-5daff4c7f426", 
     "api_identifier": "21926e9d-9206-42b0-b4b1-7e7f1eb4e7dd", 
     "api_id": "58d94cc622be39392343d4b6", 
     "api_operation_id": "58e682bde055cd0ba4215d4b", 
     "api_adapter_id": "573a1c64bceb520aac127ee5", 
     "api_subscription_id": "573a1c65bceb52192c140131", 
     "api_policy_id": "64BC4270-54AC-42DA-835C-E285F35BCA81", 
     "basic_username": "", 
     "message_version": "10", 
     "claim_business_unit_key": null, 
     "claim_user_key": null, 
    ... 
     "lasterrorsource": null, 
     "lasterrorreason": null, 
     "lasterrorscope": null, 
     "lasterrorsection": null, 
     "lasterrorpolicyid": null, 
     "id": "7/27/2017 3:09:02 PM", 
     "_rid": "9Fc0ANW4fwAoAAAAAAAADA==", 
     "_self": "dbs/9Fc0AA==/colls/9Fc0ANW4fwA=/docs/9Fc0ANW4fwAoAAAAAAAADA==/", 
     "_etag": "\"0700d90c-0000-0000-0000-597a020e0000\"", 
     "_attachments": "attachments/", 
     "_ts": 1501168140 
     }... 

मेरे CosmosDB के लिए CosmosDB के लिए लिख दिया:

Azure पोर्टल में क्वेरी एक्सप्लोरर का उपयोग करना, मैं की तरह एक प्रश्न कोशिश कर सकते हैं उदाहरण plexconnectcosmos है। एपीआई प्रबंधन और अपनी नीतियों के माध्यम से मैं

https://plexconnectcosmos.documents.azure.com/dbs/WebApi/colls/Logs/docs 
इन हेडर के साथ

करने के लिए पोस्ट कर रहा हूँ (कई बाक़ी और उम्मीद है कि कोई प्रभाव नहीं होने):

[ 
{ 
name: "Postman-Token", 
value: "756c2c21-ef23-4e5a-a63a-ae6aed961d35" 
}, 
{ 
name: "Ocp-Apim-Subscription-Key", 
value: "a2a05eff128943bc89f62b81a63aa368" 
}, 
{ 
name: "Accept-Charset", 
value: "UTF-8" 
}, 
{ 
name: "Cache-Control", 
value: "no-cache" 
}, 
{ 
name: "Content-Type", 
value: "application/query+json" 
}, 
{ 
name: "Accept", 
value: "application/json;odata=nometadata" 
}, 
{ 
name: "Accept-Encoding", 
value: "gzip,deflate" 
}, 
{ 
name: "Cookie", 
value: "x-ms-gateway-slice=008; stsservicecookie=ests; BIGipServerpmc_rest_webservices_http_prod=1242575370.20480.0000" 
}, 
{ 
name: "User-Agent", 
value: "PostmanRuntime/6.2.5" 
}, 
{ 
name: "x-ms-date", 
value: "Wed, 09 Aug 2017 20:10:09 GMT" 
}, 
{ 
name: "x-ms-version", 
value: "2017-02-22" 
}, 
{ 
name: "MaxDataServiceVersion", 
value: "3.0" 
}, 
{ 
name: "DataServiceVersion", 
value: "1.0;NetFx" 
}, 
{ 
name: "Api-Message-Id", 
value: "12427ae7-7704-44cb-b4af-d7e622898b99" 
}, 
{ 
name: "Api-Identifier", 
value: "461f0c19-8df3-4272-9ac7-c64bb776dd56" 
}, 
{ 
name: "Api-Id", 
value: "58987927bceb5204c4e59168" 
}, 
{ 
name: "Api-Operation-Id", 
value: "598b3c72e055cd14fc3abdd1" 
}, 
{ 
name: "Api-Adapter-Id", 
value: "573a1c64bceb520aac127ee5" 
}, 
{ 
name: "Api-Subscription-Id", 
value: "573a1c65bceb52192c140131" 
}, 
{ 
name: "Api-Policy-Id", 
value: "64BC4270-54AC-42DA-835C-E285F35BCA81" 
}, 
{ 
name: "X-Basic-Username", 
value: "" 
}, 
{ 
name: "x-ms-documentdb-isquery", 
value: "True" 
}, 
{ 
name: "x-ms-documentdb-query-enablecrosspartition", 
value: "False" 
}, 
{ 
name: "x-ms-max-item-count", 
value: "1000" 
}, 
{ 
name: "x-ms-documentdb-partitionkey", 
value: "573a1c65bceb52192c140131" 
}, 
{ 
name: "x-ms-partition-key", 
value: "573a1c65bceb52192c140131" 
}, 
{ 
name: "Authorization", 
value: "type=master&ver=1.0&sig=Ke...Q=" 
}, 
{ 
name: "X-Forwarded-For", 
value: "75.39.38.67" 
} 
] 

प्रतिक्रिया मैं वापस पाने या तो

{ 
    "code": "BadRequest", 
    "message": "Partition key 573a1c65bceb52192c140131 is invalid.\r\nActivityId: 61836599-fe4b-4232-b55b-2c568eecc767" 
} 

या

{ 
    "code": "Unauthorized", 
    "message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'post\ndocs\ndbs/WebApi/colls/Logs\nwed, 09 aug 2017 20:35:41 gmt\n\n'\r\nActivityId: 429....2e2" 
} 

ऐसा लगता है कि मुझे हल करने के लिए दो समस्याएं हैं। पहली जगह में मैं इस विभाजन की समस्या निवारण के बारे में कैसे जा सकता हूं? ऐसा लगता है कि यह मेरे विश्लेषण से है, यह एक वैध विभाजन है, जो पोर्टल में प्रश्नों और शीर्षकों "x-ms-documentdb-partitionkey" और "x-ms-partition-key" द्वारा सत्यापित करता है। (मैंने एमएस दस्तावेज में दोनों शीर्षलेख नाम देखे हैं, इसलिए मैं दोनों के साथ अपने अड्डों को कवर कर रहा हूं।)

"इनपुट प्राधिकरण टोकन अनुरोध की सेवा नहीं कर सकता है।" संदेश मुझे मेरी क्वेरी में कुछ भिन्न रूप से गलत बताता है। मुझे शायद डेटा मूल्य पर संदेह है? मेरी नीति एज़ूर टेबल स्टोरेज आरईएसटी एपीआई के लिए उपयोग करने से थोड़ा अलग है और मुझे कभी भी यह समस्या नहीं है। मैं उपयोग कर रहा हूँ मेरी रीड-ओनली प्राथमिक कुंजी Azure पोर्टल से लिया और एपीआई प्रबंधन के नाम पर मान में संग्रहीत:

<policies> 
    <inbound> 
     <base /> 
     <set-variable name="Content-Type" value="application/query+json" /> 
     <set-variable name="x-ms-documentdb-isquery" value="True" /> 
     <set-variable name="x-ms-documentdb-query-enablecrosspartition" value="False" /> 
     <set-variable name="x-ms-max-item-count" value="1000" /> 
     <set-variable name="x-ms-version" value="2017-02-22" /> 
     <set-header name="Content-Type" exists-action="override"> 
      <value>@((string)context.Variables["Content-Type"])</value> 
     </set-header> 
     <set-header name="x-ms-documentdb-isquery" exists-action="override"> 
      <value>@((string)context.Variables["x-ms-documentdb-isquery"])</value> 
     </set-header> 
     <set-header name="x-ms-documentdb-query-enablecrosspartition" exists-action="override"> 
      <value>@((string)context.Variables["x-ms-documentdb-query-enablecrosspartition"])</value> 
     </set-header> 
     <set-header name="x-ms-max-item-count" exists-action="override"> 
      <value>@((string)context.Variables["x-ms-max-item-count"])</value> 
     </set-header> 
     <set-header name="x-ms-version" exists-action="override"> 
      <value>@((string)context.Variables["x-ms-version"])</value> 
     </set-header> 
     <!-- MS docs may conflict here. Possibly "x-ms-documentdb-partitionkey" req'd and "x-ms-partition-key" not supported --> 
     <set-header name="x-ms-documentdb-partitionkey" exists-action="override"> 
      <value>@(context.Subscription.Id)</value> 
     </set-header> 
     <set-header name="x-ms-partition-key" exists-action="override"> 
      <value>@(context.Subscription.Id)</value> 
     </set-header> 
     <set-variable name="StringToSign" value="@(string.Format("post\ndocs\ndbs/WebApi/colls/Logs\n{0}\n\n", ((string)context.Variables["x-ms-date"]).ToLowerInvariant()))" /> 
     <set-variable name="cosmosreadonlykey" value="{{CosmosReadOnlyKey}}" /> 
     <set-variable name="SharedKey" value="@{ 
     // https://docs.microsoft.com/en-us/rest/api/documentdb/access-control-on-documentdb-resources#constructkeytoken 
     System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String((string)context.Variables["cosmosreadonlykey"])); 
     return Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes((string)context.Variables["StringToSign"]))); 
}" /> 
     <set-variable name="Authorization" value="@(string.Format("type=master&ver=1.0&sig={0}", (string)context.Variables["SharedKey"]))" /> 
     <set-header name="Authorization" exists-action="override"> 
      <value>@((string)context.Variables["Authorization"])</value> 
     </set-header> 
     <set-backend-service base-url="https://plexconnectcosmos.documents.azure.com" /> 
     <rewrite-uri template="/dbs/WebApi/colls/Logs/docs" /> 
    </inbound> 

कुछ thins मुझे आश्चर्य है: लौटे ActivityId मदद कर सके मुझे किसी भी अधिक जानकारी प्राप्त, कुछ कैसे? यहां तक ​​कि इसके बिना, क्या एज़ूर में कुछ लॉगिंग है, मुझे नहीं मिला है कि अधिक जानकारी प्रकट होगी।

यदि कुछ भी स्पष्ट रूप से गलत है कि मैं कर रहा हूं, तो कृपया मुझे बताएं।

उत्तर

3

मुझे इसे कुछ मामूली समायोजनों के साथ चल रहा है।

<policies> 
<inbound> 
    <base /> 
    <set-variable name="Content-Type" value="application/query+json" /> 
    <set-variable name="x-ms-documentdb-isquery" value="True" /> 
    <set-variable name="x-ms-documentdb-query-enablecrosspartition" value="False" /> 
    <set-variable name="x-ms-max-item-count" value="1000" /> 
    <set-variable name="x-ms-version" value="2017-02-22" /> 
    <set-variable name="x-ms-date" value="@(DateTime.UtcNow.ToString("R"))" /> 
    <set-header name="Content-Type" exists-action="override"> 
     <value>@((string)context.Variables["Content-Type"])</value> 
    </set-header> 
    <set-header name="x-ms-documentdb-isquery" exists-action="override"> 
     <value>@((string)context.Variables["x-ms-documentdb-isquery"])</value> 
    </set-header> 
    <set-header name="x-ms-documentdb-query-enablecrosspartition" exists-action="override"> 
     <value>@((string)context.Variables["x-ms-documentdb-query-enablecrosspartition"])</value> 
    </set-header> 
    <set-header name="x-ms-max-item-count" exists-action="override"> 
     <value>@((string)context.Variables["x-ms-max-item-count"])</value> 
    </set-header> 
    <set-header name="x-ms-version" exists-action="override"> 
     <value>@((string)context.Variables["x-ms-version"])</value> 
    </set-header> 
    <set-header name="x-ms-documentdb-partitionkey" exists-action="override"> 
     <value>@("[\""+context.Subscription.Id+"\"]")</value> 
    </set-header> 
    <set-header name="x-ms-date" exists-action="override"> 
     <value>@((string)context.Variables["x-ms-date"])</value> 
    </set-header> 
    <set-variable name="StringToSign" value="@(string.Format("post\ndocs\ndbs/WebApi/colls/Logs\n{0}\n\n", ((string)context.Variables["x-ms-date"]).ToLowerInvariant()))" /> 
    <set-variable name="cosmosreadonlykey" value="{{CosmosReadOnlyKey}}" /> 
    <set-variable name="SharedKey" value="@{ 
    // https://docs.microsoft.com/en-us/rest/api/documentdb/access-control-on-documentdb-resources#constructkeytoken 
    System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String((string)context.Variables["cosmosreadonlykey"])); 
    return Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes((string)context.Variables["StringToSign"]))); 
}" /> 
    <set-variable name="Authorization" value="@(string.Format("type=master&ver=1.0&sig={0}", ((string)context.Variables["SharedKey"]).Replace("&","%26").Replace("+","%2B").Replace("=","%3D")))" /> 
    <set-header name="Authorization" exists-action="override"> 
     <value>@((string)context.Variables["Authorization"])</value> 
    </set-header> 
    <set-backend-service base-url="https://mycosmosdb.documents.azure.com" /> 
    <rewrite-uri template="/dbs/WebApi/colls/Logs/docs" /> 
</inbound> 
</policies> 
  1. the partition key needs to be formatted as an array
  2. तारीख हैडर में डाल दिया है और StringToSign एक ही मूल्य
  3. did some hacky URL hex encoding के आधार पर - उचित हेक्स एन्कोडिंग
+0

साथ सुधार किया जा सकता यह अद्भुत है। मुझे देखने दो कि एन्कोडिंग सामान को आसान बनाने के लिए हम क्या कर सकते हैं। –

+0

धन्यवाद, @KaiWalter उस विभाजन कुंजी को सरणी के रूप में स्वरूपित किया गया है जो वास्तव में मुझे मिला है। –