diff --git a/apps/api/src/routes/api/v1/settings/handlers.ts b/apps/api/src/routes/api/v1/settings/handlers.ts index e12e05a0e..96389000b 100644 --- a/apps/api/src/routes/api/v1/settings/handlers.ts +++ b/apps/api/src/routes/api/v1/settings/handlers.ts @@ -3,7 +3,7 @@ import { X509Certificate } from 'node:crypto'; import type { FastifyReply, FastifyRequest } from 'fastify'; import { asyncExecShell, checkDomainsIsValidInDNS, decrypt, encrypt, errorHandler, isDev, isDNSValid, isDomainConfigured, listSettings, prisma } from '../../../../lib/common'; -import { CheckDNS, CheckDomain, DeleteDomain, OnlyIdInBody, SaveSettings, SaveSSHKey } from './types'; +import { CheckDNS, CheckDomain, DeleteDomain, OnlyIdInBody, SaveSettings, SaveSSHKey, SetDefaultRegistry } from './types'; export async function listAllSettings(request: FastifyRequest) { @@ -11,8 +11,20 @@ export async function listAllSettings(request: FastifyRequest) { const teamId = request.user.teamId; const settings = await listSettings(); const sshKeys = await prisma.sshKey.findMany({ where: { team: { id: teamId } } }) - const publicRegistries = await prisma.dockerRegistry.findMany({ where: { isSystemWide: true } }) - const registries = await prisma.dockerRegistry.findMany({ where: { team: { id: teamId } } }) + let publicRegistries = await prisma.dockerRegistry.findMany({ where: { isSystemWide: true } }) + let privateRegistries = await prisma.dockerRegistry.findMany({ where: { team: { id: teamId } } }) + publicRegistries = publicRegistries.map((registry) => { + if (registry.password) { + registry.password = decrypt(registry.password) + } + return registry + }) + privateRegistries = privateRegistries.map((registry) => { + if (registry.password) { + registry.password = decrypt(registry.password) + } + return registry + }) const unencryptedKeys = [] if (sshKeys.length > 0) { for (const key of sshKeys) { @@ -32,7 +44,7 @@ export async function listAllSettings(request: FastifyRequest) { sshKeys: unencryptedKeys, registries: { public: publicRegistries, - private: registries + private: privateRegistries } } } catch ({ status, message }) { @@ -154,4 +166,23 @@ export async function deleteCertificates(request: FastifyRequest, } catch ({ status, message }) { return errorHandler({ status, message }) } +} + +export async function setDockerRegistry(request: FastifyRequest, reply: FastifyReply) { + try { + const teamId = request.user.teamId; + const { id, username, password } = request.body; + + let encryptedPassword = '' + if (password) encryptedPassword = encrypt(password) + + if (teamId === '0') { + await prisma.dockerRegistry.update({ where: { id }, data: { username, password: encryptedPassword } }) + } else { + await prisma.dockerRegistry.updateMany({ where: { id, teamId }, data: { username, password: encryptedPassword } }) + } + return reply.code(201).send() + } catch ({ status, message }) { + return errorHandler({ status, message }) + } } \ No newline at end of file diff --git a/apps/api/src/routes/api/v1/settings/index.ts b/apps/api/src/routes/api/v1/settings/index.ts index 45e418b34..c93fa9427 100644 --- a/apps/api/src/routes/api/v1/settings/index.ts +++ b/apps/api/src/routes/api/v1/settings/index.ts @@ -2,8 +2,8 @@ import { FastifyPluginAsync } from 'fastify'; import { X509Certificate } from 'node:crypto'; import { encrypt, errorHandler, prisma } from '../../../../lib/common'; -import { checkDNS, checkDomain, deleteCertificates, deleteDomain, deleteSSHKey, listAllSettings, saveSettings, saveSSHKey } from './handlers'; -import { CheckDNS, CheckDomain, DeleteDomain, OnlyIdInBody, SaveSettings, SaveSSHKey } from './types'; +import { checkDNS, checkDomain, deleteCertificates, deleteDomain, deleteSSHKey, listAllSettings, saveSettings, saveSSHKey, setDockerRegistry } from './handlers'; +import { CheckDNS, CheckDomain, DeleteDomain, OnlyIdInBody, SaveSettings, SaveSSHKey, SetDefaultRegistry } from './types'; const root: FastifyPluginAsync = async (fastify): Promise => { @@ -20,6 +20,9 @@ const root: FastifyPluginAsync = async (fastify): Promise => { fastify.post('/sshKey', async (request, reply) => await saveSSHKey(request, reply)); fastify.delete('/sshKey', async (request, reply) => await deleteSSHKey(request, reply)); + fastify.post('/registry', async (request, reply) => await setDockerRegistry(request, reply)); + // fastify.delete<>('/registry', async (request, reply) => await deleteSSHKey(request, reply)); + fastify.post('/upload', async (request) => { try { const teamId = request.user.teamId; @@ -53,7 +56,6 @@ const root: FastifyPluginAsync = async (fastify): Promise => { }); fastify.delete('/certificate', async (request, reply) => await deleteCertificates(request, reply)) - // fastify.get('/certificates', async (request) => await getCertificates(request)) }; export default root; diff --git a/apps/api/src/routes/api/v1/settings/types.ts b/apps/api/src/routes/api/v1/settings/types.ts index d68a7bdd3..850128d51 100644 --- a/apps/api/src/routes/api/v1/settings/types.ts +++ b/apps/api/src/routes/api/v1/settings/types.ts @@ -47,4 +47,12 @@ export interface OnlyIdInBody { Body: { id: string } +} + +export interface SetDefaultRegistry { + Body: { + id: string + username: string + password: string + } } \ No newline at end of file diff --git a/apps/ui/src/lib/components/CopyPasswordField.svelte b/apps/ui/src/lib/components/CopyPasswordField.svelte index f45adcd3d..b25817ac1 100644 --- a/apps/ui/src/lib/components/CopyPasswordField.svelte +++ b/apps/ui/src/lib/components/CopyPasswordField.svelte @@ -15,7 +15,7 @@ export let placeholder = ''; export let inputStyle = ''; - let disabledClass = 'bg-coolback disabled:bg-coolblack w-full'; + let disabledClass = 'input input-primary bg-coolback disabled:bg-coolblack w-full'; let isHttps = browser && window.location.protocol === 'https:'; function copyToClipboard() { diff --git a/apps/ui/src/routes/settings/docker.svelte b/apps/ui/src/routes/settings/docker.svelte index 00548f0a0..7bef5fc15 100644 --- a/apps/ui/src/routes/settings/docker.svelte +++ b/apps/ui/src/routes/settings/docker.svelte @@ -20,16 +20,17 @@ export let registries: any; import { del, post } from '$lib/api'; import { errorNotification } from '$lib/common'; + import CopyPasswordField from '$lib/components/CopyPasswordField.svelte'; const publicRegistries = registries.public; const privateRegistries = registries.private; let isModalActive = false; let newRegistry = { - name: null, - username: null, - password: null, - url: null, + name: '', + username: '', + password: '', + url: '', isSystemWide: false }; @@ -43,6 +44,11 @@ return false; } } + async function setRegistry(registry: any) { + try { + await post(`/settings/registry`, registry); + } catch (error) {} + } async function deleteSSHKey(id: string) { const sure = confirm('Are you sure you would like to delete this SSH key?'); if (sure) { @@ -80,6 +86,39 @@ {#each publicRegistries as registry} + + {registry.name} + {(registry.isSystemWide && 'Yes') || 'No'} + + + + + + {#if !registry.isSystemWide} + + {/if} + + + {/each} + {#each privateRegistries as registry} {registry.name} {(registry.isSystemWide && 'Yes') || 'No'} @@ -95,22 +134,6 @@ {/each} - {#each privateRegistries as registry} - - {registry.name} - {(registry.isSystemWide && 'Yes') || 'No'} - {registry.username ?? 'N/A'} - {registry.password ?? 'N/A'} - - - {#if !registry.isSystemWide} - - {/if} - - - {/each} @@ -121,49 +144,46 @@