Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
fix: Auth: clear TLS related data when TLS setting is toggled off (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
cletter7 authored Jun 12, 2024
1 parent dd57d94 commit 68b129f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
44 changes: 42 additions & 2 deletions src/ConfigEditor/Auth/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('utils', () => {
});
});

it('should call `selfSignedCertificate.onToggle` correctly', () => {
it('should call `selfSignedCertificate.onToggle` correctly when toggled on', () => {
const tls = getTLSProps(config, onChange);

tls?.selfSignedCertificate.onToggle(true);
Expand All @@ -330,6 +330,26 @@ describe('utils', () => {
});
});

it('should call `selfSignedCertificate.onToggle` correctly when toggled off', () => {
const testConfig: Config = {
...config,
jsonData: { ...config.jsonData, tlsAuthWithCACert: true },
secureJsonData: { ...config.secureJsonData, tlsCACert: 'cert' },
secureJsonFields: { ...config.secureJsonFields, tlsCACert: true },
};
const tls = getTLSProps(testConfig, onChange);

tls?.selfSignedCertificate.onToggle(false);

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
...testConfig,
jsonData: { ...testConfig.jsonData, tlsAuthWithCACert: false },
secureJsonData: { ...testConfig.secureJsonData, tlsCACert: '' },
secureJsonFields: { ...testConfig.secureJsonFields, tlsCACert: false },
});
});

it('should call `selfSignedCertificate.onCertificateChange` correctly', () => {
const tls = getTLSProps(config, onChange);

Expand Down Expand Up @@ -358,7 +378,7 @@ describe('utils', () => {
});
});

it('should call `TLSClientAuth.onToggle` correctly', () => {
it('should call `TLSClientAuth.onToggle` correctly when toggled on', () => {
const tls = getTLSProps(config, onChange);

tls?.TLSClientAuth.onToggle(true);
Expand All @@ -370,6 +390,26 @@ describe('utils', () => {
});
});

it('should call `TLSClientAuth.onToggle` correctly when toggled off', () => {
const testConfig: Config = {
...config,
jsonData: { ...config.jsonData, tlsAuth: true, serverName: 'test.server.name' },
secureJsonData: { ...config.secureJsonData, tlsClientCert: 'cert', tlsClientKey: 'key' },
secureJsonFields: { ...config.secureJsonFields, tlsClientCert: true, tlsClientKey: true },
};
const tls = getTLSProps(testConfig, onChange);

tls?.TLSClientAuth.onToggle(false);

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
...testConfig,
jsonData: { ...testConfig.jsonData, tlsAuth: false, serverName: '' },
secureJsonData: { ...testConfig.secureJsonData, tlsClientCert: '', tlsClientKey: '' },
secureJsonFields: { ...testConfig.secureJsonFields, tlsClientCert: false, tlsClientKey: false },
});
});

it('should call `TLSClientAuth.onServerNameChange` correctly', () => {
const tls = getTLSProps(config, onChange);

Expand Down
30 changes: 22 additions & 8 deletions src/ConfigEditor/Auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ export function getTLSProps<C extends Config = Config>(config: C, onChange: OnCh
enabled: Boolean(config.jsonData.tlsAuthWithCACert),
certificateConfigured: config.secureJsonFields.tlsCACert,
onToggle: (enabled) =>
onChange({
...config,
jsonData: { ...config.jsonData, tlsAuthWithCACert: enabled },
}),
enabled
? onChange({
...config,
jsonData: { ...config.jsonData, tlsAuthWithCACert: enabled },
})
: onChange({
...config,
jsonData: { ...config.jsonData, tlsAuthWithCACert: enabled },
secureJsonData: { ...config.secureJsonData, tlsCACert: '' },
secureJsonFields: { ...config.secureJsonFields, tlsCACert: false },
}),
onCertificateChange: (certificate) =>
onChange({
...config,
Expand All @@ -110,10 +117,17 @@ export function getTLSProps<C extends Config = Config>(config: C, onChange: OnCh
clientCertificateConfigured: config.secureJsonFields.tlsClientCert,
clientKeyConfigured: config.secureJsonFields.tlsClientKey,
onToggle: (enabled) =>
onChange({
...config,
jsonData: { ...config.jsonData, tlsAuth: enabled },
}),
enabled
? onChange({
...config,
jsonData: { ...config.jsonData, tlsAuth: enabled },
})
: onChange({
...config,
jsonData: { ...config.jsonData, tlsAuth: enabled, serverName: '' },
secureJsonData: { ...config.secureJsonData, tlsClientCert: '', tlsClientKey: '' },
secureJsonFields: { ...config.secureJsonFields, tlsClientCert: false, tlsClientKey: false },
}),
onServerNameChange: (serverName) =>
onChange({
...config,
Expand Down

0 comments on commit 68b129f

Please sign in to comment.