Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add SSH Keys page on /preferences/ssh-keys#1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
BrunoQuaresma merged 2 commits intomainfrombq/752/add-ssh-keys
May 16, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletionssite/src/api/api.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,3 +190,13 @@ export const updateUserRoles = async (
const response = await axios.put<TypesGen.User>(`/api/v2/users/${userId}/roles`, { roles })
return response.data
}

export const getUserSSHKey = async (userId = "me"): Promise<TypesGen.GitSSHKey> => {
const response = await axios.get<TypesGen.GitSSHKey>(`/api/v2/users/${userId}/gitsshkey`)
return response.data
}

export const regenerateUserSSHKey = async (userId = "me"): Promise<TypesGen.GitSSHKey> => {
const response = await axios.put<TypesGen.GitSSHKey>(`/api/v2/users/${userId}/gitsshkey`)
return response.data
}
12 changes: 0 additions & 12 deletionssite/src/pages/PreferencesPages/AccountPage/SSHKeysPage.tsx
View file
Open in desktop

This file was deleted.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
import { fireEvent, screen, within } from "@testing-library/react"
import React from "react"
import * as API from "../../../api/api"
import { GlobalSnackbar } from "../../../components/GlobalSnackbar/GlobalSnackbar"
import { MockGitSSHKey, renderWithAuth } from "../../../testHelpers/renderHelpers"
import { Language as authXServiceLanguage } from "../../../xServices/auth/authXService"
import { Language as SSHKeysPageLanguage, SSHKeysPage } from "./SSHKeysPage"

describe("SSH Keys Page", () => {
it("shows the SSH key", async () => {
renderWithAuth(<SSHKeysPage />)
await screen.findByText(MockGitSSHKey.public_key)
})

describe("regenerate SSH key", () => {
describe("when it is success", () => {
it("shows a success message and updates the ssh key on the page", async () => {
renderWithAuth(
<>
<SSHKeysPage />
<GlobalSnackbar />
</>,
)

// Wait to the ssh be rendered on the screen
await screen.findByText(MockGitSSHKey.public_key)

// Click on the "Regenerate" button to display the confirm dialog
const regenerateButton = screen.getByRole("button", { name: SSHKeysPageLanguage.regenerateLabel })
fireEvent.click(regenerateButton)
const confirmDialog = screen.getByRole("dialog")
expect(confirmDialog).toHaveTextContent(SSHKeysPageLanguage.regenerateDialogMessage)

const newUserSSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDSC/ouD/LqiT1Rd99vDv/MwUmqzJuinLTMTpk5kVy66"
jest.spyOn(API, "regenerateUserSSHKey").mockResolvedValueOnce({
...MockGitSSHKey,
public_key: newUserSSHKey,
})

// Click on the "Confirm" button
const confirmButton = within(confirmDialog).getByRole("button", { name: SSHKeysPageLanguage.confirmLabel })
fireEvent.click(confirmButton)

// Check if the success message is displayed
await screen.findByText(authXServiceLanguage.successRegenerateSSHKey)

// Check if the API was called correctly
expect(API.regenerateUserSSHKey).toBeCalledTimes(1)

// Check if the SSH key is updated
await screen.findByText(newUserSSHKey)
})
})

describe("when it fails", () => {
it("shows an error message", async () => {
renderWithAuth(
<>
<SSHKeysPage />
<GlobalSnackbar />
</>,
)

// Wait to the ssh be rendered on the screen
await screen.findByText(MockGitSSHKey.public_key)

jest.spyOn(API, "regenerateUserSSHKey").mockRejectedValueOnce({})

// Click on the "Regenerate" button to display the confirm dialog
const regenerateButton = screen.getByRole("button", { name: SSHKeysPageLanguage.regenerateLabel })
fireEvent.click(regenerateButton)
const confirmDialog = screen.getByRole("dialog")
expect(confirmDialog).toHaveTextContent(SSHKeysPageLanguage.regenerateDialogMessage)

// Click on the "Confirm" button
const confirmButton = within(confirmDialog).getByRole("button", { name: SSHKeysPageLanguage.confirmLabel })
fireEvent.click(confirmButton)

// Check if the error message is displayed
await screen.findByText(authXServiceLanguage.errorRegenerateSSHKey)

// Check if the API was called correctly
expect(API.regenerateUserSSHKey).toBeCalledTimes(1)
})
})
})
})
70 changes: 67 additions & 3 deletionssite/src/pages/PreferencesPages/SSHKeysPage/SSHKeysPage.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,76 @@
import React from "react"
import Box from "@material-ui/core/Box"
import Button from "@material-ui/core/Button"
import CircularProgress from "@material-ui/core/CircularProgress"
import { useActor } from "@xstate/react"
import React, { useContext, useEffect } from "react"
import { CodeBlock } from "../../../components/CodeBlock/CodeBlock"
import { ConfirmDialog } from "../../../components/ConfirmDialog/ConfirmDialog"
import { Section } from "../../../components/Section/Section"
import { Stack } from "../../../components/Stack/Stack"
import { XServiceContext } from "../../../xServices/StateContext"

const Language = {
exportconst Language = {
title: "SSH Keys",
description:
"Coder automatically inserts a private key into every workspace; you can add the corresponding public key to any services (such as Git) that you need access to from your workspace.",
regenerateLabel: "Regenerate",
regenerateDialogTitle: "Regenerate SSH Key?",
regenerateDialogMessage:
"You will need to replace the public SSH key on services you use it with, and you'll need to rebuild existing workspaces.",
confirmLabel: "Confirm",
cancelLabel: "Cancel",
}

export const SSHKeysPage: React.FC = () => {
return <Section title={Language.title} description={Language.description} />
const xServices = useContext(XServiceContext)
const [authState, authSend] = useActor(xServices.authXService)
const { sshKey } = authState.context

useEffect(() => {
authSend({ type: "GET_SSH_KEY" })
}, [authSend])

return (
<>
<Section title={Language.title} description={Language.description}>
{!sshKey && (
<Box p={4}>
<CircularProgress size={26} />
</Box>
)}

{sshKey && (
<Stack>
<CodeBlock lines={[sshKey.public_key.trim()]} />
<div>
<Button
color="primary"
onClick={() => {
authSend({ type: "REGENERATE_SSH_KEY" })
}}
>
{Language.regenerateLabel}
</Button>
</div>
</Stack>
)}
</Section>

<ConfirmDialog
type="delete"
hideCancel={false}
open={authState.matches("signedIn.ssh.loaded.confirmSSHKeyRegenerate")}
confirmLoading={authState.matches("signedIn.ssh.loaded.regeneratingSSHKey")}
title={Language.regenerateDialogTitle}
confirmText={Language.confirmLabel}
onConfirm={() => {
authSend({ type: "CONFIRM_REGENERATE_SSH_KEY" })
}}
onClose={() => {
authSend({ type: "CANCEL_REGENERATE_SSH_KEY" })
}}
description={<>{Language.regenerateDialogMessage}</>}
/>
</>
)
}
7 changes: 7 additions & 0 deletionssite/src/testHelpers/entities.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -163,3 +163,10 @@ export const MockAuthMethods: TypesGen.AuthMethods = {
password: true,
github: false,
}

export const MockGitSSHKey: TypesGen.GitSSHKey = {
user_id: "1fa0200f-7331-4524-a364-35770666caa7",
created_at: "2022-05-16T14:30:34.148205897Z",
updated_at: "2022-05-16T15:29:10.302441433Z",
public_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFJOQRIM7kE30rOzrfy+/+R+nQGCk7S9pioihy+2ARbq",
}
3 changes: 3 additions & 0 deletionssite/src/testHelpers/handlers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,9 @@ export const handlers = [

return res(ctx.status(200), ctx.json(response))
}),
rest.get("/api/v2/users/:userId/gitsshkey", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockGitSSHKey))
}),

// workspaces
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {
Expand Down
111 changes: 110 additions & 1 deletionsite/src/xServices/auth/authXService.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import * as TypesGen from "../../api/typesGenerated"
import { displaySuccess } from "../../components/GlobalSnackbar/utils"
import {displayError,displaySuccess } from "../../components/GlobalSnackbar/utils"

export const Language = {
successProfileUpdate: "Updated preferences.",
successRegenerateSSHKey: "SSH Key regenerated successfully",
errorRegenerateSSHKey: "Error on regenerate the SSH Key",
}

export const checks = {
Expand All@@ -31,12 +33,87 @@ export interface AuthContext {
methods?: TypesGen.AuthMethods
permissions?: Permissions
checkPermissionsError?: Error | unknown
// SSH
sshKey?: TypesGen.GitSSHKey
getSSHKeyError?: Error | unknown
regenerateSSHKeyError?: Error | unknown
}

export type AuthEvent =
| { type: "SIGN_OUT" }
| { type: "SIGN_IN"; email: string; password: string }
| { type: "UPDATE_PROFILE"; data: TypesGen.UpdateUserProfileRequest }
| { type: "GET_SSH_KEY" }
| { type: "REGENERATE_SSH_KEY" }
| { type: "CONFIRM_REGENERATE_SSH_KEY" }
| { type: "CANCEL_REGENERATE_SSH_KEY" }
Comment on lines +46 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Should we rename thisUserEvent instead? Seems like it's more accurate.


const sshState = {
initial: "idle",
states: {
idle: {
on: {
GET_SSH_KEY: {
target: "gettingSSHKey",
},
},
},
gettingSSHKey: {
entry: "clearGetSSHKeyError",
invoke: {
src: "getSSHKey",
onDone: [
{
actions: ["assignSSHKey"],
target: "#authState.signedIn.ssh.loaded",
},
],
onError: [
{
actions: "assignGetSSHKeyError",
target: "#authState.signedIn.ssh.idle",
},
],
},
},
loaded: {
initial: "idle",
states: {
idle: {
on: {
REGENERATE_SSH_KEY: {
target: "confirmSSHKeyRegenerate",
},
},
},
confirmSSHKeyRegenerate: {
on: {
CANCEL_REGENERATE_SSH_KEY: "idle",
CONFIRM_REGENERATE_SSH_KEY: "regeneratingSSHKey",
},
},
regeneratingSSHKey: {
entry: "clearRegenerateSSHKeyError",
invoke: {
src: "regenerateSSHKey",
onDone: [
{
actions: ["assignSSHKey", "notifySuccessSSHKeyRegenerated"],
target: "#authState.signedIn.ssh.loaded.idle",
},
],
onError: [
{
actions: ["assignRegenerateSSHKeyError", "notifySSHKeyRegenerationError"],
target: "#authState.signedI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hC0gB1WeDIBguHVkGjBETU6byRYCmW06da5NbdZiKalLl+p-k4XgTYJNhxuVNKGCB77fEy5DWnAYhGWkFDUBgXUPoqLiKOoxIqGVejNKoxXPCUMgjZ8zj6sORoThBclhBE2y8N67F1o+xIvhoejavqEX-gFgl6IGFWOC2bzWNqy0AuyYxcpMf0cQghjqn+JT6GJeJynIqrI2msWZhalY2uzuN9dojwpn+epDvqHjRkUL7KBDEljiLzKyXF32mUpWkwquRCvQeuls-t94c606s8c0A5C00UjqqDja5cUXQRXiDiMwb0FmURpuWQW1tY25D7242jsxn+bi6IFhh2K4qjKP+fsqAHX1B8bKkkGb0seR0gNx87idu4JtIwzoxTdELzbheOov1SZhEWcR6moURxdHCOgPhsBoNDRDKju84hCfHS4ZAXPqg59OCn58ufeFODQPD2DY-FUqGsASmdShgvKP67nClrwgQu2EPIPb2V4+CVNf7w5TOphs22en2LDVrb9Ns4w8j1coU8iafDKJVWQagDDqmOsOKBVhXDgweNJb+ppL59TcH2ZQw1E5jSurcYK8CHCODfE0B4vRfBAA */
Expand DownExpand Up@@ -70,6 +147,12 @@ export const authMachine =
checkPermissions: {
data: TypesGen.UserAuthorizationResponse
}
getSSHKey: {
data: TypesGen.GitSSHKey
}
regenerateSSHKey: {
data: TypesGen.GitSSHKey
}
},
},
id: "authState",
Expand DownExpand Up@@ -197,6 +280,7 @@ export const authMachine =
},
},
},
ssh: sshState,
},
on: {
SIGN_OUT: {
Expand DownExpand Up@@ -249,6 +333,9 @@ export const authMachine =
checks: permissionsToCheck,
})
},
// SSH
getSSHKey: () => API.getUserSSHKey(),
regenerateSSHKey: () => API.regenerateUserSSHKey(),
},
actions: {
assignMe: assign({
Expand DownExpand Up@@ -302,6 +389,28 @@ export const authMachine =
clearGetPermissionsError: assign({
checkPermissionsError: (_) => undefined,
}),
// SSH
assignSSHKey: assign({
sshKey: (_, event) => event.data,
}),
assignGetSSHKeyError: assign({
getSSHKeyError: (_, event) => event.data,
}),
clearGetSSHKeyError: assign({
getSSHKeyError: (_) => undefined,
}),
assignRegenerateSSHKeyError: assign({
regenerateSSHKeyError: (_, event) => event.data,
}),
clearRegenerateSSHKeyError: assign({
regenerateSSHKeyError: (_) => undefined,
}),
notifySuccessSSHKeyRegenerated: () => {
displaySuccess(Language.successRegenerateSSHKey)
},
notifySSHKeyRegenerationError: () => {
displayError(Language.errorRegenerateSSHKey)
},
},
},
)

[8]ページ先頭

©2009-2025 Movatter.jp