K6 – Azure Blob Storage-AuthentifizierungJavaScript

Javascript-Forum
Guest
 K6 – Azure Blob Storage-Authentifizierung

Post by Guest »

Ich führe einige Leistungstests mit K6 durch und als Teil der Tests muss ich eine Verbindung herstellen und eine Datei in einen Azure-Blob-Container hochladen.
Die Authentifizierung meines Skripts schlägt aufgrund von fehl Eine Nichtübereinstimmung der Header. Obwohl sie bei der Überprüfung gleich sind. Ich erhalte die folgende Fehlermeldung:
ERRO[0001] Response Body: AuthenticationFailedServer konnte die Anfrage nicht authentifizieren. Stellen Sie sicher, dass der Wert des Autorisierungsheaders einschließlich der Signatur korrekt gebildet ist.

RequestId:XXXXXXX

Zeit:2025-01-10T22:02:41.3196583ZDie MAC-Signatur im Die HTTP-Anfrage „xxxxxxx“ ist nicht dasselbe wie eine berechnete Signatur. Der Server hat die folgende Zeichenfolge zum Signieren verwendet: „PUT

Dies ist das K6-Skript, das ich ausführe.
Hat das schon mal jemand gemacht?

Code: Select all

import {
check
} from 'k6';
import http from 'k6/http';
import encoding from 'k6/encoding';
import crypto from 'k6/crypto';

export const options = {
stages: [{
duration: '1m',
target: 1
},
{
duration: '10m',
target: 1
},
{
duration: '1m',
target: 0
},
],
};

// Environment variables
const storageAccount = __ENV.STORAGE_ACCOUNT;
const containerName = __ENV.CONTAINER_NAME;
const accountKey = __ENV.ACCOUNT_KEY;

if (!storageAccount || !containerName || !accountKey) {
throw new Error("Missing STORAGE_ACCOUNT, CONTAINER_NAME, or ACCESS_KEY environment variables.");
}

// Function to compute HMAC SHA256  signature
function signWithAccountKey(stringToSign, accountKey) {
console.log("Raw Account Key:", accountKey);
const decodedKey = encoding.b64decode(accountKey);
console.log("Decoded Account Key (Hex):", Array.from(decodedKey).map((byte) => byte.toString(16).padStart(2, '0')).join('')); // Log decoded key

const hmac = crypto.createHMAC('sha256', decodedKey);
hmac.update(stringToSign, 'utf8');
const signature = hmac.digest('base64');
console.log("Generated Signature:", signature); // Log the generated signature
return signature;
}

// Function to generate the    Authorization header
function  generateAuthorizationHeader(verb, urlPath, headers) {
const canonicalizedHeaders = Object.keys(headers)
.filter((key) => key.startsWith('x-ms-'))
.sort()
.map((key) => `${key}:${headers[key]}\n`)
.join('');

const canonicalizedResource = `/${storageAccount}${urlPath}`;

const stringToSign = [
verb,
'', // Content-Encoding
'', // Content-Language
headers['Content-Length'] || '', //  Content-Length
'', // Content-MD5
headers['Content-Type'] || '', // Content-Type
'', // Date (not used because we use x-ms-date)
'', // If-Modified-Since
'', // If-Match
'', // If-None-Match
'', // If-Unmodified-Since
'', // Range
canonicalizedHeaders,
canonicalizedResource,
].join('\n');

// Log the StringToSign for debugging
console.log("StringToSign:\n" + stringToSign);

const signature = signWithAccountKey(stringToSign, accountKey);
return `SharedKey ${storageAccount}:${signature}`;
}

// Base URL and content
const baseUrl = `https://${storageAccount}.blob.core.windows.net/${containerName}`;
const fileContent = 'A'.repeat(1024 * 1024 * 100); // 100 MB Dateiinhalt

Code: Select all

export default function() {
const fileName = `test-${__VU}-${__ITER}.xml`;
const urlPath = `/${containerName}/${fileName}`;
const url = `${baseUrl}/${fileName}`;
const date = new Date().toUTCString();

const headers = {
'x-ms-date': date,
'x-ms-version': '2020-10-02',
'x-ms-blob-type': 'BlockBlob',
'Content-Type': 'application/xml',
'Content-Length':  fileContent.length.toString(),
};

headers['Authorization'] =  generateAuthorizationHeader('PUT', urlPath, headers);

// Log headers for debugging
console.log("Authorization Header:", headers['Authorization']);
console.log("Headers Sent:", JSON.stringify(headers));

const res = http.put(url, fileContent,     {
headers
});

const success = check(res, {
'Upload succeeded': (r) => r.status === 201,
});

if (!success) {
console.error(`Upload failed for ${fileName}`);
console.error(`Status: ${res.status}`);
console.error(`Response Body: ${res.body}`);
console.error(`Headers Sent: ${JSON.stringify(headers)}`);
}
}
Danke

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post