Skip to content

Commit 12775a3

Browse files
committed
Use public api in more places
1 parent d6277e9 commit 12775a3

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

src/heartbeat.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ export class HeartbeatManager extends Disposable {
109109
? (workspaceInfo as Workspace)?.status?.instance?.status?.phase === WorkspaceInstanceStatus_Phase.RUNNING && (workspaceInfo as Workspace)?.status?.instance?.instanceId === this.instanceId
110110
: (workspaceInfo as WorkspaceInfo).latestInstance?.status?.phase === 'running' && (workspaceInfo as WorkspaceInfo).latestInstance?.id === this.instanceId;
111111
if (this.isWorkspaceRunning) {
112-
// TODO: use the public API
113-
await service.server.sendHeartBeat({ instanceId: this.instanceId, wasClosed });
112+
this.publicApi
113+
? (!wasClosed ? await this.publicApi.sendHeartbeat(this.workspaceId) : await this.publicApi.sendDidClose(this.workspaceId))
114+
: await service.server.sendHeartBeat({ instanceId: this.instanceId, wasClosed });
114115
if (wasClosed) {
115116
this.telemetry.sendTelemetryEvent('ide_close_signal', { workspaceId: this.workspaceId, instanceId: this.instanceId, gitpodHost: this.gitpodHost, clientKind: 'vscode' });
116117
this.logger.trace('Send ' + suffix);

src/publicApi.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55

66
import { createConnectTransport, createPromiseClient, Interceptor, PromiseClient } from '@bufbuild/connect-web';
77
import { WorkspacesService } from '@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_connectweb';
8+
import { IDEClientService } from '@gitpod/public-api/lib/gitpod/experimental/v1/ide_client_connectweb';
9+
import { UserService } from '@gitpod/public-api/lib/gitpod/experimental/v1/user_connectweb';
810
import { Workspace } from '@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_pb';
11+
import { SSHKey, User } from '@gitpod/public-api/lib/gitpod/experimental/v1/user_pb';
912

1013
export class GitpodPublicApi {
1114

1215
private workspaceService!: PromiseClient<typeof WorkspacesService>;
16+
private userService!: PromiseClient<typeof UserService>;
17+
private ideClientService!: PromiseClient<typeof IDEClientService>;
1318

14-
constructor() {
15-
}
16-
17-
async init(accessToken: string, gitpodHost: string) {
19+
constructor(accessToken: string, gitpodHost: string) {
1820
const serviceUrl = new URL(gitpodHost);
1921
serviceUrl.hostname = `api.${serviceUrl.hostname}`;
2022

@@ -29,6 +31,8 @@ export class GitpodPublicApi {
2931
});
3032

3133
this.workspaceService = createPromiseClient(WorkspacesService, transport);
34+
this.userService = createPromiseClient(UserService, transport);
35+
this.ideClientService = createPromiseClient(IDEClientService, transport);
3236
}
3337

3438
async getWorkspace(workspaceId: string): Promise<Workspace | undefined> {
@@ -40,4 +44,22 @@ export class GitpodPublicApi {
4044
const response = await this.workspaceService.getOwnerToken({ workspaceId });
4145
return response.token;
4246
}
47+
48+
async getSSHKeys(): Promise<SSHKey[]> {
49+
const response = await this.userService.listSSHKeys({});
50+
return response.keys;
51+
}
52+
53+
async sendHeartbeat(workspaceId: string): Promise<void> {
54+
await this.ideClientService.sendHeartbeat({ workspaceId });
55+
}
56+
57+
async sendDidClose(workspaceId: string): Promise<void> {
58+
await this.ideClientService.sendDidClose({ workspaceId });
59+
}
60+
61+
async getAuthenticatedUser(): Promise<User | undefined> {
62+
const response = await this.userService.getAuthenticatedUser({});
63+
return response.user;
64+
}
4365
}

src/remoteConnector.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { LocalAppClient } from '@gitpod/local-app-api-grpcweb/lib/localapp_pb_se
99
import { NodeHttpTransport } from '@improbable-eng/grpc-web-node-http-transport';
1010
import { grpc } from '@improbable-eng/grpc-web';
1111
import { Workspace, WorkspaceInstanceStatus_Phase } from '@gitpod/public-api/lib/gitpod/experimental/v1/workspaces_pb';
12-
import { WorkspaceInfo } from '@gitpod/gitpod-protocol';
12+
import { UserSSHPublicKeyValue, WorkspaceInfo } from '@gitpod/gitpod-protocol';
1313
import * as cp from 'child_process';
1414
import * as fs from 'fs';
1515
import * as http from 'http';
@@ -38,6 +38,7 @@ import { getOpenSSHVersion } from './ssh/sshVersion';
3838
import { NotificationService } from './notification';
3939
import { UserFlowTelemetry } from './common/telemetry';
4040
import { GitpodPublicApi } from './publicApi';
41+
import { SSHKey } from '@gitpod/public-api/lib/gitpod/experimental/v1/user_pb';
4142

4243
interface SSHConnectionParams {
4344
workspaceId: string;
@@ -538,7 +539,7 @@ export default class RemoteConnector extends Disposable {
538539
const [workspaceInfo, ownerToken, registeredSSHKeys] = await withServerApi(session.accessToken, serviceUrl.toString(), service => Promise.all([
539540
this.publicApi ? this.publicApi.getWorkspace(workspaceId) : service.server.getWorkspace(workspaceId),
540541
this.publicApi ? this.publicApi.getOwnerToken(workspaceId) : service.server.getOwnerToken(workspaceId),
541-
sshKeysSupported ? service.server.getSSHPublicKeys() : undefined
542+
sshKeysSupported ? (this.publicApi ? this.publicApi.getSSHKeys() : service.server.getSSHPublicKeys()) : undefined
542543
]), this.logger);
543544

544545
const isRunning = this.publicApi
@@ -618,9 +619,21 @@ export default class RemoteConnector extends Disposable {
618619
let identityKeys = await this.getIdentityKeys(hostConfiguration);
619620

620621
if (registeredSSHKeys) {
621-
this.logger.trace(`Registered public keys in Gitpod account:`, registeredSSHKeys.length ? registeredSSHKeys.map(k => `${k.name} SHA256:${k.fingerprint}`).join('\n') : 'None');
622+
const registeredKeys = this.publicApi
623+
? (registeredSSHKeys as SSHKey[]).map(k => {
624+
const parsedResult = sshUtils.parseKey(k.key);
625+
if (parsedResult instanceof Error || !parsedResult) {
626+
this.logger.error(`Error while parsing SSH public key ${k.name}:`, parsedResult);
627+
return { name: k.name, fingerprint: '' };
628+
}
629+
630+
const parsedKey = parsedResult as ParsedKey;
631+
return { name: k.name, fingerprint: crypto.createHash('sha256').update(parsedKey.getPublicSSH()).digest('base64') };
632+
})
633+
: (registeredSSHKeys as UserSSHPublicKeyValue[]).map(k => ({ name: k.name, fingerprint: k.fingerprint }));
634+
this.logger.trace(`Registered public keys in Gitpod account:`, registeredKeys.length ? registeredKeys.map(k => `${k.name} SHA256:${k.fingerprint}`).join('\n') : 'None');
622635

623-
identityKeys = identityKeys.filter(k => !!registeredSSHKeys.find(regKey => regKey.fingerprint === k.fingerprint));
636+
identityKeys = identityKeys.filter(k => !!registeredKeys.find(regKey => regKey.fingerprint === k.fingerprint));
624637
} else {
625638
if (identityKeys.length) {
626639
sshDestInfo.user = `${workspaceId}#${ownerToken}`;
@@ -811,10 +824,7 @@ export default class RemoteConnector extends Disposable {
811824
const usePublicApi = await this.experiments.getRaw<boolean>('gitpod_experimental_publicApi', session.account.id, { gitpodHost: params.gitpodHost });
812825
this.logger.info(`Going to use ${usePublicApi ? 'public' : 'server'} API`);
813826
if (usePublicApi) {
814-
if (!this.publicApi) {
815-
this.publicApi = new GitpodPublicApi();
816-
}
817-
await this.publicApi.init(session.accessToken, params.gitpodHost);
827+
this.publicApi = new GitpodPublicApi(session.accessToken, params.gitpodHost);
818828
}
819829

820830
const forceUseLocalApp = getServiceURL(params.gitpodHost) === 'https://gitpod.io'
@@ -971,7 +981,7 @@ export default class RemoteConnector extends Disposable {
971981
}
972982
}
973983

974-
private async initializeRemoteExtensions(flow: UserFlowTelemetry & { quiet: boolean, flowId: string }) {
984+
private async initializeRemoteExtensions(flow: UserFlowTelemetry & { quiet: boolean; flowId: string }) {
975985
this.telemetry.sendUserFlowStatus('enabled', flow);
976986
let syncData: { ref: string; content: string } | undefined;
977987
try {
@@ -1132,7 +1142,7 @@ export default class RemoteConnector extends Disposable {
11321142

11331143
const syncExtFlow = { ...connectionInfo, gitpodVersion: gitpodVersion.raw, userId: session.account.id, flow: 'sync_local_extensions' };
11341144
this.initializeRemoteExtensions({ ...syncExtFlow, quiet: true, flowId: uuid() });
1135-
this.context.subscriptions.push(vscode.commands.registerCommand("gitpod.installLocalExtensions", () => {
1145+
this.context.subscriptions.push(vscode.commands.registerCommand('gitpod.installLocalExtensions', () => {
11361146
this.initializeRemoteExtensions({ ...syncExtFlow, quiet: false, flowId: uuid() });
11371147
}));
11381148

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@
9393
google-protobuf "^3.19.1"
9494

9595
"@gitpod/public-api@main":
96-
version "0.1.5-main.5286"
97-
resolved "https://registry.yarnpkg.com/@gitpod/public-api/-/public-api-0.1.5-main.5286.tgz#ddb9f70c1c35fdf2d38826308d5c258a4dfffd3b"
98-
integrity sha512-E0EX+lh+QEq9/WKBb3EemKjNDoIYdzK3XBbjU/9ajWxyXfh7Knls7ysk5dS9mf0r/HBIzI40QDLl8lvS3TX5lQ==
96+
version "0.1.5-main.5676"
97+
resolved "https://registry.yarnpkg.com/@gitpod/public-api/-/public-api-0.1.5-main.5676.tgz#cfccc790db02a4a4becd1bb6018a3c9c03c33466"
98+
integrity sha512-6uYkt7rmTokERcwwPK8kA2Peq/GaEhPiSC9GF5GJhP+4Fxvdkzjx/1a7nTP9nLN7Kjilj2z77VrcrECAMPKt8A==
9999
dependencies:
100100
"@bufbuild/connect-web" "^0.2.1"
101101
"@bufbuild/protobuf" "^0.1.1"

0 commit comments

Comments
 (0)