Path: blob/main/components/dashboard/src/service/json-rpc-ssh-client.ts
2500 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { PromiseClient } from "@connectrpc/connect";7import { PartialMessage } from "@bufbuild/protobuf";8import { SSHService } from "@gitpod/public-api/lib/gitpod/v1/ssh_connect";9import {10CreateSSHPublicKeyRequest,11CreateSSHPublicKeyResponse,12DeleteSSHPublicKeyRequest,13DeleteSSHPublicKeyResponse,14ListSSHPublicKeysRequest,15ListSSHPublicKeysResponse,16} from "@gitpod/public-api/lib/gitpod/v1/ssh_pb";17import { converter } from "./public-api";18import { getGitpodService } from "./service";19import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";2021export class JsonRpcSSHClient implements PromiseClient<typeof SSHService> {22async listSSHPublicKeys(req: PartialMessage<ListSSHPublicKeysRequest>): Promise<ListSSHPublicKeysResponse> {23const result = new ListSSHPublicKeysResponse();24const sshKeys = await getGitpodService().server.getSSHPublicKeys();25result.sshKeys = sshKeys.map((i) => converter.toSSHPublicKey(i));2627return result;28}2930async createSSHPublicKey(req: PartialMessage<CreateSSHPublicKeyRequest>): Promise<CreateSSHPublicKeyResponse> {31if (!req.name || !req.key) {32throw new ApplicationError(ErrorCodes.BAD_REQUEST, "name and key are required");33}3435const response = new CreateSSHPublicKeyResponse();3637const sshKey = await getGitpodService().server.addSSHPublicKey({ name: req.name, key: req.key });38response.sshKey = converter.toSSHPublicKey(sshKey);3940return response;41}4243async deleteSSHPublicKey(req: PartialMessage<DeleteSSHPublicKeyRequest>): Promise<DeleteSSHPublicKeyResponse> {44if (!req.sshKeyId) {45throw new ApplicationError(ErrorCodes.BAD_REQUEST, "sshKeyId is required");46}4748await getGitpodService().server.deleteSSHPublicKey(req.sshKeyId);4950const response = new DeleteSSHPublicKeyResponse();51return response;52}53}545556