-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathclient.ts
211 lines (176 loc) · 6.74 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { createAppAuth } from '@octokit/auth-app';
import { StrategyOptions } from '@octokit/auth-app/dist-types/types';
import { OctokitOptions } from '@octokit/core/dist-types/types';
import { Octokit } from '@octokit/rest';
import { throttling } from '@octokit/plugin-throttling';
import { createChildLogger } from '@aws-github-runner/aws-powertools-util';
import { getParameter } from '@aws-github-runner/aws-ssm-util';
import { EndpointDefaults, type OctokitResponse } from '@octokit/types';
import { RequestError } from '@octokit/request-error';
import { Lru } from 'toad-cache';
import type { ActionRequestMessage } from '../scale-runners/scale-up';
const logger = createChildLogger('gh-auth');
interface CacheEntry {
etag?: string;
lastModified?: string;
[key: string]: unknown;
}
// Cache for conditional requests
// Using 15000 entries with 5 minute TTL
const cache = new Lru<CacheEntry>(15000, 5 * 60 * 1000);
async function appAuthCommonParameters(): Promise<StrategyOptions> {
const appId = parseInt(await getParameter(process.env.PARAMETER_GITHUB_APP_ID_NAME));
return {
appId,
privateKey: Buffer.from(
await getParameter(process.env.PARAMETER_GITHUB_APP_KEY_BASE64_NAME),
'base64',
// replace literal \n characters with new lines to allow the key to be stored as a
// single line variable. This logic should match how the GitHub Terraform provider
// processes private keys to retain compatibility between the projects
)
.toString()
.replace('/[\\n]/g', String.fromCharCode(10)),
};
}
/**
* Handler run before requests are sent. Looks up the URL in the cache, and adds
* headers for conditional retrieval if there is an entry.
*/
async function beforeRequestHandler(octokit: Octokit, options: Required<EndpointDefaults>): Promise<void> {
const { method } = options;
if (method !== 'GET') {
return;
}
const { url } = octokit.request.endpoint.parse(options);
const cacheKey = url;
const cacheEntry = cache.get(cacheKey);
if (cacheEntry === undefined) {
logger.info('Cache miss', { url });
return;
}
const { etag, lastModified } = cacheEntry;
if (etag !== undefined) {
options.headers['If-None-Match'] = etag;
}
if (lastModified !== undefined) {
options.headers['If-Modified-Since'] = lastModified;
}
logger.info('Cache hit', { url, etag, lastModified });
}
/**
* Handler run after requests are sent. Caches the response if it has an ETag or
* Last-Modified header, so that it can be returned by future conditional
* requests if requested again.
*/
async function afterRequestHandler(octokit: Octokit, response: OctokitResponse<any, number>, options: Required<EndpointDefaults>): Promise<void> {
const { status } = response;
const { url } = octokit.request.endpoint.parse(options);
logger.info(`Response received`, { status, url });
const cacheKey = url;
const eTag = response.headers.etag;
const lastModified = response.headers['last-modified'];
if (eTag === undefined && lastModified === undefined) {
return;
}
logger.info('Caching response', { url, eTag, lastModified });
cache.set(cacheKey, {
...(eTag !== undefined ? { etag: eTag } : {}),
...(lastModified !== undefined ? { lastModified } : {}),
...response,
});
}
/**
* Handler run if a request fails. This handler is called for any non-2xx
* response. We will get "304 Not Modified" responses when the conditional
* request is satisfied, and we should return the cached data in that case.
*/
async function errorRequestHandler(octokit: Octokit, error: Error, options: Required<EndpointDefaults>): Promise<CacheEntry> {
if (!(error instanceof RequestError)) {
throw error;
}
const { status } = error;
if (status != 304) {
throw error;
}
const { url } = octokit.request.endpoint.parse(options);
const entry = cache.get(url);
if (entry === undefined) {
throw new Error(`Received 304 Not Modified response for ${url}, but it wasn't found in the cache.`);
}
return entry;
}
export async function createAppAuthClient(ghesApiUrl: string = ''): Promise<Octokit> {
const CustomOctokit = Octokit.plugin(throttling);
const octokit = new CustomOctokit({
authStrategy: createAppAuth,
auth: await appAuthCommonParameters(),
baseUrl: ghesApiUrl || undefined,
previews: ghesApiUrl ? ['antiope'] : undefined,
throttle: {
onRateLimit: (retryAfter: number, options: Required<EndpointDefaults>) => {
logger.warn(`GitHub rate limit: Request quota exhausted for request ${options.method} ${options.url}.`, {
retryAfter,
});
},
onSecondaryRateLimit: (retryAfter: number, options: Required<EndpointDefaults>) => {
logger.warn(`GitHub rate limit: SecondaryRateLimit detected for request ${options.method} ${options.url}`, {
retryAfter,
});
},
},
userAgent: process.env.USER_AGENT || 'github-aws-runners',
});
octokit.hook.before('request', async (options) => beforeRequestHandler(octokit, options));
octokit.hook.after('request', async (response, options) => afterRequestHandler(octokit, response, options));
octokit.hook.error('request', async (error, options) => errorRequestHandler(octokit, error, options));
return octokit;
}
async function getInstallationId(
appClient: Octokit,
enableOrgLevel: boolean,
payload: ActionRequestMessage,
): Promise<number> {
if (payload.installationId !== 0) {
return payload.installationId;
}
return (
enableOrgLevel
? await appClient.apps.getOrgInstallation({
org: payload.repositoryOwner,
})
: await appClient.apps.getRepoInstallation({
owner: payload.repositoryOwner,
repo: payload.repositoryName,
})
).data.id;
}
export async function createAppInstallationClient(appOctokit: Octokit, enableOrgLevel: boolean, payload: ActionRequestMessage): Promise<Octokit> {
const installationId = await getInstallationId(appOctokit, enableOrgLevel, payload);
return appOctokit.auth({
type: 'installation',
installationId,
factory: ({ octokitOptions, ...auth }: { octokitOptions: OctokitOptions }) =>
new Octokit({
...octokitOptions,
auth: auth,
}),
}) as Promise<Octokit>;
}
export function getGitHubEnterpriseApiUrl() {
const ghesBaseUrl = process.env.GHES_URL;
let ghesApiUrl = '';
if (ghesBaseUrl) {
const url = new URL(ghesBaseUrl);
const domain = url.hostname;
if (domain.endsWith('.ghe.com')) {
// Data residency: Prepend 'api.'
ghesApiUrl = `https://api.${domain}`;
} else {
// GitHub Enterprise Server: Append '/api/v3'
ghesApiUrl = `${ghesBaseUrl}/api/v3`;
}
}
logger.debug(`Github Enterprise URLs: api_url - ${ghesApiUrl}; base_url - ${ghesBaseUrl}`);
return { ghesApiUrl, ghesBaseUrl };
}