Skip to content

Commit 93f6efc

Browse files
authored
chore(connection-form): add show options for connection form in vscode support VSCODE-483 (#5176)
1 parent d51a2c0 commit 93f6efc

File tree

5 files changed

+135
-21
lines changed

5 files changed

+135
-21
lines changed

packages/connection-form/src/components/advanced-options-tabs/advanced-options-tabs.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect } from 'chai';
44
import sinon from 'sinon';
55

66
import AdvancedOptionsTabs from './advanced-options-tabs';
7+
import { ConnectionFormPreferencesContext } from '../../hooks/use-connect-form-preferences';
78

89
const testUrl = 'mongodb+srv://0ranges:p!neapp1es@localhost/?ssl=true';
910

@@ -15,6 +16,7 @@ const tabs = [
1516
},
1617
{ name: 'TLS/SSL', id: 'tls' },
1718
{ name: 'Proxy/SSH', id: 'proxy' },
19+
{ name: 'In-Use Encryption', id: 'csfle' },
1820
{ name: 'Advanced', id: 'advanced' },
1921
];
2022

@@ -105,4 +107,22 @@ describe('AdvancedOptionsTabs Component', function () {
105107
screen.getAllByTestId('connection-tls-tab')[0].getAttribute('aria-label')
106108
).to.equal('TLS/SSL (2 errors)');
107109
});
110+
111+
it('should not render CSFLE when its set to false in the preferences', function () {
112+
render(
113+
<ConnectionFormPreferencesContext.Provider value={{ showCSFLE: false }}>
114+
<AdvancedOptionsTabs
115+
connectionOptions={{
116+
connectionString: testUrl,
117+
}}
118+
errors={[]}
119+
updateConnectionFormField={updateConnectionFormFieldSpy}
120+
/>
121+
</ConnectionFormPreferencesContext.Provider>
122+
);
123+
124+
const csfleTabName = tabs.find((tab) => tab.id === 'csfle')?.name;
125+
expect(csfleTabName).to.not.be.undefined;
126+
expect(screen.queryByText(csfleTabName as string)).to.not.exist;
127+
});
108128
});

packages/connection-form/src/components/advanced-options-tabs/advanced-options-tabs.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { UpdateConnectionFormField } from '../../hooks/use-connect-form';
2020
import type { ConnectionFormError, TabId } from '../../utils/validation';
2121
import { errorsByFieldTab } from '../../utils/validation';
2222
import { defaultConnectionString } from '../../constants/default-connection';
23+
import { useConnectionFormPreference } from '../../hooks/use-connect-form-preferences';
2324

2425
const tabsStyles = css({
2526
marginTop: spacing[2],
@@ -60,6 +61,7 @@ function AdvancedOptionsTabs({
6061
connectionOptions: ConnectionOptions;
6162
}): React.ReactElement {
6263
const [activeTab, setActiveTab] = useState(0);
64+
const showCSFLE = useConnectionFormPreference('showCSFLE');
6365

6466
const tabs: TabObject[] = [
6567
{ name: 'General', id: 'general', component: GeneralTab },
@@ -70,11 +72,15 @@ function AdvancedOptionsTabs({
7072
},
7173
{ name: 'TLS/SSL', id: 'tls', component: TLSTab },
7274
{ name: 'Proxy/SSH', id: 'proxy', component: ProxyAndSshTunnelTab },
73-
{
74-
name: 'In-Use Encryption',
75-
id: 'csfle',
76-
component: CSFLETab,
77-
},
75+
...(showCSFLE
76+
? [
77+
{
78+
name: 'In-Use Encryption',
79+
id: 'csfle',
80+
component: CSFLETab,
81+
} as const,
82+
]
83+
: []),
7884
{ name: 'Advanced', id: 'advanced', component: AdvancedTab },
7985
];
8086

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-tab.spec.tsx

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,86 @@ import { render, screen, fireEvent } from '@testing-library/react';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
55
import ConnectionStringUrl from 'mongodb-connection-string-url';
6+
import { AuthMechanism } from 'mongodb';
67

78
import AuthenticationTab from './authentication-tab';
89
import type { ConnectionFormError } from '../../../utils/validation';
910
import type { UpdateConnectionFormField } from '../../../hooks/use-connect-form';
11+
import { ConnectionFormPreferencesContext } from '../../../hooks/use-connect-form-preferences';
12+
import type { ConnectionFormPreferences } from '../../../hooks/use-connect-form-preferences';
1013

1114
function renderComponent({
1215
errors = [],
1316
connectionStringUrl = new ConnectionStringUrl('mongodb://localhost:27017'),
17+
connectionFormPreferences = {
18+
enableOidc: true,
19+
},
1420
updateConnectionFormField,
1521
}: {
1622
connectionStringUrl?: ConnectionStringUrl;
23+
connectionFormPreferences?: Partial<ConnectionFormPreferences>;
1724
errors?: ConnectionFormError[];
1825
updateConnectionFormField: UpdateConnectionFormField;
1926
}) {
2027
render(
21-
<AuthenticationTab
22-
errors={errors}
23-
connectionStringUrl={connectionStringUrl}
24-
updateConnectionFormField={updateConnectionFormField}
25-
connectionOptions={{
26-
connectionString: 'mongodb://localhost:27017',
27-
}}
28-
/>
28+
<ConnectionFormPreferencesContext.Provider
29+
value={connectionFormPreferences}
30+
>
31+
<AuthenticationTab
32+
errors={errors}
33+
connectionStringUrl={connectionStringUrl}
34+
updateConnectionFormField={updateConnectionFormField}
35+
connectionOptions={{
36+
connectionString: 'mongodb://localhost:27017',
37+
}}
38+
/>
39+
</ConnectionFormPreferencesContext.Provider>
2940
);
3041
}
3142

43+
const authMechanisms = [
44+
{
45+
title: 'Username/Password',
46+
id: AuthMechanism.MONGODB_DEFAULT,
47+
},
48+
{
49+
title: 'OIDC (Preview)',
50+
id: AuthMechanism.MONGODB_OIDC,
51+
},
52+
{
53+
title: 'X.509',
54+
id: AuthMechanism.MONGODB_X509,
55+
},
56+
{
57+
title: 'Kerberos',
58+
id: AuthMechanism.MONGODB_GSSAPI,
59+
},
60+
{
61+
title: 'LDAP',
62+
id: AuthMechanism.MONGODB_PLAIN,
63+
},
64+
{
65+
title: 'AWS IAM',
66+
id: AuthMechanism.MONGODB_AWS,
67+
},
68+
];
69+
3270
describe('AuthenticationTab Component', function () {
3371
let updateConnectionFormFieldSpy: sinon.SinonSpy;
3472
beforeEach(function () {
3573
updateConnectionFormFieldSpy = sinon.spy();
3674
});
3775

76+
it('renders all of the auth mechanisms', function () {
77+
renderComponent({
78+
updateConnectionFormField: updateConnectionFormFieldSpy,
79+
});
80+
81+
authMechanisms.forEach((mechanism) => {
82+
expect(screen.getByText(mechanism.title)).to.be.visible;
83+
});
84+
});
85+
3886
describe('when a new auth mechanism is clicked', function () {
3987
beforeEach(function () {
4088
renderComponent({
@@ -66,7 +114,6 @@ describe('AuthenticationTab Component', function () {
66114

67115
it('renders the username/password tab when auth is set', function () {
68116
renderComponent({
69-
errors: [],
70117
connectionStringUrl: new ConnectionStringUrl(
71118
'mongodb://a123:b123@localhost'
72119
),
@@ -79,7 +126,6 @@ describe('AuthenticationTab Component', function () {
79126

80127
it('renders the username/password tab when only password is set', function () {
81128
renderComponent({
82-
errors: [],
83129
connectionStringUrl: new ConnectionStringUrl(
84130
'mongodb://:b123@localhost',
85131
{ looseValidation: true }
@@ -90,4 +136,30 @@ describe('AuthenticationTab Component', function () {
90136
expect(screen.getByLabelText('Username')).to.be.visible;
91137
expect(screen.getByLabelText('Password')).to.be.visible;
92138
});
139+
140+
it('should not render OIDC auth when its set to false in the preferences', function () {
141+
renderComponent({
142+
connectionFormPreferences: { showOIDCAuth: false },
143+
updateConnectionFormField: updateConnectionFormFieldSpy,
144+
});
145+
146+
const oidcAuthName = authMechanisms.find(
147+
(tab) => tab.id === 'MONGODB-OIDC'
148+
)?.title;
149+
expect(oidcAuthName).to.not.be.undefined;
150+
expect(screen.queryByText(oidcAuthName as string)).to.not.exist;
151+
});
152+
153+
it('should not render Kerberos auth when its set to false in the preferences', function () {
154+
renderComponent({
155+
connectionFormPreferences: { showKerberosAuth: false },
156+
updateConnectionFormField: updateConnectionFormFieldSpy,
157+
});
158+
159+
const kerberosAuthName = authMechanisms.find(
160+
(tab) => tab.id === 'GSSAPI'
161+
)?.title;
162+
expect(kerberosAuthName).to.not.be.undefined;
163+
expect(screen.queryByText(kerberosAuthName as string)).to.not.exist;
164+
});
93165
});

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-tab.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,22 @@ function AuthenticationTab({
108108
updateConnectionFormField: UpdateConnectionFormField;
109109
connectionOptions: ConnectionOptions;
110110
}): React.ReactElement {
111+
// enableOIDC is the feature flag, showOIDC is the connection form preference.
111112
const enableOIDC = !!useConnectionFormPreference('enableOidc');
112-
const enabledAuthOptions = useMemo(() => {
113-
if (enableOIDC) {
114-
return options;
115-
}
116-
return options.filter((option) => option.id !== 'MONGODB-OIDC');
117-
}, [enableOIDC]);
113+
const showOIDC = useConnectionFormPreference('showOIDCAuth');
114+
const showKerberos = useConnectionFormPreference('showKerberosAuth');
115+
const enabledAuthOptions = useMemo(
116+
() =>
117+
options.filter((option) => {
118+
if (option.id === 'MONGODB-OIDC') {
119+
return enableOIDC && showOIDC;
120+
} else if (option.id === 'GSSAPI') {
121+
return showKerberos;
122+
}
123+
return true;
124+
}),
125+
[enableOIDC, showKerberos, showOIDC]
126+
);
118127

119128
const selectedAuthTabId =
120129
getSelectedAuthTabForConnectionString(connectionStringUrl);

packages/connection-form/src/hooks/use-connect-form-preferences.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createContext, useContext } from 'react';
22

3+
// Not all of these preference map to Compass preferences.
34
export type ConnectionFormPreferences = {
45
protectConnectionStrings: boolean;
56
forceConnectionOptions: [key: string, value: string][];
@@ -8,6 +9,9 @@ export type ConnectionFormPreferences = {
89
enableOidc: boolean;
910
enableDebugUseCsfleSchemaMap: boolean;
1011
protectConnectionStringsForNewConnections: boolean;
12+
showOIDCAuth: boolean;
13+
showKerberosAuth: boolean;
14+
showCSFLE: boolean;
1115
};
1216

1317
const defaultPreferences = {
@@ -18,6 +22,9 @@ const defaultPreferences = {
1822
enableOidc: false,
1923
enableDebugUseCsfleSchemaMap: false,
2024
protectConnectionStringsForNewConnections: false,
25+
showOIDCAuth: true,
26+
showKerberosAuth: true,
27+
showCSFLE: true,
2128
};
2229

2330
export const ConnectionFormPreferencesContext = createContext<

0 commit comments

Comments
 (0)