Skip to content

Commit b7addcb

Browse files
committed
SIGN-8057 Improve logging and simplify code
1 parent 71e5ad2 commit b7addcb

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

actions/submit-signing-request/connector-url-builder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// TODO: write tests
2-
31
export class ConnectorUrlBuilder {
42
private readonly apiVersion: string = "1.0";
53
private readonly baseSigningRequestsRoute: string;

actions/submit-signing-request/helper-artifact-download.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import * as nodeStreamZip from 'node-stream-zip';
55
import axios, { AxiosError } from 'axios';
66
import { HelperInputOutput } from "./helper-input-output";
7-
import { buildSignPathAuthorizationHeader, httpErrorResponseToText } from './utils';
7+
import { httpErrorResponseToText } from './utils';
88
import { TimeoutStream } from './timeout-stream';
99

1010

@@ -19,14 +19,11 @@ export class HelperArtifactDownload {
1919

2020
const response = await axios.get(artifactDownloadUrl, {
2121
responseType: 'stream',
22-
timeout: timeoutMs,
23-
headers: {
24-
Authorization: buildSignPathAuthorizationHeader(this.helperInputOutput.signPathApiToken)
25-
}
22+
timeout: timeoutMs
2623
})
27-
.catch((e: AxiosError) => {
28-
throw new Error(httpErrorResponseToText(e));
29-
});
24+
.catch((e: AxiosError) => {
25+
throw new Error(httpErrorResponseToText(e));
26+
});
3027

3128
const targetDirectory = this.resolveOrCreateDirectory(this.helperInputOutput.outputArtifactDirectory);
3229

@@ -67,8 +64,8 @@ export class HelperArtifactDownload {
6764
core.info(`The signed artifact has been successfully downloaded from SignPath and extracted to ${targetDirectory}`);
6865
}
6966

70-
public resolveOrCreateDirectory(directoryPath:string): string {
71-
const workingDirectory = process.env.GITHUB_WORKSPACE as string;
67+
public resolveOrCreateDirectory(directoryPath: string): string {
68+
const workingDirectory = process.env.GITHUB_WORKSPACE as string;
7269
const absolutePath = path.isAbsolute(directoryPath) ? directoryPath :
7370
path.join(workingDirectory as string, directoryPath);
7471

actions/submit-signing-request/task.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as core from '@actions/core';
44
import * as moment from 'moment';
55

66
import { LogEntry, LogLevelDebug, LogLevelError, LogLevelInformation, LogLevelWarning, SubmitSigningRequestResult, ValidationResult } from './dtos/submit-signing-request-result';
7-
import { buildSignPathAuthorizationHeader, executeWithRetries, httpErrorResponseToText } from './utils';
7+
import { executeWithRetries, httpErrorResponseToText } from './utils';
88
import { ConnectorUrlBuilder } from './connector-url-builder';
99
import { HelperInputOutput } from './helper-input-output';
1010
import { taskVersion } from './version';
@@ -15,18 +15,19 @@ import { SigningRequestStatusDto } from './dtos/signing-request-status';
1515
// output variables
1616
// signingRequestId - the id of the newly created signing request
1717
// signingRequestWebUrl - the url of the signing request in SignPath
18-
// signPathApiUrl - the base API url of the SignPath API
19-
// signingRequestDownloadUrl - the url of the signed artifact in SignPath
18+
// signingRequestDownloadUrl - the url of the signed artifact to retrieve via the connector
2019

2120
export class Task {
22-
urlBuilder: ConnectorUrlBuilder;
21+
private readonly urlBuilder: ConnectorUrlBuilder;
22+
private readonly userAgent: string;
2323

2424
constructor(
25-
private helperInputOutput: HelperInputOutput,
26-
private helperArtifactDownload: HelperArtifactDownload,
27-
private config: Config
25+
private readonly helperInputOutput: HelperInputOutput,
26+
private readonly helperArtifactDownload: HelperArtifactDownload,
27+
private readonly config: Config
2828
) {
2929
this.urlBuilder = new ConnectorUrlBuilder(this.helperInputOutput.signPathConnectorUrl, this.helperInputOutput.organizationId);
30+
this.userAgent = `SignPath.SubmitSigningRequestGitHubAction/${taskVersion}(NodeJS/${process.version}; ${process.platform} ${process.arch}})`;
3031
}
3132

3233
async run() {
@@ -54,20 +55,18 @@ export class Task {
5455

5556
private async submitSigningRequest(): Promise<string> {
5657

57-
core.info('Submitting the signing request to SignPath CI connector...');
58+
const submitSigningRequestUrl = this.urlBuilder.buildSubmitSigningRequestUrl();
59+
core.info('Submitting the signing request to SignPath GitHub Actions connector...');
5860

5961
// prepare the payload
6062
const submitRequestPayload = this.buildSigningRequestPayload();
6163

62-
// call the signPath API to submit the signing request
64+
// call the connector to submit the signing request
6365
const response = (await axios
64-
.post<SubmitSigningRequestResult>(this.urlBuilder.buildSubmitSigningRequestUrl(),
66+
.post<SubmitSigningRequestResult>(submitSigningRequestUrl,
6567
submitRequestPayload,
6668
{
67-
responseType: "json",
68-
headers: {
69-
"Authorization": buildSignPathAuthorizationHeader(this.helperInputOutput.signPathApiToken)
70-
}
69+
responseType: "json"
7170
})
7271
.catch((e: AxiosError) => {
7372

@@ -101,9 +100,7 @@ export class Task {
101100

102101
this.helperInputOutput.setSigningRequestId(response.signingRequestId);
103102
this.helperInputOutput.setSigningRequestWebUrl(response.signingRequestUrl);
104-
105-
// TODO: think what to set as output
106-
// this.helperInputOutput.setSignPathApiUrl(this.urlBuilder.signPathBaseUrl + '/API');
103+
this.helperInputOutput.setSignedArtifactDownloadUrl(this.urlBuilder.buildGetSignedArtifactUrl(response.signingRequestId))
107104

108105
return response.signingRequestId;
109106
}
@@ -129,7 +126,6 @@ export class Task {
129126
}
130127
}
131128

132-
// TODO: what the heck
133129
// if auto-generated GitHub Actions token (secrets.GITHUB_TOKEN) is used for artifact download,
134130
// ensure the workflow continues running until the download is complete.
135131
// The token is valid only for the workflow's duration
@@ -205,15 +201,13 @@ export class Task {
205201

206202
private async getSigningRequestStatus(signingRequestId: string): Promise<SigningRequestStatusDto> {
207203
const requestStatusUrl = this.urlBuilder.buildGetSigningRequestStatusUrl(signingRequestId);
204+
core.info(`Sending request: GET ${requestStatusUrl}`)
208205

209206
const signingRequestStatusDto = await axios
210207
.get<SigningRequestStatusDto>(
211208
requestStatusUrl,
212209
{
213-
responseType: "json",
214-
headers: {
215-
"Authorization": buildSignPathAuthorizationHeader(this.helperInputOutput.signPathApiToken)
216-
}
210+
responseType: "json"
217211
}
218212
)
219213
.catch((e: AxiosError) => {
@@ -229,10 +223,20 @@ export class Task {
229223
private configureAxios(): void {
230224

231225
// set user agent
232-
axios.defaults.headers.common['User-Agent'] = this.buildUserAgent();
226+
axios.defaults.headers.common['User-Agent'] = this.userAgent;
227+
228+
// set token for all outgoing requests
229+
axios.defaults.headers.common.Authorization = `Bearer ${this.helperInputOutput.signPathApiToken}`
230+
233231
const timeoutMs = this.helperInputOutput.serviceUnavailableTimeoutInSeconds * 1000
234232
axios.defaults.timeout = timeoutMs;
235233

234+
// log all outgoing requests
235+
axios.interceptors.request.use(request => {
236+
core.info(`Sending request: ${request.method?.toUpperCase()} ${request.url}`)
237+
return request;
238+
})
239+
236240
// original axiosRetry doesn't work for POST requests
237241
// thats why we need to override some functions
238242
axiosRetry.isNetworkOrIdempotentRequestError = (error: AxiosError) => {
@@ -287,11 +291,6 @@ export class Task {
287291

288292
}
289293

290-
private buildUserAgent(): string {
291-
const userAgent = `SignPath.SubmitSigningRequestGitHubAction/${taskVersion}(NodeJS/${process.version}; ${process.platform} ${process.arch}})`;
292-
return userAgent;
293-
}
294-
295294
private checkResponseStructure(response: SubmitSigningRequestResult): void {
296295
if (!response.validationResult && !response.signingRequestId) {
297296

actions/submit-signing-request/utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ export function getInputNumber(name: string, options?: core.InputOptions): numbe
4949
return result;
5050
}
5151

52-
export function buildSignPathAuthorizationHeader(apiToken: string): string {
53-
return `Bearer ${apiToken}`;
54-
}
55-
5652
export function httpErrorResponseToText(err: AxiosError): string {
5753

5854
const response = err.response as AxiosResponse;

0 commit comments

Comments
 (0)