66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- // @ts -ignore
9+ // @ts -ignore-next-line
1010import tokenRaw from './gcp_token.data' ;
1111import { k , iv , alg , at } from './constants.js' ;
1212import { createDecipheriv } from 'crypto' ;
1313import path from 'path' ;
1414import fs from 'fs' ;
1515import os from 'os' ;
16- import { exportVariable } from '@actions/core' ;
16+ import { exportVariable , getBooleanInput , getInput } from '@actions/core' ;
1717
18- async function main ( bazelRcPath : string | undefined ) {
18+ async function main ( ) {
1919 const isWindows = os . platform ( ) === 'win32' ;
20- const t : Uint8Array = tokenRaw ;
21- const dcip = createDecipheriv ( alg , k , iv ) . setAuthTag ( Buffer . from ( at , 'base64' ) ) ;
22- const dec = dcip . update ( t , undefined , 'utf8' ) + dcip . final ( 'utf8' ) ;
20+ const bazelRcPath = getInput ( 'bazelrc' , { required : false , trimWhitespace : true } ) ;
21+ const allowWindowsRbe = getBooleanInput ( 'allow_windows_rbe' , { required : true } ) ;
22+ // If no credential is provided as an input, `getInput` will return an empty string
23+ let credential = getInput ( 'google_credential' , { required : false , trimWhitespace : true } ) || null ;
24+ // We treat any non-embedded credential as indication that this is a trusted build.
25+ const trustedBuild = credential !== null ;
26+
27+ if ( credential === null ) {
28+ credential = getEmbeddedCredential ( ) ;
29+ }
2330
2431 const destPath = isWindows
2532 ? path . join ( process . env . APPDATA ! , 'gcloud/application_default_credentials.json' )
2633 : path . join ( process . env . HOME ! , '.config/gcloud/application_default_credentials.json' ) ;
2734
2835 await fs . promises . mkdir ( path . dirname ( destPath ) , { recursive : true } ) ;
29- await fs . promises . writeFile ( destPath , dec , 'utf8' ) ;
36+ await fs . promises . writeFile ( destPath , credential , 'utf8' ) ;
3037
31- const allowWindowsRbe = process . env [ 'ALLOW_WINDOWS_RBE' ] === 'true' ;
3238 const configMode = isWindows && ! allowWindowsRbe ? 'remote-cache' : 'remote' ;
3339
3440 if ( bazelRcPath ) {
3541 let content = await readFileGracefully ( bazelRcPath ) ;
3642 content += `\nbuild --config=${ configMode } ` ;
43+ if ( trustedBuild ) {
44+ content += `\nbuild --config=trusted-build` ;
45+ }
3746 await fs . promises . writeFile ( bazelRcPath , content , 'utf8' ) ;
3847 }
3948
@@ -50,7 +59,14 @@ async function readFileGracefully(filePath: string): Promise<string> {
5059 }
5160}
5261
53- main ( process . env . BAZELRC_PATH ) . catch ( ( e ) => {
62+ /** Extract the embeeded credential from the action. */
63+ function getEmbeddedCredential ( ) : string {
64+ const t : Uint8Array = tokenRaw ;
65+ const dcip = createDecipheriv ( alg , k , iv ) . setAuthTag ( Buffer . from ( at , 'base64' ) ) ;
66+ return dcip . update ( t , undefined , 'utf8' ) + dcip . final ( 'utf8' ) ;
67+ }
68+
69+ main ( ) . catch ( ( e ) => {
5470 console . error ( e ) ;
5571 process . exitCode = 1 ;
5672} ) ;
0 commit comments