Skip to content

Commit 1f99358

Browse files
Fix issue to get cache endpoint (#253)
1 parent cbe5226 commit 1f99358

File tree

8 files changed

+62385
-7648
lines changed

8 files changed

+62385
-7648
lines changed

dist/post/index.js

Lines changed: 251 additions & 201 deletions
Large diffs are not rendered by default.

dist/post/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pre/index.js

Lines changed: 62112 additions & 7330 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/pre/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "step-security-harden-runner",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Security agent for GitHub-hosted runner to monitor the build process",
55
"main": "index.js",
66
"scripts": {
@@ -23,7 +23,7 @@
2323
},
2424
"homepage": "https://github.com/step-security/harden-runner#readme",
2525
"dependencies": {
26-
"@actions/cache": "^3.0.4",
26+
"@actions/cache": "^3.1.4",
2727
"@actions/core": "^1.5.0",
2828
"@actions/exec": "^1.1.0",
2929
"@actions/github": "^5.0.0",

src/cache.ts

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,13 @@
1-
import * as core from "@actions/core";
2-
import { HttpClient } from "@actions/http-client";
3-
import { RequestOptions } from "@actions/http-client/lib/interfaces";
4-
import { BearerCredentialHandler } from "@actions/http-client/lib/auth";
5-
import * as crypto from "crypto";
6-
7-
const versionSalt = "1.0";
81
export const cacheKey = "harden-runner-cacheKey";
92
export const cacheFile = "/home/agent/cache.txt";
103

11-
function getCacheApiUrl(resource: string): string {
12-
const baseUrl: string = process.env["ACTIONS_CACHE_URL"] || "";
13-
if (!baseUrl) {
14-
throw new Error("Cache Service Url not found, unable to restore cache.");
15-
}
16-
17-
const url = `${baseUrl}_apis/artifactcache/${resource}`;
18-
core.debug(`Resource Url: ${url}`);
19-
return url;
20-
}
21-
22-
function createAcceptHeader(type: string, apiVersion: string): string {
23-
return `${type};api-version=${apiVersion}`;
24-
}
25-
26-
function getRequestOptions(): RequestOptions {
27-
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
28-
29-
const requestOptions: RequestOptions = {
30-
headers: {
31-
Accept: createAcceptHeader("application/json", "6.0-preview.1"),
32-
Authorization: `Bearer ${token}`,
33-
},
34-
};
35-
36-
return requestOptions;
37-
}
38-
39-
function createHttpClient(): HttpClient {
40-
const token = process.env["ACTIONS_RUNTIME_TOKEN"] || "";
41-
const bhandler = new BearerCredentialHandler(token);
42-
return new HttpClient("actions/cache", [bhandler], getRequestOptions());
43-
}
44-
45-
export function getCacheVersion(
46-
paths: string[],
47-
compressionMethod?: CompressionMethod
48-
): string {
49-
const components = paths.concat(
50-
!compressionMethod || compressionMethod === CompressionMethod.Gzip
51-
? []
52-
: [compressionMethod]
53-
);
54-
55-
// Add salt to cache version to support breaking changes in cache entry
56-
components.push(versionSalt);
57-
58-
return crypto.createHash("sha256").update(components.join("|")).digest("hex");
59-
}
60-
61-
export async function getCacheEntry(
62-
keys: string[],
63-
paths: string[],
64-
options?: InternalCacheOptions
65-
): Promise<ArtifactCacheEntry | null> {
66-
const httpClient = createHttpClient();
67-
const version = getCacheVersion(paths, options?.compressionMethod);
68-
const resource = `cache?keys=${encodeURIComponent(
69-
keys.join(",")
70-
)}&version=${version}`;
71-
72-
const response = await httpClient.getJson<ArtifactCacheEntry>(
73-
getCacheApiUrl(resource)
74-
);
75-
if (response.statusCode === 204) {
76-
throw new Error("Request returned 204 status");
77-
}
78-
if (!isSuccessStatusCode(response.statusCode)) {
79-
throw new Error(`Cache service responded with ${response.statusCode}`);
80-
}
81-
82-
const cacheResult = response.result;
83-
const cacheDownloadUrl = cacheResult?.archiveLocation;
84-
if (!cacheDownloadUrl) {
85-
throw new Error("Cache still be done, but not found.");
86-
}
87-
88-
return cacheResult;
89-
}
90-
91-
export interface InternalCacheOptions {
92-
compressionMethod?: CompressionMethod;
93-
cacheSize?: number;
94-
}
95-
964
export interface ArtifactCacheEntry {
975
cacheKey?: string;
986
scope?: string;
997
creationTime?: string;
1008
archiveLocation?: string;
1019
}
10210

103-
function isSuccessStatusCode(statusCode?: number): boolean {
104-
if (!statusCode) {
105-
return false;
106-
}
107-
return statusCode >= 200 && statusCode < 300;
108-
}
109-
11011
export enum CompressionMethod {
11112
Gzip = "gzip",
11213
// Long range mode was added to zstd in v1.3.2.

src/setup.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import isDocker from "is-docker";
1111
import { context } from "@actions/github";
1212
import { EOL } from "os";
1313
import {
14-
cacheFile,
14+
ArtifactCacheEntry,
1515
cacheKey,
16+
cacheFile,
1617
CompressionMethod,
17-
getCacheEntry,
1818
isValidEvent,
1919
} from "./cache";
2020

21+
import {getCacheEntry} from "@actions/cache/lib/internal/cacheHttpClient"
22+
import * as utils from '@actions/cache/lib/internal/cacheUtils'
23+
2124
(async () => {
2225
try {
2326
if (process.platform !== "linux") {
@@ -90,15 +93,16 @@ import {
9093

9194
if (isValidEvent()) {
9295
try {
93-
const cacheEntry = await getCacheEntry([cacheKey], [cacheFile], {
94-
compressionMethod: CompressionMethod.ZstdWithoutLong,
96+
let compressionMethod:CompressionMethod = await utils.getCompressionMethod()
97+
const cacheEntry:ArtifactCacheEntry = await getCacheEntry([cacheKey], [cacheFile], {
98+
compressionMethod: compressionMethod,
9599
});
96100
const url = new URL(cacheEntry.archiveLocation);
97101
core.info(`Adding cacheHost: ${url.hostname}:443 to allowed-endpoints`);
98102
confg.allowed_endpoints += ` ${url.hostname}:443`;
99103
} catch (exception) {
100104
// some exception has occurred.
101-
core.info("Unable to fetch cacheURL");
105+
core.info(`Unable to fetch cacheURL`);
102106
if (confg.egress_policy === "block") {
103107
core.info("Switching egress-policy to audit mode");
104108
confg.egress_policy = "audit";

0 commit comments

Comments
 (0)