forked from CloudSnorkel/cdk-github-runners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.svelte
397 lines (376 loc) · 15.1 KB
/
App.svelte
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<script lang="ts">
const secret = 'INSERT_SECRET_ARN_HERE';
const token = 'INSERT_TOKEN_HERE';
let instance: undefined | 'github.com' | 'ghes';
let domain = 'INSERT_DOMAIN_HERE';
let auth: undefined | 'newApp' | 'existingApp' | 'pat';
let appScope: 'user' | 'org' = 'user';
let runnerLevel: 'repo' | 'org' = 'repo';
let org = 'ORGANIZATION';
let existingAppId: string = '';
let existingAppPk: string = '';
let pat: string = '';
let success: boolean;
let result: string | undefined;
interface Permissions {
actions: 'write' | 'read';
administration?: 'write' | 'read';
organization_self_hosted_runners?: 'write' | 'read';
deployments: 'write' | 'read';
};
const repositoryPermissions: Permissions = {
actions: 'write',
administration: 'write',
deployments: 'read',
};
const organizationPermissions: Permissions = {
actions: 'write',
organization_self_hosted_runners: 'write',
deployments: 'read',
};
const manifest = {
url: 'https://github.com/CloudSnorkel/cdk-github-runners',
hook_attributes: {
url: 'INSERT_WEBHOOK_URL_HERE',
},
redirect_url: 'INSERT_BASE_URL_HERE/complete-new-app',
public: false,
default_permissions: <Permissions>repositoryPermissions,
default_events: [
'workflow_job',
],
};
function isSubmitDisabled(instance_, auth_, existingAppId_, existingAppPk_, runnerLevel_, pat_, success_) {
if (success_) {
return true;
}
if (instance_ === undefined || auth_ === undefined) {
return true;
}
if (auth_ === 'newApp') {
return false;
}
if (auth_ === 'existingApp') {
return existingAppId_ === '' || existingAppPk_ === '' || runnerLevel_ === undefined;
}
if (auth_ === 'pat') {
return pat_ === '';
}
console.error('Something is broken', instance_, auth_, existingAppId_);
return true;
}
function submitText(auth_) {
if (auth_ === 'newApp') {
return 'Create GitHub App';
}
return 'Setup';
}
function postJson(url, data): Promise<string> {
return new Promise<string>((resolve, reject) => {
fetch(`${url}?token=${token}`, {
method: 'POST',
mode: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
redirect: 'error',
})
.then(response => {
if (!response.ok) {
response.text()
.then(text => {
reject(new Error(`${text} [${response.status}]`));
})
.catch(reject);
} else {
response.text()
.then(resolve)
.catch(reject);
}
})
.catch(reject);
});
}
function submit(ev) {
ev.preventDefault();
function promise(): Promise<string> {
const rightDomain = instance === 'ghes' ? domain : 'github.com';
manifest.default_permissions =
runnerLevel === 'repo'
? repositoryPermissions
: organizationPermissions;
switch (auth) {
case 'newApp':
return postJson('domain', { domain: rightDomain, runnerLevel })
.then(_ => {
(document.getElementById('appform') as HTMLFormElement).submit();
return Promise.resolve('Redirecting to GitHub...');
});
case 'existingApp':
return postJson('app', {
appid: existingAppId,
pk: existingAppPk,
domain: rightDomain,
runnerLevel,
});
case 'pat':
return postJson('pat', {
pat: pat,
domain: rightDomain,
});
}
}
promise()
.then(successText => {
result = successText;
success = true;
})
.catch(error => {
result = `${error}`;
success = false;
});
}
</script>
<main>
<div class="container py-3 px-2">
<div class="row">
<form class="col" on:submit={submit}>
<h1>Setup GitHub Runners</h1>
<p>Answer all the questions on this page to automatically configure GitHub integration and get the
runners working. This page will not be accessible once you complete this operation. If you ever want
to access it again, edit <code>{secret}</code> and run the status function again.</p>
<h3>Choose GitHub Instance</h3>
<div class="px-3 py-3">
<p>Are your repositories hosted on GitHub.com or are you using an on-premise installation of GitHub
Enterprise Server?</p>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={instance} value="github.com"
id="github.com">
<label class="form-check-label" for="github.com">
GitHub.com
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={instance} value="ghes" id="ghes">
<label class="form-check-label" for="ghes">
GitHub Enterprise Server
</label>
</div>
</div>
{#if instance === 'ghes'}
<h3>GitHub Enterprise Server Domain</h3>
<div class="px-3 py-3">
<p>Where is GitHub Enterprise Server hosted? Type in the domain without <code>https://</code>
and without any path. It should look something like <code>github.mycompany.com</code>.</p>
<input class="form-control" bind:value={domain}>
</div>
{/if}
{#if instance}
<h3>Authentication Type</h3>
<div class="px-3 py-3">
<p>You can choose between creating a new app that will provide authentication for specific
repositories, or a personal access token that will provide access to all repositories
available to you. Apps are easier to set up and provide more fine-grained access control. If
you have previously created an app, you can choose to use an existing app.</p>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={auth} value="newApp" id="newApp">
<label class="form-check-label" for="newApp">
New GitHub App <b>(recommended)</b>
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={auth} value="existingApp"
id="existingApp">
<label class="form-check-label" for="existingApp">
Existing GitHub App
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={auth} value="pat" id="pat">
<label class="form-check-label" for="pat">
Personal Access Token
</label>
</div>
</div>
{/if}
{#if auth === 'newApp'}
<h3>New App Settings</h3>
<div class="px-3 py-3">
<p>Choose whether to create a new personal app or organization app. A private personal app can
only be used for repositories under your user. A private organization app can only be used
for repositories under that organization.</p>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={appScope} value="user"
id="userScope">
<label class="form-check-label" for="userScope">
User app
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" bind:group={appScope} value="org"
id="orgScope">
<label class="form-check-label" for="orgScope">
Organization app
</label>
</div>
{#if instance === 'ghes'}
<p class="pt-2">If multiple organizations under the same GitHub Enterprise Server need to use the runners,
you can make the app public.</p>
<div class="form-check">
<input class="form-check-input" type="checkbox" bind:checked={manifest.public} id="public">
<label class="form-check-label" for="public">
Public app
</label>
</div>
{/if}
</div>
{#if appScope === 'org'}
<h3>Organization name</h3>
<div class="px-3 py-3">
<p>What is the slug for your organization? If your repositories have a URL like
<code>https://{domain}/MyOrg/my-repo</code>
then your organization slug is <code>MyOrg</code>.</p>
<input class="form-control" bind:value={org}>
</div>
{/if}
{:else if auth === 'existingApp'}
<h3>Existing App Details</h3>
<div class="px-3 py-3">
<div class="form-group row px-3 py-2">
<label for="appid" class="col-sm-2 col-form-label">App Id</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="appid" bind:value={existingAppId}>
</div>
</div>
<div class="form-group row px-3 py-2">
<label for="pk" class="col-sm-2 col-form-label">Private Key</label>
<div class="col-sm-10">
<textarea class="form-control" id="pk" bind:value={existingAppPk} rows="10"></textarea>
</div>
</div>
<div class="form-group row px-3 py-2">
<div class="col-sm-2 col-form-label">Registration Level</div>
<div class="col-sm-10">
<div class="form-check">
<input
class="form-check-input"
type="radio"
bind:group={runnerLevel}
value="repo"
id="repo"
/>
<label class="form-check-label" for="repo">Repository</label>
</div>
<div class="form-check">
<input
class="form-check-input"
type="radio"
bind:group={runnerLevel}
value="org"
id="org"
/>
<label class="form-check-label" for="org">Organization</label>
</div>
</div>
</div>
<h4>Required Permissions</h4>
<p>The existing app must have the following permissions.</p>
<pre>{JSON.stringify(runnerLevel === 'repo' ? repositoryPermissions : organizationPermissions, undefined, 2)}</pre>
<h4>Webhook</h4>
<p>
Don't forget to set up the webhook and its secret as described in <a
href="https://github.com/CloudSnorkel/cdk-github-runners/blob/main/SETUP_GITHUB.md">SETUP_GITHUB.md</a>.
</p>
</div>
{:else if auth === 'pat'}
<h2>Personal Access Token</h2>
<div class="px-3 py-3">
<p>The <a href="https://{domain}/settings/tokens">personal access token</a> must have the <code>repo</code>
scope enabled. Don't forget to also create a webhook as described in <a
href="https://github.com/CloudSnorkel/cdk-github-runners/blob/main/SETUP_GITHUB.md">SETUP_GITHUB.md</a>.
</p>
<input class="form-control" bind:value={pat}
placeholder="Token e.g. ghp_abcdefghijklmnopqrstuvwxyz1234567890">
</div>
{/if}
{#if appScope === 'org' && auth === 'newApp'}
<h3>Registration Level</h3>
<div class="px-3 py-3">
<p>
Would you like runners to be registered on repository level, or on organization level?
</p>
<ul>
<li>
Registering runners on repository level requires the <code>administration</code>
<a href="https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/autoscaling-with-self-hosted-runners#authentication-requirements">permission</a>.
</li>
<li>
Registering runners on organization level only requires the <code>organization_self_hosted_runners</code>
<a href="https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/autoscaling-with-self-hosted-runners#authentication-requirements">permission</a>
which is more fine-grained.
</li>
<li>
Registering runners on organization level means any repository can use them, even if the app wasn't
installed on those repositories.
</li>
<li>
Do not use organization level registration if you don't fully trust all repositories in your organization.
</li>
<li>
Use organization level to reduce the permission scope this new app is given.
</li>
<li>
When in doubt, use the default repository level registration.
</li>
</ul>
<div class="form-check">
<input
class="form-check-input"
type="radio"
bind:group={runnerLevel}
value="repo"
id="repo"
/>
<label class="form-check-label" for="repo">Repository</label>
</div>
<div class="form-check">
<input
class="form-check-input"
type="radio"
bind:group={runnerLevel}
value="org"
id="org"
/>
<label class="form-check-label" for="org">Organization</label>
</div>
</div>
{/if}
<h2>Finish Setup</h2>
<div class="px-3 py-3">
{#if result === undefined}
<p>This button will be enabled once all the questions above are answered.</p>
{:else}
<div class="alert alert-{success ? 'success' : 'danger'}" role="alert">
{result}
</div>
{/if}
{#if manifest.public && auth === 'newApp'}
<p><b class="text-danger">WARNING:</b> using a public app means anyone with access to <code>{domain}</code>
can use the runners you're setting up now. Anyone can create a workflow that will run on those runners,
have access to their instance profile, and be part of their security group. Consider the security
implications before continuing.</p>
{/if}
<button type="submit" class="btn btn-success"
disabled={isSubmitDisabled(instance, auth, existingAppId, existingAppPk, runnerLevel, pat, success)}>
{submitText(auth)}
</button>
</div>
</form>
</div>
</div>
<form action="https://{domain}/{appScope === 'org' ? `organizations/${org}/` : ''}settings/apps/new?state={token}"
method="post" id="appform">
<input type="hidden" name="manifest" value={JSON.stringify(manifest)}>
</form>
</main>