Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

securefiles-common.ts now uses the name by default #307

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions common-npm-packages/securefiles-common/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
enable
} from "mockery";

export const secureFileName = "securefilename";
export const secureFileId = Math.random().toString(36).slice(2, 7);
process.env['SECUREFILE_NAME_' + secureFileId] = 'securefilename';
process.env['SECUREFILE_NAME_' + secureFileId] = secureFileName;

const tmAnswers = {
'exist': {
Expand All @@ -31,6 +32,11 @@ class AgentAPI {
rs.push(null);
return rs;
}
getSecureFilesByNames() {
return new Promise((resolve) => {
resolve([{ id: secureFileId }]);
});
}
}

class WebApi {
Expand Down Expand Up @@ -109,22 +115,22 @@ describe("securefiles-common package suites", function() {
registerMock("fs", fsMock);
const secureFiles = require("../securefiles-common");
const secureFileHelpers = new secureFiles.SecureFileHelpers();
const secureFilePath = await secureFileHelpers.downloadSecureFile(secureFileId);
const pseudoResolvedPath = await secureFileHelpers.getSecureFileTempDownloadPath(secureFileId);
const secureFilePath = await secureFileHelpers.downloadSecureFile(secureFileName);
const pseudoResolvedPath = await secureFileHelpers.getSecureFileTempDownloadPath(secureFileName);
strictEqual(secureFilePath, pseudoResolvedPath, `Result should be equal to ${pseudoResolvedPath}`);
});

it("Check deleteSecureFile", async() => {
const secureFiles = require("../securefiles-common");
const secureFileHelpers = new secureFiles.SecureFileHelpers();
secureFileHelpers.deleteSecureFile(secureFileId);
secureFileHelpers.deleteSecureFile(secureFileName);
});

it("Check getSecureFileTempDownloadPath", async() => {
const secureFiles = require("../securefiles-common");
const secureFileHelpers = new secureFiles.SecureFileHelpers();
const resolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(secureFileId);
const pseudoResolvedPath = tlClone.resolve(tlClone.getVariable("Agent.TempDirectory"), tlClone.getSecureFileName(secureFileId));
const resolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(secureFileName);
const pseudoResolvedPath = tlClone.resolve(tlClone.getVariable("Agent.TempDirectory"), secureFileName);
strictEqual(resolvedPath, pseudoResolvedPath, `Resolved path "${resolvedPath}" should be equal to "${pseudoResolvedPath}"`);
});
});
2 changes: 1 addition & 1 deletion common-npm-packages/securefiles-common/Tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tlClone.getVariable = variable => {
return variable;
};
tlClone.getEndpointAuthorizationParameter = (id: string, key: string, optional: boolean) => `${id}_${key}_${optional}`;
tlClone.getSecureFileName = (secureFileId: number) => secureFileId;
//tlClone.getSecureFileName = (secureFileId: number) => secureFileId;
tlClone.getSecureFileTicket = () => {
return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export class SecureFileHelpers {
}
}

async downloadSecureFile(secureFileId: string) {
tl.debug('Mock downloadSecureFile with id = ' + secureFileId);
const fileName: string = `${secureFileId}${SecureFileHelpers.fileExtension}`;
async downloadSecureFile(secureFileName: string) {
tl.debug('Mock downloadSecureFile with name = ' + secureFileName);
const fileName: string = `${secureFileName}${SecureFileHelpers.fileExtension}`;
const tempDownloadPath: string = `/build/temp/${fileName}`;
return tempDownloadPath;
}

deleteSecureFile(secureFileId: string) {
tl.debug('Mock deleteSecureFile with id = ' + secureFileId);
deleteSecureFile(secureFileName: string) {
tl.debug('Mock deleteSecureFile with name = ' + secureFileName);
}

static setFileExtension(extension: string): void {
Expand Down
31 changes: 21 additions & 10 deletions common-npm-packages/securefiles-common/securefiles-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,28 @@ export class SecureFileHelpers {

/**
* Download secure file contents to a temporary location for the build
* @param secureFileId
* @param secureFileName
*/
async downloadSecureFile(secureFileId: string): Promise<string> {
const tempDownloadPath: string = this.getSecureFileTempDownloadPath(secureFileId);
async downloadSecureFile(secureFileName: string): Promise<string> {
const tempDownloadPath: string = this.getSecureFileTempDownloadPath(secureFileName);

tl.debug('Downloading secure file contents to: ' + tempDownloadPath);
const file: NodeJS.WritableStream = fs.createWriteStream(tempDownloadPath);

const agentApi = await this.serverConnection.getTaskAgentApi();
// Get the id of the secure file
// Use the names function instead of the name function cause this function provides strict name matching
const secureFiles = await agentApi.getSecureFilesByNames(tl.getVariable('SYSTEM.TEAMPROJECT'), [secureFileName]);

if (!secureFiles) {
throw new Error(`Secure file ${secureFileName} not found.`);
} else if (secureFiles.length !== 1) {
throw new Error(`Expected 1 secure file with name ${secureFileName}, but found ${secureFiles.length}.`);
}

const secureFile = secureFiles[0];

const secureFileId= secureFile.id;
const ticket = tl.getSecureFileTicket(secureFileId);
if (!ticket) {
// Workaround bug #7491. tl.loc only works if the consuming tasks define the resource string.
Expand All @@ -61,10 +73,10 @@ export class SecureFileHelpers {

/**
* Delete secure file from the temporary location for the build
* @param secureFileId
* @param secureFileName
*/
deleteSecureFile(secureFileId: string): void {
const tempDownloadPath: string = this.getSecureFileTempDownloadPath(secureFileId);
deleteSecureFile(secureFileName: string): void {
const tempDownloadPath: string = this.getSecureFileTempDownloadPath(secureFileName);
if (tl.exist(tempDownloadPath)) {
tl.debug('Deleting secure file at: ' + tempDownloadPath);
tl.rmRF(tempDownloadPath);
Expand All @@ -73,11 +85,10 @@ export class SecureFileHelpers {

/**
* Returns the temporary download location for the secure file
* @param secureFileId
* @param secureFileName
*/
getSecureFileTempDownloadPath(secureFileId: string): string {
const fileName: string = tl.getSecureFileName(secureFileId);
return tl.resolve(tl.getVariable('Agent.TempDirectory'), fileName);
getSecureFileTempDownloadPath(secureFileName: string): string {
return tl.resolve(tl.getVariable('Agent.TempDirectory'), secureFileName);
}
}

Expand Down