Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/service/json-rpc-ssh-client.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { PromiseClient } from "@connectrpc/connect";
8
import { PartialMessage } from "@bufbuild/protobuf";
9
import { SSHService } from "@gitpod/public-api/lib/gitpod/v1/ssh_connect";
10
import {
11
CreateSSHPublicKeyRequest,
12
CreateSSHPublicKeyResponse,
13
DeleteSSHPublicKeyRequest,
14
DeleteSSHPublicKeyResponse,
15
ListSSHPublicKeysRequest,
16
ListSSHPublicKeysResponse,
17
} from "@gitpod/public-api/lib/gitpod/v1/ssh_pb";
18
import { converter } from "./public-api";
19
import { getGitpodService } from "./service";
20
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
21
22
export class JsonRpcSSHClient implements PromiseClient<typeof SSHService> {
23
async listSSHPublicKeys(req: PartialMessage<ListSSHPublicKeysRequest>): Promise<ListSSHPublicKeysResponse> {
24
const result = new ListSSHPublicKeysResponse();
25
const sshKeys = await getGitpodService().server.getSSHPublicKeys();
26
result.sshKeys = sshKeys.map((i) => converter.toSSHPublicKey(i));
27
28
return result;
29
}
30
31
async createSSHPublicKey(req: PartialMessage<CreateSSHPublicKeyRequest>): Promise<CreateSSHPublicKeyResponse> {
32
if (!req.name || !req.key) {
33
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "name and key are required");
34
}
35
36
const response = new CreateSSHPublicKeyResponse();
37
38
const sshKey = await getGitpodService().server.addSSHPublicKey({ name: req.name, key: req.key });
39
response.sshKey = converter.toSSHPublicKey(sshKey);
40
41
return response;
42
}
43
44
async deleteSSHPublicKey(req: PartialMessage<DeleteSSHPublicKeyRequest>): Promise<DeleteSSHPublicKeyResponse> {
45
if (!req.sshKeyId) {
46
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "sshKeyId is required");
47
}
48
49
await getGitpodService().server.deleteSSHPublicKey(req.sshKeyId);
50
51
const response = new DeleteSSHPublicKeyResponse();
52
return response;
53
}
54
}
55
56