簡體   English   中英

Azure 存儲 SAS 令牌在 localhost 上工作,但在部署在 Azure Z30136395F018719317C521 上時不起作用

[英]Azure Storage SAS token working on localhost but not when deployed on Azure Kubernetes

我正在使用 SAS 令牌從 React spa 將文件上傳和下載到 Azure 存儲。

在本地主機上運行時,一切正常,但是當部署到 Azure 上的 Kubernetes 時,我收到以下身份驗證錯誤。

onError RestError: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:e6bfca97-c01e-0030-2e29-4e7d7c000000
Time:2020-06-29T15:26:39.7164613Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was w

2020-06-29T20:26:39Z
/blob/datalake/container/Natural_Language_Processing.pdf

負責上傳的javascript代碼是

// upload to Azure
const blobName = file.name;
const accountSas = resp.data.SAS;
const account = resp.data.account;
const containerName = resp.data.container;
const anonymousCredential = new AnonymousCredential();
const blobServiceClient = new BlobServiceClient(
    `https://${account}.blob.core.windows.net?${accountSas}`,
    anonymousCredential
);
// Create a container
const containerClient = blobServiceClient.getContainerClient(
    containerName
);
// Create a blob
const content = file;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(
    content,
    Buffer.byteLength(content)
);

而用於 SAS 令牌生成的后端 Python 代碼如下

if content['up_down'] == 'download':
    permission = BlobSasPermissions(read=True)
else:
    permission = BlobSasPermissions(write=True)

account_name = os.getenv("STORAGE_ACCOUNT_NAME")
container_name = metadata.get_container_name()
blob_name = content['filePath']
expiry = datetime.utcnow() + timedelta(hours=5)

options = {
    'account_name': account_name,
    'container_name': container_name,
    'blob_name': blob_name,
    'account_key': os.getenv("STORAGE_ACCESS_KEY"),
    'permission': permission,
    'expiry': expiry
}

SAS = generate_blob_sas(**options)

其中generate_blob_sas是從 azure-storage-blob(版本 12.3.1)導入的。

關於如何解決這個問題的任何想法?

在摸索了很長時間尋找解決方案之后,我弄清楚了問題出在哪里。

它與用於訪問 blob 的 Python 庫無關,而是與 Kubernetes pod 中的環境變量有關。

使用 yaml 文件將環境變量作為機密傳遞給 Kubernetes(如此鏈接中所述)。 使用這種方法,秘密需要進行 base64 編碼。 為此,我使用了以下

echo 'secret' | base64
>> c2VjcmV0Cg==

但是,通過這種方式,默認情況下, echo命令會在 output 后面附加一個換行符。 我應該使用的是

echo -n 'secret' | base64
>> c2VjcmV0

這個錯誤特別難發現,特別是因為在打印時,錯誤的解決方案似乎會導致正確的結果

echo 'secret' | base64 | base64 -d
>> secret

無論如何,我希望我的錯誤能幫助將來的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM