So I've been working on a project where I want to build out basic CRUD like functionality for Azure Table Storage however I keep getting stuck on generating a valid SharedKeyLite signature.
I am able to generate a valid signature for GET and POST but when I want to PUT or DELETE and entity for some bizarre reason the signature is now invalid?
const axios = require('axios');
const client = axios.create({});
const SharedKeyGenerate = require('./SharedKeyGenerator');
const AZURE_STORAGE_KEY = 'STORAGE_KEY';
const AZURE_STORAGE_ACCOUNT = 'igdevharuntest';
const sharedKey = new SharedKeyGenerate(AZURE_STORAGE_ACCOUNT, 'powerbiTableStorage', AZURE_STORAGE_KEY);
client.defaults.baseURL = `https://${AZURE_STORAGE_ACCOUNT}.table.core.windows.net`;
client.defaults.timeout = 2000;
/**
* get tables from azure table storage
* with optional filter
* @param {string} path uri path
* @param {string} filter filter for the web request
*/
async function getTables(path, filter) {
try {
const response = await client.get(`/${path}${filter}`, {
headers: {
'x-ms-date': new Date().toUTCString(),
'Authorization': sharedKey.GenerateSharedKeyLite(path, 'GET', client),
Accept: 'application/json;odata=nometadata',
'x-ms-version': '2015-12-11',
}
});
console.log(response.data);
} catch (err) {
console.log(err.response.data);
}
}
async function addEntityToTable(tableName) {
const payload = {
'expiresOn': '2019-10-16T17:27:36.046Z',
'accessToken': 'abcsd123456',
'PartitionKey': 'NewPartitionKey12',
'RowKey': 'CompletelyNewKey12'
};
try {
const response = await client.post(`/${tableName}`, payload,{
headers: {
'x-ms-date': new Date().toUTCString(),
'Authorization': sharedKey.GenerateSharedKeyLite(tableName, "POST"),
Accept: 'application/json;odata=nometadata',
'x-ms-version': '2015-12-11',
}
});
console.log(response.data);
} catch (err) {
console.log(err.response);
}
}
async function updateEntity(path, filter) {
const payload = {
"PartitionKey": "ABCDEFG1234567RANDOMPARTITIONKEY1111",
"RowKey": "ABCDEFG1234567",
"expiresOn": "2019-10-16T17:37:07.099Z",
"accessToken": "eyJ0eXAiOiJKV1QiLCjjJhbGciOiJSUzI1NiIsIng1dCI6ImFQY3R3X29kdlJPb0VOZzNWb09sSWgydGlFcyIsImtpZCI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CBHcm91cC5SZWFkIEdyb3VwLlJlYWQuQWxsIE1ldGFkYXRhLlZpZXdfQW55IFJlcG9ydC5SZWFkLkFsbCBSZXBvcnQuUmVhZFdyaXRlLkFsbCBXb3Jrc3BhY2UuUmVhZC5BbGwgV29ya3NwYWNlLlJlYWRXcml0ZS5BbGwiLCJzdWIiOiJkQzlDN1hSbnktdHcyWGc2QkRqYUZULWg3cmo1T05lN2VkOWV6dTZqWmQ4IiwidGlkIjoiMWMxNzY3MTItMTVmNy00MThjLTk1YWMtNDZmNmE2YTU5NWM1IiwidW5pcXVlX25hbWUiOiJoc2hlaWtoYWxpQGlnbG9vc29mdHdhcmUuY29tIiwidXBuIjoiaHNoZWlraGFsaUBpZ2xvb3NvZnR3YXJlLmNvbSIsInV0aSI6ImxhYUd6UWVKRDBxZFVTbDZtRjRmQUEiLCJ2ZXIiOiIxLjAifQ.prKqNPn75_CWdbLvsl5VVvuZAK-PEI2n1DlU4gFayt_eLPzllZUlEpVqIgVgTAzeccYEj5Z6vBKpMjXT7ftwCVjnKQitidGILehaEfrWiXX3xU4ZatPQ_TNc6Y6NzMyIQTWAbPkCHfpFnBlbAD0xp9Kl-bpAq_QXbl4yIa6_IQMRMwi5WdWd8WJLLdxKQTkWiKkGBBl-La3wgYrWfzBXMzBLhlfMk_vqsOyJdg1jOUEnUmScqxKh5DUR5DvoRtdeVxc2rDz1GWM8MTdhviB0CRub7bKeMA35rLzEui69L4o8gT_FuXLXqvVLDL9sq7OZNX8q3BL-VxLkq6GSOpnkcg"
};
try {
const response = await client.put(`/${path}${filter}`, payload,{
headers: {
'x-ms-date': new Date().toUTCString(),
'Accept-Charset': 'UTF-8',
'Authorization': sharedKey.GenerateSharedKeyLite(path, "PUT"),
Accept: 'application/json;odata=nometadata',
'x-ms-version': '2015-12-11',
'Content-Type': 'application/json',
'If-Match': '*'
}
});
console.log(response);
} catch (err) {
console.log(err.response.data);
}
}
async function deleteEntity(path, filter, partitionKey, rowKey) {
try {
const response = await client.delete(`/${path}${filter}`, {
headers: {
'x-ms-date': new Date().toUTCString(),
'Accept-Charset': 'UTF-8',
'Authorization': sharedKey.GenerateSharedKeyLite(path),
Accept: 'application/json;odata=nometadata',
'x-ms-version': '2015-12-11',
'Content-Type': 'application/json',
'If-Match': '*'
}
});
console.log(response);
} catch (err) {
console.log(err.response.data);
}
}
//Works
getTables("powerbiTableStorage()", "?$filter=PartitionKey%20eq%20'ABCDEFG1234567RANDOMPARTITIONKEY1111'%20and%20RowKey%20eq%20'ABCDEFG1234567'");
//Works
addEntityToTable('powerbiTableStorage()');
//does not work
updateEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "?$filter=PartitionKey%20eq%20'ABCDEFG1234567RANDOMPARTITIONKEY1111'%20and%20RowKey%20eq%20'ABCDEFG1234567'");
//does not work
deleteEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "");
Here is the error message: { 'odata.error': { code: 'AuthenticationFailed', message: { lang: 'en-US', value: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\n' + 'RequestId:d5684fff-8002-001c-0dd9-9fe2c5000000\n' + 'Time:2019-11-20T19:31:28.7740552Z' } } }
Can someone help me out here?
A quick update...
here is what I get when I run a GET request
stringToSign: Thu, 21 Nov 2019 15:48:07 GMT /igdevharuntest/Tables
Signature: SharedKeyLite igdevharuntest:PxpjkL+WwN7ZtHD1NXctjtMKSdWuAjNY3xS3jo7/n/Q=
{
value: [
{ TableName: 'photowallCache' },
{ TableName: 'powerbiTableStorage' },
{ TableName: 'tblProjectStatus' }
]
}
Shared Key Generate:
const Utf8 = require('crypto-js/enc-utf8');
const Base64 = require('crypto-js/enc-base64');
const hmacSHA256 = require('crypto-js/hmac-sha256');
class SharedKeyGenerator {
constructor(storageAccount, tableName, storageKey) {
this.storageAccountName = storageAccount;
this.storageAccountKey = storageKey;
this.tableName = tableName;
}
/**
* generates a shared key lite for authorization
*
* @param path path of the resource
* @returns {string} signed signature
*/
GenerateSharedKeyLite(path) {
const date = new Date().toUTCString();
let stringToSign = date + '\n' + this._getCanonicalizedResource(path);
const hash = hmacSHA256(Utf8.parse(stringToSign), Base64.parse(this.storageAccountKey));
const signature = Base64.stringify(hash);
console.log(stringToSign);
console.log('SharedKeyLite ' + this.storageAccountName + ':' + signature);
return 'SharedKeyLite ' + this.storageAccountName + ':' + signature;
}
_getCanonicalizedResource(path) {
return `/${this.storageAccountName}/${path}`;
}
}
module.exports = SharedKeyGenerator;
Here is what I get when I do a PUT request
stringToSign: Thu, 21 Nov 2019 15:48:07 GMT /igdevharuntest/powerbiTableStorage(PartitionKey=NewPartitionKey12,RowKey=CompletelyNewKey12)
Signature: SharedKeyLite igdevharuntest:dt14aw/McZet9JDiZDxnrZnwfMm8AfZZ7jNnTjVJ71A=
{
'odata.error': {
code: 'AuthenticationFailed',
message: {
lang: 'en-US',
value: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\n' +
'RequestId:a9f6868a-c002-001b-6c83-a01440000000\n' +
'Time:2019-11-21T15:48:07.3247287Z'
}
}
}
---- UPDATE -----
So I noticed that when I do a GET with my PartitionKey and RowKey values in parentheses I get an AuthenticationFailed error.
Ex: uri = GET:https://{azure_storage_account}.table.core.windows.net/TableStorageName(PartitionKey='PartitionKey',RowKey='RowKey')
Response is
{
'odata.error': {
code: 'AuthenticationFailed',
message: {
lang: 'en-US',
value: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\n' +
'RequestId:a9f6868a-c002-001b-6c83-a01440000000\n' +
'Time:2019-11-21T15:48:07.3247287Z'
}
}
}
If I keep the parentheses empty but add a filter as a query parameter this works just fine and I get the specific entity for my table.
Ex: GET:https://{azure_storage_account}.table.core.windows.net/TableStorageNam()?$filter=PartitionKey eq 'PartitionKey' and RowKey eq 'RowKey'
Response is:
{
value: [
{
PartitionKey: 'PartitionKey',
RowKey: 'RowKey',
Timestamp: '2019-11-20T18:16:24.5881171Z',
expiresOn: '2019-10-16T17:27:36.046Z',
accessToken: 'abcsd123456'
}
]
}
I'm thinking the way I'm building the canonicalized resource may be incorrect for values inside the parentheses? I have no clue.. I'm stuck now. All my header values are correct so I don't think that's the issue.
_getCanonicalizedResource
method? Also, please include the detailed error message. – Gaurav Mantri