Skip to content

Commit 48bf8e0

Browse files
committed
Generate setup key when installing netbird from routing peer modal
1 parent 5c2dba7 commit 48bf8e0

File tree

4 files changed

+133
-32
lines changed

4 files changed

+133
-32
lines changed

src/modules/networks/routing-peers/NetworkRoutingPeerModal.tsx

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import { cn } from "@utils/helpers";
2424
import { uniqBy } from "lodash";
2525
import {
2626
ArrowDownWideNarrow,
27-
CirclePlusIcon,
27+
DownloadIcon,
2828
ExternalLinkIcon,
2929
FolderGit2,
30+
Loader2,
3031
MonitorSmartphoneIcon,
3132
PlusCircle,
3233
Power,
@@ -35,10 +36,12 @@ import {
3536
VenetianMask,
3637
} from "lucide-react";
3738
import React, { useState } from "react";
39+
import { useSWRConfig } from "swr";
3840
import { Network, NetworkRouter } from "@/interfaces/Network";
3941
import { Peer } from "@/interfaces/Peer";
42+
import { SetupKey } from "@/interfaces/SetupKey";
4043
import useGroupHelper from "@/modules/groups/useGroupHelper";
41-
import SetupKeyModal from "@/modules/setup-keys/SetupKeyModal";
44+
import SetupModal from "@/modules/setup-netbird-modal/SetupModal";
4245

4346
type Props = {
4447
network: Network;
@@ -240,8 +243,8 @@ function RoutingPeerModalContent({
240243
<SegmentedTabs.Content value={"peer"}>
241244
<div>
242245
<HelpText>
243-
Assign a single or multiple peers as routing peers for the
244-
network.
246+
Assign a single or multiple Linux peers as routing peers
247+
for the network.
245248
</HelpText>
246249
<PeerSelector
247250
onChange={setRoutingPeer}
@@ -267,29 +270,15 @@ function RoutingPeerModalContent({
267270

268271
<div className={cn("flex justify-between items-center mt-3")}>
269272
<div>
270-
<Label>Install Routing Peer</Label>
273+
<Label>{"Don't have a routing peer?"}</Label>
271274
<HelpText className={""}>
272-
You can install NetBird with a Setup Key on one or more Linux
275+
You can install NetBird with a setup key on one or more Linux
273276
machines to act as routing peers.
274277
</HelpText>
275278
</div>
276-
<Button
277-
variant={"secondary"}
278-
size={"xs"}
279-
className={"ml-8"}
280-
onClick={() => setSetupKeyModal(true)}
281-
>
282-
<CirclePlusIcon size={14} />
283-
Create Setup Key
284-
</Button>
285-
{setupKeyModal && (
286-
<SetupKeyModal
287-
open={setupKeyModal}
288-
setOpen={setSetupKeyModal}
289-
showOnlyRoutingPeerOS={true}
290-
name={`Routing Peer (${network.name})`}
291-
/>
292-
)}
279+
<InstallNetBirdWithSetupKeyButton
280+
name={`Routing Peer (${network.name})`}
281+
/>
293282
</div>
294283
</div>
295284
</TabsContent>
@@ -409,3 +398,74 @@ function RoutingPeerModalContent({
409398
</ModalContent>
410399
);
411400
}
401+
402+
type InstallNetBirdWithSetupKeyButtonProps = {
403+
name?: string;
404+
};
405+
406+
const InstallNetBirdWithSetupKeyButton = ({
407+
name,
408+
}: InstallNetBirdWithSetupKeyButtonProps) => {
409+
const setupKeyRequest = useApiCall<SetupKey>("/setup-keys", true);
410+
const { mutate } = useSWRConfig();
411+
412+
const [installModal, setInstallModal] = useState(false);
413+
const [setupKey, setSetupKey] = useState<SetupKey>();
414+
const [isLoading, setIsLoading] = useState(false);
415+
416+
const createSetupKey = async () => {
417+
const loadingTimeout = setTimeout(() => setIsLoading(true), 1000);
418+
419+
await setupKeyRequest
420+
.post({
421+
name,
422+
type: "one-off",
423+
expires_in: 24 * 60 * 60, // 1 day expiration
424+
revoked: false,
425+
auto_groups: [],
426+
usage_limit: 1,
427+
ephemeral: false,
428+
})
429+
.then((setupKey) => {
430+
setInstallModal(true);
431+
setSetupKey(setupKey);
432+
mutate("/setup-keys");
433+
})
434+
.finally(() => {
435+
setIsLoading(false);
436+
clearTimeout(loadingTimeout);
437+
});
438+
};
439+
440+
return (
441+
<>
442+
<Button
443+
variant={"secondary"}
444+
size={"xs"}
445+
className={"ml-8"}
446+
onClick={createSetupKey}
447+
disabled={isLoading}
448+
>
449+
{isLoading ? (
450+
<Loader2 size={14} className={"animate-spin delay-1000"} />
451+
) : (
452+
<DownloadIcon size={14} />
453+
)}
454+
Install NetBird
455+
</Button>
456+
{setupKey && (
457+
<Modal
458+
open={installModal}
459+
onOpenChange={setInstallModal}
460+
key={setupKey.key}
461+
>
462+
<SetupModal
463+
showClose={true}
464+
setupKey={setupKey.key}
465+
showOnlyRoutingPeerOS={true}
466+
/>
467+
</Modal>
468+
)}
469+
</>
470+
);
471+
};

src/modules/setup-netbird-modal/DockerTab.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ import { ExternalLinkIcon } from "lucide-react";
99
import Link from "next/link";
1010
import React from "react";
1111
import { OperatingSystem } from "@/interfaces/OperatingSystem";
12+
import { RoutingPeerSetupKeyInfo } from "@/modules/setup-netbird-modal/SetupModal";
1213

1314
type Props = {
1415
setupKey?: string;
16+
showSetupKeyInfo?: boolean;
1517
};
1618

17-
export default function DockerTab({ setupKey }: Readonly<Props>) {
19+
export default function DockerTab({
20+
setupKey,
21+
showSetupKeyInfo = false,
22+
}: Readonly<Props>) {
1823
return (
1924
<TabsContent value={String(OperatingSystem.DOCKER)}>
2025
<TabsContentPadding>
@@ -39,7 +44,10 @@ export default function DockerTab({ setupKey }: Readonly<Props>) {
3944
</div>
4045
</Steps.Step>
4146
<Steps.Step step={2}>
42-
<p>Run NetBird container</p>
47+
<p>
48+
Run NetBird container
49+
{showSetupKeyInfo && <RoutingPeerSetupKeyInfo />}
50+
</p>
4351
<Code>
4452
<Code.Line>docker run --rm -d \</Code.Line>
4553
<Code.Line> --cap-add=NET_ADMIN \</Code.Line>

src/modules/setup-netbird-modal/LinuxTab.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ import { getNetBirdUpCommand } from "@utils/netbird";
1313
import { TerminalSquareIcon } from "lucide-react";
1414
import React from "react";
1515
import { OperatingSystem } from "@/interfaces/OperatingSystem";
16-
import { SetupKeyParameter } from "@/modules/setup-netbird-modal/SetupModal";
16+
import {
17+
RoutingPeerSetupKeyInfo,
18+
SetupKeyParameter,
19+
} from "@/modules/setup-netbird-modal/SetupModal";
1720

1821
type Props = {
1922
setupKey?: string;
23+
showSetupKeyInfo?: boolean;
2024
};
2125

22-
export default function LinuxTab({ setupKey }: Readonly<Props>) {
26+
export default function LinuxTab({
27+
setupKey,
28+
showSetupKeyInfo = false,
29+
}: Readonly<Props>) {
2330
return (
2431
<TabsContent value={String(OperatingSystem.LINUX)}>
2532
<TabsContentPadding>
@@ -32,7 +39,10 @@ export default function LinuxTab({ setupKey }: Readonly<Props>) {
3239
<Code>curl -fsSL https://pkgs.netbird.io/install.sh | sh</Code>
3340
</Steps.Step>
3441
<Steps.Step step={2} line={false}>
35-
<p>Run NetBird {!setupKey && "and log in the browser"}</p>
42+
<p>
43+
Run NetBird {!setupKey && "and log in the browser"}
44+
{showSetupKeyInfo && <RoutingPeerSetupKeyInfo />}
45+
</p>
3646
<Code>
3747
<Code.Line>
3848
{getNetBirdUpCommand()}
@@ -86,7 +96,10 @@ export default function LinuxTab({ setupKey }: Readonly<Props>) {
8696
</Code>
8797
</Steps.Step>
8898
<Steps.Step step={3} line={false}>
89-
<p>Run NetBird {!setupKey && "and log in the browser"}</p>
99+
<p>
100+
Run NetBird {!setupKey && "and log in the browser"}
101+
{showSetupKeyInfo && <RoutingPeerSetupKeyInfo />}
102+
</p>
90103
<Code>
91104
<Code.Line>
92105
{getNetBirdUpCommand()}

src/modules/setup-netbird-modal/SetupModal.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ export function SetupModalContent({
167167
</TabsTrigger>
168168
</TabsList>
169169

170-
<LinuxTab setupKey={setupKey} />
170+
<LinuxTab
171+
setupKey={setupKey}
172+
showSetupKeyInfo={showOnlyRoutingPeerOS}
173+
/>
171174
<WindowsTab setupKey={setupKey} />
172175
<MacOSTab setupKey={setupKey} />
173176

@@ -178,7 +181,10 @@ export function SetupModalContent({
178181
</>
179182
)}
180183

181-
<DockerTab setupKey={setupKey} />
184+
<DockerTab
185+
setupKey={setupKey}
186+
showSetupKeyInfo={showOnlyRoutingPeerOS}
187+
/>
182188
</Tabs>
183189
{footer && (
184190
<ModalFooter variant={"setup"}>
@@ -213,8 +219,22 @@ export const SetupKeyParameter = ({ setupKey }: SetupKeyParameterProps) => {
213219
setupKey && (
214220
<>
215221
{" "}
216-
--setup-key <span className={"text-netbird"}>{setupKey}</span>{" "}
222+
--setup-key <span className={"text-netbird"}>{setupKey}</span>
217223
</>
218224
)
219225
);
220226
};
227+
228+
export const RoutingPeerSetupKeyInfo = () => {
229+
return (
230+
<div
231+
className={
232+
"flex gap-2 mt-1 items-center text-xs text-nb-gray-300 font-normal mb-1"
233+
}
234+
>
235+
This setup key can be used only once within the next 24 hours.
236+
<br />
237+
When expired, the same key can not be used again.
238+
</div>
239+
);
240+
};

0 commit comments

Comments
 (0)