@@ -19,6 +19,10 @@ import { Configuration, PolicyResponse } from "./interfaces";
1919import { fetchPolicy , mergeConfigs } from "./policy-utils" ;
2020import * as cache from "@actions/cache" ;
2121import { getCacheEntry } from "@actions/cache/lib/internal/cacheHttpClient" ;
22+ import * as cacheTwirpClient from "@actions/cache/lib/internal/shared/cacheTwirpClient" ;
23+ import { GetCacheEntryDownloadURLRequest } from "@actions/cache/lib/generated/results/api/v1/cache" ;
24+ import { getCacheServiceVersion } from "@actions/cache/lib/internal/config" ;
25+
2226import * as utils from "@actions/cache/lib/internal/cacheUtils" ;
2327import { isArcRunner , sendAllowedEndpoints } from "./arc-runner" ;
2428import { STEPSECURITY_API_URL , STEPSECURITY_WEB_URL } from "./configs" ;
@@ -114,28 +118,81 @@ interface MonitorResponse {
114118 } catch ( exception ) {
115119 console . log ( exception ) ;
116120 }
117- try {
118- const compressionMethod : CompressionMethod =
119- await utils . getCompressionMethod ( ) ;
120- const cacheFilePath = path . join ( __dirname , "cache.txt" ) ;
121- core . info ( `cacheFilePath ${ cacheFilePath } ` ) ;
122- const cacheEntry : ArtifactCacheEntry = await getCacheEntry (
123- [ cacheKey ] ,
124- [ cacheFilePath ] ,
125- {
126- compressionMethod : compressionMethod ,
121+
122+ const cacheServiceVersion : string = getCacheServiceVersion ( ) ;
123+
124+ switch ( cacheServiceVersion ) {
125+ case "v2" :
126+ core . info ( `cache version: v2` ) ;
127+ try {
128+ const cacheFilePath = path . join ( __dirname , "cache.txt" ) ;
129+ core . info ( `cacheFilePath ${ cacheFilePath } ` ) ;
130+
131+ const twirpClient = cacheTwirpClient . internalCacheTwirpClient ( ) ;
132+ const compressionMethod = await utils . getCompressionMethod ( ) ;
133+
134+ const request : GetCacheEntryDownloadURLRequest = {
135+ key : cacheKey ,
136+ restoreKeys : [ ] ,
137+ version : utils . getCacheVersion (
138+ [ cacheFilePath ] ,
139+ compressionMethod ,
140+ false
141+ ) ,
142+ } ;
143+
144+ const response = await twirpClient . GetCacheEntryDownloadURL (
145+ request
146+ ) ;
147+
148+ if ( ! response . ok ) {
149+ core . debug (
150+ `Cache not found for version ${ request . version } of keys: ${ cacheKey } `
151+ ) ;
152+ return undefined ;
153+ }
154+
155+ const url = new URL ( response . signedDownloadUrl ) ;
156+ core . info (
157+ `Adding cacheHost: ${ url . hostname } :443 to allowed-endpoints`
158+ ) ;
159+
160+ confg . allowed_endpoints += ` ${ url . hostname } :443` ;
161+ } catch ( e ) {
162+ core . error ( `v2 failed: ${ e } ` ) ;
163+ }
164+ break ;
165+
166+ case "v1" :
167+ core . info ( `cache version: v1` ) ;
168+
169+ try {
170+ const compressionMethod : CompressionMethod =
171+ await utils . getCompressionMethod ( ) ;
172+ const cacheFilePath = path . join ( __dirname , "cache.txt" ) ;
173+ core . info ( `cacheFilePath ${ cacheFilePath } ` ) ;
174+
175+ const cacheEntry : ArtifactCacheEntry = await getCacheEntry (
176+ [ cacheKey ] ,
177+ [ cacheFilePath ] ,
178+ {
179+ compressionMethod : compressionMethod ,
180+ }
181+ ) ;
182+ const url = new URL ( cacheEntry . archiveLocation ) ;
183+ core . info (
184+ `Adding cacheHost: ${ url . hostname } :443 to allowed-endpoints`
185+ ) ;
186+
187+ confg . allowed_endpoints += ` ${ url . hostname } :443` ;
188+ } catch ( exception ) {
189+ // some exception has occurred.
190+ core . info ( `Unable to fetch cacheURL ${ exception } ` ) ;
191+ if ( confg . egress_policy === "block" ) {
192+ core . info ( "Switching egress-policy to audit mode" ) ;
193+ confg . egress_policy = "audit" ;
194+ }
127195 }
128- ) ;
129- const url = new URL ( cacheEntry . archiveLocation ) ;
130- core . info ( `Adding cacheHost: ${ url . hostname } :443 to allowed-endpoints` ) ;
131- confg . allowed_endpoints += ` ${ url . hostname } :443` ;
132- } catch ( exception ) {
133- // some exception has occurred.
134- core . info ( `Unable to fetch cacheURL` ) ;
135- if ( confg . egress_policy === "block" ) {
136- core . info ( "Switching egress-policy to audit mode" ) ;
137- confg . egress_policy = "audit" ;
138- }
139196 }
140197 }
141198
0 commit comments