@@ -22,6 +22,8 @@ import { throttling } from '@octokit/plugin-throttling';
2222import { createChildLogger } from '@aws-github-runner/aws-powertools-util' ;
2323import { getParameter } from '@aws-github-runner/aws-ssm-util' ;
2424import { EndpointDefaults } from '@octokit/types' ;
25+ import { getInstallationAuthObject , getAuthConfig , createAuthCacheKey , createAuthConfigCacheKey } from './cache' ;
26+ import type { GithubAppConfig } from './types' ;
2527
2628const logger = createChildLogger ( 'gh-auth' ) ;
2729
@@ -64,32 +66,47 @@ export async function createGithubInstallationAuth(
6466 installationId : number | undefined ,
6567 ghesApiUrl = '' ,
6668) : Promise < InstallationAccessTokenAuthentication > {
67- const auth = await createAuth ( installationId , ghesApiUrl ) ;
68- const installationAuthOptions : InstallationAuthOptions = { type : 'installation' , installationId } ;
69- return auth ( installationAuthOptions ) ;
69+ const cacheKey = createAuthCacheKey ( 'installation' , installationId , ghesApiUrl ) ;
70+
71+ return getInstallationAuthObject ( cacheKey , async ( ) => {
72+ const auth = await createAuth ( installationId , ghesApiUrl ) ;
73+ const installationAuthOptions : InstallationAuthOptions = { type : 'installation' , installationId } ;
74+ return auth ( installationAuthOptions ) ;
75+ } ) ;
7076}
7177
7278async function createAuth ( installationId : number | undefined , ghesApiUrl : string ) : Promise < AuthInterface > {
73- const appId = parseInt ( await getParameter ( process . env . PARAMETER_GITHUB_APP_ID_NAME ) ) ;
74- let authOptions : StrategyOptions = {
75- appId,
76- privateKey : Buffer . from (
79+ const configCacheKey = createAuthConfigCacheKey ( ghesApiUrl ) ;
80+
81+ const config = await getAuthConfig ( configCacheKey , async ( ) : Promise < GithubAppConfig > => {
82+ const appId = parseInt ( await getParameter ( process . env . PARAMETER_GITHUB_APP_ID_NAME ) ) ;
83+ const privateKey = Buffer . from (
7784 await getParameter ( process . env . PARAMETER_GITHUB_APP_KEY_BASE64_NAME ) ,
7885 'base64' ,
7986 // replace literal \n characters with new lines to allow the key to be stored as a
8087 // single line variable. This logic should match how the GitHub Terraform provider
8188 // processes private keys to retain compatibility between the projects
8289 )
8390 . toString ( )
84- . replace ( '/[\\n]/g' , String . fromCharCode ( 10 ) ) ,
91+ . replace ( '/[\\n]/g' , String . fromCharCode ( 10 ) ) ;
92+
93+ return {
94+ appId,
95+ privateKey,
96+ } ;
97+ } ) ;
98+
99+ let authOptions : StrategyOptions = {
100+ appId : config . appId ,
101+ privateKey : config . privateKey ,
85102 } ;
86103 if ( installationId ) authOptions = { ...authOptions , installationId } ;
87104
88105 logger . debug ( `GHES API URL: ${ ghesApiUrl } ` ) ;
89106 if ( ghesApiUrl ) {
90107 authOptions . request = request . defaults ( {
91108 baseUrl : ghesApiUrl ,
92- } ) ;
109+ } ) as RequestInterface ;
93110 }
94111 return createAppAuth ( authOptions ) ;
95112}
0 commit comments