Does Get-AzureStorageTable work with SAS token?
Cmdlet(s)
Get-AzureStorageTable, New-AzureStorageTableSASToken, New-AzureStorageContext
PowerShell Version
5.1.15063.413
Module Version
3.1.0 Azure.Storage
OS Version
10.0.15063.413
Description
I'm not sure if this is a bug, but I can't figure it out based on the documentation.
Get-AzureStorageTable throws this exception when using an SAS token with a storage context:
Get-AzureStorageTable : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error Message: Forbidden
Debug Output
11:15:40 AM - GetAzureStorageAccountKeyCommand begin processing with ParameterSet '__AllParameterSets'.
11:15:40 AM - using account id [email protected]'...
[Common.Authentication]: Authenticating using Account: [email protected]', environment: 'AzureCloud', tenant: '########-####-####-####-############'
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - AcquireTokenHandlerBase: === Token Acquisition started:
Authority: https://login.microsoftonline.com/########-####-####-####-############/
Resource: https://management.core.windows.net/
ClientId: ########-####-####-####-############
CacheType: Microsoft.Azure.Commands.Common.Authentication.AuthenticationStoreTokenCache (3 items)
Authentication Target: User
Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: Looking up cache for a token...
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: An item matching the requested resource was found in the cache
Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: 56.367214585 minutes left until token in cache expires
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: A matching item (access token or refresh token or both) was found in the cache
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - AcquireTokenHandlerBase: === Token Acquisition finished successfully. An access token was retuned:
Access Token Hash: ############################################
Refresh Token Hash: ############################################
Expiration Time: 06/27/2017 16:12:02 +00:00
User Hash: ############################################
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: - TokenCache: Serializing token cache with 3 items.
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - AcquireTokenHandlerBase: === Token Acquisition started:
Authority: https://login.microsoftonline.com/########-####-####-####-############/
Resource: https://management.core.windows.net/
ClientId: ########-####-####-####-############
CacheType: Microsoft.Azure.Commands.Common.Authentication.AuthenticationStoreTokenCache (3 items)
Authentication Target: User
Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: Looking up cache for a token...
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: An item matching the requested resource was found in the cache
Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: 56.36718125 minutes left until token in cache expires
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - TokenCache: A matching item (access token or refresh token or both) was found in the cache
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: ########-####-####-####-############ - AcquireTokenHandlerBase: === Token Acquisition finished successfully. An access token was retuned:
Access Token Hash: ############################################
Refresh Token Hash: ############################################
Expiration Time: 06/27/2017 16:12:02 +00:00
User Hash: ############################################
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
06/27/2017 15:15:40: - TokenCache: Serializing token cache with 3 items.
============================ HTTP REQUEST ============================
HTTP Method:
POST
Absolute Uri:
https://management.azure.com/subscriptions/########-####-####-####-############/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/myStorageAccount/listKeys?
api-version=2017-06-01
Headers:
x-ms-client-request-id : ########-####-####-####-############
accept-language : en-US
Body:
============================ HTTP RESPONSE ============================
Status Code:
OK
Headers:
Pragma : no-cache
x-ms-request-id : ########-####-####-####-############
Cache-Control : no-cache
Server : Microsoft-Azure-Storage-Resource-Provider/1.0,Microsoft-HTTPAPI/2.0
x-ms-ratelimit-remaining-subscription-writes: 1199
x-ms-correlation-request-id : ########-####-####-####-############
x-ms-routing-request-id : EASTUS2:20170627T151541Z:########-####-####-####-############
Strict-Transport-Security : max-age=31536000; includeSubDomains
Date : Tue, 27 Jun 2017 15:15:40 GMT
Body:
{
"keys": [
{
"keyName": "key1",
"permissions": "Full",
"value": "############################################################################################"
},
{
"keyName": "key2",
"permissions": "Full",
"value": "###########################################################################################"
}
]
}
11:15:41 AM - GetAzureStorageAccountKeyCommand end processing.
11:15:41 AM - GetAzureStorageAccountKeyCommand end processing.
11:15:41 AM - NewAzureStorageContext begin processing with ParameterSet 'AccountNameAndKey'.
11:15:41 AM - NewAzureStorageContext end processing.
11:15:41 AM - Init Operation Context for 'NewAzureStorageTableSasTokenCommand' with client request id Azure-Storage-PowerShell-########-####-####-####-############. If you want to get more
details, please add "-Debug" to your command.
11:15:41 AM - NewAzureStorageTableSasTokenCommand begin processing with ParameterSet 'SasPermission'.
11:15:41 AM - Use storage account 'myStorageAccount' from storage context.
11:15:41 AM - NewAzureStorageTableSasTokenCommand end processing, Start 66 remote calls. Finish 38 remote calls. Elapsed time 91268128.65 ms. Client operation id:
Azure-Storage-PowerShell-########-####-####-####-############.
11:15:41 AM - NewAzureStorageTableSasTokenCommand end processing.
11:15:41 AM - NewAzureStorageContext begin processing with ParameterSet 'SasToken'.
11:15:41 AM - NewAzureStorageContext end processing.
11:15:41 AM - Init Operation Context for 'GetAzureStorageTableCommand' with client request id Azure-Storage-PowerShell-########-####-####-####-############. If you want to get more details,
please add "-Debug" to your command.
11:15:41 AM - GetAzureStorageTableCommand begin processing with ParameterSet 'TableName'.
11:15:41 AM - Use storage account '[SasToken]' from storage context.
11:15:41 AM - Start 67th remote call, method: GET, destination: https://myStorageAccount.table.core.windows.net/Tables('myTable')?sv=2016-05-31&tn=myTable&sig=MQI6Maw%2Fi%2Bf
IiE29fxwwYzQpJ5NPD6VJYdzHGF%2FNYfM%3D&se=2017-06-27T16:15:41Z&sp=raud&api-version=2016-05-31&.
11:15:41 AM - Finish remote call for https://myStorageAccount.table.core.windows.net/Tables('myTable')?sv=2016-05-31&tn=myTable&sig=MQI6Maw%2Fi%2BfIiE29fxwwYzQpJ5NPD6VJYdzHGF
%2FNYfM%3D&se=2017-06-27T16:15:41Z&sp=raud&api-version=2016-05-31& with status code 403(Forbidden) and service request id ########-####-####-####-############. Elapsed time 125.28 ms.
Get-AzureStorageTable : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error Message: Forbidden
At C:\Users\jeffr\Documents\WindowsPowerShell\Test-SASTable.ps1:8 char:10
+ $table = Get-AzureStorageTable -Name $TableName -Context $ctx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureStorageTable], StorageException
+ FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet.GetAzureStorageTableCommand
11:15:41 AM - GetAzureStorageTableCommand end processing, Start 67 remote calls. Finish 39 remote calls. Elapsed time 91268265.47 ms. Client operation id:
Azure-Storage-PowerShell-########-####-####-####-############.
11:15:41 AM - GetAzureStorageTableCommand end processing.
Script/Steps for Reproduction
$TableRGName = 'myResourceGroup'
$TableSAName = 'farunschedulecta598'
$TableName = 'myTable'
$StorageAccountKey = (Get-AzureRmStorageAccountKey -StorageAccountName $TableSAName -ResourceGroupName $TableRGName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $TableSAName -StorageAccountKey $StorageAccountKey
$token = New-AzureStorageTableSASToken -Name $TableName -Permission "raud" -Context $context
$ctx = New-AzureStorageContext -StorageAccountName $TableSAName -SasToken $token
$table = Get-AzureStorageTable -Name $TableName -Context $ctx
@blueww Hey Wei, would you mind taking a look at this issue?
@SiloReed From you code, I see you created a Table SAS, which only support operation inside a table (e.g. query entities), but don’t support to list tables. (Storage Client Library use Query Table Rest API to check table exsit, which need list permission) So Get-AzureStorageTable cmdlet need a SAS with list table permission.
You can use following cmdlet to create a SAS that can used in Get-AzureStorageTable cmdlet:
$TableRGName = 'myResourceGroup'
$TableSAName = 'farunschedulecta598'
$TableName = 'myTable'
$StorageAccountKey = (Get-AzureRmStorageAccountKey -StorageAccountName $TableSAName -ResourceGroupName $TableRGName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $TableSAName -StorageAccountKey $StorageAccountKey
# create an Account SAS with list permission of table.
$token = New-AzureStorageAccountSASToken -Service Table -ResourceType Container -Permission l -Context $context
$ctx = New-AzureStorageContext -StorageAccountName $TableSAName -SasToken $token
$table = Get-AzureStorageTable -Name $TableName -Context $ctx
For the SAS permission, you can reference: https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS?redirectedfrom=MSDN For Table Rest API, you can reference: https://docs.microsoft.com/en-us/rest/api/storageservices/table-service-rest-api
Let me know if you have futher questions. Thanks!
As the Issue is resolved in the reply, and no further customer feedback, close this issue.
The issue still remains. If we received a SAS token from a customer e.g. with read rights to a table on table storage, we can't use powershell to get this information from this table as this SAS token does not have rights to list tables. Any possibility to make Get-AzureStorageTable skip the list request?
@etm-admin
The Get-AzureStorageTable is to list the table, it need list permission. If the SAS only has read only has permission inside a table ,but no list table, it's can't be used in Get-AzureStorageTable.
If you really want to create a CloudTable Object in Powershell with ReadOnly SAS, you can use the Azure Storage Library API in Powershell Like:
$uri = [System.Uri] "https://[AccountName].table.core.windows.net/table?sv=2017-04-17&tn=table&sig=***&se=2017-12-31T16%3A00%3A00Z&sp=r"
$table = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.CloudTable $uri
But please note this object is the CloudTable Object in Azure Storage Library, not same as the object return from Get-AzureStorageTable. Anyway, you can use it to execute Table Operation supported by Azure Storage Library .