Skip to content

Commit

Permalink
Use the storage proxy to avoid CORS issues (#866)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
billti authored Nov 21, 2023
1 parent 74b7ed2 commit 81873fe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 2 additions & 1 deletion vscode/src/azure/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
QuantumUris,
checkCorsConfig,
compileToBitcode,
useProxy,
} from "./networkRequests";
import { getQirForActiveWindow } from "../qirGeneration";
import { targetSupportQir } from "./providerProperties";
Expand Down Expand Up @@ -228,7 +229,7 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {
const token = await getTokenForWorkspace(workspace);
if (!token) return;
try {
await checkCorsConfig(token, quantumUris);
if (!useProxy) await checkCorsConfig(token, quantumUris);
} catch (e: any) {
log.debug("CORS check failed. ", e);

Expand Down
17 changes: 11 additions & 6 deletions vscode/src/azure/networkRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

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

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

export const useProxy = true;

export async function azureRequest(
uri: string,
token: string,
Expand Down Expand Up @@ -65,6 +66,8 @@ export async function azureRequest(
export async function storageRequest(
uri: string,
method: string,
token?: string,
proxy?: string,
extraHeaders?: [string, string][],
body?: string | Uint8Array,
correlationId?: string,
Expand All @@ -73,15 +76,13 @@ export async function storageRequest(
["x-ms-version", "2023-01-03"],
["x-ms-date", new Date().toUTCString()],
];
const storageProxy: string | undefined = workspace
.getConfiguration("Q#")
.get("storageProxy"); // e.g. in settings.json: "Q#.storageProxy": "https://qsx-proxy.azurewebsites.net/api/proxy";
if (token) headers.push(["Authorization", `Bearer ${token}`]);

if (extraHeaders?.length) headers.push(...extraHeaders);
if (storageProxy) {
if (proxy) {
log.debug(`Setting x-proxy-to header to ${uri}`);
headers.push(["x-proxy-to", uri]);
uri = storageProxy;
uri = proxy;
}
try {
log.debug(`Fetching ${uri} with method ${method}`);
Expand Down Expand Up @@ -165,6 +166,10 @@ export class QuantumUris {
sasUri() {
return `${this.endpoint}${this.id}/storage/sasUri?api-version=${this.apiVersion}`;
}

storageProxy() {
return `${this.endpoint}${this.id}/storage/proxy?api-version=${this.apiVersion}`;
}
}

export class StorageUris {
Expand Down
17 changes: 15 additions & 2 deletions vscode/src/azure/workspaceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
QuantumUris,
ResponseTypes,
storageRequest,
useProxy,
} from "./networkRequests";
import { WorkspaceConnection } from "./treeView";
import {
Expand Down Expand Up @@ -453,7 +454,12 @@ export async function getJobFiles(
const sasUri = decodeURI(sasResponse.sasUri);
log.trace(`Got SAS URI: ${sasUri}`);

const file = await storageRequest(sasUri, "GET");
const file = await storageRequest(
sasUri,
"GET",
useProxy ? token : undefined,
useProxy ? quantumUris.storageProxy() : undefined,
);

if (!file) {
sendTelemetryEvent(
Expand Down Expand Up @@ -545,6 +551,8 @@ export async function submitJob(
await storageRequest(
containerPutUri,
"PUT",
useProxy ? token : undefined,
useProxy ? quantumUris.storageProxy() : undefined,
undefined,
undefined,
correlationId,
Expand All @@ -555,7 +563,12 @@ export async function submitJob(
await storageRequest(
inputDataUri,
"PUT",
[["x-ms-blob-type", "BlockBlob"]],
useProxy ? token : undefined,
useProxy ? quantumUris.storageProxy() : undefined,
[
["x-ms-blob-type", "BlockBlob"],
["Content-Type", "text/plain"],
],
qirFile,
correlationId,
);
Expand Down

0 comments on commit 81873fe

Please sign in to comment.