Skip to content

Commit 81873fe

Browse files
authored
Use the storage proxy to avoid CORS issues (microsoft#866)
Services added a proxy API for storage requests to avoid the CORS issues. It closely maps the (undocumented) storage proxy endpoint & config we already supported, so I'm making the minimal changes here to use that by default. Once we're happy it's stable, I'll take out the old code and just leave the proxy path in.
1 parent 74b7ed2 commit 81873fe

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

vscode/src/azure/commands.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
QuantumUris,
2424
checkCorsConfig,
2525
compileToBitcode,
26+
useProxy,
2627
} from "./networkRequests";
2728
import { getQirForActiveWindow } from "../qirGeneration";
2829
import { targetSupportQir } from "./providerProperties";
@@ -228,7 +229,7 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {
228229
const token = await getTokenForWorkspace(workspace);
229230
if (!token) return;
230231
try {
231-
await checkCorsConfig(token, quantumUris);
232+
if (!useProxy) await checkCorsConfig(token, quantumUris);
232233
} catch (e: any) {
233234
log.debug("CORS check failed. ", e);
234235

vscode/src/azure/networkRequests.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// Licensed under the MIT License.
33

44
import { log } from "qsharp-lang";
5-
import { workspace } from "vscode";
65
import { EventType, UserFlowStatus, sendTelemetryEvent } from "../telemetry";
76
import { getRandomGuid } from "../utils";
87

98
const publicMgmtEndpoint = "https://management.azure.com";
109

10+
export const useProxy = true;
11+
1112
export async function azureRequest(
1213
uri: string,
1314
token: string,
@@ -65,6 +66,8 @@ export async function azureRequest(
6566
export async function storageRequest(
6667
uri: string,
6768
method: string,
69+
token?: string,
70+
proxy?: string,
6871
extraHeaders?: [string, string][],
6972
body?: string | Uint8Array,
7073
correlationId?: string,
@@ -73,15 +76,13 @@ export async function storageRequest(
7376
["x-ms-version", "2023-01-03"],
7477
["x-ms-date", new Date().toUTCString()],
7578
];
76-
const storageProxy: string | undefined = workspace
77-
.getConfiguration("Q#")
78-
.get("storageProxy"); // e.g. in settings.json: "Q#.storageProxy": "https://qsx-proxy.azurewebsites.net/api/proxy";
79+
if (token) headers.push(["Authorization", `Bearer ${token}`]);
7980

8081
if (extraHeaders?.length) headers.push(...extraHeaders);
81-
if (storageProxy) {
82+
if (proxy) {
8283
log.debug(`Setting x-proxy-to header to ${uri}`);
8384
headers.push(["x-proxy-to", uri]);
84-
uri = storageProxy;
85+
uri = proxy;
8586
}
8687
try {
8788
log.debug(`Fetching ${uri} with method ${method}`);
@@ -165,6 +166,10 @@ export class QuantumUris {
165166
sasUri() {
166167
return `${this.endpoint}${this.id}/storage/sasUri?api-version=${this.apiVersion}`;
167168
}
169+
170+
storageProxy() {
171+
return `${this.endpoint}${this.id}/storage/proxy?api-version=${this.apiVersion}`;
172+
}
168173
}
169174

170175
export class StorageUris {

vscode/src/azure/workspaceActions.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
QuantumUris,
1010
ResponseTypes,
1111
storageRequest,
12+
useProxy,
1213
} from "./networkRequests";
1314
import { WorkspaceConnection } from "./treeView";
1415
import {
@@ -453,7 +454,12 @@ export async function getJobFiles(
453454
const sasUri = decodeURI(sasResponse.sasUri);
454455
log.trace(`Got SAS URI: ${sasUri}`);
455456

456-
const file = await storageRequest(sasUri, "GET");
457+
const file = await storageRequest(
458+
sasUri,
459+
"GET",
460+
useProxy ? token : undefined,
461+
useProxy ? quantumUris.storageProxy() : undefined,
462+
);
457463

458464
if (!file) {
459465
sendTelemetryEvent(
@@ -545,6 +551,8 @@ export async function submitJob(
545551
await storageRequest(
546552
containerPutUri,
547553
"PUT",
554+
useProxy ? token : undefined,
555+
useProxy ? quantumUris.storageProxy() : undefined,
548556
undefined,
549557
undefined,
550558
correlationId,
@@ -555,7 +563,12 @@ export async function submitJob(
555563
await storageRequest(
556564
inputDataUri,
557565
"PUT",
558-
[["x-ms-blob-type", "BlockBlob"]],
566+
useProxy ? token : undefined,
567+
useProxy ? quantumUris.storageProxy() : undefined,
568+
[
569+
["x-ms-blob-type", "BlockBlob"],
570+
["Content-Type", "text/plain"],
571+
],
559572
qirFile,
560573
correlationId,
561574
);

0 commit comments

Comments
 (0)