azure-pipelines-tasks-securefiles-common 2.272.0 → 2.280.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Tests/L0.d.ts +1 -1
- package/Tests/L0.js +212 -212
- package/package.json +36 -32
- package/securefiles-common-mock.d.ts +7 -7
- package/securefiles-common-mock.js +40 -40
- package/securefiles-common.d.ts +20 -20
- package/securefiles-common.js +101 -101
package/Tests/L0.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const secureFileId: string;
|
|
1
|
+
export declare const secureFileId: string;
|
package/Tests/L0.js
CHANGED
|
@@ -1,212 +1,212 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.secureFileId = void 0;
|
|
13
|
-
const assert_1 = require("assert");
|
|
14
|
-
const stream_1 = require("stream");
|
|
15
|
-
const sinon = require("sinon");
|
|
16
|
-
const tl = require("azure-pipelines-task-lib/task");
|
|
17
|
-
const nodeApi = require("azure-devops-node-api");
|
|
18
|
-
const fs = require("fs");
|
|
19
|
-
const node_test_1 = require("node:test");
|
|
20
|
-
exports.secureFileId = Math.random().toString(36).slice(2, 7);
|
|
21
|
-
process.env['SECUREFILE_NAME_' + exports.secureFileId] = 'securefilename';
|
|
22
|
-
function createMockStream(options = {}) {
|
|
23
|
-
const { statusCode = 200, statusMessage = 'OK', contentType = 'application/octet-stream', data = 'data', emitError, } = options;
|
|
24
|
-
const rs = new stream_1.Readable();
|
|
25
|
-
rs._read = () => { };
|
|
26
|
-
rs.statusCode = statusCode;
|
|
27
|
-
rs.statusMessage = statusMessage;
|
|
28
|
-
rs.headers = { 'content-type': contentType };
|
|
29
|
-
if (emitError) {
|
|
30
|
-
setTimeout(() => {
|
|
31
|
-
rs.emit('error', emitError);
|
|
32
|
-
}, 10);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
if (data)
|
|
36
|
-
rs.push(data);
|
|
37
|
-
rs.push(null);
|
|
38
|
-
}
|
|
39
|
-
return rs;
|
|
40
|
-
}
|
|
41
|
-
function createMockWriteStream() {
|
|
42
|
-
const ws = new stream_1.Writable();
|
|
43
|
-
ws._write = function (chunk, encoding, done) {
|
|
44
|
-
done();
|
|
45
|
-
};
|
|
46
|
-
return ws;
|
|
47
|
-
}
|
|
48
|
-
const getMaxRetries = (maxRetries) => maxRetries >= 0 ? maxRetries : 5;
|
|
49
|
-
(0, node_test_1.describe)("securefiles-common package suites", function () {
|
|
50
|
-
let sandbox;
|
|
51
|
-
let getVariableStub;
|
|
52
|
-
let getEndpointAuthorizationParameterStub;
|
|
53
|
-
let getSecureFileNameStub;
|
|
54
|
-
let getSecureFileTicketStub;
|
|
55
|
-
let resolveStub;
|
|
56
|
-
let existStub;
|
|
57
|
-
let rmRFStub;
|
|
58
|
-
let debugStub;
|
|
59
|
-
let getHttpProxyConfigurationStub;
|
|
60
|
-
let getPersonalAccessTokenHandlerStub;
|
|
61
|
-
let webApiStub;
|
|
62
|
-
let createWriteStreamStub;
|
|
63
|
-
(0, node_test_1.before)(() => {
|
|
64
|
-
sandbox = sinon.createSandbox();
|
|
65
|
-
});
|
|
66
|
-
(0, node_test_1.after)(() => {
|
|
67
|
-
sandbox.restore();
|
|
68
|
-
});
|
|
69
|
-
(0, node_test_1.beforeEach)(() => {
|
|
70
|
-
// Stub task lib functions
|
|
71
|
-
getVariableStub = sandbox.stub(tl, "getVariable").callsFake((variable) => {
|
|
72
|
-
if (variable.toLowerCase() === 'system.teamfoundationcollectionuri') {
|
|
73
|
-
return 'https://localhost/';
|
|
74
|
-
}
|
|
75
|
-
return variable;
|
|
76
|
-
});
|
|
77
|
-
getEndpointAuthorizationParameterStub = sandbox.stub(tl, "getEndpointAuthorizationParameter")
|
|
78
|
-
.callsFake((id, key, optional) => `${id}_${key}_${optional}`);
|
|
79
|
-
getSecureFileNameStub = sandbox.stub(tl, "getSecureFileName")
|
|
80
|
-
.callsFake((secureFileId) => secureFileId);
|
|
81
|
-
getSecureFileTicketStub = sandbox.stub(tl, "getSecureFileTicket").returns("ticket");
|
|
82
|
-
resolveStub = sandbox.stub(tl, "resolve").callsFake((...args) => args.join('/'));
|
|
83
|
-
existStub = sandbox.stub(tl, "exist").returns(true);
|
|
84
|
-
rmRFStub = sandbox.stub(tl, "rmRF");
|
|
85
|
-
debugStub = sandbox.stub(tl, "debug");
|
|
86
|
-
getHttpProxyConfigurationStub = sandbox.stub(tl, "getHttpProxyConfiguration").returns(null);
|
|
87
|
-
// Stub azure-devops-node-api
|
|
88
|
-
getPersonalAccessTokenHandlerStub = sandbox.stub(nodeApi, "getPersonalAccessTokenHandler").returns({});
|
|
89
|
-
});
|
|
90
|
-
(0, node_test_1.afterEach)(() => {
|
|
91
|
-
sandbox.restore();
|
|
92
|
-
});
|
|
93
|
-
const secureFilesHelpersProps = [
|
|
94
|
-
[10, 1000],
|
|
95
|
-
[undefined, undefined],
|
|
96
|
-
[undefined, 30],
|
|
97
|
-
[3, undefined]
|
|
98
|
-
];
|
|
99
|
-
secureFilesHelpersProps.forEach((args) => {
|
|
100
|
-
const [maxRetries, socketTimeout] = args;
|
|
101
|
-
(0, node_test_1.it)(`Check SecureFileHelpers instance properties with args: [${maxRetries}, ${socketTimeout}]`, () => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
// Stub WebApi constructor
|
|
103
|
-
const mockWebApi = {
|
|
104
|
-
options: {
|
|
105
|
-
maxRetries: getMaxRetries(maxRetries),
|
|
106
|
-
socketTimeout: socketTimeout
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
110
|
-
const secureFiles = require("../securefiles-common");
|
|
111
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers(...args);
|
|
112
|
-
(0, assert_1.strictEqual)(secureFileHelpers.serverConnection.options.maxRetries, getMaxRetries(maxRetries), `Result should be equal ${maxRetries}`);
|
|
113
|
-
(0, assert_1.strictEqual)(secureFileHelpers.serverConnection.options.socketTimeout, socketTimeout, `Result should be equal ${socketTimeout}`);
|
|
114
|
-
}));
|
|
115
|
-
});
|
|
116
|
-
(0, node_test_1.it)("Check downloadSecureFile", () => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
const mockStream = createMockStream({
|
|
118
|
-
statusCode: 200,
|
|
119
|
-
statusMessage: 'OK',
|
|
120
|
-
contentType: 'application/octet-stream'
|
|
121
|
-
});
|
|
122
|
-
const mockAgentApi = {
|
|
123
|
-
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
124
|
-
};
|
|
125
|
-
const mockWebApi = {
|
|
126
|
-
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
127
|
-
options: { maxRetries: 5, socketTimeout: undefined }
|
|
128
|
-
};
|
|
129
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
130
|
-
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
131
|
-
const secureFiles = require("../securefiles-common");
|
|
132
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
133
|
-
const secureFilePath = yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
134
|
-
const pseudoResolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(exports.secureFileId);
|
|
135
|
-
(0, assert_1.strictEqual)(secureFilePath, pseudoResolvedPath, `Result should be equal to ${pseudoResolvedPath}`);
|
|
136
|
-
}));
|
|
137
|
-
(0, node_test_1.it)("Check deleteSecureFile", () => __awaiter(this, void 0, void 0, function* () {
|
|
138
|
-
const mockWebApi = {
|
|
139
|
-
options: { maxRetries: 5, socketTimeout: undefined }
|
|
140
|
-
};
|
|
141
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
142
|
-
const secureFiles = require("../securefiles-common");
|
|
143
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
144
|
-
secureFileHelpers.deleteSecureFile(exports.secureFileId);
|
|
145
|
-
sinon.assert.called(rmRFStub);
|
|
146
|
-
}));
|
|
147
|
-
(0, node_test_1.it)("Check getSecureFileTempDownloadPath", () => __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
const mockWebApi = {
|
|
149
|
-
options: { maxRetries: 5, socketTimeout: undefined }
|
|
150
|
-
};
|
|
151
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
152
|
-
const secureFiles = require("../securefiles-common");
|
|
153
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
154
|
-
const resolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(exports.secureFileId);
|
|
155
|
-
sinon.assert.calledWith(resolveStub, sinon.match.any, exports.secureFileId);
|
|
156
|
-
(0, assert_1.strictEqual)(typeof resolvedPath, 'string', 'Resolved path should be a string');
|
|
157
|
-
}));
|
|
158
|
-
(0, node_test_1.it)("Should handle HTTP error responses", () => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
const mockStream = createMockStream({
|
|
160
|
-
statusCode: 500,
|
|
161
|
-
statusMessage: 'Internal Server Error',
|
|
162
|
-
contentType: 'application/json',
|
|
163
|
-
data: '',
|
|
164
|
-
});
|
|
165
|
-
const mockAgentApi = {
|
|
166
|
-
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
167
|
-
};
|
|
168
|
-
const mockWebApi = {
|
|
169
|
-
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
170
|
-
options: { maxRetries: 5, socketTimeout: undefined }
|
|
171
|
-
};
|
|
172
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
173
|
-
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
174
|
-
const secureFiles = require("../securefiles-common");
|
|
175
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
176
|
-
try {
|
|
177
|
-
yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
178
|
-
throw new Error("Expected error was not thrown");
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
(0, assert_1.strictEqual)(error.message.includes("HTTP 500"), true, "Should contain HTTP error status");
|
|
182
|
-
(0, assert_1.strictEqual)(error.message.includes("Internal Server Error"), true, "Should contain HTTP error message");
|
|
183
|
-
}
|
|
184
|
-
}));
|
|
185
|
-
(0, node_test_1.it)("Should handle stream errors during download", () => __awaiter(this, void 0, void 0, function* () {
|
|
186
|
-
const mockStream = createMockStream({
|
|
187
|
-
statusCode: 200,
|
|
188
|
-
statusMessage: 'OK',
|
|
189
|
-
contentType: 'application/octet-stream',
|
|
190
|
-
emitError: new Error('Network connection lost')
|
|
191
|
-
});
|
|
192
|
-
const mockAgentApi = {
|
|
193
|
-
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
194
|
-
};
|
|
195
|
-
const mockWebApi = {
|
|
196
|
-
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
197
|
-
options: { maxRetries: 5, socketTimeout: undefined }
|
|
198
|
-
};
|
|
199
|
-
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
200
|
-
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
201
|
-
const secureFiles = require("../securefiles-common");
|
|
202
|
-
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
203
|
-
try {
|
|
204
|
-
yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
205
|
-
throw new Error("Expected error was not thrown");
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
(0, assert_1.strictEqual)(error.message.includes("Failed to download secure file"), true, "Should handle stream errors");
|
|
209
|
-
(0, assert_1.strictEqual)(error.message.includes("Network connection lost"), true, "Should include original error message");
|
|
210
|
-
}
|
|
211
|
-
}));
|
|
212
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.secureFileId = void 0;
|
|
13
|
+
const assert_1 = require("assert");
|
|
14
|
+
const stream_1 = require("stream");
|
|
15
|
+
const sinon = require("sinon");
|
|
16
|
+
const tl = require("azure-pipelines-task-lib/task");
|
|
17
|
+
const nodeApi = require("azure-devops-node-api");
|
|
18
|
+
const fs = require("fs");
|
|
19
|
+
const node_test_1 = require("node:test");
|
|
20
|
+
exports.secureFileId = Math.random().toString(36).slice(2, 7);
|
|
21
|
+
process.env['SECUREFILE_NAME_' + exports.secureFileId] = 'securefilename';
|
|
22
|
+
function createMockStream(options = {}) {
|
|
23
|
+
const { statusCode = 200, statusMessage = 'OK', contentType = 'application/octet-stream', data = 'data', emitError, } = options;
|
|
24
|
+
const rs = new stream_1.Readable();
|
|
25
|
+
rs._read = () => { };
|
|
26
|
+
rs.statusCode = statusCode;
|
|
27
|
+
rs.statusMessage = statusMessage;
|
|
28
|
+
rs.headers = { 'content-type': contentType };
|
|
29
|
+
if (emitError) {
|
|
30
|
+
setTimeout(() => {
|
|
31
|
+
rs.emit('error', emitError);
|
|
32
|
+
}, 10);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
if (data)
|
|
36
|
+
rs.push(data);
|
|
37
|
+
rs.push(null);
|
|
38
|
+
}
|
|
39
|
+
return rs;
|
|
40
|
+
}
|
|
41
|
+
function createMockWriteStream() {
|
|
42
|
+
const ws = new stream_1.Writable();
|
|
43
|
+
ws._write = function (chunk, encoding, done) {
|
|
44
|
+
done();
|
|
45
|
+
};
|
|
46
|
+
return ws;
|
|
47
|
+
}
|
|
48
|
+
const getMaxRetries = (maxRetries) => maxRetries >= 0 ? maxRetries : 5;
|
|
49
|
+
(0, node_test_1.describe)("securefiles-common package suites", function () {
|
|
50
|
+
let sandbox;
|
|
51
|
+
let getVariableStub;
|
|
52
|
+
let getEndpointAuthorizationParameterStub;
|
|
53
|
+
let getSecureFileNameStub;
|
|
54
|
+
let getSecureFileTicketStub;
|
|
55
|
+
let resolveStub;
|
|
56
|
+
let existStub;
|
|
57
|
+
let rmRFStub;
|
|
58
|
+
let debugStub;
|
|
59
|
+
let getHttpProxyConfigurationStub;
|
|
60
|
+
let getPersonalAccessTokenHandlerStub;
|
|
61
|
+
let webApiStub;
|
|
62
|
+
let createWriteStreamStub;
|
|
63
|
+
(0, node_test_1.before)(() => {
|
|
64
|
+
sandbox = sinon.createSandbox();
|
|
65
|
+
});
|
|
66
|
+
(0, node_test_1.after)(() => {
|
|
67
|
+
sandbox.restore();
|
|
68
|
+
});
|
|
69
|
+
(0, node_test_1.beforeEach)(() => {
|
|
70
|
+
// Stub task lib functions
|
|
71
|
+
getVariableStub = sandbox.stub(tl, "getVariable").callsFake((variable) => {
|
|
72
|
+
if (variable.toLowerCase() === 'system.teamfoundationcollectionuri') {
|
|
73
|
+
return 'https://localhost/';
|
|
74
|
+
}
|
|
75
|
+
return variable;
|
|
76
|
+
});
|
|
77
|
+
getEndpointAuthorizationParameterStub = sandbox.stub(tl, "getEndpointAuthorizationParameter")
|
|
78
|
+
.callsFake((id, key, optional) => `${id}_${key}_${optional}`);
|
|
79
|
+
getSecureFileNameStub = sandbox.stub(tl, "getSecureFileName")
|
|
80
|
+
.callsFake((secureFileId) => secureFileId);
|
|
81
|
+
getSecureFileTicketStub = sandbox.stub(tl, "getSecureFileTicket").returns("ticket");
|
|
82
|
+
resolveStub = sandbox.stub(tl, "resolve").callsFake((...args) => args.join('/'));
|
|
83
|
+
existStub = sandbox.stub(tl, "exist").returns(true);
|
|
84
|
+
rmRFStub = sandbox.stub(tl, "rmRF");
|
|
85
|
+
debugStub = sandbox.stub(tl, "debug");
|
|
86
|
+
getHttpProxyConfigurationStub = sandbox.stub(tl, "getHttpProxyConfiguration").returns(null);
|
|
87
|
+
// Stub azure-devops-node-api
|
|
88
|
+
getPersonalAccessTokenHandlerStub = sandbox.stub(nodeApi, "getPersonalAccessTokenHandler").returns({});
|
|
89
|
+
});
|
|
90
|
+
(0, node_test_1.afterEach)(() => {
|
|
91
|
+
sandbox.restore();
|
|
92
|
+
});
|
|
93
|
+
const secureFilesHelpersProps = [
|
|
94
|
+
[10, 1000],
|
|
95
|
+
[undefined, undefined],
|
|
96
|
+
[undefined, 30],
|
|
97
|
+
[3, undefined]
|
|
98
|
+
];
|
|
99
|
+
secureFilesHelpersProps.forEach((args) => {
|
|
100
|
+
const [maxRetries, socketTimeout] = args;
|
|
101
|
+
(0, node_test_1.it)(`Check SecureFileHelpers instance properties with args: [${maxRetries}, ${socketTimeout}]`, () => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
// Stub WebApi constructor
|
|
103
|
+
const mockWebApi = {
|
|
104
|
+
options: {
|
|
105
|
+
maxRetries: getMaxRetries(maxRetries),
|
|
106
|
+
socketTimeout: socketTimeout
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
110
|
+
const secureFiles = require("../securefiles-common");
|
|
111
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers(...args);
|
|
112
|
+
(0, assert_1.strictEqual)(secureFileHelpers.serverConnection.options.maxRetries, getMaxRetries(maxRetries), `Result should be equal ${maxRetries}`);
|
|
113
|
+
(0, assert_1.strictEqual)(secureFileHelpers.serverConnection.options.socketTimeout, socketTimeout, `Result should be equal ${socketTimeout}`);
|
|
114
|
+
}));
|
|
115
|
+
});
|
|
116
|
+
(0, node_test_1.it)("Check downloadSecureFile", () => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const mockStream = createMockStream({
|
|
118
|
+
statusCode: 200,
|
|
119
|
+
statusMessage: 'OK',
|
|
120
|
+
contentType: 'application/octet-stream'
|
|
121
|
+
});
|
|
122
|
+
const mockAgentApi = {
|
|
123
|
+
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
124
|
+
};
|
|
125
|
+
const mockWebApi = {
|
|
126
|
+
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
127
|
+
options: { maxRetries: 5, socketTimeout: undefined }
|
|
128
|
+
};
|
|
129
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
130
|
+
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
131
|
+
const secureFiles = require("../securefiles-common");
|
|
132
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
133
|
+
const secureFilePath = yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
134
|
+
const pseudoResolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(exports.secureFileId);
|
|
135
|
+
(0, assert_1.strictEqual)(secureFilePath, pseudoResolvedPath, `Result should be equal to ${pseudoResolvedPath}`);
|
|
136
|
+
}));
|
|
137
|
+
(0, node_test_1.it)("Check deleteSecureFile", () => __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
const mockWebApi = {
|
|
139
|
+
options: { maxRetries: 5, socketTimeout: undefined }
|
|
140
|
+
};
|
|
141
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
142
|
+
const secureFiles = require("../securefiles-common");
|
|
143
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
144
|
+
secureFileHelpers.deleteSecureFile(exports.secureFileId);
|
|
145
|
+
sinon.assert.called(rmRFStub);
|
|
146
|
+
}));
|
|
147
|
+
(0, node_test_1.it)("Check getSecureFileTempDownloadPath", () => __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
const mockWebApi = {
|
|
149
|
+
options: { maxRetries: 5, socketTimeout: undefined }
|
|
150
|
+
};
|
|
151
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
152
|
+
const secureFiles = require("../securefiles-common");
|
|
153
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
154
|
+
const resolvedPath = secureFileHelpers.getSecureFileTempDownloadPath(exports.secureFileId);
|
|
155
|
+
sinon.assert.calledWith(resolveStub, sinon.match.any, exports.secureFileId);
|
|
156
|
+
(0, assert_1.strictEqual)(typeof resolvedPath, 'string', 'Resolved path should be a string');
|
|
157
|
+
}));
|
|
158
|
+
(0, node_test_1.it)("Should handle HTTP error responses", () => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
const mockStream = createMockStream({
|
|
160
|
+
statusCode: 500,
|
|
161
|
+
statusMessage: 'Internal Server Error',
|
|
162
|
+
contentType: 'application/json',
|
|
163
|
+
data: '',
|
|
164
|
+
});
|
|
165
|
+
const mockAgentApi = {
|
|
166
|
+
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
167
|
+
};
|
|
168
|
+
const mockWebApi = {
|
|
169
|
+
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
170
|
+
options: { maxRetries: 5, socketTimeout: undefined }
|
|
171
|
+
};
|
|
172
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
173
|
+
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
174
|
+
const secureFiles = require("../securefiles-common");
|
|
175
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
176
|
+
try {
|
|
177
|
+
yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
178
|
+
throw new Error("Expected error was not thrown");
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
(0, assert_1.strictEqual)(error.message.includes("HTTP 500"), true, "Should contain HTTP error status");
|
|
182
|
+
(0, assert_1.strictEqual)(error.message.includes("Internal Server Error"), true, "Should contain HTTP error message");
|
|
183
|
+
}
|
|
184
|
+
}));
|
|
185
|
+
(0, node_test_1.it)("Should handle stream errors during download", () => __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
const mockStream = createMockStream({
|
|
187
|
+
statusCode: 200,
|
|
188
|
+
statusMessage: 'OK',
|
|
189
|
+
contentType: 'application/octet-stream',
|
|
190
|
+
emitError: new Error('Network connection lost')
|
|
191
|
+
});
|
|
192
|
+
const mockAgentApi = {
|
|
193
|
+
downloadSecureFile: sandbox.stub().resolves(mockStream)
|
|
194
|
+
};
|
|
195
|
+
const mockWebApi = {
|
|
196
|
+
getTaskAgentApi: sandbox.stub().resolves(mockAgentApi),
|
|
197
|
+
options: { maxRetries: 5, socketTimeout: undefined }
|
|
198
|
+
};
|
|
199
|
+
webApiStub = sandbox.stub(nodeApi, "WebApi").returns(mockWebApi);
|
|
200
|
+
createWriteStreamStub = sandbox.stub(fs, "createWriteStream").returns(createMockWriteStream());
|
|
201
|
+
const secureFiles = require("../securefiles-common");
|
|
202
|
+
const secureFileHelpers = new secureFiles.SecureFileHelpers();
|
|
203
|
+
try {
|
|
204
|
+
yield secureFileHelpers.downloadSecureFile(exports.secureFileId);
|
|
205
|
+
throw new Error("Expected error was not thrown");
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
(0, assert_1.strictEqual)(error.message.includes("Failed to download secure file"), true, "Should handle stream errors");
|
|
209
|
+
(0, assert_1.strictEqual)(error.message.includes("Network connection lost"), true, "Should include original error message");
|
|
210
|
+
}
|
|
211
|
+
}));
|
|
212
|
+
});
|
package/package.json
CHANGED
|
@@ -1,32 +1,36 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "azure-pipelines-tasks-securefiles-common",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Azure Pipelines tasks SecureFiles Common",
|
|
5
|
-
"main": "securefiles-common.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"build": "cd ../build-scripts && npm install && cd ../securefiles-common && node make.js"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"@types/
|
|
23
|
-
"@types/
|
|
24
|
-
"
|
|
25
|
-
"azure-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"sinon": "^
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "azure-pipelines-tasks-securefiles-common",
|
|
3
|
+
"version": "2.280.0",
|
|
4
|
+
"description": "Azure Pipelines tasks SecureFiles Common",
|
|
5
|
+
"main": "securefiles-common.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "cd ../build-scripts && npm install && cd ../securefiles-common && node make.js",
|
|
8
|
+
"build:ci": "node make.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/microsoft/azure-pipelines-tasks-common-packages.git",
|
|
13
|
+
"directory": "common-npm-packages/securefiles-common"
|
|
14
|
+
},
|
|
15
|
+
"author": "Microsoft Corporation",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/microsoft/azure-pipelines-tasks-common-packages/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/microsoft/azure-pipelines-tasks-common-packages#readme",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@types/mocha": "^5.2.7",
|
|
23
|
+
"@types/node": "^18.0.0",
|
|
24
|
+
"@types/q": "^1.5.4",
|
|
25
|
+
"azure-devops-node-api": "^17.0.1",
|
|
26
|
+
"azure-pipelines-task-lib": "^5.280.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/sinon": "^21.0.0",
|
|
30
|
+
"sinon": "^19.0.2",
|
|
31
|
+
"typescript": "^4.9.5"
|
|
32
|
+
},
|
|
33
|
+
"overrides": {
|
|
34
|
+
"brace-expansion": "^1.1.18"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export declare class SecureFileHelpers {
|
|
2
|
-
private static fileExtension;
|
|
3
|
-
constructor(retryCount?: number);
|
|
4
|
-
downloadSecureFile(secureFileId: string): Promise<string>;
|
|
5
|
-
deleteSecureFile(secureFileId: string): void;
|
|
6
|
-
static setFileExtension(extension: string): void;
|
|
7
|
-
}
|
|
1
|
+
export declare class SecureFileHelpers {
|
|
2
|
+
private static fileExtension;
|
|
3
|
+
constructor(retryCount?: number);
|
|
4
|
+
downloadSecureFile(secureFileId: string): Promise<string>;
|
|
5
|
+
deleteSecureFile(secureFileId: string): void;
|
|
6
|
+
static setFileExtension(extension: string): void;
|
|
7
|
+
}
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.SecureFileHelpers = void 0;
|
|
13
|
-
const tl = require("azure-pipelines-task-lib/task");
|
|
14
|
-
class SecureFileHelpers {
|
|
15
|
-
constructor(retryCount) {
|
|
16
|
-
tl.debug('Mock SecureFileHelpers constructor');
|
|
17
|
-
if (retryCount) {
|
|
18
|
-
tl.debug('Mock SecureFileHelpers retry count set to: ' + retryCount);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
tl.debug('Mock SecureFileHelpers retry count not set.');
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
downloadSecureFile(secureFileId) {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
tl.debug('Mock downloadSecureFile with id = ' + secureFileId);
|
|
27
|
-
const fileName = `${secureFileId}${SecureFileHelpers.fileExtension}`;
|
|
28
|
-
const tempDownloadPath = `/build/temp/${fileName}`;
|
|
29
|
-
return tempDownloadPath;
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
deleteSecureFile(secureFileId) {
|
|
33
|
-
tl.debug('Mock deleteSecureFile with id = ' + secureFileId);
|
|
34
|
-
}
|
|
35
|
-
static setFileExtension(extension) {
|
|
36
|
-
this.fileExtension = extension;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.SecureFileHelpers = SecureFileHelpers;
|
|
40
|
-
SecureFileHelpers.fileExtension = ".filename";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SecureFileHelpers = void 0;
|
|
13
|
+
const tl = require("azure-pipelines-task-lib/task");
|
|
14
|
+
class SecureFileHelpers {
|
|
15
|
+
constructor(retryCount) {
|
|
16
|
+
tl.debug('Mock SecureFileHelpers constructor');
|
|
17
|
+
if (retryCount) {
|
|
18
|
+
tl.debug('Mock SecureFileHelpers retry count set to: ' + retryCount);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
tl.debug('Mock SecureFileHelpers retry count not set.');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
downloadSecureFile(secureFileId) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
tl.debug('Mock downloadSecureFile with id = ' + secureFileId);
|
|
27
|
+
const fileName = `${secureFileId}${SecureFileHelpers.fileExtension}`;
|
|
28
|
+
const tempDownloadPath = `/build/temp/${fileName}`;
|
|
29
|
+
return tempDownloadPath;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
deleteSecureFile(secureFileId) {
|
|
33
|
+
tl.debug('Mock deleteSecureFile with id = ' + secureFileId);
|
|
34
|
+
}
|
|
35
|
+
static setFileExtension(extension) {
|
|
36
|
+
this.fileExtension = extension;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.SecureFileHelpers = SecureFileHelpers;
|
|
40
|
+
SecureFileHelpers.fileExtension = ".filename";
|
package/securefiles-common.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { WebApi } from 'azure-devops-node-api';
|
|
2
|
-
export declare class SecureFileHelpers {
|
|
3
|
-
serverConnection: WebApi;
|
|
4
|
-
constructor(retryCount?: number, socketTimeout?: number);
|
|
5
|
-
/**
|
|
6
|
-
* Download secure file contents to a temporary location for the build
|
|
7
|
-
* @param secureFileId
|
|
8
|
-
*/
|
|
9
|
-
downloadSecureFile(secureFileId: string): Promise<string>;
|
|
10
|
-
/**
|
|
11
|
-
* Delete secure file from the temporary location for the build
|
|
12
|
-
* @param secureFileId
|
|
13
|
-
*/
|
|
14
|
-
deleteSecureFile(secureFileId: string): void;
|
|
15
|
-
/**
|
|
16
|
-
* Returns the temporary download location for the secure file
|
|
17
|
-
* @param secureFileId
|
|
18
|
-
*/
|
|
19
|
-
getSecureFileTempDownloadPath(secureFileId: string): string;
|
|
20
|
-
}
|
|
1
|
+
import { WebApi } from 'azure-devops-node-api';
|
|
2
|
+
export declare class SecureFileHelpers {
|
|
3
|
+
serverConnection: WebApi;
|
|
4
|
+
constructor(retryCount?: number, socketTimeout?: number);
|
|
5
|
+
/**
|
|
6
|
+
* Download secure file contents to a temporary location for the build
|
|
7
|
+
* @param secureFileId
|
|
8
|
+
*/
|
|
9
|
+
downloadSecureFile(secureFileId: string): Promise<string>;
|
|
10
|
+
/**
|
|
11
|
+
* Delete secure file from the temporary location for the build
|
|
12
|
+
* @param secureFileId
|
|
13
|
+
*/
|
|
14
|
+
deleteSecureFile(secureFileId: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the temporary download location for the secure file
|
|
17
|
+
* @param secureFileId
|
|
18
|
+
*/
|
|
19
|
+
getSecureFileTempDownloadPath(secureFileId: string): string;
|
|
20
|
+
}
|
package/securefiles-common.js
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.SecureFileHelpers = void 0;
|
|
13
|
-
const fs = require("fs");
|
|
14
|
-
const Q = require("q");
|
|
15
|
-
const tl = require("azure-pipelines-task-lib/task");
|
|
16
|
-
const azure_devops_node_api_1 = require("azure-devops-node-api");
|
|
17
|
-
class SecureFileHelpers {
|
|
18
|
-
constructor(retryCount, socketTimeout) {
|
|
19
|
-
const serverUrl = tl.getVariable('System.TeamFoundationCollectionUri');
|
|
20
|
-
const serverCreds = tl.getEndpointAuthorizationParameter('SYSTEMVSSCONNECTION', 'ACCESSTOKEN', false);
|
|
21
|
-
const authHandler = (0, azure_devops_node_api_1.getPersonalAccessTokenHandler)(serverCreds, true);
|
|
22
|
-
const maxRetries = retryCount && retryCount >= 0 ? retryCount : 5; // Default to 5 if not specified
|
|
23
|
-
tl.debug('Secure file retry count set to: ' + maxRetries);
|
|
24
|
-
const proxy = tl.getHttpProxyConfiguration();
|
|
25
|
-
let options = {
|
|
26
|
-
allowRetries: true,
|
|
27
|
-
maxRetries,
|
|
28
|
-
socketTimeout
|
|
29
|
-
};
|
|
30
|
-
if (proxy) {
|
|
31
|
-
options = Object.assign(Object.assign({}, options), { proxy, ignoreSslError: true });
|
|
32
|
-
}
|
|
33
|
-
;
|
|
34
|
-
this.serverConnection = new azure_devops_node_api_1.WebApi(serverUrl, authHandler, options);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Download secure file contents to a temporary location for the build
|
|
38
|
-
* @param secureFileId
|
|
39
|
-
*/
|
|
40
|
-
downloadSecureFile(secureFileId) {
|
|
41
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
-
const tempDownloadPath = this.getSecureFileTempDownloadPath(secureFileId);
|
|
43
|
-
tl.debug('Downloading secure file contents to: ' + tempDownloadPath);
|
|
44
|
-
const agentApi = yield this.serverConnection.getTaskAgentApi();
|
|
45
|
-
const ticket = tl.getSecureFileTicket(secureFileId);
|
|
46
|
-
if (!ticket) {
|
|
47
|
-
// Workaround bug #7491. tl.loc only works if the consuming tasks define the resource string.
|
|
48
|
-
throw new Error(`Download ticket for SecureFileId ${secureFileId} not found.`);
|
|
49
|
-
}
|
|
50
|
-
tl.debug(`Starting secure file download for SecureFileId: ${secureFileId}`);
|
|
51
|
-
const response = yield agentApi.downloadSecureFile(tl.getVariable('SYSTEM.TEAMPROJECT'), secureFileId, ticket, false);
|
|
52
|
-
const defer = Q.defer();
|
|
53
|
-
response.on('error', (err) => {
|
|
54
|
-
defer.reject(new Error(`Failed to download secure file. ${err.message}`));
|
|
55
|
-
});
|
|
56
|
-
const httpResponse = response;
|
|
57
|
-
tl.debug(`HTTP Status: ${httpResponse.statusCode} ${httpResponse.statusMessage}`);
|
|
58
|
-
tl.debug(`Content-Type: ${httpResponse.headers['content-type'] || 'unknown'}`);
|
|
59
|
-
if (httpResponse.statusCode && httpResponse.statusCode != 200) {
|
|
60
|
-
let errorBody = '';
|
|
61
|
-
httpResponse.on('data', (chunk) => {
|
|
62
|
-
errorBody += chunk.toString();
|
|
63
|
-
});
|
|
64
|
-
httpResponse.on('end', () => {
|
|
65
|
-
defer.reject(new Error(`Failed to download secure file. HTTP ${httpResponse.statusCode}: ${httpResponse.statusMessage}. Error content: ${errorBody}`));
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const file = fs.createWriteStream(tempDownloadPath);
|
|
69
|
-
const stream = response.pipe(file);
|
|
70
|
-
stream.on('finish', () => {
|
|
71
|
-
defer.resolve();
|
|
72
|
-
});
|
|
73
|
-
stream.on('error', (err) => {
|
|
74
|
-
defer.reject(new Error(`Error writing secure file to disk. ${err.message}`));
|
|
75
|
-
});
|
|
76
|
-
yield defer.promise;
|
|
77
|
-
tl.debug('Downloaded secure file contents to: ' + tempDownloadPath);
|
|
78
|
-
return tempDownloadPath;
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Delete secure file from the temporary location for the build
|
|
83
|
-
* @param secureFileId
|
|
84
|
-
*/
|
|
85
|
-
deleteSecureFile(secureFileId) {
|
|
86
|
-
const tempDownloadPath = this.getSecureFileTempDownloadPath(secureFileId);
|
|
87
|
-
if (tl.exist(tempDownloadPath)) {
|
|
88
|
-
tl.debug('Deleting secure file at: ' + tempDownloadPath);
|
|
89
|
-
tl.rmRF(tempDownloadPath);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Returns the temporary download location for the secure file
|
|
94
|
-
* @param secureFileId
|
|
95
|
-
*/
|
|
96
|
-
getSecureFileTempDownloadPath(secureFileId) {
|
|
97
|
-
const fileName = tl.getSecureFileName(secureFileId);
|
|
98
|
-
return tl.resolve(tl.getVariable('Agent.TempDirectory'), fileName);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.SecureFileHelpers = SecureFileHelpers;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SecureFileHelpers = void 0;
|
|
13
|
+
const fs = require("fs");
|
|
14
|
+
const Q = require("q");
|
|
15
|
+
const tl = require("azure-pipelines-task-lib/task");
|
|
16
|
+
const azure_devops_node_api_1 = require("azure-devops-node-api");
|
|
17
|
+
class SecureFileHelpers {
|
|
18
|
+
constructor(retryCount, socketTimeout) {
|
|
19
|
+
const serverUrl = tl.getVariable('System.TeamFoundationCollectionUri');
|
|
20
|
+
const serverCreds = tl.getEndpointAuthorizationParameter('SYSTEMVSSCONNECTION', 'ACCESSTOKEN', false);
|
|
21
|
+
const authHandler = (0, azure_devops_node_api_1.getPersonalAccessTokenHandler)(serverCreds, true);
|
|
22
|
+
const maxRetries = retryCount && retryCount >= 0 ? retryCount : 5; // Default to 5 if not specified
|
|
23
|
+
tl.debug('Secure file retry count set to: ' + maxRetries);
|
|
24
|
+
const proxy = tl.getHttpProxyConfiguration();
|
|
25
|
+
let options = {
|
|
26
|
+
allowRetries: true,
|
|
27
|
+
maxRetries,
|
|
28
|
+
socketTimeout
|
|
29
|
+
};
|
|
30
|
+
if (proxy) {
|
|
31
|
+
options = Object.assign(Object.assign({}, options), { proxy, ignoreSslError: true });
|
|
32
|
+
}
|
|
33
|
+
;
|
|
34
|
+
this.serverConnection = new azure_devops_node_api_1.WebApi(serverUrl, authHandler, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Download secure file contents to a temporary location for the build
|
|
38
|
+
* @param secureFileId
|
|
39
|
+
*/
|
|
40
|
+
downloadSecureFile(secureFileId) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const tempDownloadPath = this.getSecureFileTempDownloadPath(secureFileId);
|
|
43
|
+
tl.debug('Downloading secure file contents to: ' + tempDownloadPath);
|
|
44
|
+
const agentApi = yield this.serverConnection.getTaskAgentApi();
|
|
45
|
+
const ticket = tl.getSecureFileTicket(secureFileId);
|
|
46
|
+
if (!ticket) {
|
|
47
|
+
// Workaround bug #7491. tl.loc only works if the consuming tasks define the resource string.
|
|
48
|
+
throw new Error(`Download ticket for SecureFileId ${secureFileId} not found.`);
|
|
49
|
+
}
|
|
50
|
+
tl.debug(`Starting secure file download for SecureFileId: ${secureFileId}`);
|
|
51
|
+
const response = yield agentApi.downloadSecureFile(tl.getVariable('SYSTEM.TEAMPROJECT'), secureFileId, ticket, false);
|
|
52
|
+
const defer = Q.defer();
|
|
53
|
+
response.on('error', (err) => {
|
|
54
|
+
defer.reject(new Error(`Failed to download secure file. ${err.message}`));
|
|
55
|
+
});
|
|
56
|
+
const httpResponse = response;
|
|
57
|
+
tl.debug(`HTTP Status: ${httpResponse.statusCode} ${httpResponse.statusMessage}`);
|
|
58
|
+
tl.debug(`Content-Type: ${httpResponse.headers['content-type'] || 'unknown'}`);
|
|
59
|
+
if (httpResponse.statusCode && httpResponse.statusCode != 200) {
|
|
60
|
+
let errorBody = '';
|
|
61
|
+
httpResponse.on('data', (chunk) => {
|
|
62
|
+
errorBody += chunk.toString();
|
|
63
|
+
});
|
|
64
|
+
httpResponse.on('end', () => {
|
|
65
|
+
defer.reject(new Error(`Failed to download secure file. HTTP ${httpResponse.statusCode}: ${httpResponse.statusMessage}. Error content: ${errorBody}`));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const file = fs.createWriteStream(tempDownloadPath);
|
|
69
|
+
const stream = response.pipe(file);
|
|
70
|
+
stream.on('finish', () => {
|
|
71
|
+
defer.resolve();
|
|
72
|
+
});
|
|
73
|
+
stream.on('error', (err) => {
|
|
74
|
+
defer.reject(new Error(`Error writing secure file to disk. ${err.message}`));
|
|
75
|
+
});
|
|
76
|
+
yield defer.promise;
|
|
77
|
+
tl.debug('Downloaded secure file contents to: ' + tempDownloadPath);
|
|
78
|
+
return tempDownloadPath;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Delete secure file from the temporary location for the build
|
|
83
|
+
* @param secureFileId
|
|
84
|
+
*/
|
|
85
|
+
deleteSecureFile(secureFileId) {
|
|
86
|
+
const tempDownloadPath = this.getSecureFileTempDownloadPath(secureFileId);
|
|
87
|
+
if (tl.exist(tempDownloadPath)) {
|
|
88
|
+
tl.debug('Deleting secure file at: ' + tempDownloadPath);
|
|
89
|
+
tl.rmRF(tempDownloadPath);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the temporary download location for the secure file
|
|
94
|
+
* @param secureFileId
|
|
95
|
+
*/
|
|
96
|
+
getSecureFileTempDownloadPath(secureFileId) {
|
|
97
|
+
const fileName = tl.getSecureFileName(secureFileId);
|
|
98
|
+
return tl.resolve(tl.getVariable('Agent.TempDirectory'), fileName);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.SecureFileHelpers = SecureFileHelpers;
|