diff --git a/extensions/podman/src/registry-setup.spec.ts b/extensions/podman/src/registry-setup.spec.ts index 06c3a65a90f..9fb931bf0e7 100644 --- a/extensions/podman/src/registry-setup.spec.ts +++ b/extensions/podman/src/registry-setup.spec.ts @@ -56,17 +56,17 @@ afterEach(() => { console.error = originalConsoleError; }); +type ReadFileType = ( + path: string, + options: string, + callback: (err: NodeJS.ErrnoException | undefined, data: string | Buffer) => void, +) => void; + test('should work with invalid JSON auth file', async () => { // mock the existSync const existSyncSpy = vi.spyOn(fs, 'existsSync'); existSyncSpy.mockReturnValue(true); - type ReadFileType = ( - path: string, - options: string, - callback: (err: NodeJS.ErrnoException | undefined, data: string | Buffer) => void, - ) => void; - // mock the readFile const readFileSpy = vi.spyOn(fs, 'readFile') as unknown as MockedFunction; @@ -94,3 +94,71 @@ test('should work with invalid JSON auth file', async () => { // expect error was logged expect(consoleErroMock).toHaveBeenCalledWith('Error parsing auth file', expect.anything()); }); + +test('should work with JSON auth file', async () => { + // mock the existSync + const existSyncSpy = vi.spyOn(fs, 'existsSync'); + existSyncSpy.mockReturnValue(true); + + // mock the readFile + const readFileSpy = vi.spyOn(fs, 'readFile') as unknown as MockedFunction; + const auth = Buffer.from('user:password').toString('base64'); + + readFileSpy.mockImplementation( + (_path: string, _encoding: string, callback: (err: Error | null, data: string) => void) => { + // mock the error + + callback(undefined, JSON.stringify({ auths: { 'myregistry.io': { auth: auth } } })); + }, + ); + + // mock the location + const authJsonLocation = '/tmp/containers/auth.json'; + const mockGetAuthFileLocation = vi.spyOn(registrySetup, 'getAuthFileLocation'); + mockGetAuthFileLocation.mockReturnValue(authJsonLocation); + + // expect an error + const authFile = await registrySetup.publicReadAuthFile(); + + // expect the file to have a single entry + expect(authFile.auths['myregistry.io']).toBeDefined(); + expect(authFile.auths['myregistry.io'].auth).toBe(auth); + expect(authFile.auths['myregistry.io']['podmanDesktopAlias']).not.toBeDefined(); + + // expect read with the correct file + expect(readFileSpy).toHaveBeenCalledWith(authJsonLocation, 'utf-8', expect.anything()); +}); + +test('should work with JSON auth file and alias', async () => { + // mock the existSync + const existSyncSpy = vi.spyOn(fs, 'existsSync'); + existSyncSpy.mockReturnValue(true); + + // mock the readFile + const readFileSpy = vi.spyOn(fs, 'readFile') as unknown as MockedFunction; + const auth = Buffer.from('user:password').toString('base64'); + + readFileSpy.mockImplementation( + (_path: string, _encoding: string, callback: (err: Error | null, data: string) => void) => { + // mock the error + + callback(undefined, JSON.stringify({ auths: { 'myregistry.io': { auth: auth, podmanDesktopAlias: 'alias' } } })); + }, + ); + + // mock the location + const authJsonLocation = '/tmp/containers/auth.json'; + const mockGetAuthFileLocation = vi.spyOn(registrySetup, 'getAuthFileLocation'); + mockGetAuthFileLocation.mockReturnValue(authJsonLocation); + + // expect an error + const authFile = await registrySetup.publicReadAuthFile(); + + // expect the file to have a single entry + expect(authFile.auths['myregistry.io']).toBeDefined(); + expect(authFile.auths['myregistry.io'].auth).toBe(auth); + expect(authFile.auths['myregistry.io']['podmanDesktopAlias']).toBe('alias'); + + // expect read with the correct file + expect(readFileSpy).toHaveBeenCalledWith(authJsonLocation, 'utf-8', expect.anything()); +}); diff --git a/extensions/podman/src/registry-setup.ts b/extensions/podman/src/registry-setup.ts index 038612a2b0d..da39cfd9f61 100644 --- a/extensions/podman/src/registry-setup.ts +++ b/extensions/podman/src/registry-setup.ts @@ -27,6 +27,7 @@ import { isLinux, isMac, isWindows } from './util'; export type ContainerAuthConfigEntry = { [key: string]: { auth: string; + podmanDesktopAlias: string | undefined; }; }; @@ -70,6 +71,7 @@ export class RegistrySetup { serverUrl, username, secret, + alias: value['podmanDesktopAlias'], }; inFileRegistries.push(registry); } @@ -117,6 +119,7 @@ export class RegistrySetup { } authFile.auths[registry.serverUrl] = { auth: Buffer.from(`${registry.username}:${registry.secret}`).toString('base64'), + podmanDesktopAlias: registry.alias, }; await this.writeAuthFile(JSON.stringify(authFile, undefined, 8)); @@ -149,6 +152,7 @@ export class RegistrySetup { } authFile.auths[registry.serverUrl] = { auth: Buffer.from(`${registry.username}:${registry.secret}`).toString('base64'), + podmanDesktopAlias: registry.alias, }; await this.writeAuthFile(JSON.stringify(authFile, undefined, 8)); diff --git a/packages/extension-api/src/extension-api.d.ts b/packages/extension-api/src/extension-api.d.ts index e436b49697f..ab2fe1bc650 100644 --- a/packages/extension-api/src/extension-api.d.ts +++ b/packages/extension-api/src/extension-api.d.ts @@ -623,6 +623,7 @@ declare module '@podman-desktop/api' { username: string; secret: string; insecure?: boolean; + alias?: string; } export interface RegistryProvider { diff --git a/packages/renderer/src/lib/preferences/PreferencesRegistriesEditing.svelte b/packages/renderer/src/lib/preferences/PreferencesRegistriesEditing.svelte index dd50e6573bf..d278c7f34e0 100644 --- a/packages/renderer/src/lib/preferences/PreferencesRegistriesEditing.svelte +++ b/packages/renderer/src/lib/preferences/PreferencesRegistriesEditing.svelte @@ -65,6 +65,7 @@ function markRegistryAsModified(registry: containerDesktopAPI.Registry) { serverUrl: registry.serverUrl, username: registry.username, secret: registry.secret, + alias: registry.alias, } as containerDesktopAPI.Registry; originRegistries = [...originRegistries, originRegistry]; @@ -282,7 +283,7 @@ function removeExistingRegistry(registry: containerDesktopAPI.Registry) { {:else if !registry.username && !registry.secret} {:else} - {registry.username} + {registry.alias ?? registry.username} {/if}