import { GoogleAuth } from 'google-auth-library' ; import { Credential , errorMessage , fromBase64 , request , } from '@google-github-actions/actions-utils' ; const { version : appVersion } = require ( '../package.json' ) ; const userAgent = `github-actions-get-secretmanager-secrets/${ appVersion }` ; param credentials param endpoint type ClientOptions = { credentials ? : Credential ; endpoint ? : string ; } ; param opts returns export class Client { readonly defaultEndpoint = 'https://secretmanager.googleapis.com/v1' ; readonly defaultScope = 'https://www.googleapis.com/auth/cloud-platform' ; readonly auth : GoogleAuth ; readonly endpoint : string ; constructor ( opts ? ) { this . endpoint = opts ?. endpoint || this . defaultEndpoint ; this . auth = new GoogleAuth ( { scopes : [ this . defaultScope ] , credentials : opts ?. credentials , } ) ; } param ref returns async accessSecret ( ref ) { if ( ! ref ) { throw new Error ( `Secret ref "${ ref }" is empty!` ) ; } try { const token = await this . auth . getAccessToken ( ) ; const response = await request ( 'GET' , `${ this . endpoint }/${ ref }:access` , null , { headers : { 'Authorization' : `Bearer ${ token }` , 'User-Agent' : userAgent , } , } ) ; const parsed = JSON . parse ( response ) ; const b64data = parsed ?. payload ?. data ; if ( ! b64data ) { throw new Error ( `Secret "${ ref }" returned no data!` ) ; } return fromBase64 ( b64data ) ; } catch ( err ) { const msg = errorMessage ( err ) ; throw new Error ( `Failed to access secret "${ ref }": ${ msg }` ) ; } } }