Skip to content

Commit

Permalink
chore(compass-web): add connections storage implementation for compas…
Browse files Browse the repository at this point in the history
…s-web COMPASS-7917 (#5863)

* chore(atlas-service): cleanup backend presets and add new values

* chore(compass-web): implement connections-storage for cloud environment

* chore(compass-web): emit connections-changed after polling is finished

* chore(compass-web): remove only from test

* chore(web): clearer code for non-atlas connection in the sandbox
  • Loading branch information
gribnoysup authored Jun 5, 2024
1 parent 8057162 commit 9661413
Show file tree
Hide file tree
Showing 16 changed files with 688 additions and 173 deletions.
5 changes: 3 additions & 2 deletions packages/atlas-service/src/atlas-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { createNoopLoggerAndTelemetry } from '@mongodb-js/compass-logging/provid
import { CompassAtlasAuthService } from './compass-atlas-auth-service';

const ATLAS_CONFIG = {
atlasApiBaseUrl: 'http://example.com/api/private',
atlasApiUnauthBaseUrl: 'http://api.example.com',
wsBaseUrl: 'ws://example.com',
cloudBaseUrl: 'ws://example.com/cloud',
atlasApiBaseUrl: 'http://example.com/api',
atlasLogin: {
clientId: 'some-client-id',
issuer: 'http://example.com/oauth2/default',
Expand Down
20 changes: 13 additions & 7 deletions packages/atlas-service/src/atlas-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ export class AtlasService {
) {
this.config = getAtlasConfig(preferences);
}
privateUnAuthEndpoint(path: string): string {
return `${this.config.atlasApiUnauthBaseUrl}/${path}`;
adminApiEndpoint(path?: string, requestId?: string): string {
const uri = encodeURI(
`${this.config.atlasApiBaseUrl}${path ? `/${path}` : ''}`
);
const query = requestId
? `?request_id=${encodeURIComponent(requestId)}`
: '';
return `${uri}${query}`;
}
cloudEndpoint(path?: string): string {
return encodeURI(`${this.config.cloudBaseUrl}${path ? `/${path}` : ''}`);
}
privateAtlasEndpoint(path: string, requestId?: string): string {
return `${this.config.atlasApiBaseUrl}/${path}${
requestId ? `?request_id=${requestId}` : ''
}`;
driverProxyEndpoint(path?: string): string {
return encodeURI(`${this.config.wsBaseUrl}${path ? `/${path}` : ''}`);
}
async fetch(url: RequestInfo, init?: RequestInit): Promise<Response> {
throwIfNetworkTrafficDisabled(this.preferences);
Expand Down Expand Up @@ -75,7 +82,6 @@ export class AtlasService {
throw err;
}
}

async authenticatedFetch(
url: RequestInfo,
init?: RequestInit
Expand Down
5 changes: 3 additions & 2 deletions packages/atlas-service/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ describe('CompassAuthServiceMain', function () {
};

const defaultConfig = {
atlasApiBaseUrl: 'http://example.com',
atlasApiUnauthBaseUrl: 'http://example.com/unauth',
wsBaseUrl: 'ws://example.com',
cloudBaseUrl: 'ws://example.com/cloud',
atlasApiBaseUrl: 'http://example.com/api',
atlasLogin: {
issuer: 'http://example.com',
clientId: '1234abcd',
Expand Down
80 changes: 55 additions & 25 deletions packages/atlas-service/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,63 +78,95 @@ export async function throwIfNotOk(
}

export type AtlasServiceConfig = {
/**
* MongoDB Driver WebSocket proxy base url
*/
wsBaseUrl: string;
/**
* Cloud UI backend base url
*/
cloudBaseUrl: string;
/**
* Atlas admin API base url
*/
atlasApiBaseUrl: string;
atlasApiUnauthBaseUrl: string;
/**
* Atlas OIDC config
*/
atlasLogin: {
clientId: string;
issuer: string;
};
/**
* Atlas Account Portal UI base url
*/
authPortalUrl: string;
};

/**
* Atlas service backend configurations.
* - compass-dev: locally running compass kanopy backend (localhost)
* - compass: compass kanopy backend (compass.mongodb.com)
* - atlas-local: local mms backend (localhost)
* - atlas-dev: dev mms backend (cloud-dev.mongodb.com)
* - atlas: mms backend (cloud.mongodb.com)
* - atlas-local: local mms backend (localhost)
* - atlas-dev: dev mms backend (cloud-dev.mongodb.com)
* - atlas: mms backend (cloud.mongodb.com)
* - web-sandbox-atlas-local: local mms backend + proxy (localhost / proxy prefix)
* - web-sandbox-atlas-dev: dev mms backend + proxy (cloud-dev.mongodb.com / proxy prefix)
* - web-sandbox-atlas: mms backend + proxy (cloud.mongodb.com / proxy prefix)
*/
const config = {
'compass-dev': {
atlasApiBaseUrl: 'http://localhost:8080',
atlasApiUnauthBaseUrl: 'http://localhost:8080',
'atlas-local': {
wsBaseUrl: 'ws://localhost:61001/ws',
cloudBaseUrl: '',
atlasApiBaseUrl: 'http://localhost:8080/api/private',
atlasLogin: {
clientId: '0oajzdcznmE8GEyio297',
issuer: 'https://auth.mongodb.com/oauth2/default',
clientId: '0oaq1le5jlzxCuTbu357',
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account.mongodb.com/account/login',
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
},
compass: {
atlasApiBaseUrl: 'https://compass.mongodb.com',
atlasApiUnauthBaseUrl: 'https://compass.mongodb.com',
'atlas-dev': {
wsBaseUrl: '',
cloudBaseUrl: '',
atlasApiBaseUrl: 'https://cloud-dev.mongodb.com/api/private',
atlasLogin: {
clientId: '0oaq1le5jlzxCuTbu357',
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
},
atlas: {
wsBaseUrl: '',
cloudBaseUrl: '',
atlasApiBaseUrl: 'https://cloud.mongodb.com/api/private',
atlasLogin: {
clientId: '0oajzdcznmE8GEyio297',
issuer: 'https://auth.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account.mongodb.com/account/login',
},
'atlas-local': {
'web-sandbox-atlas-local': {
wsBaseUrl: 'ws://localhost:1337',
cloudBaseUrl: '/cloud-mongodb-com',
atlasApiBaseUrl: 'http://localhost:8080/api/private',
atlasApiUnauthBaseUrl: 'http://localhost:8080/api/private/unauth',
atlasLogin: {
clientId: '0oaq1le5jlzxCuTbu357',
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
},
'atlas-dev': {
'web-sandbox-atlas-dev': {
wsBaseUrl: 'ws://localhost:1337',
cloudBaseUrl: '/cloud-mongodb-com',
atlasApiBaseUrl: 'https://cloud-dev.mongodb.com/api/private',
atlasApiUnauthBaseUrl: 'https://cloud-dev.mongodb.com/api/private/unauth',
atlasLogin: {
clientId: '0oaq1le5jlzxCuTbu357',
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
},
atlas: {
'web-sandbox-atlas': {
wsBaseUrl: 'ws://localhost:1337',
cloudBaseUrl: '/cloud-mongodb-com',
atlasApiBaseUrl: 'https://cloud.mongodb.com/api/private',
atlasApiUnauthBaseUrl: 'https://cloud.mongodb.com/api/private/unauth',
atlasLogin: {
clientId: '0oajzdcznmE8GEyio297',
issuer: 'https://auth.mongodb.com/oauth2/default',
Expand All @@ -148,9 +180,7 @@ export function getAtlasConfig(
) {
const { atlasServiceBackendPreset } = preferences.getPreferences();
const envConfig = {
atlasApiBaseUrl: process.env.COMPASS_ATLAS_SERVICE_BASE_URL_OVERRIDE,
atlasApiUnauthBaseUrl:
process.env.COMPASS_ATLAS_SERVICE_UNAUTH_BASE_URL_OVERRIDE,
atlasApiBaseUrl: process.env.COMPASS_ATLAS_SERVICE_UNAUTH_BASE_URL_OVERRIDE,
atlasLogin: {
clientId: process.env.COMPASS_CLIENT_ID_OVERRIDE,
issuer: process.env.COMPASS_OIDC_ISSUER_OVERRIDE,
Expand All @@ -160,7 +190,7 @@ export function getAtlasConfig(
return defaultsDeep(
envConfig,
config[atlasServiceBackendPreset]
) as typeof envConfig & typeof config[keyof typeof config];
) as AtlasServiceConfig;
}

export function getTrackingUserInfo(userInfo: AtlasUserInfo) {
Expand Down
3 changes: 1 addition & 2 deletions packages/compass-generative-ai/src/atlas-ai-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class MockAtlasAuthService extends AtlasAuthService {

class MockAtlasService {
getCurrentUser = () => Promise.resolve(ATLAS_USER);
privateUnAuthEndpoint = (url: string) => [BASE_URL, url].join('/');
privateAtlasEndpoint = (url: string, requestId?: string) =>
adminApiEndpoint = (url: string, requestId?: string) =>
`${[BASE_URL, url].join('/')}${
requestId ? `?request_id=${requestId}` : ''
}`;
Expand Down
6 changes: 3 additions & 3 deletions packages/compass-generative-ai/src/atlas-ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type GenerativeAiInput = {
// want to ensure we're not uploading massive documents (some folks have documents > 1mb).
const AI_MAX_REQUEST_SIZE = 100000;
const AI_MIN_SAMPLE_DOCUMENTS = 1;
const USER_AI_URI = (userId: string) => `ai/api/v1/hello/${userId}`;
const USER_AI_URI = (userId: string) => `unauth/ai/api/v1/hello/${userId}`;
const AGGREGATION_URI = 'ai/api/v1/mql-aggregation';
const QUERY_URI = 'ai/api/v1/mql-query';

Expand Down Expand Up @@ -227,7 +227,7 @@ export class AtlasAiService {

private async getAIFeatureEnablement(): Promise<AIFeatureEnablement> {
const userId = this.preferences.getPreferencesUser().id;
const url = this.atlasService.privateUnAuthEndpoint(USER_AI_URI(userId));
const url = this.atlasService.adminApiEndpoint(USER_AI_URI(userId));
const res = await this.atlasService.fetch(url, {
headers: {
Accept: 'application/json',
Expand Down Expand Up @@ -282,7 +282,7 @@ export class AtlasAiService {
const { signal, requestId, ...rest } = input;
const msgBody = buildQueryOrAggregationMessageBody(rest);

const url = this.atlasService.privateAtlasEndpoint(uri, requestId);
const url = this.atlasService.adminApiEndpoint(uri, requestId);

this.logger.log.info(
this.logger.mongoLogId(1_001_000_308),
Expand Down
18 changes: 12 additions & 6 deletions packages/compass-preferences-model/src/preferences-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export type UserConfigurablePreferences = PermanentFeatureFlags &
// except for user preferences doesn't allow required preferences to be
// defined, so we are sticking it here
atlasServiceBackendPreset:
| 'compass-dev'
| 'compass'
| 'atlas-local'
| 'atlas-dev'
| 'atlas';
| 'atlas'
| 'web-sandbox-atlas-local'
| 'web-sandbox-atlas-dev'
| 'web-sandbox-atlas';
// Features that are enabled by default in Compass, but are disabled in Data
// Explorer
enableExplainPlan: boolean;
Expand Down Expand Up @@ -631,8 +632,6 @@ export const storedUserPreferencesProps: Required<{

/**
* Chooses atlas service backend configuration from preset
* - compass-dev: locally running compass kanopy backend (localhost)
* - compass: compass kanopy backend (compass.mongodb.com)
* - atlas-local: local mms backend (http://localhost:8080)
* - atlas-dev: dev mms backend (cloud-dev.mongodb.com)
* - atlas: mms backend (cloud.mongodb.com)
Expand All @@ -645,7 +644,14 @@ export const storedUserPreferencesProps: Required<{
short: 'Configuration used by atlas service',
},
validator: z
.enum(['compass-dev', 'compass', 'atlas-dev', 'atlas-local', 'atlas'])
.enum([
'atlas-dev',
'atlas-local',
'atlas',
'web-sandbox-atlas-dev',
'web-sandbox-atlas-local',
'web-sandbox-atlas',
])
.default('atlas'),
type: 'string',
},
Expand Down
8 changes: 0 additions & 8 deletions packages/compass-web/sandbox/atlas-cluster-connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ export function useAtlasClusterConnectionsList(): AtlasClusterConnectionsListRet
id: cluster.uniqueId,
connectionOptions: {
connectionString: connectionString.toString(),
lookup() {
return {
wsURL: 'ws://localhost:1337',
projectId: projectId,
clusterName: cluster.name,
srvAddress: cluster.srvAddress,
};
},
},
atlasMetadata: {
orgId: '',
Expand Down
Loading

0 comments on commit 9661413

Please sign in to comment.