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

CB-5288 fix: connection secrets cache #2748

Merged
merged 3 commits into from
Jul 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export type ConnectionInitConfig = Omit<
| 'includeCredentialsSaved'
| 'includeProperties'
| 'includeProviderProperties'
| 'includeSharedSecrets'
| 'customIncludeOptions'
>;
export type ConnectionInfoIncludes = Omit<GetUserConnectionsQueryVariables, 'id'>;
Expand Down Expand Up @@ -595,7 +594,6 @@ export class ConnectionInfoResource extends CachedMapResource<IConnectionInfoPar
includeCredentialsSaved: false,
includeProperties: false,
includeProviderProperties: false,
includeSharedSecrets: false,
customIncludeOptions: false,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { toJS } from 'mobx';

import { injectable } from '@cloudbeaver/core-di';
import { CachedMapResource, isResourceAlias, ResourceKey, resourceKeyList, ResourceKeyList, ResourceKeyUtils } from '@cloudbeaver/core-resource';
import { GraphQLService, SecretInfo } from '@cloudbeaver/core-sdk';
import { isDefined, schemaValidationError } from '@cloudbeaver/core-utils';

import { CONNECTION_INFO_PARAM_SCHEMA, IConnectionInfoParams } from './CONNECTION_INFO_PARAM_SCHEMA';
import {
ConnectionInfoActiveProjectKey,
ConnectionInfoProjectKey,
ConnectionInfoResource,
createConnectionParam,
isConnectionInfoParamEqual,
} from './ConnectionInfoResource';

export type PublicSecretInfo = SecretInfo;

@injectable()
export class ConnectionPublicSecretsResource extends CachedMapResource<IConnectionInfoParams, PublicSecretInfo[]> {
constructor(
private readonly graphQLService: GraphQLService,
private connectionInfoResource: ConnectionInfoResource,
) {
super();
this.sync(connectionInfoResource, key => {
const connectionInfoProjectKey = this.aliases.isAlias(key, ConnectionInfoProjectKey);
const connectionInfoActiveProjectKey = this.aliases.isAlias(key, ConnectionInfoActiveProjectKey);

if (connectionInfoProjectKey) {
return resourceKeyList(
connectionInfoResource
.get(connectionInfoProjectKey as any as ResourceKeyList<any>)
?.filter(isDefined)
.map(createConnectionParam),
);
}

if (connectionInfoActiveProjectKey) {
return resourceKeyList(
connectionInfoResource
.get(connectionInfoActiveProjectKey as any as ResourceKeyList<any>)
?.filter(isDefined)
.map(createConnectionParam),
);
}

return key;
});
this.connectionInfoResource.onItemDelete.addHandler(this.delete.bind(this));
}

protected async loader(originalKey: ResourceKey<IConnectionInfoParams>): Promise<Map<IConnectionInfoParams, PublicSecretInfo[]>> {
if (isResourceAlias(originalKey)) {
throw new Error('Aliases are not supported in this resource');
}

await ResourceKeyUtils.forEachAsync(originalKey, async key => {
const { connections } = await this.graphQLService.sdk.getPublicConnectionSecrets({
projectId: key.projectId,
connectionId: key.connectionId,
});

if (connections.length === 0) {
throw new Error('Connection not found');
}
const connection = connections[0];

this.set(key, connection.sharedSecrets);
});

return this.data;
}

isKeyEqual(param: IConnectionInfoParams, second: IConnectionInfoParams): boolean {
return isConnectionInfoParamEqual(param, second);
}

protected validateKey(key: IConnectionInfoParams): boolean {
const parse = CONNECTION_INFO_PARAM_SCHEMA.safeParse(toJS(key));
if (!parse.success) {
this.logger.warn(`Invalid resource key ${schemaValidationError(parse.error, { prefix: null }).toString()}`);
}
return parse.success;
}
}
1 change: 1 addition & 0 deletions webapp/packages/core-connections/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export * from './ContainerResource';
export * from './ConnectionsLocaleService';
export * from './createConnectionFolderParam';
export * from './DatabaseAuthModelsResource';
export * from './ConnectionPublicSecretsResource';
export * from './DatabaseConnection';
export * from './DBDriverResource';
export * from './CONNECTION_INFO_PARAM_SCHEMA';
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-connections/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export const manifest: PluginManifest = {
() => import('./ConnectionInfoEventHandler').then(m => m.ConnectionInfoEventHandler),
() => import('./ConnectionFolderEventHandler').then(m => m.ConnectionFolderEventHandler),
() => import('./ConnectionsSettingsService').then(m => m.ConnectionsSettingsService),
() => import('./ConnectionPublicSecretsResource').then(m => m.ConnectionPublicSecretsResource),
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mutation closeConnection(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: closeConnection(projectId: $projectId, id: $connectionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mutation createConnection(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: createConnection(projectId: $projectId, config: $config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mutation createConnectionFromNode(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: copyConnectionFromNode(projectId: $projectId, nodePath: $nodePath, config: $config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mutation createConnectionFromTemplate(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: createConnectionFromTemplate(projectId: $projectId, templateId: $templateId, connectionName: $connectionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ query getTemplateConnections(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connections: templateConnections(projectId: $projectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ query getUserConnections(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connections: userConnections(projectId: $projectId, id: $connectionId, projectIds: $projectIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mutation initConnection(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: initConnection(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query getPublicConnectionSecrets($projectId: ID!, $connectionId: ID!) {
connections: userConnections(projectId: $projectId, id: $connectionId) {
sharedSecrets {
displayName
secretId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mutation setConnectionNavigatorSettings(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: setConnectionNavigatorSettings(projectId: $projectId, id: $connectionId, settings: $settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mutation updateConnection(
$includeAuthNeeded: Boolean!
$includeProperties: Boolean!
$includeProviderProperties: Boolean!
$includeSharedSecrets: Boolean!
$customIncludeOptions: Boolean!
) {
connection: updateConnection(projectId: $projectId, config: $config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ fragment DatabaseConnection on ConnectionInfo {
...ObjectOriginInfo
}

sharedSecrets @include(if: $includeSharedSecrets) {
displayName
secretId
}
authNeeded @include(if: $includeAuthNeeded)
authModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class TemplateConnectionsResource extends CachedDataResource<Connection[]
includeCredentialsSaved: false,
includeProperties: false,
includeProviderProperties: false,
includeSharedSecrets: false,
customIncludeOptions: false,
});
return connections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { observer } from 'mobx-react-lite';

import { CommonDialogHeader, CommonDialogWrapper, useResource, useTranslate } from '@cloudbeaver/core-blocks';
import { ConnectionInfoResource, DBDriverResource, type IConnectionInfoParams } from '@cloudbeaver/core-connections';
import { ConnectionInfoResource, ConnectionPublicSecretsResource, DBDriverResource, type IConnectionInfoParams } from '@cloudbeaver/core-connections';
import type { DialogComponent } from '@cloudbeaver/core-dialogs';

import { DatabaseCredentialsAuthDialog } from './DatabaseCredentialsAuthDialog/DatabaseCredentialsAuthDialog';
Expand All @@ -23,11 +23,12 @@ interface Payload {
export const DatabaseAuthDialog: DialogComponent<Payload> = observer(function DatabaseAuthDialog({ payload, options, rejectDialog, resolveDialog }) {
const connectionInfoLoader = useResource(DatabaseAuthDialog, ConnectionInfoResource, {
key: payload.connection,
includes: ['includeAuthNeeded', 'includeSharedSecrets', 'includeNetworkHandlersConfig', 'includeCredentialsSaved'],
includes: ['includeAuthNeeded', 'includeNetworkHandlersConfig', 'includeCredentialsSaved'],
});
const translate = useTranslate();
const driverLoader = useResource(DatabaseAuthDialog, DBDriverResource, connectionInfoLoader.data?.driverId || null);
const useSharedCredentials = (connectionInfoLoader.data?.sharedSecrets?.length || 0) > 1;
const connectionPublicSecretsLoader = useResource(DatabaseSecretAuthDialog, ConnectionPublicSecretsResource, payload.connection);
const useSharedCredentials = (connectionPublicSecretsLoader.data?.length || 0) > 1;

let subtitle = connectionInfoLoader.data?.name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
useObservableRef,
useResource,
} from '@cloudbeaver/core-blocks';
import { ConnectionInfoResource, type IConnectionInfoParams } from '@cloudbeaver/core-connections';
import { ConnectionInfoResource, ConnectionPublicSecretsResource, type IConnectionInfoParams } from '@cloudbeaver/core-connections';

interface Props {
connectionKey: IConnectionInfoParams;
Expand All @@ -30,7 +30,7 @@ interface Props {
export const DatabaseSecretAuthDialog = observer<Props>(function DatabaseSecretAuthDialog({ connectionKey, onLogin }) {
const connectionInfoLoader = useResource(DatabaseSecretAuthDialog, ConnectionInfoResource, {
key: connectionKey,
includes: ['includeAuthNeeded', 'includeSharedSecrets', 'includeNetworkHandlersConfig', 'includeCredentialsSaved'],
includes: ['includeAuthNeeded', 'includeNetworkHandlersConfig', 'includeCredentialsSaved'],
});
const state = useObservableRef(
() => ({
Expand All @@ -43,7 +43,8 @@ export const DatabaseSecretAuthDialog = observer<Props>(function DatabaseSecretA
},
false,
);
const secrets = connectionInfoLoader.data?.sharedSecrets || [];
const connectionPublicSecretsLoader = useResource(DatabaseSecretAuthDialog, ConnectionPublicSecretsResource, connectionKey);
const secrets = connectionPublicSecretsLoader.data || [];

async function handleSecretSelect(secretId: string) {
try {
Expand Down
Loading