Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v15] Clean up TypeScript types related to tsh daemon #40153

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions web/packages/teleterm/src/mainProcess/mainProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
shell,
} from 'electron';
import { ChannelCredentials } from '@grpc/grpc-js';
import { GrpcTransport } from '@protobuf-ts/grpc-transport';

import { FileStorage, RuntimeSettings } from 'teleterm/types';
import { subscribeToFileStorageEvents } from 'teleterm/services/fileStorage';
Expand All @@ -48,8 +49,8 @@ import { getAssetPath } from 'teleterm/mainProcess/runtimeSettings';
import { RootClusterUri } from 'teleterm/ui/uri';
import Logger from 'teleterm/logger';
import * as grpcCreds from 'teleterm/services/grpcCredentials';
import { createTshdClient } from 'teleterm/services/tshd/createClient';
import { TshdClient } from 'teleterm/services/tshd/types';
import { createTshdClient, TshdClient } from 'teleterm/services/tshd';
import { loggingInterceptor } from 'teleterm/services/tshd/interceptors';

import {
ConfigService,
Expand Down Expand Up @@ -629,7 +630,12 @@ async function setUpTshdClient({
tshdAddress: string;
}): Promise<TshdClient> {
const creds = await createGrpcCredentials(runtimeSettings);
return createTshdClient(tshdAddress, creds);
const transport = new GrpcTransport({
host: tshdAddress,
channelCredentials: creds,
interceptors: [loggingInterceptor(new Logger('tshd'))],
});
return createTshdClient(transport);
}

async function createGrpcCredentials(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ipcMain } from 'electron';
import { isAbortError } from 'shared/utils/abortError';

import { proxyHostToBrowserProxyHost } from 'teleterm/services/tshd/cluster';
import { TshdClient } from 'teleterm/services/tshd/types';
import { TshdClient } from 'teleterm/services/tshd';
import { Logger } from 'teleterm/types';
import { MainProcessIpc } from 'teleterm/mainProcess/types';
import * as tshd from 'teleterm/services/tshd/types';
Expand Down
9 changes: 8 additions & 1 deletion web/packages/teleterm/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import { contextBridge } from 'electron';
import { ChannelCredentials, ServerCredentials } from '@grpc/grpc-js';
import { GrpcTransport } from '@protobuf-ts/grpc-transport';

import { createTshdClient } from 'teleterm/services/tshd/createClient';
import { loggingInterceptor } from 'teleterm/services/tshd/interceptors';
import createMainProcessClient from 'teleterm/mainProcess/mainProcessClient';
import { createFileLoggerService } from 'teleterm/services/logger';
import Logger from 'teleterm/logger';
Expand Down Expand Up @@ -60,7 +62,12 @@ async function getElectronGlobals(): Promise<ElectronGlobals> {
mainProcessClient.getResolvedChildProcessAddresses(),
createGrpcCredentials(runtimeSettings),
]);
const tshClient = createTshdClient(addresses.tsh, credentials.tshd);
const tshdTransport = new GrpcTransport({
host: addresses.tsh,
channelCredentials: credentials.tshd,
interceptors: [loggingInterceptor(new Logger('tshd'))],
});
const tshClient = createTshdClient(tshdTransport);
const ptyServiceClient = createPtyService(
addresses.shared,
credentials.shared,
Expand Down
25 changes: 9 additions & 16 deletions web/packages/teleterm/src/services/tshd/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import grpc from '@grpc/grpc-js';
import { GrpcTransport } from '@protobuf-ts/grpc-transport';
import { TerminalServiceClient } from 'gen-proto-ts/teleport/lib/teleterm/v1/service_pb.client';
import {
ITerminalServiceClient,
TerminalServiceClient,
} from 'gen-proto-ts/teleport/lib/teleterm/v1/service_pb.client';

import Logger from 'teleterm/logger';
import { CloneableClient, cloneClient } from './cloneableClient';

import { cloneClient } from './cloneableClient';
import * as types from './types';
import { loggingInterceptor } from './interceptors';
// Creating the client type based on the interface (ITerminalServiceClient) and not the class
// (TerminalServiceClient) lets us omit a bunch of properties when mocking a client.
export type TshdClient = CloneableClient<ITerminalServiceClient>;

export function createTshdClient(
addr: string,
credentials: grpc.ChannelCredentials
): types.TshdClient {
const logger = new Logger('tshd');
const transport = new GrpcTransport({
host: addr,
channelCredentials: credentials,
interceptors: [loggingInterceptor(logger)],
});
export function createTshdClient(transport: GrpcTransport): TshdClient {
return cloneClient(new TerminalServiceClient(transport));
}
6 changes: 3 additions & 3 deletions web/packages/teleterm/src/services/tshd/fixtures/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
makeRootCluster,
makeAppGateway,
} from 'teleterm/services/tshd/testHelpers';
import { MockedUnaryCall } from 'teleterm/services/tshd/cloneableClient';

import * as types from '../types';
import { TshdClient } from '../createClient';
import { MockedUnaryCall } from '../cloneableClient';

export class MockTshClient implements types.TshdClient {
export class MockTshClient implements TshdClient {
listRootClusters = () => new MockedUnaryCall({ clusters: [] });
listLeafClusters = () => new MockedUnaryCall({ clusters: [] });
getKubes = () =>
Expand Down
21 changes: 21 additions & 0 deletions web/packages/teleterm/src/services/tshd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export * from './createClient';
export { cloneAbortSignal, isTshdRpcError } from './cloneableClient';
export type { CloneableAbortSignal } from './cloneableClient';
33 changes: 30 additions & 3 deletions web/packages/teleterm/src/services/tshd/interceptors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { UnaryCall, MethodInfo } from '@protobuf-ts/runtime-rpc';
import { UnaryCall, MethodInfo, ServiceInfo } from '@protobuf-ts/runtime-rpc';

import Logger from 'teleterm/logger';

Expand All @@ -35,7 +35,10 @@ it('do not log sensitive info like password', () => {

interceptor.interceptUnary(
() => ({ then: () => Promise.resolve({ response: '' }) }) as UnaryCall,
{ name: 'LogIn' } as MethodInfo,
{
name: 'LogIn',
service: { typeName: 'FooService' } as ServiceInfo,
} as MethodInfo,
{
passw: {},
userData: {
Expand All @@ -46,8 +49,32 @@ it('do not log sensitive info like password', () => {
{}
);

expect(infoLogger).toHaveBeenCalledWith('LogIn request:', {
expect(infoLogger).toHaveBeenCalledWith(expect.any(String), {
passw: '~FILTERED~',
userData: { login: 'admin', password: '~FILTERED~' },
});
});

it('includes service and method name', () => {
const infoLogger = jest.fn();
Logger.init({
createLogger: () => ({
info: infoLogger,
error: () => {},
warn: () => {},
}),
});
const interceptor = loggingInterceptor(new Logger());

interceptor.interceptUnary(
() => ({ then: () => Promise.resolve({ response: '' }) }) as UnaryCall,
{
name: 'Foo',
service: { typeName: 'FooService' } as ServiceInfo,
} as MethodInfo,
{},
{}
);

expect(infoLogger).toHaveBeenCalledWith('send FooService Foo', {});
});
22 changes: 13 additions & 9 deletions web/packages/teleterm/src/services/tshd/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { RpcInterceptor } from '@protobuf-ts/runtime-rpc';
import { MethodInfo, RpcInterceptor } from '@protobuf-ts/runtime-rpc';

import { isObject } from 'shared/utils/highbar';

Expand All @@ -30,7 +30,7 @@ export function loggingInterceptor(logger: Logger): RpcInterceptor {
const output = next(method, input, options);
const { logRequest, logResponse, logError } = makeMethodLogger(
logger,
method.name
method
);

logRequest(input);
Expand All @@ -44,7 +44,7 @@ export function loggingInterceptor(logger: Logger): RpcInterceptor {
const output = next(method, options);
const { logRequest, logResponse, logError } = makeMethodLogger(
logger,
method.name
method
);

const originalSend = output.requests.send.bind(output.requests);
Expand All @@ -62,7 +62,7 @@ export function loggingInterceptor(logger: Logger): RpcInterceptor {
const output = next(method, input, options);
const { logRequest, logResponse, logError } = makeMethodLogger(
logger,
method.name
method
);

logRequest(input);
Expand All @@ -81,7 +81,7 @@ export function loggingInterceptor(logger: Logger): RpcInterceptor {
const output = next(method, options);
const { logRequest, logResponse, logError } = makeMethodLogger(
logger,
method.name
method
);

const originalSend = output.requests.send.bind(output.requests);
Expand Down Expand Up @@ -127,17 +127,21 @@ export function filterSensitiveProperties(toFilter: object): object {
return acc;
}

function makeMethodLogger(logger: Logger, methodName: string) {
function makeMethodLogger(logger: Logger, method: MethodInfo<object, object>) {
// Service name and method name are separated with a space and not a slash on purpose.
// This way the Chromium console can break down the log message more easily on narrower widths.
const methodDesc = `${method.service.typeName} ${method.name}`;

return {
logRequest: (input: object) => {
logger.info(`${methodName} request:`, filterSensitiveProperties(input));
logger.info(`send ${methodDesc}`, filterSensitiveProperties(input));
},
logResponse: (output: object) => {
const toLog = output ? filterSensitiveProperties(output) : null;
logger.info(`${methodName} response:`, toLog);
logger.info(`receive ${methodDesc}`, toLog);
},
logError: (error: unknown) => {
logger.error(`${methodName} response:`, `${error}`);
logger.error(`receive ${methodDesc}`, `${error}`);
},
};
}
9 changes: 8 additions & 1 deletion web/packages/teleterm/src/services/tshd/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { GrpcTransport } from '@protobuf-ts/grpc-transport';

import { createInsecureClientCredentials } from 'teleterm/services/grpcCredentials';

import { createTshdClient } from './createClient';
Expand All @@ -24,6 +26,11 @@ import { createTshdClient } from './createClient';
// Dependencies must be provided as `--path` values to `buf generate` in build.assets/genproto.sh.
test('generated protos import necessary dependencies', () => {
expect(() => {
createTshdClient('localhost:0', createInsecureClientCredentials());
createTshdClient(
new GrpcTransport({
host: 'localhost:1337',
channelCredentials: createInsecureClientCredentials(),
})
);
}).not.toThrow();
});
Loading
Loading