Skip to content

Commit

Permalink
Revert "Fix some SAS token issues (1281) (#1282)"
Browse files Browse the repository at this point in the history
This reverts commit e547da1.
  • Loading branch information
Stephen Weatherford committed Apr 15, 2021
1 parent 6a13e0d commit 5755738
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 72 deletions.
11 changes: 2 additions & 9 deletions src/documents/templates/ChildTemplateCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { assert } from "../../fixed_assert";
import { Span } from '../../language/Span';
import { LanguageServerState } from '../../languageclient/startArmLanguageServer';
import { assertNever } from '../../util/assertNever';
import { parseUri } from "../../util/uri";
import { parseUri, stringifyUri } from "../../util/uri";
import { ResolvableCodeLens } from '../DeploymentDocument';
import { IParameterValuesSourceProvider } from '../parameters/IParameterValuesSourceProvider';
import { SelectParameterFileCodeLens } from "./deploymentTemplateCodeLenses";
Expand Down Expand Up @@ -161,14 +161,7 @@ export class LinkedTemplateCodeLens extends ChildTemplateCodeLens {
friendlyPath = `.${ext.pathSeparator}${friendlyPath}`;
}
} else {
const maxQueryLength = 40;
let shortenedUri = linkedUri;
if (linkedUri && linkedUri?.query.length > maxQueryLength) {
shortenedUri = shortenedUri?.with({
query: `${linkedUri.query.slice(0, maxQueryLength)}...`
});
}
friendlyPath = shortenedUri ? shortenedUri.toString(true) : undefined;
friendlyPath = linkedUri ? stringifyUri(linkedUri) : undefined;
}
} catch (error) {
console.warn(parseError(error).message);
Expand Down
4 changes: 2 additions & 2 deletions src/documents/templates/getNormalizedDocumentKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// ---------------------------------------------------------------------------------------------

import * as vscode from "vscode";
import { decodeLinkedTemplateScheme } from "../../util/linkedTemplateScheme";
import { removeLinkedTemplateScheme } from "../../util/linkedTemplateScheme";
import { normalizeUri } from "../../util/normalizedPaths";

export function getNormalizedDocumentKey(documentUri: vscode.Uri): string {
// We want a normalized file path to use as key, but also need to differentiate documents with different URI schemes
const uri = decodeLinkedTemplateScheme(documentUri);
const uri = removeLinkedTemplateScheme(documentUri);
return normalizeUri(uri);
}
8 changes: 2 additions & 6 deletions src/documents/templates/linkedTemplates/linkedTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { IProvideOpenedDocuments } from '../../../IProvideOpenedDocuments';
import { ContainsBehavior } from "../../../language/Span";
import { filterByType } from '../../../util/filterByType';
import { httpGet } from '../../../util/httpGet';
import { decodeLinkedTemplateScheme, prependLinkedTemplateScheme } from '../../../util/linkedTemplateScheme';
import { prependLinkedTemplateScheme, removeLinkedTemplateScheme } from '../../../util/linkedTemplateScheme';
import { normalizeUri } from '../../../util/normalizedPaths';
import { pathExists } from '../../../util/pathExists';
import { parseUri, stringifyUri } from '../../../util/uri';
Expand Down Expand Up @@ -89,7 +89,6 @@ export async function onRequestOpenLinkedFile(
openErrorType: string;
fileScheme: string;
hasQuery: string;
hasSas: string;
}>context.telemetry.properties;
properties.openErrorType = '';
properties.openResult = 'Error';
Expand All @@ -103,7 +102,6 @@ export async function onRequestOpenLinkedFile(

properties.fileScheme = requestedLinkUri.scheme;
properties.hasQuery = String(!!requestedLinkUri.query);
properties.uriHasSas = String(!!requestedLinkUri.query.match('[&?]sig='));

if (requestedLinkUri.scheme === documentSchemes.untitled) {
properties.openErrorType = 'template not saved';
Expand Down Expand Up @@ -142,7 +140,7 @@ export async function onRequestOpenLinkedFile(
}

export async function tryLoadNonLocalLinkedFile(uri: Uri, context: IActionContext, open: boolean): Promise<string> {
uri = decodeLinkedTemplateScheme(uri);
uri = removeLinkedTemplateScheme(uri);
let content: string;
try {
content = await httpGet(stringifyUri(uri));
Expand Down Expand Up @@ -252,8 +250,6 @@ export function assignTemplateGraphToDeploymentTemplate(
export async function openLinkedTemplateFileCommand(linkedTemplateUri: Uri, actionContext: IActionContext): Promise<void> {
let targetUri: Uri;
actionContext.telemetry.properties.scheme = linkedTemplateUri.scheme;
actionContext.telemetry.properties.uriHasQuery = String(!!linkedTemplateUri.query);
actionContext.telemetry.properties.uriHasSas = String(!!linkedTemplateUri.query.match('[&?]sig='));

if (linkedTemplateUri.scheme === documentSchemes.file) {
const exists = await pathExists(linkedTemplateUri);
Expand Down
7 changes: 4 additions & 3 deletions src/util/linkedTemplateScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function prependLinkedTemplateScheme(uri: Uri): Uri {
}
}

export function decodeLinkedTemplateScheme(uri: Uri): Uri {
export function removeLinkedTemplateScheme(uri: Uri): Uri {
if (uri.scheme === documentSchemes.linkedTemplate) {
const extractedUri = uri.toString(true).replace(/^linked-template:/, '');
return parseUri(extractedUri);
return parseUri(
stringifyUri(uri).
replace(/^linked-template:/, ''));
}

return uri;
Expand Down
4 changes: 2 additions & 2 deletions src/util/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export function parseUri(uri: string): Uri {
}

export function stringifyUri(uri: Uri): string {
// can't use true argument, seems to create invalid SAS tokens (or at least Azure storage doesn't like them)
return uri.toString(false);
// true: skips the aggressive encoding
return uri.toString(true);
}
7 changes: 1 addition & 6 deletions test/global.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ setup(function (this: Mocha.IBeforeAndAfterContext): void {
teardown(function (this: Mocha.IBeforeAndAfterContext): void {
// console.warn("Teardown");
if (!this.currentTest.state || this.currentTest.state === 'failed') {
if (testLog.toString()) {
console.warn("Failed");
console.warn(`Failed. TEST LOG:\n${testLog.toString()}\n`);
} else {
console.warn("Failed (test log is empty)");
}
console.warn(`\n========= TESTLOG =========:\n${testLog.toString()}\n`);
deleteTestLog();
}
});
55 changes: 11 additions & 44 deletions test/linkedTemplateScheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// ----------------------------------------------------------------------------

import * as assert from "assert";
import { Uri } from "vscode";
import { decodeLinkedTemplateScheme, parseUri, prependLinkedTemplateScheme as encodeLinkedTemplateScheme, stringifyUri } from "../extension.bundle";
import { parseUri, prependLinkedTemplateScheme, removeLinkedTemplateScheme, stringifyUri } from "../extension.bundle";

// tslint:disable:max-func-body-length no-http-string max-line-length no-null-keyword

Expand All @@ -13,20 +12,20 @@ suite("linkedTemplateScheme", () => {
test("not there -> noop", () => {
const uriString = 'https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uri = parseUri(uriString);
assert.equal(uri.toString(true), uriString);
assert.equal(stringifyUri(uri), uriString);

let uri2 = decodeLinkedTemplateScheme(uri);
assert.equal(uri2.toString(true), uriString);
let uri2 = removeLinkedTemplateScheme(uri);
assert.equal(stringifyUri(uri2), uriString);
});

test("is there -> remove", () => {
const uriString = 'linked-template:https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uriStringWithScheme = `linked-template:${uriString}`;
const uriWithScheme = parseUri(uriStringWithScheme);
assert.equal(uriWithScheme.toString(true), uriStringWithScheme);
assert.equal(stringifyUri(uriWithScheme), uriStringWithScheme);

let uri2 = decodeLinkedTemplateScheme(uriWithScheme);
assert.equal(uri2.toString(true), uriString);
let uri2 = removeLinkedTemplateScheme(uriWithScheme);
assert.equal(stringifyUri(uri2), uriString);
});
});

Expand All @@ -36,49 +35,17 @@ suite("linkedTemplateScheme", () => {
const uriStringWithScheme = `linked-template:${uriString}`;
const uri = parseUri(uriString);

let uri2 = encodeLinkedTemplateScheme(uri);
assert.equal(uri2.toString(true), uriStringWithScheme);
let uri2 = prependLinkedTemplateScheme(uri);
assert.equal(stringifyUri(uri2), uriStringWithScheme);
});

test("is there -> don't add", () => {
const uriString = 'https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uriStringWithScheme = `linked-template:${uriString}`;
const uriWithScheme = parseUri(uriStringWithScheme);

let uri2 = encodeLinkedTemplateScheme(uriWithScheme);
assert.equal(uri2.toString(true), uriStringWithScheme);
});
});

suite("lossless round-tripping #1281", () => {
test("round-trip 1", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const encoded = encodeLinkedTemplateScheme(Uri.parse(original));
const decoded = decodeLinkedTemplateScheme(encoded);
assert.equal(decoded, original);
});

test("round-trip 2", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const encoded = encodeLinkedTemplateScheme(Uri.parse(original));
const encodedAsString = stringifyUri(encoded);
const decoded = decodeLinkedTemplateScheme(parseUri(encodedAsString));
assert.equal(decoded, original);
});

test("round-trip 3", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A00%3A00Z%26sr%3Db%26sp%3Dr%26sig%3DVzUR3rEwRDBK75F7riLkt%2FqQNhl5LbTQ0tjoLAr5M7w%3D`;
const encoded = encodeLinkedTemplateScheme(parseUri(original));
const decoded = decodeLinkedTemplateScheme(encoded);
assert.equal(decoded, original);
});

test("round-trip 4", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const parsed = parseUri(original);
const stringified = stringifyUri(parsed);
const parsed2 = parseUri(stringified);
assert.equal(parsed2.query, parsed.query);
let uri2 = prependLinkedTemplateScheme(uriWithScheme);
assert.equal(stringifyUri(uri2), uriStringWithScheme);
});
});
});

0 comments on commit 5755738

Please sign in to comment.