mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 05:56:16 +00:00
#591 - server-ssh-keys
This commit is contained in:
@ -6,20 +6,14 @@
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Models\Server;
|
||||
use App\Models\SshKey;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DeployKeyToServer
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
*
|
||||
* @throws SSHError
|
||||
*/
|
||||
public function deploy(Server $server, array $input): void
|
||||
public function deploy(Server $server, SshKey $sshKey): void
|
||||
{
|
||||
/** @var SshKey $sshKey */
|
||||
$sshKey = SshKey::query()->findOrFail($input['key_id']);
|
||||
$server->sshKeys()->attach($sshKey, [
|
||||
'status' => SshKeyStatus::ADDING,
|
||||
]);
|
||||
@ -28,18 +22,4 @@ public function deploy(Server $server, array $input): void
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string>>
|
||||
*/
|
||||
public static function rules(User $user, Server $server): array
|
||||
{
|
||||
return [
|
||||
'key_id' => [
|
||||
'required',
|
||||
Rule::exists('ssh_keys', 'id')->where('user_id', $user->id),
|
||||
Rule::unique('server_ssh_keys', 'ssh_key_id')->where('server_id', $server->id),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
use App\Actions\SshKey\CreateSshKey;
|
||||
use App\Actions\SshKey\DeleteKeyFromServer;
|
||||
use App\Actions\SshKey\DeployKeyToServer;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\SshKeyResource;
|
||||
use App\Models\Project;
|
||||
@ -41,6 +42,9 @@ public function index(Project $project, Server $server): ResourceCollection
|
||||
return SshKeyResource::collection($server->sshKeys()->simplePaginate(25));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/', name: 'api.projects.servers.ssh-keys.create', middleware: 'ability:write')]
|
||||
#[Endpoint(title: 'create', description: 'Deploy ssh key to server.')]
|
||||
#[BodyParam(name: 'key_id', description: 'The ID of the key.')]
|
||||
@ -58,9 +62,7 @@ public function create(Request $request, Project $project, Server $server): SshK
|
||||
|
||||
$sshKey = null;
|
||||
if ($request->has('key_id')) {
|
||||
$this->validate($request, DeployKeyToServer::rules($user, $server));
|
||||
|
||||
/** @var ?SshKey $sshKey */
|
||||
/** @var SshKey $sshKey */
|
||||
$sshKey = $user->sshKeys()->findOrFail($request->key_id);
|
||||
}
|
||||
|
||||
@ -69,7 +71,7 @@ public function create(Request $request, Project $project, Server $server): SshK
|
||||
$sshKey = app(CreateSshKey::class)->create($user, $request->all());
|
||||
}
|
||||
|
||||
app(DeployKeyToServer::class)->deploy($server, ['key_id' => $sshKey->id]);
|
||||
app(DeployKeyToServer::class)->deploy($server, $sshKey);
|
||||
|
||||
return new SshKeyResource($sshKey);
|
||||
}
|
||||
|
63
app/Http/Controllers/ServerSshKeyController.php
Normal file
63
app/Http/Controllers/ServerSshKeyController.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\SshKey\DeleteKeyFromServer;
|
||||
use App\Actions\SshKey\DeployKeyToServer;
|
||||
use App\Exceptions\SSHError;
|
||||
use App\Http\Resources\SshKeyResource;
|
||||
use App\Models\Server;
|
||||
use App\Models\SshKey;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Spatie\RouteAttributes\Attributes\Delete;
|
||||
use Spatie\RouteAttributes\Attributes\Get;
|
||||
use Spatie\RouteAttributes\Attributes\Middleware;
|
||||
use Spatie\RouteAttributes\Attributes\Post;
|
||||
use Spatie\RouteAttributes\Attributes\Prefix;
|
||||
|
||||
#[Prefix('servers/{server}/ssh-keys')]
|
||||
#[Middleware(['auth', 'has-project'])]
|
||||
class ServerSshKeyController extends Controller
|
||||
{
|
||||
#[Get('/', name: 'server-ssh-keys')]
|
||||
public function index(Server $server): Response
|
||||
{
|
||||
$this->authorize('viewAnyServer', [SshKey::class, $server]);
|
||||
|
||||
return Inertia::render('server-ssh-keys/index', [
|
||||
'sshKeys' => SshKeyResource::collection($server->sshKeys()->with('user')->simplePaginate(config('web.pagination_size'))),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Post('/', name: 'server-ssh-keys.store')]
|
||||
public function store(Request $request, Server $server): RedirectResponse
|
||||
{
|
||||
$this->authorize('createServer', [SshKey::class, $server]);
|
||||
|
||||
/** @var SshKey $sshKey */
|
||||
$sshKey = user()->sshKeys()->findOrFail($request->input('key'));
|
||||
|
||||
app(DeployKeyToServer::class)->deploy($server, $sshKey);
|
||||
|
||||
return back()->with('success', 'SSH key deployed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHError
|
||||
*/
|
||||
#[Delete('/{sshKey}', name: 'server-ssh-keys.destroy')]
|
||||
public function destroy(Server $server, SshKey $sshKey): RedirectResponse
|
||||
{
|
||||
$this->authorize('deleteServer', [SshKey::class, $server]);
|
||||
|
||||
app(DeleteKeyFromServer::class)->delete($server, $sshKey);
|
||||
|
||||
return back()->with('success', 'SSH key deleted.');
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
use App\Models\SshKey;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Spatie\RouteAttributes\Attributes\Delete;
|
||||
@ -17,7 +18,7 @@
|
||||
|
||||
#[Prefix('settings/ssh-keys')]
|
||||
#[Middleware(['auth'])]
|
||||
class SSHKeyController extends Controller
|
||||
class SshKeyController extends Controller
|
||||
{
|
||||
#[Get('/', name: 'ssh-keys')]
|
||||
public function index(): Response
|
||||
@ -29,6 +30,14 @@ public function index(): Response
|
||||
]);
|
||||
}
|
||||
|
||||
#[Get('/json', name: 'ssh-keys.json')]
|
||||
public function json(): ResourceCollection
|
||||
{
|
||||
$this->authorize('viewAny', SshKey::class);
|
||||
|
||||
return SshKeyResource::collection(user()->sshKeys()->get());
|
||||
}
|
||||
|
||||
#[Post('/', name: 'ssh-keys.store')]
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
@ -16,7 +16,7 @@ public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'user' => $this->user_id ? new UserResource($this->user) : null,
|
||||
'user' => new UserResource($this->whenLoaded('user')),
|
||||
'name' => $this->name,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\SshKeyFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
@ -16,7 +17,7 @@
|
||||
*/
|
||||
class SshKey extends AbstractModel
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\SshKeyFactory> */
|
||||
/** @use HasFactory<SshKeyFactory> */
|
||||
use HasFactory;
|
||||
|
||||
use SoftDeletes;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ReactNode } from 'react';
|
||||
|
||||
export default function HeaderContainer({ children }: { children: ReactNode }) {
|
||||
return <div className="flex items-center justify-between">{children}</div>;
|
||||
return <div className="flex items-start justify-between gap-2">{children}</div>;
|
||||
}
|
||||
|
@ -20,9 +20,12 @@ export default function LogOutput({ children }: { children: ReactNode }) {
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<ScrollArea ref={scrollRef} className="bg-accent/50 text-accent-foreground relative h-[500px] w-full p-4 font-mono text-sm whitespace-pre-line">
|
||||
{children}
|
||||
<div className="relative w-full">
|
||||
<ScrollArea
|
||||
ref={scrollRef}
|
||||
className="bg-accent/50 text-accent-foreground relative h-[500px] w-full overflow-auto p-4 font-mono text-sm break-all whitespace-pre-wrap"
|
||||
>
|
||||
<div>{children}</div>
|
||||
<div ref={endRef} />
|
||||
<ScrollBar orientation="vertical" />
|
||||
</ScrollArea>
|
||||
@ -37,7 +40,7 @@ export default function LogOutput({ children }: { children: ReactNode }) {
|
||||
<TooltipTrigger asChild>
|
||||
<div>{autoScroll ? <ClockArrowDownIcon className="h-4 w-4" /> : <ArrowDown className="h-4 w-4" />}</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>{autoScroll ? 'Turn off auto scroll' : 'Auto scroll down'}</TooltipContent>
|
||||
<TooltipContent side="left">{autoScroll ? 'Turn off auto scroll' : 'Auto scroll down'}</TooltipContent>
|
||||
</Tooltip>
|
||||
</Button>
|
||||
</div>
|
||||
|
@ -40,7 +40,7 @@ function DialogContent({ className, children, ...props }: React.ComponentProps<t
|
||||
<DialogPrimitive.Content
|
||||
data-slot="dialog-content"
|
||||
className={cn(
|
||||
'bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] rounded-lg border shadow-lg duration-200 sm:max-w-lg',
|
||||
'bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] overflow-x-hidden rounded-lg border shadow-lg duration-200 sm:max-w-lg',
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
|
@ -6,6 +6,7 @@ import {
|
||||
DatabaseIcon,
|
||||
FlameIcon,
|
||||
HomeIcon,
|
||||
KeyIcon,
|
||||
ListEndIcon,
|
||||
MousePointerClickIcon,
|
||||
RocketIcon,
|
||||
@ -103,11 +104,12 @@ export default function ServerLayout({ children }: { children: ReactNode }) {
|
||||
icon: ListEndIcon,
|
||||
isDisabled: isMenuDisabled,
|
||||
},
|
||||
// {
|
||||
// title: 'SSH Keys',
|
||||
// href: '#',
|
||||
// icon: KeyIcon,
|
||||
// },
|
||||
{
|
||||
title: 'SSH Keys',
|
||||
href: route('server-ssh-keys', { server: page.props.server.id }),
|
||||
icon: KeyIcon,
|
||||
isDisabled: isMenuDisabled,
|
||||
},
|
||||
// {
|
||||
// title: 'Services',
|
||||
// href: '#',
|
||||
|
@ -5,7 +5,7 @@ import ServerLayout from '@/layouts/server/layout';
|
||||
import HeaderContainer from '@/components/header-container';
|
||||
import Heading from '@/components/heading';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { PlusIcon } from 'lucide-react';
|
||||
import { BookOpenIcon, PlusIcon } from 'lucide-react';
|
||||
import Container from '@/components/container';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { CronJob } from '@/types/cronjob';
|
||||
@ -26,6 +26,12 @@ export default function CronJobIndex() {
|
||||
<HeaderContainer>
|
||||
<Heading title="Cron jobs" description="Here you can manage server's cron jobs" />
|
||||
<div className="flex items-center gap-2">
|
||||
<a href="https://vitodeploy.com/docs/servers/cronjobs" target="_blank">
|
||||
<Button variant="outline">
|
||||
<BookOpenIcon />
|
||||
<span className="hidden lg:block">Docs</span>
|
||||
</Button>
|
||||
</a>
|
||||
<CronJobForm serverId={page.props.server.id}>
|
||||
<Button>
|
||||
<PlusIcon />
|
||||
|
@ -6,7 +6,7 @@ import ServerLayout from '@/layouts/server/layout';
|
||||
import HeaderContainer from '@/components/header-container';
|
||||
import Heading from '@/components/heading';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { PlusIcon } from 'lucide-react';
|
||||
import { BookOpenIcon, PlusIcon } from 'lucide-react';
|
||||
import Container from '@/components/container';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { columns } from '@/pages/firewall/components/columns';
|
||||
@ -26,6 +26,12 @@ export default function Firewall() {
|
||||
<HeaderContainer>
|
||||
<Heading title="Firewall" description="Here you can manage server's firewall rules" />
|
||||
<div className="flex items-center gap-2">
|
||||
<a href="https://vitodeploy.com/docs/servers/firewall" target="_blank">
|
||||
<Button variant="outline">
|
||||
<BookOpenIcon />
|
||||
<span className="hidden lg:block">Docs</span>
|
||||
</Button>
|
||||
</a>
|
||||
<RuleForm serverId={page.props.server.id}>
|
||||
<Button>
|
||||
<PlusIcon />
|
||||
|
125
resources/js/pages/server-ssh-keys/components/columns.tsx
Normal file
125
resources/js/pages/server-ssh-keys/components/columns.tsx
Normal file
@ -0,0 +1,125 @@
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
import DateTime from '@/components/date-time';
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useForm, usePage } from '@inertiajs/react';
|
||||
import { LoaderCircleIcon, MoreVerticalIcon } from 'lucide-react';
|
||||
import FormSuccessful from '@/components/form-successful';
|
||||
import { useState } from 'react';
|
||||
import { SshKey } from '@/types/ssh-key';
|
||||
import { Server } from '@/types/server';
|
||||
|
||||
function Delete({ sshKey }: { sshKey: SshKey }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const form = useForm();
|
||||
const page = usePage<{
|
||||
server: Server;
|
||||
}>();
|
||||
|
||||
const submit = () => {
|
||||
form.delete(
|
||||
route('server-ssh-keys.destroy', {
|
||||
server: page.props.server.id,
|
||||
sshKey: sshKey.id,
|
||||
}),
|
||||
{
|
||||
onSuccess: () => {
|
||||
setOpen(false);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<DropdownMenuItem variant="destructive" onSelect={(e) => e.preventDefault()}>
|
||||
Delete
|
||||
</DropdownMenuItem>
|
||||
</DialogTrigger>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
Delete {sshKey.name} from {page.props.server.name}
|
||||
</DialogTitle>
|
||||
<DialogDescription className="sr-only">Delete ssh key</DialogDescription>
|
||||
</DialogHeader>
|
||||
<p className="p-4">
|
||||
Are you sure you want to delete this key from <b>{page.props.server.name}</b>?
|
||||
</p>
|
||||
<DialogFooter>
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline">Cancel</Button>
|
||||
</DialogClose>
|
||||
<Button variant="destructive" disabled={form.processing} onClick={submit}>
|
||||
{form.processing && <LoaderCircleIcon className="animate-spin" />}
|
||||
<FormSuccessful successful={form.recentlySuccessful} />
|
||||
Delete
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export const columns: ColumnDef<SshKey>[] = [
|
||||
{
|
||||
accessorKey: 'name',
|
||||
header: 'Name',
|
||||
enableColumnFilter: true,
|
||||
enableSorting: true,
|
||||
},
|
||||
{
|
||||
accessorKey: 'user',
|
||||
header: 'User',
|
||||
enableColumnFilter: true,
|
||||
enableSorting: true,
|
||||
cell: ({ row }) => {
|
||||
return (
|
||||
<span>
|
||||
{row.original.user?.name} ({row.original.user?.email})
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: 'created_at',
|
||||
header: 'Created at',
|
||||
enableColumnFilter: true,
|
||||
enableSorting: true,
|
||||
cell: ({ row }) => {
|
||||
return <DateTime date={row.original.created_at} />;
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
enableColumnFilter: false,
|
||||
enableSorting: false,
|
||||
cell: ({ row }) => {
|
||||
return (
|
||||
<div className="flex items-center justify-end">
|
||||
<DropdownMenu modal={false}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" className="h-8 w-8 p-0">
|
||||
<span className="sr-only">Open menu</span>
|
||||
<MoreVerticalIcon />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<Delete sshKey={row.original} />
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
75
resources/js/pages/server-ssh-keys/components/deploy-key.tsx
Normal file
75
resources/js/pages/server-ssh-keys/components/deploy-key.tsx
Normal file
@ -0,0 +1,75 @@
|
||||
import { LoaderCircle } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog';
|
||||
import { useForm, usePage } from '@inertiajs/react';
|
||||
import { FormEventHandler, ReactNode, useState } from 'react';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import InputError from '@/components/ui/input-error';
|
||||
import { Form, FormField, FormFields } from '@/components/ui/form';
|
||||
import { Server } from '@/types/server';
|
||||
import SshKeySelect from '@/pages/ssh-keys/components/ssh-key-select';
|
||||
|
||||
export default function DeployKey({ children }: { children: ReactNode }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const page = usePage<{
|
||||
server: Server;
|
||||
}>();
|
||||
|
||||
const form = useForm<
|
||||
Required<{
|
||||
key: string;
|
||||
}>
|
||||
>({
|
||||
key: '',
|
||||
});
|
||||
|
||||
const submit: FormEventHandler = (e) => {
|
||||
e.preventDefault();
|
||||
form.post(route('server-ssh-keys.store', { server: page.props.server.id }), {
|
||||
onSuccess: () => {
|
||||
setOpen(false);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>{children}</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Deploy ssh key</DialogTitle>
|
||||
<DialogDescription className="sr-only">Deploy ssh key</DialogDescription>
|
||||
</DialogHeader>
|
||||
<Form id="deploy-ssh-key-form" onSubmit={submit} className="p-4">
|
||||
<FormFields>
|
||||
<FormField>
|
||||
<Label htmlFor="name">Key</Label>
|
||||
<SshKeySelect value={form.data.key} onValueChange={(value) => form.setData('key', value)} />
|
||||
<InputError message={form.errors.key} />
|
||||
</FormField>
|
||||
</FormFields>
|
||||
</Form>
|
||||
<DialogFooter>
|
||||
<DialogClose asChild>
|
||||
<Button type="button" variant="outline">
|
||||
Cancel
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button form="deploy-ssh-key-form" onClick={submit} disabled={form.processing}>
|
||||
{form.processing && <LoaderCircle className="animate-spin" />}
|
||||
Deploy
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
47
resources/js/pages/server-ssh-keys/index.tsx
Normal file
47
resources/js/pages/server-ssh-keys/index.tsx
Normal file
@ -0,0 +1,47 @@
|
||||
import { Head, usePage } from '@inertiajs/react';
|
||||
import Container from '@/components/container';
|
||||
import Heading from '@/components/heading';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { SshKey } from '@/types/ssh-key';
|
||||
import { columns } from '@/pages/server-ssh-keys/components/columns';
|
||||
import { PaginatedData } from '@/types';
|
||||
import DeployKey from '@/pages/server-ssh-keys/components/deploy-key';
|
||||
import ServerLayout from '@/layouts/server/layout';
|
||||
import HeaderContainer from '@/components/header-container';
|
||||
import { BookOpenIcon, RocketIcon } from 'lucide-react';
|
||||
|
||||
type Page = {
|
||||
sshKeys: PaginatedData<SshKey>;
|
||||
};
|
||||
|
||||
export default function SshKeys() {
|
||||
const page = usePage<Page>();
|
||||
|
||||
return (
|
||||
<ServerLayout>
|
||||
<Head title="SSH Keys" />
|
||||
<Container className="max-w-5xl">
|
||||
<HeaderContainer>
|
||||
<Heading title="SSH Keys" description="Here you can manage the ssh keys deployed to the server" />
|
||||
<div className="flex items-center gap-2">
|
||||
<a href="https://vitodeploy.com/docs/servers/ssh-keys" target="_blank">
|
||||
<Button variant="outline">
|
||||
<BookOpenIcon />
|
||||
<span className="hidden lg:block">Docs</span>
|
||||
</Button>
|
||||
</a>
|
||||
<DeployKey>
|
||||
<Button>
|
||||
<RocketIcon />
|
||||
Deploy key
|
||||
</Button>
|
||||
</DeployKey>
|
||||
</div>
|
||||
</HeaderContainer>
|
||||
|
||||
<DataTable columns={columns} paginatedData={page.props.sshKeys} />
|
||||
</Container>
|
||||
</ServerLayout>
|
||||
);
|
||||
}
|
@ -23,7 +23,7 @@ type SshKeyForm = {
|
||||
public_key: string;
|
||||
};
|
||||
|
||||
export default function AddSshKey({ children }: { children: ReactNode }) {
|
||||
export default function AddSshKey({ children, onKeyAdded }: { children: ReactNode; onKeyAdded?: () => void }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const form = useForm<Required<SshKeyForm>>({
|
||||
@ -36,6 +36,9 @@ export default function AddSshKey({ children }: { children: ReactNode }) {
|
||||
form.post(route('ssh-keys.store'), {
|
||||
onSuccess: () => {
|
||||
setOpen(false);
|
||||
if (onKeyAdded) {
|
||||
onKeyAdded();
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
@ -16,9 +16,9 @@ import { useForm } from '@inertiajs/react';
|
||||
import { LoaderCircleIcon, MoreVerticalIcon } from 'lucide-react';
|
||||
import FormSuccessful from '@/components/form-successful';
|
||||
import { useState } from 'react';
|
||||
import { SSHKey } from '@/types/ssh-key';
|
||||
import { SshKey } from '@/types/ssh-key';
|
||||
|
||||
function Delete({ sshKey }: { sshKey: SSHKey }) {
|
||||
function Delete({ sshKey }: { sshKey: SshKey }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const form = useForm();
|
||||
|
||||
@ -57,7 +57,7 @@ function Delete({ sshKey }: { sshKey: SSHKey }) {
|
||||
);
|
||||
}
|
||||
|
||||
export const columns: ColumnDef<SSHKey>[] = [
|
||||
export const columns: ColumnDef<SshKey>[] = [
|
||||
{
|
||||
accessorKey: 'id',
|
||||
header: 'ID',
|
||||
|
50
resources/js/pages/ssh-keys/components/ssh-key-select.tsx
Normal file
50
resources/js/pages/ssh-keys/components/ssh-key-select.tsx
Normal file
@ -0,0 +1,50 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import axios from 'axios';
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import React from 'react';
|
||||
import { SelectTriggerProps } from '@radix-ui/react-select';
|
||||
import { SshKey } from '@/types/ssh-key';
|
||||
import AddSshKey from '@/pages/ssh-keys/components/add-ssh-key';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { PlusIcon } from 'lucide-react';
|
||||
|
||||
export default function SshKeySelect({
|
||||
value,
|
||||
onValueChange,
|
||||
...props
|
||||
}: {
|
||||
value: string;
|
||||
onValueChange: (value: string) => void;
|
||||
} & SelectTriggerProps) {
|
||||
const query = useQuery<SshKey[]>({
|
||||
queryKey: ['sshKey'],
|
||||
queryFn: async () => {
|
||||
return (await axios.get(route('ssh-keys.json'))).data;
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<Select value={value} onValueChange={onValueChange} disabled={query.isFetching}>
|
||||
<SelectTrigger {...props}>
|
||||
<SelectValue placeholder={query.isFetching ? 'Loading...' : 'Select a key'} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
{query.isSuccess &&
|
||||
query.data.map((sshKey: SshKey) => (
|
||||
<SelectItem key={`db-${sshKey.name}`} value={sshKey.id.toString()}>
|
||||
{sshKey.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<AddSshKey onKeyAdded={() => query.refetch()}>
|
||||
<Button variant="outline">
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
</AddSshKey>
|
||||
</div>
|
||||
);
|
||||
}
|
@ -4,13 +4,13 @@ import Container from '@/components/container';
|
||||
import Heading from '@/components/heading';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { SSHKey } from '@/types/ssh-key';
|
||||
import { SshKey } from '@/types/ssh-key';
|
||||
import { columns } from '@/pages/ssh-keys/components/columns';
|
||||
import AddSshKey from '@/pages/ssh-keys/components/add-ssh-key';
|
||||
import { PaginatedData } from '@/types';
|
||||
|
||||
type Page = {
|
||||
sshKeys: PaginatedData<SSHKey>;
|
||||
sshKeys: PaginatedData<SshKey>;
|
||||
};
|
||||
|
||||
export default function SshKeys() {
|
||||
|
@ -5,7 +5,7 @@ import ServerLayout from '@/layouts/server/layout';
|
||||
import HeaderContainer from '@/components/header-container';
|
||||
import Heading from '@/components/heading';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { PlusIcon } from 'lucide-react';
|
||||
import { BookOpenIcon, PlusIcon } from 'lucide-react';
|
||||
import Container from '@/components/container';
|
||||
import { DataTable } from '@/components/data-table';
|
||||
import { Worker } from '@/types/worker';
|
||||
@ -26,6 +26,12 @@ export default function WorkerIndex() {
|
||||
<HeaderContainer>
|
||||
<Heading title="Workers" description="Here you can manage server's workers" />
|
||||
<div className="flex items-center gap-2">
|
||||
<a href="https://vitodeploy.com/docs/servers/workers" target="_blank">
|
||||
<Button variant="outline">
|
||||
<BookOpenIcon />
|
||||
<span className="hidden lg:block">Docs</span>
|
||||
</Button>
|
||||
</a>
|
||||
<WorkerForm serverId={page.props.server.id}>
|
||||
<Button>
|
||||
<PlusIcon />
|
||||
|
2
resources/js/types/ssh-key.d.ts
vendored
2
resources/js/types/ssh-key.d.ts
vendored
@ -1,6 +1,6 @@
|
||||
import { User } from '@/types/user';
|
||||
|
||||
export interface SSHKey {
|
||||
export interface SshKey {
|
||||
id: number;
|
||||
user?: User;
|
||||
name: string;
|
||||
|
@ -5,17 +5,15 @@
|
||||
use App\Enums\SshKeyStatus;
|
||||
use App\Facades\SSH;
|
||||
use App\Models\SshKey;
|
||||
use App\Web\Pages\Servers\SSHKeys\Index;
|
||||
use App\Web\Pages\Servers\SSHKeys\Widgets\SshKeysList;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Inertia\Testing\AssertableInertia;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ServerKeysTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_see_server_keys()
|
||||
public function test_see_server_keys(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
@ -29,12 +27,12 @@ public function test_see_server_keys()
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
|
||||
$this->get(Index::getUrl(['server' => $this->server]))
|
||||
$this->get(route('server-ssh-keys', ['server' => $this->server->id]))
|
||||
->assertSuccessful()
|
||||
->assertSeeText('My first key');
|
||||
->assertInertia(fn (AssertableInertia $page) => $page->component('server-ssh-keys/index'));
|
||||
}
|
||||
|
||||
public function test_delete_ssh_key()
|
||||
public function test_delete_ssh_key(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
@ -50,11 +48,8 @@ public function test_delete_ssh_key()
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
|
||||
Livewire::test(SshKeysList::class, [
|
||||
'server' => $this->server,
|
||||
])
|
||||
->callTableAction('delete', $sshKey->id)
|
||||
->assertSuccessful();
|
||||
$this->delete(route('server-ssh-keys.destroy', ['server' => $this->server->id, 'sshKey' => $sshKey->id]))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseMissing('server_ssh_keys', [
|
||||
'server_id' => $this->server->id,
|
||||
@ -62,29 +57,7 @@ public function test_delete_ssh_key()
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_add_new_ssh_key()
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
Livewire::test(Index::class, [
|
||||
'server' => $this->server,
|
||||
])
|
||||
->callAction('deploy', [
|
||||
'type' => 'new',
|
||||
'name' => 'My first key',
|
||||
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3CCnyBbpCgOJ0AWUSfBZ+mYAsYzcQDegPkBx1kyE0bXT1yX4+6uYx1Jh6NxWgLyaU0BaP4nsClrK1u5FojQHd8J7ycc0N3H8B+v2NPzj1Q6bFnl40saastONVm+d4edbCg9BowGAafLcf9ALsognqqOWQbK/QOpAhg25IAe47eiY3IjDGMHlsvaZkMtkDhT4t1mK8ZLjxw5vjyVYgINJefR981bIxMFrXy+0xBCsYOZxMIoAJsgCkrAGlI4kQHKv0SQVccSyTE1eziIZa5b3QUlXj8ogxMfK/EOD7Aoqinw652k4S5CwFs/LLmjWcFqCKDM6CSggWpB78DZ729O6zFvQS9V99/9SsSV7Qc5ML7B0DKzJ/tbHkaAE8xdZnQnZFVUegUMtUmjvngMaGlYsxkAZrUKsFRoh7xfXVkDyRBaBSslRNe8LFsXw9f7Q+3jdZ5vhGhmp+TBXTlgxApwR023411+ABE9y0doCx8illya3m2olEiiMZkRclgqsWFSk=',
|
||||
])
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('server_ssh_keys', [
|
||||
'server_id' => $this->server->id,
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_add_existing_key()
|
||||
public function test_add_existing_key(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
@ -96,122 +69,14 @@ public function test_add_existing_key()
|
||||
'public_key' => 'public-key-content',
|
||||
]);
|
||||
|
||||
Livewire::test(Index::class, [
|
||||
'server' => $this->server,
|
||||
$this->post(route('server-ssh-keys.store', ['server' => $this->server->id]), [
|
||||
'key' => $sshKey->id,
|
||||
])
|
||||
->callAction('deploy', [
|
||||
'type' => 'existing',
|
||||
'key_id' => $sshKey->id,
|
||||
])
|
||||
->assertSuccessful();
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('server_ssh_keys', [
|
||||
'server_id' => $this->server->id,
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ssh_key_data_provider
|
||||
*/
|
||||
public function test_create_ssh_key_handles_invalid_or_partial_keys(array $postBody, bool $expectedToSucceed): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
// Some existing amount of seed data to make the test more realistic
|
||||
SshKey::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'name' => 'My first key',
|
||||
'public_key' => 'public-key-content',
|
||||
]);
|
||||
|
||||
$postBody['type'] = 'new';
|
||||
$response = Livewire::test(Index::class, [
|
||||
'server' => $this->server,
|
||||
])
|
||||
->callAction('deploy', $postBody);
|
||||
|
||||
if ($expectedToSucceed) {
|
||||
$response->assertSuccessful();
|
||||
$this->assertDatabaseHas('ssh_keys', [
|
||||
'name' => $postBody['name'],
|
||||
]);
|
||||
$this->assertDatabaseHas('server_ssh_keys', [
|
||||
'server_id' => $this->server->id,
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
} else {
|
||||
$response->assertHasActionErrors([
|
||||
'public_key',
|
||||
]);
|
||||
$this->assertDatabaseMissing('server_ssh_keys', [
|
||||
'server_id' => $this->server->id,
|
||||
'status' => SshKeyStatus::ADDED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function ssh_key_data_provider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// RSA type
|
||||
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== test@test.local',
|
||||
],
|
||||
self::EXPECT_SUCCESS,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// DSS
|
||||
'public_key' => 'ssh-dss AAAAB3NzaC1kc3MAAACBAPLQ1v111G0vuWwurCJdg0rt2C8OwWW88sR3sCS+CPjvqPyRtFOiJLqnO0/J/tIlCZVHN0VWgKN19jOiMy2Kx2mMZoAJ0z6AGxQMoTB0eqBeYYfAfxL9KwVw6EV7QWXTu5/EDbl+6k60EQ9EXOIsnhTQsok2Ki52Td0TUxfA3Vy7AAAAFQDx8Fi0pWPoJDn7jUqog7L748iBowAAAIADrgFxfpQuujYgxS2RXX8euxgrfa1KMQNT2kXQ3781L7ihS5EjyFy03K2pV0DSQo2NeFSJv9rtJNXfCDoAofUhgugZkMWUAv8ebZsy6SxWAocjw6A5ED1YkvA03eNUEaSm9vb8ts8m1wJme6Urx41Yh9c2YVXB6yJ7T1jaeZrhbQAAAIEA3hp8vZeImWol/tTm4LE1FXkqU7cMo863MAs5gdqRhJ5Xnwvsbx1WSVajJY78e8s/Yd4GhBAJpNjAVfep0CqbfJeNKV/D6oCKCfVikKefRw4GIREtAfkijlh/WOkwmWE0VcZRQk1sIizYtqIwtghvMvzDRSGFMC3l6hF5AHYyrSg= test@test.local',
|
||||
],
|
||||
self::EXPECT_SUCCESS,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// ECDSA type
|
||||
'public_key' => 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBY0xeD9/iTVZZO/qVFRjAwtNmC0zHVWqY4Q7nmB4nGvfpHhlze+rEpxXwNWW5olIcAjAXJO+gQCa4iNV6UYDp8= test@test.local',
|
||||
],
|
||||
self::EXPECT_SUCCESS,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// ED25519 type
|
||||
'public_key' => 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIxUhYjgZEXKYnlerxHYyL/e0HZ4C9t3WCpQ/LCPQZMp test@test.local',
|
||||
],
|
||||
self::EXPECT_SUCCESS,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// Partial ED25519 type
|
||||
'public_key' => 'ssh-afeef AAAAC3NzaC1lZDI1NTE5AAAAIIxUhYjgZEXKYnlerxHYyL/e0HZ4C9t3WCpQ/LCPQZMp test@test.local',
|
||||
],
|
||||
self::EXPECT_FAILURE,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// Partial ED25519 type
|
||||
'public_key' => 'ssh-ed25519 AAAAC3NzAffefIIIAAAAAAAIIxUhYjgZEXKYnlerxHYyL/e0HZ4C9t3WCpQ/LCPQZMp test@test.local',
|
||||
],
|
||||
self::EXPECT_FAILURE,
|
||||
],
|
||||
[
|
||||
[
|
||||
'name' => 'My first key',
|
||||
// Partial DSS type, close but not quite
|
||||
'public_key' => 'ssh-dss AAAAB3NzaC1kc3MAAACBAPLQ1v111G0vuWwurCJdga830asW88sR3sCS+CPjvqPyRtFOiJLqnO0/J/tIlCZVHN0VWgKN19jOiMy2Kx2mMZoAJ0z6AGxQMoTB0eqBeYYfAfxL9KwVw6EV7QWXTu5/EDbl+6k60EQ9EXOIsnhTQsok2Ki52Td0TUxfA3Vy7AAAAFQDx8Fi0pWPoJDn7jUqog7L748iBowAAAIADrgFxfpQuujYgxS2RXX8euxgrfa1KMQNT2kXQ3781L7ihS5EjyFy03K2pV0DSQo2NeFSJv9rtJNXfCDoAofUhgugZkMWUAv8ebZsy6SxWAocjw6A5ED1YkvA03eNUEaSm9vb8ts8m1wJme6Urx41Yh9c2YVXB6yJ7T1jaeZrhbQAAAIEA3hp8vZeImWol/tTm4LE1FXkqU7cMo863MAs5gdqRhJ5Xnwvsbx1WSVajJYa103s/Yd4GhBAJpNjAVfep0CqbfJeNKV/D6oCKCfVikKefRw4GIREtAfkijlh/WOkwmWE0VcZRQk1sIizYtqIwtghvMvzDRSGFMC3l6hF5AHYyrSg= test@test.local',
|
||||
],
|
||||
self::EXPECT_FAILURE,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user