-
Notifications
You must be signed in to change notification settings - Fork 268
/
PreferencesRegistriesEditing.svelte
637 lines (581 loc) · 26.1 KB
/
PreferencesRegistriesEditing.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<script lang="ts">
import type * as containerDesktopAPI from '@podman-desktop/api';
import { onMount } from 'svelte';
import { registriesInfos, registriesSuggestedInfos } from '../../stores/registries';
import DropdownMenu from '../ui/DropdownMenu.svelte';
import DropdownMenuItem from '../ui/DropDownMenuItem.svelte';
import { faTrash, faUser, faUserPen } from '@fortawesome/free-solid-svg-icons';
import SettingsPage from './SettingsPage.svelte';
// contains the original instances of registries when user clicks on `Edit password` menu item
// to be able to roll back changes when `Cancel` button is clicked
let originRegistries: containerDesktopAPI.Registry[] = [];
// login error responses
let errorResponses: { serverUrl: string; error: string }[] = [];
// list of server urls for which we show the password
let showPasswordForServerUrls: string[] = [];
// show or hide new registry form
export let showNewRegistryForm = false;
// at this moment it should be `podman`, but later can be any
let defaultProviderSourceName: string;
// store password input fields to handle password showing trigger
let passwordElements: { serverUrl: string; element: HTMLInputElement }[] = [];
// List of suggested registries to show
let suggestedRegistries: containerDesktopAPI.RegistrySuggestedProvider[] = [];
// List of registries to keep track of hidden / unhidden inputs
let listedSuggestedRegistries = [];
// Busy flag while attempting login
let loggingIn = false;
// used when user tries to add new registry
const newRegistryRequest = {
source: '',
serverUrl: '',
username: '',
secret: '',
} as containerDesktopAPI.Registry;
onMount(async () => {
let providerSourceNames = await window.getImageRegistryProviderNames();
if (providerSourceNames && providerSourceNames.length > 0) {
defaultProviderSourceName = providerSourceNames[0];
}
});
$: {
suggestedRegistries;
}
function markRegistryAsModified(registry: containerDesktopAPI.Registry) {
setPasswordForRegistryVisible(registry, false);
// create a backup instance of registry with initial data to have an ability to roll back user changes
const originRegistry = {
source: registry.source,
serverUrl: registry.serverUrl,
username: registry.username,
secret: registry.secret,
} as containerDesktopAPI.Registry;
originRegistries = [...originRegistries, originRegistry];
}
function markRegistryAsClean(registry: containerDesktopAPI.Registry) {
let originRegistry = originRegistries.find(r => r.serverUrl === registry.serverUrl);
registriesInfos.update(registries => {
const registryInfo = registries.find(r => r.serverUrl === originRegistry.serverUrl);
// here we check if values in edited registry are the same with the origin one and if they are differed,
// then we roll back changes made by user when Cancel button is clicked
if (
registryInfo &&
originRegistry &&
(originRegistry.username !== registryInfo.username || originRegistry.secret !== registryInfo.secret)
) {
registryInfo.username = originRegistry.username;
registryInfo.secret = originRegistry.secret;
}
return registries;
});
originRegistries = originRegistries.filter(r => r.serverUrl !== registry.serverUrl);
clearErrorResponse(registry.serverUrl);
setPasswordForRegistryVisible(registry, false);
}
function setPasswordForRegistryVisible(registry: containerDesktopAPI.Registry, visible: boolean) {
const serverUrl = registry === newRegistryRequest ? '' : registry.serverUrl;
const index = showPasswordForServerUrls.findIndex(r => r === serverUrl);
if (visible && index === -1) {
showPasswordForServerUrls = [...showPasswordForServerUrls, serverUrl];
let passwordInputElement = passwordElements.find(el => el.serverUrl === serverUrl);
if (passwordInputElement) {
passwordInputElement.element.type = 'text';
}
} else if (!visible && index > -1) {
showPasswordForServerUrls = showPasswordForServerUrls.filter(r => r !== serverUrl);
let passwordInputElement = passwordElements.find(el => el.serverUrl === serverUrl);
if (passwordInputElement) {
passwordInputElement.element.type = 'password';
}
}
}
function clearErrorResponse(serverUrl: string) {
setErrorResponse(serverUrl, undefined);
}
function setErrorResponse(serverUrl: string, message: string | undefined) {
if (message) {
errorResponses = [...errorResponses, { serverUrl: serverUrl, error: message }];
} else {
errorResponses = errorResponses.filter(o => o.serverUrl !== serverUrl);
}
}
function setNewSuggestedRegistryFormVisible(i: number, registry: containerDesktopAPI.RegistrySuggestedProvider) {
// Hide the new registry form if it's visible
setNewRegistryFormVisible(false);
// Hide all suggested registries (which also makes sure that we clear any saved credentials)
hideSuggestedRegistries();
// Set the value of the URL to the one we want to show
newRegistryRequest.serverUrl = registry.url;
// Unhide the one we want to show
listedSuggestedRegistries[i] = true;
}
// Separate function to hide everything and make sure that we clear any saved credentials
function hideSuggestedRegistries() {
// Hide everythihng
listedSuggestedRegistries.forEach((_, index) => {
listedSuggestedRegistries[index] = false;
});
// Clear all usernames and passwords
clearSavedCredentials();
}
function setNewRegistryFormVisible(visible: boolean) {
// Hide any "suggested" registries which may be open
hideSuggestedRegistries();
// Cleared saved credentials before we show
if (!visible) {
clearSavedCredentials();
}
// Show the new registry form
showNewRegistryForm = visible;
}
function clearSavedCredentials() {
clearErrorResponse(newRegistryRequest.serverUrl);
setPasswordForRegistryVisible(newRegistryRequest, false);
newRegistryRequest.serverUrl = '';
newRegistryRequest.username = '';
newRegistryRequest.secret = '';
}
async function loginToRegistry(registry: containerDesktopAPI.Registry) {
loggingIn = true;
clearErrorResponse(registry.serverUrl);
setPasswordForRegistryVisible(registry, false);
registry.source = defaultProviderSourceName;
const newRegistry = registry === newRegistryRequest;
// Always check credentials before creating image / updating to see if they pass.
// if we happen to get a certificate verification issue, as the user if they would like to
// continue with the registry anyway.
try {
await window.checkImageCredentials(registry);
} catch (error) {
if (error instanceof Error && error.message.includes('unable to verify the first certificate')) {
const result = await window.showMessageBox({
title: 'Invalid Certificate',
type: 'warning',
message: 'The certificate for this registry is not trusted / verifiable. Would you like to still add it?',
buttons: ['Yes', 'No'],
});
if (result && result.response === 0) {
registry.insecure = true;
} else {
setErrorResponse(registry.serverUrl, error.message);
loggingIn = false;
return;
}
}
}
try {
if (newRegistry) {
await window.createImageRegistry(registry.source, registry);
} else {
await window.updateImageRegistry(registry);
}
} catch (error) {
setErrorResponse(registry.serverUrl, error.message);
}
if (!errorResponses.some(o => o.serverUrl === registry.serverUrl)) {
if (newRegistry) {
setNewRegistryFormVisible(false);
} else {
originRegistries = originRegistries.filter(r => r.serverUrl !== registry.serverUrl);
}
}
loggingIn = false;
}
function removeExistingRegistry(registry: containerDesktopAPI.Registry) {
window.unregisterImageRegistry(registry);
setPasswordForRegistryVisible(registry, false);
}
const processPasswordElement = (node: HTMLInputElement, registry: containerDesktopAPI.Registry) => {
const serverUrl = registry === newRegistryRequest ? '' : registry.serverUrl;
passwordElements = [...passwordElements, { serverUrl: serverUrl, element: node }];
return {
destroy() {
passwordElements = passwordElements.filter(el => el.serverUrl !== serverUrl);
},
};
};
</script>
<SettingsPage title="Registries">
<div slot="actions">
<button
on:click="{() => setNewRegistryFormVisible(true)}"
class="pf-c-button pf-m-primary transition ease-in-out delay-50 hover:cursor-pointer h-7 w-36 text-sm rounded-md shadow hover:shadow-lg"
type="button"
disabled="{showNewRegistryForm}">
<span class="pf-c-button__icon pf-m-start">
<i class="fas fa-plus-circle" aria-hidden="true"></i>
</span>
Add registry
</button>
</div>
<div class="container bg-charcoal-600 rounded-md p-3">
<!-- Registries table start -->
<div class="w-full border-t border-b border-gray-900">
<div class="flex w-full">
<div class="flex-1 text-left py-4 pl-5 text-sm font-bold w-auto">Registry Location</div>
<div class="text-left py-4 text-sm font-bold w-1/4">Username</div>
<div class="text-left py-4 text-sm font-bold w-2/5">Password</div>
</div>
{#each $registriesInfos as registry}
<!-- containerDesktopAPI.Registry row start -->
<div class="flex flex-col w-full border-t border-gray-900">
<div class="flex flex-row items-center pt-4 pb-3">
<div class="flex-1 pl-5 pr-5 text-sm w-auto m-auto">
<div class="flex w-full h-full">
<div class="flex items-center">
<!-- Only show if a "suggested" registry icon has been added -->
{#if registry.icon}
<img
alt="{registry.name}"
src="{'data:image/png;base64,' + registry.icon}"
width="24"
height="24" />
{/if}
{#if registry.name}
<span class="ml-2">
{registry.name}
</span>
{:else}
<span class="ml-0">
{registry.serverUrl.replace('https://', '')}
</span>
{/if}
</div>
</div>
</div>
<!-- Username -->
<div class="text-sm w-1/4 m-auto">
{#if originRegistries.some(r => r.serverUrl === registry.serverUrl)}
<div class="text-left h-7 pr-5 mt-1.5 mb-0.5 text-sm w-full">
<input
type="text"
placeholder="Username"
bind:value="{registry.username}"
class="block px-3 block w-full h-full transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none" />
</div>
{:else if !registry.username && !registry.secret}
<button class="font-bold" on:click="{() => markRegistryAsModified(registry)}">Login now</button>
{:else}
{registry.username}
{/if}
</div>
<!-- Password -->
<div class="text-sm w-2/5">
<div class="flex flex-row">
{#if originRegistries.some(r => r.serverUrl === registry.serverUrl)}
<div class="flex text-left h-7 pr-5 text-sm w-full">
<div class="relative flex-1">
<div class="absolute inset-y-0 right-0 flex px-1">
<input
id="password-toggle-{registry.serverUrl}"
class="hidden"
type="checkbox"
value="false"
tabindex="-1"
on:change="{() =>
setPasswordForRegistryVisible(
registry,
!showPasswordForServerUrls.some(r => r === registry.serverUrl),
)}" />
<label
class="px-2 py-1 text text-gray-900 cursor-pointer"
for="password-toggle-{registry.serverUrl}">
{#if showPasswordForServerUrls.some(r => r === registry.serverUrl)}
<i class="fas fa-eye-slash"></i>
{:else}
<i class="fas fa-eye"></i>
{/if}
</label>
</div>
<input
use:processPasswordElement="{registry}"
type="password"
placeholder="Password"
bind:value="{registry.secret}"
class="px-3 block w-full h-full transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none pr-10" />
</div>
</div>
<div class="h-7 text-sm">
<button
on:click="{() => loginToRegistry(registry)}"
disabled="{loggingIn}"
class="pf-c-button pf-m-primary transition ease-in-out delay-50 hover:cursor-pointer w-full h-full rounded-md shadow hover:shadow-lg"
type="button">
Login
</button>
</div>
<div class="h-7 text-sm">
<button
on:click="{() => markRegistryAsClean(registry)}"
class="transition ease-in-out delay-50 hover:cursor-pointer w-16 h-full"
type="button">
Cancel
</button>
</div>
{:else}
<!-- Password field start -->
<div class="container mx-auto w-full self-center items-center truncate">
{#if !registry.username && !registry.secret}
<span class="no-user-select"> </span>
{:else if showPasswordForServerUrls.some(r => r === registry.serverUrl)}
{registry.secret}
{:else}
····················
{/if}
</div>
<!-- Password field end -->
<!-- Show/hide password start -->
<div class="self-center w-8">
{#if registry.username && registry.secret}
{#if showPasswordForServerUrls.some(r => r === registry.serverUrl)}
<button
type="button"
class="inline-flex w-full justify-center text-sm shadow-sm"
id="hide-password"
title="Hide password"
aria-expanded="true"
aria-haspopup="true"
on:click="{() => setPasswordForRegistryVisible(registry, false)}">
<i class="fa fa-eye-slash"></i>
</button>
{:else}
<button
type="button"
class="inline-flex w-full justify-center text-sm shadow-sm"
id="show-password"
title="Show password"
aria-expanded="true"
aria-haspopup="true"
on:click="{() => setPasswordForRegistryVisible(registry, true)}">
<i class="fa fa-eye"></i>
</button>
{/if}
{/if}
</div>
<!-- Show/hide password end -->
<!-- containerDesktopAPI.Registry menu start -->
<DropdownMenu>
<DropdownMenuItem
title="Login"
onClick="{() => markRegistryAsModified(registry)}"
hidden="{!!registry.username && !!registry.secret}"
icon="{faUser}" />
<DropdownMenuItem
title="Edit password"
onClick="{() => markRegistryAsModified(registry)}"
hidden="{!registry.username && !registry.secret}"
icon="{faUserPen}" />
<DropdownMenuItem
title="Remove"
onClick="{() => removeExistingRegistry(registry)}"
icon="{faTrash}" />
</DropdownMenu>
{/if}
</div>
</div>
</div>
<div class="flex flex-row-reverse w-full pb-3 -mt-2">
<span class="w-2/3 pl-4 text-sm font-bold">
{#if originRegistries.some(r => r.serverUrl === registry.serverUrl)}
{errorResponses.find(o => o.serverUrl === registry.serverUrl)?.error || ''}
{/if}
</span>
</div>
</div>
<!-- containerDesktopAPI.Registry row end -->
{/each}
{#each $registriesSuggestedInfos as registry, i (registry)}
<!-- Add new registry form start -->
<div class="flex flex-col w-full border-t border-gray-900">
<div class="flex flex-row items-center pt-4 pb-3">
<div class="flex-1 pl-5 pr-5 text-sm w-auto m-auto">
<div class="flex w-full h-full">
<div class="flex items-center">
{#if registry.icon}
<img
alt="{registry.name}"
src="{'data:image/png;base64,' + registry.icon}"
width="24"
height="24" />
{/if}
<!-- By default, just show the name, but if we go to add it, show the full URL including https -->
<span class="ml-2 text-gray-700">
{#if listedSuggestedRegistries[i]}
https://{registry.url}
{:else}
{registry.name}
{/if}
</span>
</div>
</div>
</div>
<div class="flex pr-5 text-sm w-1/4">
{#if listedSuggestedRegistries[i]}
<input
type="text"
placeholder="Username"
bind:value="{newRegistryRequest.username}"
class="px-3 block w-full h-7 pr-5 transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none" />
{/if}
</div>
<div class="text-sm w-2/5">
<div class="flex flex-row items-center">
<div class="relative flex-1 mr-5">
{#if listedSuggestedRegistries[i]}
<div class="absolute inset-y-0 right-0 flex">
<input
id="password-toggle-new-registry"
class="hidden"
type="checkbox"
value="false"
tabindex="-1"
on:change="{() =>
setPasswordForRegistryVisible(
newRegistryRequest,
!showPasswordForServerUrls.some(r => r === ''),
)}" />
<label class="px-2 py-1 text text-gray-900 cursor-pointer" for="password-toggle-new-registry">
{#if showPasswordForServerUrls.some(r => r === '')}
<i class="fas fa-eye-slash"></i>
{:else}
<i class="fas fa-eye"></i>
{/if}
</label>
</div>
<input
use:processPasswordElement="{newRegistryRequest}"
type="password"
placeholder="Password"
bind:value="{newRegistryRequest.secret}"
class="px-3 block w-full h-7 transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none pr-10" />
{/if}
</div>
<div class="flex text-sm">
{#if listedSuggestedRegistries[i]}
<button
on:click="{() => loginToRegistry(newRegistryRequest)}"
disabled="{!newRegistryRequest.serverUrl ||
!newRegistryRequest.username ||
!newRegistryRequest.secret ||
loggingIn}"
class="inline pf-c-button pf-m-primary transition ease-in-out delay-50 hover:cursor-pointer h-full rounded-md shadow hover:shadow-lg justify-center"
type="button">
Login
</button>
{/if}
</div>
<div class="flex text-sm">
<div class="h-7 pr-5 text-sm">
{#if listedSuggestedRegistries[i]}
<button
on:click="{() => hideSuggestedRegistries()}"
class="transition ease-in-out delay-50 hover:cursor-pointer h-full justify-center w-16"
type="button">
Cancel
</button>
{:else}
<button
on:click="{() => setNewSuggestedRegistryFormVisible(i, registry)}"
class="inline pf-c-button pf-m-primary transition ease-in-out delay-50 hover:cursor-pointer h-full rounded-md shadow hover:shadow-lg justify-center pb-1"
type="button">
Configure
</button>
{/if}
</div>
</div>
</div>
</div>
</div>
<div class="flex flex-row w-full pb-3 -mt-2 pl-10">
{#if listedSuggestedRegistries[i]}
<span class="text-sm font-bold whitespace-pre-line">
{errorResponses.find(o => o.serverUrl === newRegistryRequest.serverUrl)?.error || ''}
</span>
{/if}
</div>
</div>
<!-- Add new registry form end -->
{/each}
{#if showNewRegistryForm}
<!-- Add new registry form start -->
<div class="flex flex-col w-full border-t border-gray-900">
<div class="flex flex-row items-center pt-4 pb-3">
<div class="flex-1 pl-10 pr-5 text-sm w-auto m-auto">
<input
type="text"
placeholder="URL (HTTPS only)"
bind:value="{newRegistryRequest.serverUrl}"
class="px-3 block w-full h-7 pr-5 transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none" />
</div>
<div class="flex pr-5 text-sm w-1/4">
<input
type="text"
placeholder="Username"
bind:value="{newRegistryRequest.username}"
class="px-3 block w-full h-7 pr-5 transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none" />
</div>
<div class="text-sm w-2/5">
<div class="flex flex-row">
<div class="relative flex-1 mr-5">
<div class="absolute inset-y-0 right-0 flex">
<input
id="password-toggle-new-registry"
class="hidden"
type="checkbox"
value="false"
tabindex="-1"
on:change="{() =>
setPasswordForRegistryVisible(
newRegistryRequest,
!showPasswordForServerUrls.some(r => r === ''),
)}" />
<label class="px-2 py-1 text text-gray-900 cursor-pointer" for="password-toggle-new-registry">
{#if showPasswordForServerUrls.some(r => r === '')}
<i class="fas fa-eye-slash"></i>
{:else}
<i class="fas fa-eye"></i>
{/if}
</label>
</div>
<input
use:processPasswordElement="{newRegistryRequest}"
type="password"
placeholder="Password"
bind:value="{newRegistryRequest.secret}"
class="px-3 block w-full h-7 transition ease-in-out delay-50 bg-charcoal-800 text-gray-700 placeholder-gray-700 rounded-sm focus:outline-none pr-10" />
</div>
<div class="flex text-sm">
<button
on:click="{() => loginToRegistry(newRegistryRequest)}"
disabled="{!newRegistryRequest.serverUrl ||
!newRegistryRequest.username ||
!newRegistryRequest.secret ||
loggingIn}"
class="inline pf-c-button pf-m-primary transition ease-in-out delay-50 hover:cursor-pointer h-full rounded-md shadow hover:shadow-lg justify-center"
type="button">
Login
</button>
</div>
<div class="flex text-sm">
<button
on:click="{() => setNewRegistryFormVisible(false)}"
class="transition ease-in-out delay-50 hover:cursor-pointer h-full justify-center w-16"
type="button">
Cancel
</button>
</div>
</div>
</div>
</div>
<div class="flex flex-row w-full pb-3 -mt-2 pl-10">
<span class="text-sm font-bold whitespace-pre-line">
{errorResponses.find(o => o.serverUrl === newRegistryRequest.serverUrl)?.error || ''}
</span>
</div>
</div>
<!-- Add new registry form end -->
{/if}
</div>
<!-- Registries table end -->
</div>
</SettingsPage>