Skip to content

Commit 46d6e79

Browse files
authored
Merge pull request #1619 from RedisInsight/bugfix/RI-4064-4059-4062-4063_import_database
Bugfix/ri 4064 4059 4062 4063 import database
2 parents f2eafac + 23279fe commit 46d6e79

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

redisinsight/api/src/constants/error-messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default {
2020
INVALID_CA_BODY: 'Invalid CA body',
2121
INVALID_SSH_PRIVATE_KEY_BODY: 'Invalid SSH private key body',
2222
SSH_AGENTS_ARE_NOT_SUPPORTED: 'SSH Agents are not supported',
23+
INVALID_SSH_BODY: 'Invalid SSH body',
2324
INVALID_CERTIFICATE_BODY: 'Invalid certificate body',
2425
INVALID_PRIVATE_KEY: 'Invalid private key',
2526
CERTIFICATE_NAME_IS_NOT_DEFINED: 'Certificate name is not defined',

redisinsight/api/src/modules/database-import/exceptions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './invalid-ca-certificate-body.exception';
22
export * from './invalid-client-certificate-body.exception';
33
export * from './invalid-client-private-key.exception';
44
export * from './invalid-certificate-name.exception';
5+
export * from './invalid-ssh-body.exception';
56
export * from './invalid-ssh-private-key-body.exception';
67
export * from './size-limit-exceeded-database-import-file.exception';
78
export * from './no-database-import-file-provided.exception';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { HttpException } from '@nestjs/common';
2+
import ERROR_MESSAGES from 'src/constants/error-messages';
3+
4+
export class InvalidSshBodyException extends HttpException {
5+
constructor(message: string = ERROR_MESSAGES.INVALID_SSH_BODY) {
6+
const response = {
7+
message,
8+
statusCode: 400,
9+
error: 'Invalid SSH body',
10+
};
11+
12+
super(response, 400);
13+
}
14+
}

redisinsight/api/src/modules/database-import/ssh-import.service.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Test, TestingModule } from '@nestjs/testing';
77
import { SshImportService } from 'src/modules/database-import/ssh-import.service';
88
import {
99
InvalidSshPrivateKeyBodyException,
10+
InvalidSshBodyException,
1011
SshAgentsAreNotSupportedException,
1112
} from 'src/modules/database-import/exceptions';
1213

@@ -105,11 +106,34 @@ describe('SshImportService', () => {
105106
it('should throw an error when ssh agent provided', async () => {
106107
try {
107108
await service.processSshOptions({
109+
...mockSshImportDataPK,
108110
sshAgentPath: '/agent/path',
109111
});
110112
} catch (e) {
111113
expect(e).toBeInstanceOf(SshAgentsAreNotSupportedException);
112114
}
113115
});
114116
});
117+
118+
it('should throw an error when no username defined', async () => {
119+
try {
120+
await service.processSshOptions({
121+
...mockSshImportDataPK,
122+
sshUsername: undefined,
123+
});
124+
} catch (e) {
125+
expect(e).toBeInstanceOf(InvalidSshBodyException);
126+
}
127+
});
128+
129+
it('should throw an error when no port defined', async () => {
130+
try {
131+
await service.processSshOptions({
132+
...mockSshImportDataPK,
133+
sshPassword: undefined,
134+
});
135+
} catch (e) {
136+
expect(e).toBeInstanceOf(InvalidSshBodyException);
137+
}
138+
});
115139
});

redisinsight/api/src/modules/database-import/ssh-import.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Injectable } from '@nestjs/common';
2+
import { isUndefined } from 'lodash';
23
import {
34
getPemBodyFromFileSync,
45
isValidSshPrivateKey,
56
} from 'src/common/utils';
67
import {
7-
InvalidSshPrivateKeyBodyException, SshAgentsAreNotSupportedException
8+
InvalidSshPrivateKeyBodyException, InvalidSshBodyException, SshAgentsAreNotSupportedException,
89
} from 'src/modules/database-import/exceptions';
910
import { SshOptions } from 'src/modules/ssh/models/ssh-options';
1011

@@ -17,12 +18,17 @@ export class SshImportService {
1718
async processSshOptions(data: any): Promise<Partial<SshOptions>> {
1819
let sshOptions: Partial<SshOptions> = {
1920
host: data.sshHost,
20-
port: parseInt(data.sshPort, 10),
21-
username: data.sshUsername,
2221
};
2322

23+
if (isUndefined(data.sshPort) || isUndefined(data.sshUsername)) {
24+
throw new InvalidSshBodyException();
25+
} else {
26+
sshOptions.port = parseInt(data.sshPort, 10);
27+
sshOptions.username = data.sshUsername;
28+
}
29+
2430
if (data.sshPrivateKey) {
25-
sshOptions.passphrase = data.sshPassphrase || data.sshPassword;
31+
sshOptions.passphrase = data.sshPassphrase || data.sshPassword || null;
2632

2733
if (isValidSshPrivateKey(data.sshPrivateKey)) {
2834
sshOptions.privateKey = data.sshPrivateKey;
@@ -35,7 +41,7 @@ export class SshImportService {
3541
}
3642
}
3743
} else {
38-
sshOptions.password = data.sshPassword;
44+
sshOptions.password = data.sshPassword || null;
3945
}
4046

4147
if (!sshOptions || (sshOptions?.privateKey && !isValidSshPrivateKey(sshOptions.privateKey))) {

redisinsight/ui/src/pages/home/components/AddInstanceForm/InstanceFormWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const getInitialValues = (editedInstance: Nullable<Instance>) => ({
6666
tls: !!editedInstance?.tls ?? false,
6767
ssh: !!editedInstance?.ssh ?? false,
6868
sshPassType: editedInstance?.sshOptions
69-
? (editedInstance.sshOptions.password ? SshPassType.Password : SshPassType.PrivateKey)
69+
? (editedInstance.sshOptions.privateKey ? SshPassType.PrivateKey : SshPassType.Password)
7070
: SshPassType.Password
7171
})
7272

0 commit comments

Comments
 (0)